KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*******************************************************************************
2  * Copyright (c) 2006, 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.core.runtime.IStatus;
16 import org.eclipse.jface.dialogs.IMessageProvider;
17 import org.eclipse.pde.internal.core.WorkspaceModelManager;
18 import org.eclipse.pde.internal.core.builders.CompilerFlags;
19 import org.eclipse.swt.widgets.Control;
20 import org.eclipse.ui.forms.IManagedForm;
21 import org.eclipse.ui.forms.IMessageManager;
22
23
24 /**
25  * AbstractControlValidator
26  *
27  */

28 public abstract class AbstractControlValidator implements IControlValidator, IValidatorMessageHandler {
29
30     public static final Object JavaDoc F_DEFAULT_MESSAGE_KEY = "k"; //$NON-NLS-1$
31

32     private boolean fEnabled;
33     
34     private IManagedForm fManagedForm;
35     
36     private Control fControl;
37     
38     private String JavaDoc fMessagePrefix;
39     
40     private boolean fIsValid;
41     
42     private IProject fProject;
43     
44     /**
45      * @param managedForm
46      * @param control
47      * @param project
48      */

49     public AbstractControlValidator(IManagedForm managedForm, Control control,
50             IProject project) {
51         fProject = project;
52         fManagedForm = managedForm;
53         fControl = control;
54         fMessagePrefix = null;
55         fEnabled = autoEnable();
56         reset();
57     }
58     
59     /**
60      * @return
61      */

62     protected boolean autoEnable() {
63         boolean isBinaryProject = WorkspaceModelManager.isBinaryProject(fProject);
64         // Enable validator if this is a source projec, the control is enabled
65
// and the control is not disposed
66
if ((isBinaryProject == false) &&
67                 fControl.getEnabled() &&
68                 (fControl.isDisposed() == false)) {
69             return true;
70         }
71         return false;
72     }
73     
74     /* (non-Javadoc)
75      * @see org.eclipse.pde.internal.ui.editor.IControlValdiator#getEnabled()
76      */

77     public boolean getEnabled() {
78         return fEnabled;
79     }
80
81     /* (non-Javadoc)
82      * @see org.eclipse.pde.internal.ui.editor.IControlValdiator#setEnabled(boolean)
83      */

84     public void setEnabled(boolean enabled) {
85         // Nothing to do here if enablement is not being changed
86
if (enabled == fEnabled) {
87             return;
88         }
89         // Update enablement
90
fEnabled = enabled;
91         // Automatically perform actions depending on enablement
92
if (fEnabled) {
93             // Re-validate control if validator is enabled
94
validate();
95         } else {
96             // Reset validation state if validator is disabled
97
reset();
98         }
99     }
100
101     /* (non-Javadoc)
102      * @see org.eclipse.pde.internal.ui.editor.IControlValdiator#validate()
103      */

104     public boolean validate() {
105         // Skip validation if the validator is disabled
106
if (fEnabled == false) {
107             return fIsValid;
108         }
109         // Validate the control
110
fIsValid = validateControl();
111         // If the control is valid, remove all the messages associated with
112
// the control (in case they were not individually removed by the
113
// child class)
114
if (fIsValid) {
115             fManagedForm.getMessageManager().removeMessages(fControl);
116         }
117         return fIsValid;
118     }
119
120     /**
121      * @return
122      */

123     protected abstract boolean validateControl();
124     
125     /* (non-Javadoc)
126      * @see org.eclipse.pde.internal.ui.editor.validation.IValidatorMessageHandler#addMessage(java.lang.Object, java.lang.String, int)
127      */

128     public void addMessage(Object JavaDoc key, String JavaDoc messageText, int messageType) {
129         // Add a prefix, if one was specified
130
if (fMessagePrefix != null) {
131             messageText = fMessagePrefix + ' ' + messageText;
132         }
133         // Delegate to message manager
134
fManagedForm.getMessageManager().addMessage(key, messageText, null,
135                 messageType, fControl);
136     }
137
138     /* (non-Javadoc)
139      * @see org.eclipse.pde.internal.ui.editor.validation.IValidatorMessageHandler#addMessage(java.lang.String, int)
140      */

141     public void addMessage(String JavaDoc messageText, int messageType) {
142         // Add a prefix, if one was specified
143
if (fMessagePrefix != null) {
144             messageText = fMessagePrefix + ' ' + messageText;
145         }
146         // Delegate to message manager
147
fManagedForm.getMessageManager().addMessage(
148                 F_DEFAULT_MESSAGE_KEY, messageText, null,
149                 messageType, fControl);
150     }
151     
152     /**
153      * @param status
154      * @return
155      */

156     public static int getMessageType(IStatus status) {
157         int severity = status.getSeverity();
158         // Translate severity to the equivalent message provider type
159
if (severity == IStatus.OK) {
160             return IMessageProvider.NONE;
161         } else if (severity == IStatus.ERROR) {
162             return IMessageProvider.ERROR;
163         } else if (severity == IStatus.WARNING) {
164             return IMessageProvider.WARNING;
165         } else if (severity == IStatus.INFO) {
166             return IMessageProvider.INFORMATION;
167         }
168         // IStatus.CANCEL
169
return IMessageProvider.NONE;
170     }
171     
172     /**
173      * @param project
174      * @param compilerFlagId
175      * @return
176      */

177     public static int getMessageType(IProject project, String JavaDoc compilerFlagId) {
178         int severity = CompilerFlags.getFlag(project, compilerFlagId);
179         // Translate severity to the equivalent message provider type
180
if (severity == CompilerFlags.IGNORE) {
181             return IMessageProvider.NONE;
182         } else if (severity == CompilerFlags.ERROR) {
183             return IMessageProvider.ERROR;
184         } else {
185             // CompilerFlags.WARNING
186
return IMessageProvider.WARNING;
187         }
188     }
189     
190     /* (non-Javadoc)
191      * @see org.eclipse.pde.internal.ui.editor.validation.IValidatorMessageHandler#removeMessage(java.lang.Object)
192      */

193     public void removeMessage(Object JavaDoc key) {
194         fManagedForm.getMessageManager().removeMessage(key, fControl);
195     }
196     
197     /* (non-Javadoc)
198      * @see org.eclipse.pde.internal.ui.editor.validation.IValidatorMessageHandler#setMessagePrefix(java.lang.String)
199      */

200     public void setMessagePrefix(String JavaDoc prefix) {
201         fMessagePrefix = prefix;
202     }
203     
204     /* (non-Javadoc)
205      * @see org.eclipse.pde.internal.ui.editor.validation.IValidatorMessageHandler#getMessagePrefix()
206      */

207     public String JavaDoc getMessagePrefix() {
208         return fMessagePrefix;
209     }
210     
211     /* (non-Javadoc)
212      * @see org.eclipse.pde.internal.ui.editor.validation.IValidatorMessageHandler#getManagedForm()
213      */

214     public IManagedForm getManagedForm() {
215         return fManagedForm;
216     }
217     
218     /* (non-Javadoc)
219      * @see org.eclipse.pde.internal.ui.editor.validation.IValidatorMessageHandler#getMessageManager()
220      */

221     public IMessageManager getMessageManager() {
222         return fManagedForm.getMessageManager();
223     }
224     
225     /* (non-Javadoc)
226      * @see org.eclipse.pde.internal.ui.editor.validation.IControlValidator#setRefresh(boolean)
227      */

228     public void setRefresh(boolean refresh) {
229         getMessageManager().setAutoUpdate(refresh);
230     }
231     
232     /* (non-Javadoc)
233      * @see org.eclipse.pde.internal.ui.editor.validation.IControlValdiator#getControl()
234      */

235     public Control getControl() {
236         return fControl;
237     }
238     
239     /* (non-Javadoc)
240      * @see org.eclipse.pde.internal.ui.editor.validation.IControlValidator#isValid()
241      */

242     public boolean isValid() {
243         return fIsValid;
244     }
245
246     /* (non-Javadoc)
247      * @see org.eclipse.pde.internal.ui.editor.validation.IControlValidator#reset()
248      */

249     public void reset() {
250         fIsValid = true;
251         fManagedForm.getMessageManager().removeMessages(fControl);
252     }
253 }
254
Popular Tags