001: /*******************************************************************************
002: * Copyright (c) 2006, 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.core.runtime.IStatus;
014: import org.eclipse.jface.dialogs.IMessageProvider;
015: import org.eclipse.pde.internal.core.WorkspaceModelManager;
016: import org.eclipse.pde.internal.core.builders.CompilerFlags;
017: import org.eclipse.swt.widgets.Control;
018: import org.eclipse.ui.forms.IManagedForm;
019: import org.eclipse.ui.forms.IMessageManager;
020:
021: /**
022: * AbstractControlValidator
023: *
024: */
025: public abstract class AbstractControlValidator implements
026: IControlValidator, IValidatorMessageHandler {
027:
028: public static final Object F_DEFAULT_MESSAGE_KEY = "k"; //$NON-NLS-1$
029:
030: private boolean fEnabled;
031:
032: private IManagedForm fManagedForm;
033:
034: private Control fControl;
035:
036: private String fMessagePrefix;
037:
038: private boolean fIsValid;
039:
040: private IProject fProject;
041:
042: /**
043: * @param managedForm
044: * @param control
045: * @param project
046: */
047: public AbstractControlValidator(IManagedForm managedForm,
048: Control control, IProject project) {
049: fProject = project;
050: fManagedForm = managedForm;
051: fControl = control;
052: fMessagePrefix = null;
053: fEnabled = autoEnable();
054: reset();
055: }
056:
057: /**
058: * @return
059: */
060: protected boolean autoEnable() {
061: boolean isBinaryProject = WorkspaceModelManager
062: .isBinaryProject(fProject);
063: // Enable validator if this is a source projec, the control is enabled
064: // and the control is not disposed
065: if ((isBinaryProject == false) && fControl.getEnabled()
066: && (fControl.isDisposed() == false)) {
067: return true;
068: }
069: return false;
070: }
071:
072: /* (non-Javadoc)
073: * @see org.eclipse.pde.internal.ui.editor.IControlValdiator#getEnabled()
074: */
075: public boolean getEnabled() {
076: return fEnabled;
077: }
078:
079: /* (non-Javadoc)
080: * @see org.eclipse.pde.internal.ui.editor.IControlValdiator#setEnabled(boolean)
081: */
082: public void setEnabled(boolean enabled) {
083: // Nothing to do here if enablement is not being changed
084: if (enabled == fEnabled) {
085: return;
086: }
087: // Update enablement
088: fEnabled = enabled;
089: // Automatically perform actions depending on enablement
090: if (fEnabled) {
091: // Re-validate control if validator is enabled
092: validate();
093: } else {
094: // Reset validation state if validator is disabled
095: reset();
096: }
097: }
098:
099: /* (non-Javadoc)
100: * @see org.eclipse.pde.internal.ui.editor.IControlValdiator#validate()
101: */
102: public boolean validate() {
103: // Skip validation if the validator is disabled
104: if (fEnabled == false) {
105: return fIsValid;
106: }
107: // Validate the control
108: fIsValid = validateControl();
109: // If the control is valid, remove all the messages associated with
110: // the control (in case they were not individually removed by the
111: // child class)
112: if (fIsValid) {
113: fManagedForm.getMessageManager().removeMessages(fControl);
114: }
115: return fIsValid;
116: }
117:
118: /**
119: * @return
120: */
121: protected abstract boolean validateControl();
122:
123: /* (non-Javadoc)
124: * @see org.eclipse.pde.internal.ui.editor.validation.IValidatorMessageHandler#addMessage(java.lang.Object, java.lang.String, int)
125: */
126: public void addMessage(Object key, String messageText,
127: int messageType) {
128: // Add a prefix, if one was specified
129: if (fMessagePrefix != null) {
130: messageText = fMessagePrefix + ' ' + messageText;
131: }
132: // Delegate to message manager
133: fManagedForm.getMessageManager().addMessage(key, messageText,
134: null, messageType, fControl);
135: }
136:
137: /* (non-Javadoc)
138: * @see org.eclipse.pde.internal.ui.editor.validation.IValidatorMessageHandler#addMessage(java.lang.String, int)
139: */
140: public void addMessage(String messageText, int messageType) {
141: // Add a prefix, if one was specified
142: if (fMessagePrefix != null) {
143: messageText = fMessagePrefix + ' ' + messageText;
144: }
145: // Delegate to message manager
146: fManagedForm.getMessageManager().addMessage(
147: F_DEFAULT_MESSAGE_KEY, messageText, null, messageType,
148: fControl);
149: }
150:
151: /**
152: * @param status
153: * @return
154: */
155: public static int getMessageType(IStatus status) {
156: int severity = status.getSeverity();
157: // Translate severity to the equivalent message provider type
158: if (severity == IStatus.OK) {
159: return IMessageProvider.NONE;
160: } else if (severity == IStatus.ERROR) {
161: return IMessageProvider.ERROR;
162: } else if (severity == IStatus.WARNING) {
163: return IMessageProvider.WARNING;
164: } else if (severity == IStatus.INFO) {
165: return IMessageProvider.INFORMATION;
166: }
167: // IStatus.CANCEL
168: return IMessageProvider.NONE;
169: }
170:
171: /**
172: * @param project
173: * @param compilerFlagId
174: * @return
175: */
176: public static int getMessageType(IProject project,
177: String 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 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 prefix) {
201: fMessagePrefix = prefix;
202: }
203:
204: /* (non-Javadoc)
205: * @see org.eclipse.pde.internal.ui.editor.validation.IValidatorMessageHandler#getMessagePrefix()
206: */
207: public String 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: }
|