KickJava   Java API By Example, From Geeks To Geeks.

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


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.core.runtime.CoreException;
16 import org.eclipse.core.runtime.IStatus;
17 import org.eclipse.jdt.core.IJavaProject;
18 import org.eclipse.jdt.core.JavaCore;
19 import org.eclipse.jface.dialogs.IMessageProvider;
20 import org.eclipse.pde.core.plugin.IFragmentModel;
21 import org.eclipse.pde.core.plugin.IPluginModelBase;
22 import org.eclipse.pde.core.plugin.PluginRegistry;
23 import org.eclipse.pde.internal.core.AbstractNLModel;
24 import org.eclipse.pde.internal.core.NLResourceHelper;
25 import org.eclipse.pde.internal.core.PDECore;
26 import org.eclipse.pde.internal.core.builders.CompilerFlags;
27 import org.eclipse.pde.internal.core.util.PDEJavaHelper;
28 import org.eclipse.pde.internal.core.util.VersionUtil;
29 import org.eclipse.pde.internal.ui.PDEUIMessages;
30 import org.osgi.framework.InvalidSyntaxException;
31
32 /**
33  * ControlValidationUtility
34  *
35  */

36 public class ControlValidationUtility {
37
38     /**
39      * @param text
40      * @param validator
41      * @return
42      */

43     public static boolean validateRequiredField(String JavaDoc value,
44             IValidatorMessageHandler validator, int messageType) {
45         // Check to see if a value was specified
46
if (value.length() == 0) {
47             validator.addMessage(
48                     PDEUIMessages.ControlValidationUtility_errorMsgValueMustBeSpecified,
49                     messageType);
50             return false;
51         }
52         return true;
53     }
54
55     /**
56      * @param text
57      * @param validator
58      * @param required
59      * @param model
60      * @param project
61      * @return
62      */

63     public static boolean validateTranslatableField(String JavaDoc value,
64             IValidatorMessageHandler validator,
65             IPluginModelBase model, IProject project) {
66         
67         // Check the compiler flag and translate it into a message type
68
int messageType = AbstractControlValidator.getMessageType(project,
69                 CompilerFlags.P_NOT_EXTERNALIZED);
70         // If the message type is none, no validation is required
71
// Same as IGNORE
72
if (messageType == IMessageProvider.NONE) {
73             return true;
74         }
75
76         // Check to see if the name has been externalized
77
if (value.startsWith("%") == false) { //$NON-NLS-1$
78
validator.addMessage(
79                     PDEUIMessages.ControlValidationUtility_errorMsgValueNotExternalized,
80                     messageType);
81             return false;
82         }
83         
84         // Check to see if the key is in the plugin's property file
85
if (model instanceof AbstractNLModel) {
86             NLResourceHelper helper = ((AbstractNLModel)model).getNLResourceHelper();
87             if ((helper == null) ||
88                     (helper.resourceExists(value) == false)) {
89                 validator.addMessage(
90                         PDEUIMessages.ControlValidationUtility_errorMsgKeyNotFound,
91                         messageType);
92                 return false;
93             }
94         }
95         return true;
96     }
97     
98     /**
99      * @param text
100      * @param validator
101      * @param required
102      * @return
103      */

104     public static boolean validateVersionField(String JavaDoc value,
105             IValidatorMessageHandler validator) {
106         // Check for invalid version
107
IStatus status = VersionUtil.validateVersion(value);
108         if (status.isOK() == false) {
109             validator.addMessage(
110                     status.getMessage(),
111                     AbstractControlValidator.getMessageType(status));
112             return false;
113         }
114         return true;
115     }
116     
117     /**
118      * @param text
119      * @param validator
120      * @param model
121      * @param project
122      */

123     public static boolean validatePlatformFilterField(String JavaDoc value,
124             IValidatorMessageHandler validator) {
125         // Check to see if the platform filter syntax is valid
126
try {
127             PDECore.getDefault().getBundleContext().createFilter(value);
128         } catch (InvalidSyntaxException ise) {
129             validator.addMessage(
130                     PDEUIMessages.ControlValidationUtility_errorMsgFilterInvalidSyntax,
131                     IMessageProvider.ERROR);
132             return false;
133         }
134
135         return true;
136     }
137     
138     /**
139      * @param text
140      * @param validator
141      * @param project
142      * @return
143      */

144     public static boolean validateActivatorField(String JavaDoc value,
145             IValidatorMessageHandler validator, IProject project) {
146         
147         // Check the compiler flag and translate it into a message type
148
int messageType = AbstractControlValidator.getMessageType(project,
149                 CompilerFlags.P_UNKNOWN_CLASS);
150         // If the message type is none, no validation is required
151
// Same as IGNORE
152
if (messageType == IMessageProvider.NONE) {
153             return true;
154         }
155         
156         // Check to see if the class is on the plug-in classpath
157
try {
158             if (project.hasNature(JavaCore.NATURE_ID)) {
159                 IJavaProject javaProject = JavaCore.create(project);
160                 // Look for this activator in the project's classpath
161
if (!PDEJavaHelper.isOnClasspath(value, javaProject)) {
162                     validator.addMessage(
163                             PDEUIMessages.ControlValidationUtility_errorMsgNotOnClasspath,
164                             messageType);
165                     return false;
166                 }
167             }
168         } catch (CoreException ce) {
169             // Ignore
170
}
171         
172         return true;
173     }
174     
175     /**
176      * @param value
177      * @param validator
178      * @param model
179      * @param project
180      * @return
181      */

182     public static boolean validateFragmentHostPluginField(String JavaDoc value,
183             IValidatorMessageHandler validator, IProject project) {
184
185         // Check the compiler flag and translate it into a message type
186
// If the message type is none, it is the same as IGNORE
187
int reqAttMessageType = AbstractControlValidator.getMessageType(project,
188                 CompilerFlags.P_NO_REQUIRED_ATT);
189         // Check to see if the host plug-in was defined
190
if ((reqAttMessageType != IMessageProvider.NONE) &&
191                 validateRequiredField(value, validator, reqAttMessageType) == false) {
192             return false;
193         }
194         // Check the compiler flag and translate it into a message type
195
int unresImpMessageType = AbstractControlValidator.getMessageType(project,
196                 CompilerFlags.P_UNRESOLVED_IMPORTS);
197         // If the message type is none, no validation is required
198
// Same as IGNORE
199
if (unresImpMessageType == IMessageProvider.NONE) {
200             return true;
201         }
202         // Check to see if the host plugin is defined, enabled and not a
203
// fragment itself
204
IPluginModelBase hostModel = PluginRegistry.findModel(value);
205         if ((hostModel == null) ||
206                 (hostModel instanceof IFragmentModel) ||
207                 (hostModel.isEnabled() == false)) {
208             validator.addMessage(
209                     PDEUIMessages.ControlValidationUtility_errorMsgPluginUnresolved,
210                     unresImpMessageType);
211             return false;
212         }
213             
214         return true;
215     }
216     
217 }
218
Popular Tags