Announcement

Collapse
No announcement yet.
X
  • Filter
  • Time
Clear All
new posts

    How to allow user to disable/enable a FormItem by double-clicking it

    I have a collection of forms used to edit a data source with a lot of fields. The data source is actually summarizing multiple database rows to supply the one "Record" being edited in the forms. For each actual database field there is a corresponding "Count" field in the datasource that contains the number of distinct values for the field in the various database rows that make up the Record.

    If a FormItem contains a field with a value Count greater than 1 I want to disable the FormItem but let the user choose to enable it if they want. I thought of using a DoubleClick hander on the FormItem to toggle between the disabled and enabled states, but of course, once the FormItem is disabled the double-click does nothing. I can't just add a button to the form because each individual FormItem needs it's own toggle.

    Any suggestions on how to effect this per field "toggle"? I suppose I could add a CheckboxItem next to every field, but that would make the form look pretty "busy". Is there any action the user can take on a disabled field that would generate an event that I can handle to do the toggle?

    #2
    If you want to use double click, how about instead of disabling the item, you actually use two items -- an editable TextItem and a non editable item (say StaticTextItem).
    When the user double clicks simply hide one item and show the other
    Here's a very crude example:

    Code:
    	@Override
    	public void onModuleLoad() {
    		
    		FormItem item1 = new TextItem("item1");
    		item1.setShowTitle(false);
    		
    		FormItem disabledItem1 = new StaticTextItem("item1_disabled");
    		disabledItem1.setTextBoxStyle("textItem");
    		disabledItem1.setShowTitle(false);
    		disabledItem1.setVisible(false);
    		
    		disabledItem1.setValueFormatter(new FormItemValueFormatter () {
    
    			@Override
    			public String formatValue(Object value, Record record,
    					DynamicForm form, FormItem item) {
    				return form.getValueAsString("item1");
    			}
    			
    		});
    		
    		item1.addDoubleClickHandler(new DoubleClickHandler() {
    			
    			@Override
    			public void onDoubleClick(DoubleClickEvent event) {
    				event.getForm().hideItem("item1");
    				event.getForm().showItem("item1_disabled");
    				
    			}
    		});
    
    		disabledItem1.addDoubleClickHandler(new DoubleClickHandler() {
    			
    			@Override
    			public void onDoubleClick(DoubleClickEvent event) {
    				event.getForm().hideItem("item1_disabled");
    				event.getForm().showItem("item1");
    				
    			}
    		});
    		DynamicForm testForm = new DynamicForm();
    		testForm.setItems(item1, disabledItem1);
    		testForm.draw();
    		
    	}
    You may need to tweak the sizing, styling etc if you want the "disabled" version to look exactly like the enabled one but this shouldn't be too difficult, and you'd probably auto-generate these pairs of items in your code based on the form fields, and perhaps use a showIfCondition rather than just calling show() and hide() directly. Overall this should be a workable approach

    Comment


      #3
      Works like a charm! Thanks for the help.

      Comment


        #4
        One follow up question. How can I reset a specific FormItem to it's original value. I can use resetValues() is available for the ValuesManager and DynamicForm, but for a specific FormItem?

        And a second follow up question. It doesn't appear that a SelectItem responds to double-click. The technique you showed earlier works fine for other types of FormItems, but when the "item1" in the example is a SelectItem, double-clicking on it does nothing.
        Last edited by jay.l.fisher; 22 Sep 2010, 18:41.

        Comment


          #5
          I've managed to solve my first follow up question by simply hanging the old value as an attribute on the FormItem and getting it back when I need it.

          But I'm still not having any luck with double-clicking a SelectItem. Is that a bug or is there something about a SelectItem that makes it unable to respond to a double-click?

          Comment


            #6
            Hi Jay
            The doubleClick event on a SelectItem isn't supported by default -- when the drop down list of options is shown in response to the first click on the item, it shows a clickMask, meaning the second click of the double click event is swallowed by the click mask and dismisses the drop down list.

            We could probably come up with a solution to get this working but it'd probably be a framework change.

            Perhaps you could use a form item icon rather than a double click event to switch to the disabled view?

            Comment


              #7
              By the way - we recognize that there should be some way to get at the values stored by dynamicForm.rememberValues() (The method that stores out values on setValues() and form init, which is the set of values that will get reset to when resetValues is called).
              We've therefore just added APIs dynamicForm.getOldValues() (and dynamicForm.getChangedValues()) to get at these values - this'll be present in nightly builds going forward

              Comment


                #8
                The form icon will probably work OK. Thanks for adding access to the remembered form values. Very helpful.

                Comment

                Working...
                X