001: /*******************************************************************************
002: * Copyright (c) 2005, 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;
011:
012: import java.io.File;
013: import java.io.IOException;
014: import java.io.InputStream;
015: import java.net.MalformedURLException;
016: import java.net.URL;
017:
018: import org.eclipse.core.resources.IContainer;
019: import org.eclipse.core.resources.IFile;
020: import org.eclipse.core.resources.IProject;
021: import org.eclipse.core.resources.IResource;
022: import org.eclipse.core.resources.IWorkspaceRoot;
023: import org.eclipse.core.runtime.IPath;
024: import org.eclipse.core.runtime.Path;
025: import org.eclipse.jface.dialogs.IMessageProvider;
026: import org.eclipse.jface.dialogs.MessageDialog;
027: import org.eclipse.osgi.util.NLS;
028: import org.eclipse.pde.core.plugin.IPluginModelBase;
029: import org.eclipse.pde.core.plugin.PluginRegistry;
030: import org.eclipse.pde.internal.core.iproduct.IProduct;
031: import org.eclipse.pde.internal.core.util.CoreUtility;
032: import org.eclipse.pde.internal.ui.PDEPlugin;
033: import org.eclipse.pde.internal.ui.PDEUIMessages;
034: import org.eclipse.pde.internal.ui.editor.product.LauncherSection;
035: import org.eclipse.pde.internal.ui.editor.validation.IValidatorMessageHandler;
036: import org.eclipse.pde.internal.ui.parts.FormEntry;
037: import org.eclipse.swt.SWTException;
038: import org.eclipse.swt.graphics.ImageData;
039: import org.eclipse.swt.graphics.ImageLoader;
040: import org.eclipse.ui.PartInitException;
041: import org.eclipse.ui.ide.IDE;
042:
043: public class EditorUtilities {
044:
045: private static final int F_EXACT_IMAGE_SIZE = 0;
046: private static final int F_MAX_IMAGE_SIZE = 1;
047: private static final int F_ICO_IMAGE = 2;
048: private static final int F_IMAGE_DEPTH = 3;
049:
050: static class ValidationMessage {
051: String fMessage;
052: int fSeverity = IMessageProvider.WARNING;
053:
054: ValidationMessage(String message) {
055: fMessage = message;
056: }
057: }
058:
059: static class ValidationInfo {
060: int maxWidth, maxHeight, warningWidth, warningHeight,
061: requiredDepth;
062: }
063:
064: private static ImageData[] getImageData(
065: IValidatorMessageHandler validator, FormEntry provider,
066: IProduct product) {
067: String imagePath = provider.getText().getText();
068: String message = null;
069: try {
070: IPath path = getFullPath(new Path(imagePath), product);
071: URL url = new URL(path.toString());
072: ImageLoader loader = new ImageLoader();
073: InputStream stream = url.openStream();
074: ImageData[] idata = loader.load(stream);
075: stream.close();
076: if (idata != null && idata.length > 0)
077: return idata;
078: message = PDEUIMessages.EditorUtilities_noImageData;
079: } catch (SWTException e) {
080: message = PDEUIMessages.EditorUtilities_pathNotValidImage;
081: } catch (MalformedURLException e) {
082: message = PDEUIMessages.EditorUtilities_invalidFilePath;
083: } catch (IOException e) {
084: message = PDEUIMessages.EditorUtilities_invalidFilePath;
085: }
086: validator.addMessage(message, IMessageProvider.WARNING);
087: return null;
088: }
089:
090: private static boolean containsEmptyField(FormEntry provider) {
091: return provider.getText().getText().length() == 0;
092: }
093:
094: private static boolean imageEntryInternalValidate(
095: IValidatorMessageHandler validator, FormEntry provider,
096: IProduct product, ValidationInfo info, int validationType) {
097: if (containsEmptyField(provider))
098: return true;
099: ImageData[] idata = getImageData(validator, provider, product);
100: if (idata == null)
101: return false;
102:
103: ValidationMessage ms = null;
104: switch (validationType) {
105: case F_MAX_IMAGE_SIZE:
106: ms = getMS_maxImageSize(idata[0], info.maxWidth,
107: info.maxHeight, info.warningWidth,
108: info.warningHeight);
109: break;
110: case F_ICO_IMAGE:
111: ms = getMS_icoImage(idata);
112: break;
113: case F_IMAGE_DEPTH: // do not break after F_IMAGEDEPTH since we are also checking exact size
114: ms = getMS_imageDepth(idata[0], info.requiredDepth);
115: case F_EXACT_IMAGE_SIZE:
116: if (ms == null)
117: ms = getMS_exactImageSize(idata[0], info.maxWidth,
118: info.maxHeight);
119: break;
120: }
121:
122: if (ms != null) {
123: validator.addMessage(ms.fMessage, ms.fSeverity);
124: }
125:
126: return ms == null;
127: }
128:
129: public static boolean imageEntryHasValidIco(
130: IValidatorMessageHandler validator, FormEntry provider,
131: IProduct product) {
132: ValidationInfo info = new ValidationInfo();
133: return imageEntryInternalValidate(validator, provider, product,
134: info, F_ICO_IMAGE);
135: }
136:
137: public static boolean imageEntrySizeDoesNotExceed(
138: IValidatorMessageHandler validator, FormEntry provider,
139: IProduct product, int mwidth, int mheight, int wwidth,
140: int wheight) {
141: ValidationInfo info = new ValidationInfo();
142: info.maxWidth = mwidth;
143: info.maxHeight = mheight;
144: info.warningWidth = wwidth;
145: info.warningHeight = wheight;
146: return imageEntryInternalValidate(validator, provider, product,
147: info, F_MAX_IMAGE_SIZE);
148: }
149:
150: public static boolean imageEntryHasExactSize(
151: IValidatorMessageHandler validator, FormEntry provider,
152: IProduct product, int width, int height) {
153: ValidationInfo info = new ValidationInfo();
154: info.maxWidth = width;
155: info.maxHeight = height;
156: return imageEntryInternalValidate(validator, provider, product,
157: info, F_EXACT_IMAGE_SIZE);
158: }
159:
160: public static boolean imageEntryHasExactDepthAndSize(
161: IValidatorMessageHandler validator, FormEntry provider,
162: IProduct product, int width, int height, int depth) {
163: ValidationInfo info = new ValidationInfo();
164: info.maxWidth = width;
165: info.maxHeight = height;
166: info.requiredDepth = depth;
167: return imageEntryInternalValidate(validator, provider, product,
168: info, F_IMAGE_DEPTH);
169: }
170:
171: private static ValidationMessage getMS_icoImage(
172: ImageData[] imagedata) {
173: int totalSizes = LauncherSection.F_WIN_ICON_DIMENSIONS.length;
174: boolean[] found = new boolean[totalSizes];
175: for (int i = 0; i < imagedata.length; i++) {
176: int width = imagedata[i].width;
177: int height = imagedata[i].height;
178: int depth = imagedata[i].depth;
179: for (int w = 0; w < totalSizes; w++)
180: if (width == LauncherSection.F_WIN_ICON_DIMENSIONS[w][0]
181: && height == LauncherSection.F_WIN_ICON_DIMENSIONS[w][1]
182: && depth == LauncherSection.F_WIN_ICON_DEPTHS[w])
183: found[w] = true;
184: }
185: StringBuffer sb = new StringBuffer();
186: for (int i = 0; i < found.length; i++) {
187: if (!found[i]) {
188: if (sb.length() == 0)
189: sb.append(PDEUIMessages.EditorUtilities_icoError);
190: else
191: sb.append(", "); //$NON-NLS-1$
192: int width = LauncherSection.F_WIN_ICON_DIMENSIONS[i][0];
193: int height = LauncherSection.F_WIN_ICON_DIMENSIONS[i][1];
194: int depth = LauncherSection.F_WIN_ICON_DEPTHS[i];
195: sb.append(NLS.bind(
196: PDEUIMessages.EditorUtilities_missingIcoNote,
197: getSizeString(width, height), Integer
198: .toString(depth)));
199: }
200: }
201: if (sb.length() > 0)
202: return new ValidationMessage(sb.toString());
203: return null;
204: }
205:
206: private static ValidationMessage getMS_exactImageSize(
207: ImageData imagedata, int mwidth, int mheight) {
208: int width = imagedata.width;
209: int height = imagedata.height;
210: if (width != mwidth || height != mheight)
211: return new ValidationMessage(NLS.bind(
212: PDEUIMessages.EditorUtilities_incorrectSize,
213: getSizeString(width, height)));
214: return null;
215: }
216:
217: private static ValidationMessage getMS_maxImageSize(
218: ImageData imagedata, int mwidth, int mheight, int wwidth,
219: int wheight) {
220: int width = imagedata.width;
221: int height = imagedata.height;
222: if (width > mwidth || height > mheight)
223: return new ValidationMessage(NLS.bind(
224: PDEUIMessages.EditorUtilities_imageTooLarge,
225: getSizeString(width, height)));
226: else if (width > wwidth || height > wheight)
227: return new ValidationMessage(NLS.bind(
228: PDEUIMessages.EditorUtilities_imageTooLargeInfo,
229: getSizeString(wwidth, wheight)));
230: return null;
231: }
232:
233: private static ValidationMessage getMS_imageDepth(
234: ImageData imagedata, int depth) {
235: if (imagedata.depth != depth)
236: return new ValidationMessage(NLS.bind(
237: PDEUIMessages.EditorUtilities_incorrectImageDepth,
238: Integer.toString(imagedata.depth)));
239: return null;
240: }
241:
242: private static String getSizeString(int width, int height) {
243: return width + " x " + height; //$NON-NLS-1$
244: }
245:
246: private static IPath getFullPath(IPath path, IProduct product)
247: throws MalformedURLException {
248: String filePath = path.toString();
249: IWorkspaceRoot root = PDEPlugin.getWorkspace().getRoot();
250: // look in root
251: if (filePath.indexOf('/') == 0) {
252: IResource resource = root.findMember(filePath);
253: if (resource != null)
254: return new Path(
255: "file:", resource.getLocation().toString()); //$NON-NLS-1$
256: throw new MalformedURLException();
257: }
258: // look in project
259: IProject project = product.getModel().getUnderlyingResource()
260: .getProject();
261: IResource resource = project.findMember(filePath);
262: if (resource != null)
263: return new Path("file:", resource.getLocation().toString()); //$NON-NLS-1$
264:
265: // look in external models
266: IPluginModelBase model = PluginRegistry.findModel(product
267: .getDefiningPluginId());
268: if (model != null && model.getInstallLocation() != null) {
269: File modelNode = new File(model.getInstallLocation());
270: String pluginPath = modelNode.getAbsolutePath();
271: if (modelNode.isFile()
272: && CoreUtility.jarContainsResource(modelNode,
273: filePath, false))
274: return new Path(
275: "jar:file:", pluginPath + "!/" + filePath); //$NON-NLS-1$ //$NON-NLS-2$
276: return new Path("file:", pluginPath + "/" + filePath); //$NON-NLS-1$ //$NON-NLS-2$
277: }
278: // no file found - throw exception
279: throw new MalformedURLException();
280: }
281:
282: private static IPath getRootPath(IPath path, String definingPluginId) {
283: IPluginModelBase model = PluginRegistry
284: .findModel(definingPluginId);
285: if (model != null && model.getInstallLocation() != null) {
286: IPath newPath = new Path(model.getInstallLocation())
287: .append(path);
288: IWorkspaceRoot root = PDEPlugin.getWorkspace().getRoot();
289: IContainer container = root
290: .getContainerForLocation(newPath);
291: if (container != null)
292: return container.getFullPath();
293: }
294: return path;
295: }
296:
297: private static IResource getImageResource(String value,
298: String definingPluginId) {
299: if (value == null)
300: return null;
301: IPath path = new Path(value);
302: if (path.isEmpty())
303: return null;
304:
305: if (!path.isAbsolute()) {
306: path = getRootPath(path, definingPluginId);
307: }
308: IWorkspaceRoot root = PDEPlugin.getWorkspace().getRoot();
309: return root.findMember(path);
310: }
311:
312: public static void openImage(String value, String definingPluginId) {
313: IResource resource = getImageResource(value, definingPluginId);
314: try {
315: if (resource != null && resource instanceof IFile)
316: IDE.openEditor(PDEPlugin.getActivePage(),
317: (IFile) resource, true);
318: else
319: MessageDialog.openWarning(PDEPlugin
320: .getActiveWorkbenchShell(),
321: PDEUIMessages.AboutSection_open,
322: PDEUIMessages.AboutSection_warning); //
323: } catch (PartInitException e) {
324: }
325: }
326:
327: }
|