Announcement Announcement Module
No announcement yet.
Enable Apply button only when Form is Dirty Page Title Module
Move Remove Collapse
Conversation Detail Module
  • Filter
  • Time
  • Show
Clear All
new posts

  • Enable Apply button only when Form is Dirty

    I have a TabbedDialogPage in JPanel simliar to AppliationDialog with DialogPage. I also have Apply Revert Help button bar on the panel. I want to enable the Apply only when form is ditry. How should I do that?

    Also when the form is commited Apply should be disabled again.


  • #2
    I think that You have to use interceptor, listen for contsraintViolated and constraintSatisfied
    and then update status of button



    • #3
      I'm not having the source with me, so I could be way off.:wink:

      Try adding a valuechangedlistener to your form. if something changes, set the enabled state of your button to form.isDirty(). I think when the form has been committed, it's no longer dirty, so it should work.

      Hope this helps,



      • #4
        I has problems with form.isDirty(), but I work with unbuffered form model and it is problem maybe



        • #5
          My problem is that I have TabbedPageDialog which has 3-4 different Forms. And I have one button bar (Apply Revert Help) for TabbedDialogPage. So each form doesn't have any commit or revert commands .

          TabbedDialogPage updates the button depending on the PageComplete property of each FormBackedDialogPage, which is DilaogPage with Form. Only way this DialogPage is conneted to Form value of through newSingleLineResultsReporter, which means it gets enabled/disabled when Form has errors/noerros.

          I guess I just little confused how to get the FormModel.isDirty into this picture as well???



          • #6
            Rcp need formModel id or name for multiple views and multiple forms - I add this propertiy for me, but I don't know for CVS



            • #7
              Rcp need formModel id or name for multiple views and multiple forms
              Can you please explain bit more! or a code sample !!



              • #8
                I add id,contextId, boolean doValidation (getter and setter) in ValidatingFormModel

                id is for identify FormModel in any form, contextId is set for validation in modes my application
                (I have update,new and search mode - update change row in database, new add new row and search search rows in database) - I don't want validation in search mode and I call
                setDoValidation to false - when interceptor fire any event I recognzie formModel with id and when I set interceptor I have contextId and set that interceptor work in any mode , for instance :
                constants :
                public static final int ALL = -1;
                public static final int SEARCH = 1 << 1;
                public static final int NEW = 1 << 2;
                public static final int NORMAL = 1 << 3;
                public static final int USER = 1 << 4;

                I set formModel contextId to NEW | NORMAL
                NORMAL is update mode and validation don't work in search mode
                I extend ValidationListener (DefaultValidationListener) and add contextId and I set contextId in any mode and interceptor work with only formModel with this mode
                ValidationFormModel constraintViolated and constraintSatisfied work only doValidation is true and interceptor's processComponent,constraintViolated and constraintSatisfied check
                I make RevertAsYouTypeTextValueSetter (used in SwingFormModell) and it don't accept
                violated constraint except required - for instance - if field have constraint length <= 5
                when You type 123456 digit 6 is revert and field leave with 12345 (you get beep for now)

                In SwingFormModel change line 537 :
                // new AsYouTypeTextValueSetter(component, valueModel);
                new RevertAsYouTypeTextValueSetter(component,valueMode l,formModel);

                ant try

                this is code :
                * Copyright 2002-2004 the original author or authors.
                * Licensed under the Apache License, Version 2.0 (the "License"); you may not
                * use this file except in compliance with the License. You may obtain a copy of
                * the License at
                * Unless required by applicable law or agreed to in writing, software
                * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
                * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
                * License for the specific language governing permissions and limitations under
                * the License.
                package org.springframework.richclient.forms;

                import java.awt.Toolkit;

                import javax.swing.event.DocumentEvent;
                import javax.swing.event.DocumentListener;
                import javax.swing.text.AttributeSet;
                import javax.swing.text.BadLocationException;
                import javax.swing.text.JTextComponent;
                import javax.swing.text.PlainDocument;

                import org.springframework.binding.form.ConfigurableFormM odel;
                import org.springframework.binding.form.ValidationEvent;
                import org.springframework.binding.form.ValidationListene r;
                import gFormModel;
                import org.springframework.binding.value.ValueModel;
                import org.springframework.util.Assert;

                * @author snpe
                public class RevertAsYouTypeTextValueSetter extends AbstractValueSetter implements DocumentListener {

                private JTextComponent control;

                private boolean settingText;

                private ValidatingFormModel formModel;

                private ValueModel valueModel;

                public RevertAsYouTypeTextValueSetter(JTextComponent control, ValueModel valueModel,ConfigurableFormModel formModel) {
                this.control = control;
                this.control.setDocument(new ValidationDocument());
                this.control.getDocument().addDocumentListener(thi s);
                this.formModel = (ValidatingFormModel) formModel;
                this.valueModel = valueModel;

                public void removeUpdate(DocumentEvent e) {

                public void insertUpdate(DocumentEvent e) {

                public void changedUpdate(DocumentEvent e) {

                private void controlTextValueChanged() {
                if (!settingText) {

                protected void setControlValue(Object value) {
                // this try block will coalesce the 2 DocumentEvents that
                // JTextComponent.setText() fires into 1 call to
                // componentValueChanged()
                try {
                settingText = true;
                finally {
                settingText = false;

                private class ValidationDocument extends PlainDocument implements ValidationListener {

                private boolean revert;

                public void insertString(int offs, String str, AttributeSet a)
                throws BadLocationException {

                String currentText = getText(0, getLength());
                String beforeOffset = currentText.substring(0, offs);
                String afterOffset = currentText.substring(offs, currentText.length());
                String proposedResult = beforeOffset + str + afterOffset;

                if (!settingText) {
                if (revert) {
                // do revert
                // we can make message in constraintViolated and send for instance to advisor
                } else

                public void constraintSatisfied(ValidationEvent event) {
                if (logger.isDebugEnabled())

                public void constraintViolated(ValidationEvent event) {
                if (logger.isDebugEnabled())
                logger.debug("constraintViolated - revert=true");
                revert = true;

                // this call validation for empty field after removing
                /* protected void removeUpdate(DefaultDocumentEvent chng) {
                String currentText = null;
                try {
                currentText = getText(0,getLength());
                } catch (BadLocationException e) {
                logger.warn("remove text error");
                if (currentText == null || currentText.length() ==0)
                RevertAsYouTypeTextValueSetter.this.formModel.vali date();


                • #9
                  snpe, thanks. But I guess i am not sure how your demonstrate the form ditry capability. Sorry if I am missing something..



                  • #10
                    I have more forms in more views.I make special interceptor which check changes in forms:
                    - forms id,ValidatingFormModel id,ValidatingFormModel contextId,SwingFormModel id and
                    special interceptor contextId help me that find forms and field which is changed and then I set
                    correct form dirty

                    Rever value setter is for validation only - it isn't for dirty/clean form, but help me when validate property :

                    For instance, when any validation is violated old RCP accept change and decorate componet like error (red icon in edge) - my setter don't accept change than do beep and revert component value in old correct value - yet example, when You set that filed is number (accpet only digit 0-9)
                    and You type 12a then RCP accept 12a and show field error - my setter do beep and value in component revert to 12



                    • #11
                      Thanks snpe. NOw I see what you are saying. But I think there should be support for formDirty in composite dialog page context. I don't think my requirements are somewhat special.

                      But I don't get a response Keith or Ollie then I will try to cook up something along the lines of your code.

                      Thanks again!



                      • #12
                        Take a look at AbstractForm. That's most likely where we will add this behaivior at some point. Notice the "getCommitCommand()" action, which encapsulates production of controls that apply a commit action to the formodel - e.g an apply button. It would be straightforward to have this command calculate its enabled state based on the dirty state of the form model. Is there a JIRA issue for this? It's a very good enhancement.


                        • #13

                          I have lookek getCommitCommand() and tried to enable/disable it. Since I hae 3 forms in a TabbedDilalogPage and another control similar to TabbedApplicationDialog, which I call TabbedApplicationPanel, which has the button bar. And currently following TabbedApplicationDialog, it is enabled/dislabed on PageComplete porperty of DialogPage. I don't see how dialogPage gets information about each of its page's form?

                          But I will log this issue to JIRA along with another issue that I had mentioned here regarding dependent properties.



                          • #14
                            Re: Enable Apply button only when Form is Dirty

                            reload AbstractForm.setEnabled(boolean);

                            	public void setEnabled&#40;boolean enabled&#41; &#123;