KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > pde > internal > ui > editor > EditorUtilities


1 /*******************************************************************************
2  * Copyright (c) 2005, 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 package org.eclipse.pde.internal.ui.editor;
12
13 import java.io.File JavaDoc;
14 import java.io.IOException JavaDoc;
15 import java.io.InputStream JavaDoc;
16 import java.net.MalformedURLException JavaDoc;
17 import java.net.URL JavaDoc;
18
19 import org.eclipse.core.resources.IContainer;
20 import org.eclipse.core.resources.IFile;
21 import org.eclipse.core.resources.IProject;
22 import org.eclipse.core.resources.IResource;
23 import org.eclipse.core.resources.IWorkspaceRoot;
24 import org.eclipse.core.runtime.IPath;
25 import org.eclipse.core.runtime.Path;
26 import org.eclipse.jface.dialogs.IMessageProvider;
27 import org.eclipse.jface.dialogs.MessageDialog;
28 import org.eclipse.osgi.util.NLS;
29 import org.eclipse.pde.core.plugin.IPluginModelBase;
30 import org.eclipse.pde.core.plugin.PluginRegistry;
31 import org.eclipse.pde.internal.core.iproduct.IProduct;
32 import org.eclipse.pde.internal.core.util.CoreUtility;
33 import org.eclipse.pde.internal.ui.PDEPlugin;
34 import org.eclipse.pde.internal.ui.PDEUIMessages;
35 import org.eclipse.pde.internal.ui.editor.product.LauncherSection;
36 import org.eclipse.pde.internal.ui.editor.validation.IValidatorMessageHandler;
37 import org.eclipse.pde.internal.ui.parts.FormEntry;
38 import org.eclipse.swt.SWTException;
39 import org.eclipse.swt.graphics.ImageData;
40 import org.eclipse.swt.graphics.ImageLoader;
41 import org.eclipse.ui.PartInitException;
42 import org.eclipse.ui.ide.IDE;
43
44 public class EditorUtilities {
45     
46     private static final int F_EXACT_IMAGE_SIZE = 0;
47     private static final int F_MAX_IMAGE_SIZE = 1;
48     private static final int F_ICO_IMAGE = 2;
49     private static final int F_IMAGE_DEPTH = 3;
50     
51     
52     static class ValidationMessage {
53         String JavaDoc fMessage;
54         int fSeverity = IMessageProvider.WARNING;
55         ValidationMessage(String JavaDoc message) {
56             fMessage = message;
57         }
58     }
59     
60     static class ValidationInfo {
61         int maxWidth, maxHeight, warningWidth, warningHeight, requiredDepth;
62     }
63     
64     
65     private static ImageData[] getImageData(IValidatorMessageHandler validator,
66             FormEntry provider,
67             IProduct product) {
68         String JavaDoc imagePath = provider.getText().getText();
69         String JavaDoc message = null;
70         try {
71             IPath path = getFullPath(new Path(imagePath), product);
72             URL JavaDoc url = new URL JavaDoc(path.toString());
73             ImageLoader loader = new ImageLoader();
74             InputStream JavaDoc stream = url.openStream();
75             ImageData[] idata = loader.load(stream);
76             stream.close();
77             if (idata != null && idata.length > 0)
78                 return idata;
79             message = PDEUIMessages.EditorUtilities_noImageData;
80         } catch (SWTException e) {
81             message = PDEUIMessages.EditorUtilities_pathNotValidImage;
82         } catch (MalformedURLException JavaDoc e) {
83             message = PDEUIMessages.EditorUtilities_invalidFilePath;
84         } catch (IOException JavaDoc e) {
85             message = PDEUIMessages.EditorUtilities_invalidFilePath;
86         }
87         validator.addMessage(message, IMessageProvider.WARNING);
88         return null;
89     }
90     
91     private static boolean containsEmptyField(FormEntry provider) {
92         return provider.getText().getText().length() == 0;
93     }
94
95     private static boolean imageEntryInternalValidate(IValidatorMessageHandler validator, FormEntry provider, IProduct product, ValidationInfo info, int validationType) {
96         if (containsEmptyField(provider))
97             return true;
98         ImageData[] idata = getImageData(validator, provider, product);
99         if (idata == null)
100             return false;
101         
102         ValidationMessage ms = null;
103         switch(validationType) {
104         case F_MAX_IMAGE_SIZE:
105             ms = getMS_maxImageSize(idata[0], info.maxWidth, info.maxHeight, info.warningWidth, info.warningHeight);
106             break;
107         case F_ICO_IMAGE:
108             ms = getMS_icoImage(idata);
109             break;
110         case F_IMAGE_DEPTH: // do not break after F_IMAGEDEPTH since we are also checking exact size
111
ms = getMS_imageDepth(idata[0], info.requiredDepth);
112         case F_EXACT_IMAGE_SIZE:
113             if (ms == null)
114                 ms = getMS_exactImageSize(idata[0], info.maxWidth, info.maxHeight);
115             break;
116         }
117         
118         if (ms != null) {
119             validator.addMessage(ms.fMessage, ms.fSeverity);
120         }
121         
122         return ms == null;
123     }
124     
125     public static boolean imageEntryHasValidIco(IValidatorMessageHandler validator, FormEntry provider, IProduct product) {
126         ValidationInfo info = new ValidationInfo();
127         return imageEntryInternalValidate(validator, provider, product, info, F_ICO_IMAGE);
128     }
129     
130     public static boolean imageEntrySizeDoesNotExceed(IValidatorMessageHandler validator,
131             FormEntry provider,
132             IProduct product, int mwidth, int mheight, int wwidth, int wheight) {
133         ValidationInfo info = new ValidationInfo();
134         info.maxWidth = mwidth;
135         info.maxHeight = mheight;
136         info.warningWidth = wwidth;
137         info.warningHeight = wheight;
138         return imageEntryInternalValidate(validator, provider, product, info, F_MAX_IMAGE_SIZE);
139     }
140     
141     public static boolean imageEntryHasExactSize(IValidatorMessageHandler validator,
142             FormEntry provider,
143             IProduct product, int width, int height) {
144         ValidationInfo info = new ValidationInfo();
145         info.maxWidth = width;
146         info.maxHeight = height;
147         return imageEntryInternalValidate(validator, provider, product, info, F_EXACT_IMAGE_SIZE);
148     }
149     
150     public static boolean imageEntryHasExactDepthAndSize(IValidatorMessageHandler validator,
151             FormEntry provider,
152             IProduct product, int width, int height, int depth) {
153         ValidationInfo info = new ValidationInfo();
154         info.maxWidth = width;
155         info.maxHeight = height;
156         info.requiredDepth = depth;
157         return imageEntryInternalValidate(validator, provider, product, info, F_IMAGE_DEPTH);
158     }
159     
160     private static ValidationMessage getMS_icoImage(ImageData[] imagedata) {
161         int totalSizes = LauncherSection.F_WIN_ICON_DIMENSIONS.length;
162         boolean[] found = new boolean[totalSizes];
163         for (int i = 0; i < imagedata.length; i++) {
164             int width = imagedata[i].width;
165             int height = imagedata[i].height;
166             int depth = imagedata[i].depth;
167             for (int w = 0; w < totalSizes; w++)
168                 if (width == LauncherSection.F_WIN_ICON_DIMENSIONS[w][0] &&
169                         height == LauncherSection.F_WIN_ICON_DIMENSIONS[w][1] &&
170                         depth == LauncherSection.F_WIN_ICON_DEPTHS[w])
171                     found[w] = true;
172         }
173         StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
174         for (int i = 0; i < found.length; i++) {
175             if (!found[i]) {
176                 if (sb.length() == 0)
177                     sb.append(PDEUIMessages.EditorUtilities_icoError);
178                 else
179                     sb.append(", "); //$NON-NLS-1$
180
int width = LauncherSection.F_WIN_ICON_DIMENSIONS[i][0];
181                 int height = LauncherSection.F_WIN_ICON_DIMENSIONS[i][1];
182                 int depth = LauncherSection.F_WIN_ICON_DEPTHS[i];
183                 sb.append(NLS.bind(PDEUIMessages.EditorUtilities_missingIcoNote, getSizeString(width, height), Integer.toString(depth)));
184             }
185         }
186         if (sb.length() > 0)
187             return new ValidationMessage(sb.toString());
188         return null;
189     }
190
191     private static ValidationMessage getMS_exactImageSize(ImageData imagedata, int mwidth, int mheight) {
192         int width = imagedata.width;
193         int height = imagedata.height;
194         if (width != mwidth || height != mheight)
195             return new ValidationMessage(
196                     NLS.bind(PDEUIMessages.EditorUtilities_incorrectSize,
197                             getSizeString(width, height)));
198         return null;
199     }
200     
201     private static ValidationMessage getMS_maxImageSize(ImageData imagedata, int mwidth, int mheight, int wwidth, int wheight) {
202         int width = imagedata.width;
203         int height = imagedata.height;
204         if (width > mwidth || height > mheight)
205             return new ValidationMessage(
206                     NLS.bind(PDEUIMessages.EditorUtilities_imageTooLarge,
207                             getSizeString(width, height)));
208         else if (width > wwidth || height > wheight)
209             return new ValidationMessage(
210                     NLS.bind(PDEUIMessages.EditorUtilities_imageTooLargeInfo, getSizeString(wwidth, wheight)));
211         return null;
212     }
213     
214     private static ValidationMessage getMS_imageDepth(ImageData imagedata, int depth) {
215         if (imagedata.depth != depth)
216             return new ValidationMessage(
217                     NLS.bind(PDEUIMessages.EditorUtilities_incorrectImageDepth, Integer.toString(imagedata.depth)));
218         return null;
219     }
220     
221     private static String JavaDoc getSizeString(int width, int height) {
222         return width + " x " + height; //$NON-NLS-1$
223
}
224     
225     private static IPath getFullPath(IPath path, IProduct product) throws MalformedURLException JavaDoc {
226         String JavaDoc filePath = path.toString();
227         IWorkspaceRoot root = PDEPlugin.getWorkspace().getRoot();
228         // look in root
229
if (filePath.indexOf('/') == 0) {
230             IResource resource = root.findMember(filePath);
231             if (resource != null)
232                 return new Path("file:", resource.getLocation().toString()); //$NON-NLS-1$
233
throw new MalformedURLException JavaDoc();
234         }
235         // look in project
236
IProject project = product.getModel().getUnderlyingResource().getProject();
237         IResource resource = project.findMember(filePath);
238         if (resource != null)
239             return new Path("file:", resource.getLocation().toString()); //$NON-NLS-1$
240

241         // look in external models
242
IPluginModelBase model = PluginRegistry.findModel(product.getDefiningPluginId());
243         if (model != null && model.getInstallLocation() != null) {
244             File JavaDoc modelNode = new File JavaDoc(model.getInstallLocation());
245             String JavaDoc pluginPath = modelNode.getAbsolutePath();
246             if (modelNode.isFile() && CoreUtility.jarContainsResource(modelNode, filePath, false))
247                 return new Path("jar:file:", pluginPath + "!/" + filePath); //$NON-NLS-1$ //$NON-NLS-2$
248
return new Path("file:", pluginPath + "/" + filePath); //$NON-NLS-1$ //$NON-NLS-2$
249
}
250         // no file found - throw exception
251
throw new MalformedURLException JavaDoc();
252     }
253     
254     private static IPath getRootPath(IPath path, String JavaDoc definingPluginId) {
255         IPluginModelBase model = PluginRegistry.findModel(definingPluginId);
256         if (model != null && model.getInstallLocation() != null) {
257             IPath newPath = new Path(model.getInstallLocation()).append(path);
258             IWorkspaceRoot root = PDEPlugin.getWorkspace().getRoot();
259             IContainer container = root.getContainerForLocation(newPath);
260             if (container != null)
261                 return container.getFullPath();
262         }
263         return path;
264     }
265     
266     private static IResource getImageResource(String JavaDoc value, String JavaDoc definingPluginId) {
267         if (value == null)
268             return null;
269         IPath path = new Path(value);
270         if (path.isEmpty())
271             return null;
272         
273         if (!path.isAbsolute()) {
274             path = getRootPath(path, definingPluginId);
275         }
276         IWorkspaceRoot root = PDEPlugin.getWorkspace().getRoot();
277         return root.findMember(path);
278     }
279     
280     public static void openImage(String JavaDoc value, String JavaDoc definingPluginId) {
281         IResource resource = getImageResource(value, definingPluginId);
282         try {
283             if (resource != null && resource instanceof IFile)
284                 IDE.openEditor(PDEPlugin.getActivePage(), (IFile)resource, true);
285             else
286                 MessageDialog.openWarning(PDEPlugin.getActiveWorkbenchShell(), PDEUIMessages.AboutSection_open, PDEUIMessages.AboutSection_warning); //
287
} catch (PartInitException e) {
288         }
289     }
290
291 }
292
Popular Tags