1 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 28 public abstract class AbstractControlValidator implements IControlValidator, IValidatorMessageHandler { 29 30 public static final Object F_DEFAULT_MESSAGE_KEY = "k"; 32 private boolean fEnabled; 33 34 private IManagedForm fManagedForm; 35 36 private Control fControl; 37 38 private String fMessagePrefix; 39 40 private boolean fIsValid; 41 42 private IProject fProject; 43 44 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 62 protected boolean autoEnable() { 63 boolean isBinaryProject = WorkspaceModelManager.isBinaryProject(fProject); 64 if ((isBinaryProject == false) && 67 fControl.getEnabled() && 68 (fControl.isDisposed() == false)) { 69 return true; 70 } 71 return false; 72 } 73 74 77 public boolean getEnabled() { 78 return fEnabled; 79 } 80 81 84 public void setEnabled(boolean enabled) { 85 if (enabled == fEnabled) { 87 return; 88 } 89 fEnabled = enabled; 91 if (fEnabled) { 93 validate(); 95 } else { 96 reset(); 98 } 99 } 100 101 104 public boolean validate() { 105 if (fEnabled == false) { 107 return fIsValid; 108 } 109 fIsValid = validateControl(); 111 if (fIsValid) { 115 fManagedForm.getMessageManager().removeMessages(fControl); 116 } 117 return fIsValid; 118 } 119 120 123 protected abstract boolean validateControl(); 124 125 128 public void addMessage(Object key, String messageText, int messageType) { 129 if (fMessagePrefix != null) { 131 messageText = fMessagePrefix + ' ' + messageText; 132 } 133 fManagedForm.getMessageManager().addMessage(key, messageText, null, 135 messageType, fControl); 136 } 137 138 141 public void addMessage(String messageText, int messageType) { 142 if (fMessagePrefix != null) { 144 messageText = fMessagePrefix + ' ' + messageText; 145 } 146 fManagedForm.getMessageManager().addMessage( 148 F_DEFAULT_MESSAGE_KEY, messageText, null, 149 messageType, fControl); 150 } 151 152 156 public static int getMessageType(IStatus status) { 157 int severity = status.getSeverity(); 158 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 return IMessageProvider.NONE; 170 } 171 172 177 public static int getMessageType(IProject project, String compilerFlagId) { 178 int severity = CompilerFlags.getFlag(project, compilerFlagId); 179 if (severity == CompilerFlags.IGNORE) { 181 return IMessageProvider.NONE; 182 } else if (severity == CompilerFlags.ERROR) { 183 return IMessageProvider.ERROR; 184 } else { 185 return IMessageProvider.WARNING; 187 } 188 } 189 190 193 public void removeMessage(Object key) { 194 fManagedForm.getMessageManager().removeMessage(key, fControl); 195 } 196 197 200 public void setMessagePrefix(String prefix) { 201 fMessagePrefix = prefix; 202 } 203 204 207 public String getMessagePrefix() { 208 return fMessagePrefix; 209 } 210 211 214 public IManagedForm getManagedForm() { 215 return fManagedForm; 216 } 217 218 221 public IMessageManager getMessageManager() { 222 return fManagedForm.getMessageManager(); 223 } 224 225 228 public void setRefresh(boolean refresh) { 229 getMessageManager().setAutoUpdate(refresh); 230 } 231 232 235 public Control getControl() { 236 return fControl; 237 } 238 239 242 public boolean isValid() { 243 return fIsValid; 244 } 245 246 249 public void reset() { 250 fIsValid = true; 251 fManagedForm.getMessageManager().removeMessages(fControl); 252 } 253 } 254 | Popular Tags |