KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > pde > internal > ui > editor > validation > TextValidator


1 /*******************************************************************************
2  * Copyright (c) 2007 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11
12 package org.eclipse.pde.internal.ui.editor.validation;
13
14 import org.eclipse.core.resources.IProject;
15 import org.eclipse.swt.events.ModifyEvent;
16 import org.eclipse.swt.events.ModifyListener;
17 import org.eclipse.swt.widgets.Text;
18 import org.eclipse.ui.forms.IManagedForm;
19
20 /**
21  * TextControlValidator
22  *
23  */

24 public abstract class TextValidator extends AbstractControlValidator {
25
26     private ModifyListener fModifyListener;
27     
28     private boolean fAutoValidate;
29     
30     private String JavaDoc fCurrentText;
31     
32     /**
33      * @param managedForm
34      * @param control
35      * @param project
36      * @param autoValidate
37      */

38     public TextValidator(IManagedForm managedForm, Text control,
39             IProject project, boolean autoValidate) {
40         super(managedForm, control, project);
41         // Turn on / off auto-validation
42
// If auto-validation is on, validation is triggered by modify text
43
// events. Otherwise, manual calls to validate must be made.
44
fAutoValidate = autoValidate;
45         // Initialize the text validator
46
intialize();
47     }
48
49     /**
50      *
51      */

52     private void intialize() {
53         // Save the current contents of the Text field
54
fCurrentText = getText().getText();
55         // No listeners required if auto-validation is off
56
if (fAutoValidate == false) {
57             return;
58         }
59         // Create the listeners for auto-validation
60
createListeners();
61         // Add the listeners if the validator is enabled
62
if (getEnabled()) {
63             addListeners();
64         }
65     }
66     
67     /* (non-Javadoc)
68      * @see org.eclipse.pde.internal.ui.editor.validation.AbstractControlValidator#setEnabled(boolean)
69      */

70     public void setEnabled(boolean enabled) {
71         // Nothing to do here if enablement is not being changed
72
if (getEnabled() == enabled) {
73             return;
74         }
75         // Update validator enablement
76
super.setEnabled(enabled);
77         // No listeners required if auto-validation is off
78
if (fAutoValidate == false) {
79             return;
80         }
81         // Add listeners if enabled; otherwise, remove them
82
if (getEnabled()) {
83             addListeners();
84         } else {
85             removeListeners();
86         }
87     }
88     
89     /**
90      *
91      */

92     protected void createListeners() {
93         fModifyListener = new ModifyListener() {
94             public void modifyText(ModifyEvent e) {
95                 handleModifyTextEvent(e);
96             }
97         };
98     }
99
100     /**
101      * @param e
102      */

103     protected void handleModifyTextEvent(ModifyEvent e) {
104         // Validation is not required if the current text contents is the
105
// same as the new text contents
106
String JavaDoc newText = getText().getText();
107         if (newText.equals(fCurrentText)) {
108             return;
109         }
110         // Save the current contents of the Text field
111
fCurrentText = newText;
112         // Perform auto-validation
113
validate();
114     }
115     
116     /**
117      *
118      */

119     protected void addListeners() {
120         getText().addModifyListener(fModifyListener);
121     }
122     
123     /**
124      *
125      */

126     protected void removeListeners() {
127         getText().removeModifyListener(fModifyListener);
128     }
129     
130     /**
131      * @return
132      */

133     protected Text getText() {
134         return (Text)getControl();
135     }
136     
137     /* (non-Javadoc)
138      * @see org.eclipse.pde.internal.ui.editor.validation.AbstractControlValidator#autoEnable()
139      */

140     protected boolean autoEnable() {
141         // Enable validator if the text field is editable
142
if (getText().getEditable() == false) {
143             return false;
144         }
145         return super.autoEnable();
146     }
147 }
148
Popular Tags