Page 1 of 2 12 LastLast
Results 1 to 10 of 14

Thread: Enable Apply button only when Form is Dirty

  1. #1
    Join Date
    Aug 2004
    Location
    Northridge, CA
    Posts
    151

    Default 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.

    Amad

  2. #2
    Join Date
    Aug 2004
    Posts
    203

    Default

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

    regards

  3. #3
    Join Date
    Sep 2004
    Location
    Ghent, Belgium
    Posts
    224

    Default

    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,

    Peter

  4. #4
    Join Date
    Aug 2004
    Posts
    203

    Default

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

    regards

  5. #5
    Join Date
    Aug 2004
    Location
    Northridge, CA
    Posts
    151

    Default

    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???

    Amad

  6. #6
    Join Date
    Aug 2004
    Posts
    203

    Default

    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

    regards

  7. #7
    Join Date
    Aug 2004
    Location
    Northridge, CA
    Posts
    151

    Default

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

    Amad

  8. #8
    Join Date
    Aug 2004
    Posts
    203

    Default

    Amad,
    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
    contextId
    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

    regards
    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
    *
    * http://www.apache.org/licenses/LICENSE-2.0
    *
    * 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 org.springframework.binding.form.support.Validatin 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) {
    super(valueModel);
    Assert.notNull(control);
    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) {
    controlTextValueChanged();
    }

    public void insertUpdate(DocumentEvent e) {
    controlTextValueChanged();
    }

    public void changedUpdate(DocumentEvent e) {
    controlTextValueChanged();
    }

    private void controlTextValueChanged() {
    if (!settingText) {
    componentValueChanged(control.getText());
    }
    }

    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;
    control.setText((String)value);
    }
    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;

    formModel.addValidationListener(this);
    revert=false;
    if (!settingText) {
    componentValueChanged(proposedResult);
    }
    formModel.removeValidationListener(this);
    if (revert) {
    // do revert
    componentValueChanged(currentText);
    // we can make message in constraintViolated and send for instance to advisor
    Toolkit.getDefaultToolkit().beep();
    } else
    super.insertString(offs,str,a);
    }

    public void constraintSatisfied(ValidationEvent event) {
    if (logger.isDebugEnabled())
    logger.debug("constraintSatisfied");
    }

    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) {
    super.removeUpdate(chng);
    String currentText = null;
    try {
    currentText = getText(0,getLength());
    } catch (BadLocationException e) {
    logger.warn("remove text error");
    e.printStackTrace();
    }
    if (currentText == null || currentText.length() ==0)
    RevertAsYouTypeTextValueSetter.this.formModel.vali date();
    }*/
    }
    }

  9. #9
    Join Date
    Aug 2004
    Location
    Northridge, CA
    Posts
    151

    Default

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

    Amad

  10. #10
    Join Date
    Aug 2004
    Posts
    203

    Default

    Amad,
    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

    regards

Similar Threads

  1. Replies: 8
    Last Post: Oct 30th, 2012, 11:33 AM
  2. Replies: 3
    Last Post: Jun 8th, 2010, 03:27 AM
  3. Replies: 9
    Last Post: May 4th, 2006, 09:53 AM
  4. Replies: 1
    Last Post: Sep 22nd, 2005, 04:08 PM
  5. Replies: 4
    Last Post: Apr 15th, 2005, 05:46 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •