001: /*******************************************************************************
002: * Copyright (c) 2007 IBM Corporation and others.
003: * All rights reserved. This program and the accompanying materials
004: * are made available under the terms of the Eclipse Public License v1.0
005: * which accompanies this distribution, and is available at
006: * http://www.eclipse.org/legal/epl-v10.html
007: *
008: * Contributors:
009: * IBM Corporation - initial API and implementation
010: *******************************************************************************/package org.eclipse.pde.internal.ui.editor.validation;
011:
012: import org.eclipse.core.resources.IProject;
013: import org.eclipse.swt.events.ModifyEvent;
014: import org.eclipse.swt.events.ModifyListener;
015: import org.eclipse.swt.widgets.Text;
016: import org.eclipse.ui.forms.IManagedForm;
017:
018: /**
019: * TextControlValidator
020: *
021: */
022: public abstract class TextValidator extends AbstractControlValidator {
023:
024: private ModifyListener fModifyListener;
025:
026: private boolean fAutoValidate;
027:
028: private String fCurrentText;
029:
030: /**
031: * @param managedForm
032: * @param control
033: * @param project
034: * @param autoValidate
035: */
036: public TextValidator(IManagedForm managedForm, Text control,
037: IProject project, boolean autoValidate) {
038: super (managedForm, control, project);
039: // Turn on / off auto-validation
040: // If auto-validation is on, validation is triggered by modify text
041: // events. Otherwise, manual calls to validate must be made.
042: fAutoValidate = autoValidate;
043: // Initialize the text validator
044: intialize();
045: }
046:
047: /**
048: *
049: */
050: private void intialize() {
051: // Save the current contents of the Text field
052: fCurrentText = getText().getText();
053: // No listeners required if auto-validation is off
054: if (fAutoValidate == false) {
055: return;
056: }
057: // Create the listeners for auto-validation
058: createListeners();
059: // Add the listeners if the validator is enabled
060: if (getEnabled()) {
061: addListeners();
062: }
063: }
064:
065: /* (non-Javadoc)
066: * @see org.eclipse.pde.internal.ui.editor.validation.AbstractControlValidator#setEnabled(boolean)
067: */
068: public void setEnabled(boolean enabled) {
069: // Nothing to do here if enablement is not being changed
070: if (getEnabled() == enabled) {
071: return;
072: }
073: // Update validator enablement
074: super .setEnabled(enabled);
075: // No listeners required if auto-validation is off
076: if (fAutoValidate == false) {
077: return;
078: }
079: // Add listeners if enabled; otherwise, remove them
080: if (getEnabled()) {
081: addListeners();
082: } else {
083: removeListeners();
084: }
085: }
086:
087: /**
088: *
089: */
090: protected void createListeners() {
091: fModifyListener = new ModifyListener() {
092: public void modifyText(ModifyEvent e) {
093: handleModifyTextEvent(e);
094: }
095: };
096: }
097:
098: /**
099: * @param e
100: */
101: protected void handleModifyTextEvent(ModifyEvent e) {
102: // Validation is not required if the current text contents is the
103: // same as the new text contents
104: String newText = getText().getText();
105: if (newText.equals(fCurrentText)) {
106: return;
107: }
108: // Save the current contents of the Text field
109: fCurrentText = newText;
110: // Perform auto-validation
111: validate();
112: }
113:
114: /**
115: *
116: */
117: protected void addListeners() {
118: getText().addModifyListener(fModifyListener);
119: }
120:
121: /**
122: *
123: */
124: protected void removeListeners() {
125: getText().removeModifyListener(fModifyListener);
126: }
127:
128: /**
129: * @return
130: */
131: protected Text getText() {
132: return (Text) getControl();
133: }
134:
135: /* (non-Javadoc)
136: * @see org.eclipse.pde.internal.ui.editor.validation.AbstractControlValidator#autoEnable()
137: */
138: protected boolean autoEnable() {
139: // Enable validator if the text field is editable
140: if (getText().getEditable() == false) {
141: return false;
142: }
143: return super.autoEnable();
144: }
145: }
|