1 11 package org.eclipse.pde.internal.ui.editor; 12 13 import java.io.File ; 14 import java.io.IOException ; 15 import java.io.InputStream ; 16 import java.net.MalformedURLException ; 17 import java.net.URL ; 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 fMessage; 54 int fSeverity = IMessageProvider.WARNING; 55 ValidationMessage(String 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 imagePath = provider.getText().getText(); 69 String message = null; 70 try { 71 IPath path = getFullPath(new Path(imagePath), product); 72 URL url = new URL (path.toString()); 73 ImageLoader loader = new ImageLoader(); 74 InputStream 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 e) { 83 message = PDEUIMessages.EditorUtilities_invalidFilePath; 84 } catch (IOException 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: 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 sb = new StringBuffer (); 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(", "); 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 getSizeString(int width, int height) { 222 return width + " x " + height; } 224 225 private static IPath getFullPath(IPath path, IProduct product) throws MalformedURLException { 226 String filePath = path.toString(); 227 IWorkspaceRoot root = PDEPlugin.getWorkspace().getRoot(); 228 if (filePath.indexOf('/') == 0) { 230 IResource resource = root.findMember(filePath); 231 if (resource != null) 232 return new Path("file:", resource.getLocation().toString()); throw new MalformedURLException (); 234 } 235 IProject project = product.getModel().getUnderlyingResource().getProject(); 237 IResource resource = project.findMember(filePath); 238 if (resource != null) 239 return new Path("file:", resource.getLocation().toString()); 241 IPluginModelBase model = PluginRegistry.findModel(product.getDefiningPluginId()); 243 if (model != null && model.getInstallLocation() != null) { 244 File modelNode = new File (model.getInstallLocation()); 245 String pluginPath = modelNode.getAbsolutePath(); 246 if (modelNode.isFile() && CoreUtility.jarContainsResource(modelNode, filePath, false)) 247 return new Path("jar:file:", pluginPath + "!/" + filePath); return new Path("file:", pluginPath + "/" + filePath); } 250 throw new MalformedURLException (); 252 } 253 254 private static IPath getRootPath(IPath path, String 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 value, String 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 value, String 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); } catch (PartInitException e) { 288 } 289 } 290 291 } 292 | Popular Tags |