1 11 package org.eclipse.jdt.internal.ui.compare; 12 13 import java.io.BufferedReader ; 14 import java.io.IOException ; 15 import java.io.InputStream ; 16 import java.io.InputStreamReader ; 17 import java.io.UnsupportedEncodingException ; 18 import java.util.ArrayList ; 19 import java.util.List ; 20 import java.util.MissingResourceException ; 21 import java.util.ResourceBundle ; 22 23 import org.eclipse.core.runtime.Assert; 24 import org.eclipse.core.runtime.CoreException; 25 import org.eclipse.core.runtime.IPath; 26 27 import org.eclipse.core.resources.ResourcesPlugin; 28 29 import org.eclipse.swt.graphics.Image; 30 31 import org.eclipse.jface.action.IAction; 32 import org.eclipse.jface.resource.ImageDescriptor; 33 34 import org.eclipse.jface.text.IDocument; 35 import org.eclipse.jface.text.IDocumentPartitioner; 36 37 import org.eclipse.compare.CompareConfiguration; 38 import org.eclipse.compare.IEncodedStreamContentAccessor; 39 import org.eclipse.compare.IStreamContentAccessor; 40 41 import org.eclipse.jdt.core.IJavaElement; 42 import org.eclipse.jdt.core.IMember; 43 import org.eclipse.jdt.core.IType; 44 45 import org.eclipse.jdt.ui.JavaElementLabels; 46 import org.eclipse.jdt.ui.text.IJavaPartitions; 47 import org.eclipse.jdt.ui.text.JavaTextTools; 48 49 import org.eclipse.jdt.internal.ui.JavaPlugin; 50 import org.eclipse.jdt.internal.ui.JavaPluginImages; 51 import org.eclipse.jdt.internal.ui.propertiesfileeditor.PropertiesFileDocumentSetupParticipant; 52 53 54 class JavaCompareUtilities { 55 56 private static final char PACKAGEDECLARATION= '%'; 57 private static final char IMPORTDECLARATION= '#'; 58 private static final char IMPORT_CONTAINER= '<'; 59 private static final char FIELD= '^'; 60 private static final char METHOD= '~'; 61 private static final char INITIALIZER= '|'; 62 private static final char COMPILATIONUNIT= '{'; 63 private static final char TYPE= '['; 64 65 static String getString(ResourceBundle bundle, String key, String dfltValue) { 66 67 if (bundle != null) { 68 try { 69 return bundle.getString(key); 70 } catch (MissingResourceException x) { 71 } 73 } 74 return dfltValue; 75 } 76 77 static String getString(ResourceBundle bundle, String key) { 78 return getString(bundle, key, key); 79 } 80 81 static int getInteger(ResourceBundle bundle, String key, int dfltValue) { 82 83 if (bundle != null) { 84 try { 85 String s= bundle.getString(key); 86 if (s != null) 87 return Integer.parseInt(s); 88 } catch (NumberFormatException x) { 89 } catch (MissingResourceException x) { 91 } 93 } 94 return dfltValue; 95 } 96 97 static ImageDescriptor getImageDescriptor(int type) { 98 switch (type) { 99 case IJavaElement.INITIALIZER: 100 case IJavaElement.METHOD: 101 return getImageDescriptor("obj16/compare_method.gif"); case IJavaElement.FIELD: 103 return getImageDescriptor("obj16/compare_field.gif"); case IJavaElement.PACKAGE_DECLARATION: 105 return JavaPluginImages.DESC_OBJS_PACKDECL; 106 case IJavaElement.IMPORT_DECLARATION: 107 return JavaPluginImages.DESC_OBJS_IMPDECL; 108 case IJavaElement.IMPORT_CONTAINER: 109 return JavaPluginImages.DESC_OBJS_IMPCONT; 110 case IJavaElement.COMPILATION_UNIT: 111 return JavaPluginImages.DESC_OBJS_CUNIT; 112 } 113 return ImageDescriptor.getMissingImageDescriptor(); 114 } 115 116 static ImageDescriptor getTypeImageDescriptor(boolean isClass) { 117 if (isClass) 118 return JavaPluginImages.DESC_OBJS_CLASS; 119 return JavaPluginImages.DESC_OBJS_INTERFACE; 120 } 121 122 static ImageDescriptor getEnumImageDescriptor() { 123 return JavaPluginImages.DESC_OBJS_ENUM; 124 } 125 126 static ImageDescriptor getAnnotationImageDescriptor() { 127 return JavaPluginImages.DESC_OBJS_ANNOTATION; 128 } 129 130 static ImageDescriptor getImageDescriptor(IMember element) { 131 int t= element.getElementType(); 132 if (t == IJavaElement.TYPE) { 133 IType type= (IType) element; 134 try { 135 return getTypeImageDescriptor(type.isClass()); 136 } catch (CoreException e) { 137 JavaPlugin.log(e); 138 return JavaPluginImages.DESC_OBJS_GHOST; 139 } 140 } 141 return getImageDescriptor(t); 142 } 143 144 148 static String getJavaElementID(IJavaElement je) { 149 150 if (je instanceof IMember && ((IMember)je).isBinary()) 151 return null; 152 153 StringBuffer sb= new StringBuffer (); 154 155 switch (je.getElementType()) { 156 case IJavaElement.COMPILATION_UNIT: 157 sb.append(COMPILATIONUNIT); 158 break; 159 case IJavaElement.TYPE: 160 sb.append(TYPE); 161 sb.append(je.getElementName()); 162 break; 163 case IJavaElement.FIELD: 164 sb.append(FIELD); 165 sb.append(je.getElementName()); 166 break; 167 case IJavaElement.METHOD: 168 sb.append(METHOD); 169 sb.append(JavaElementLabels.getElementLabel(je, JavaElementLabels.M_PARAMETER_TYPES)); 170 break; 171 case IJavaElement.INITIALIZER: 172 String id= je.getHandleIdentifier(); 173 int pos= id.lastIndexOf(INITIALIZER); 174 if (pos >= 0) 175 sb.append(id.substring(pos)); 176 break; 177 case IJavaElement.PACKAGE_DECLARATION: 178 sb.append(PACKAGEDECLARATION); 179 break; 180 case IJavaElement.IMPORT_CONTAINER: 181 sb.append(IMPORT_CONTAINER); 182 break; 183 case IJavaElement.IMPORT_DECLARATION: 184 sb.append(IMPORTDECLARATION); 185 sb.append(je.getElementName()); 186 break; 187 default: 188 return null; 189 } 190 return sb.toString(); 191 } 192 193 197 static String buildID(int type, String name) { 198 StringBuffer sb= new StringBuffer (); 199 switch (type) { 200 case JavaNode.CU: 201 sb.append(COMPILATIONUNIT); 202 break; 203 case JavaNode.CLASS: 204 case JavaNode.INTERFACE: 205 case JavaNode.ENUM: 206 case JavaNode.ANNOTATION: 207 sb.append(TYPE); 208 sb.append(name); 209 break; 210 case JavaNode.FIELD: 211 sb.append(FIELD); 212 sb.append(name); 213 break; 214 case JavaNode.CONSTRUCTOR: 215 case JavaNode.METHOD: 216 sb.append(METHOD); 217 sb.append(name); 218 break; 219 case JavaNode.INIT: 220 sb.append(INITIALIZER); 221 sb.append(name); 222 break; 223 case JavaNode.PACKAGE: 224 sb.append(PACKAGEDECLARATION); 225 break; 226 case JavaNode.IMPORT: 227 sb.append(IMPORTDECLARATION); 228 sb.append(name); 229 break; 230 case JavaNode.IMPORT_CONTAINER: 231 sb.append(IMPORT_CONTAINER); 232 break; 233 default: 234 Assert.isTrue(false); 235 break; 236 } 237 return sb.toString(); 238 } 239 240 static ImageDescriptor getImageDescriptor(String relativePath) { 241 IPath path= JavaPluginImages.ICONS_PATH.append(relativePath); 242 return JavaPluginImages.createImageDescriptor(JavaPlugin.getDefault().getBundle(), path, true); 243 } 244 245 static boolean getBoolean(CompareConfiguration cc, String key, boolean dflt) { 246 if (cc != null) { 247 Object value= cc.getProperty(key); 248 if (value instanceof Boolean ) 249 return ((Boolean ) value).booleanValue(); 250 } 251 return dflt; 252 } 253 254 static Image getImage(IMember member) { 255 ImageDescriptor id= getImageDescriptor(member); 256 return id.createImage(); 257 } 258 259 static JavaTextTools getJavaTextTools() { 260 JavaPlugin plugin= JavaPlugin.getDefault(); 261 if (plugin != null) 262 return plugin.getJavaTextTools(); 263 return null; 264 } 265 266 static IDocumentPartitioner createJavaPartitioner() { 267 JavaTextTools tools= getJavaTextTools(); 268 if (tools != null) 269 return tools.createDocumentPartitioner(); 270 return null; 271 } 272 273 static void setupDocument(IDocument document) { 274 JavaTextTools tools= getJavaTextTools(); 275 if (tools != null) 276 tools.setupJavaDocumentPartitioner(document, IJavaPartitions.JAVA_PARTITIONING); 277 } 278 279 static void setupPropertiesFileDocument(IDocument document) { 280 PropertiesFileDocumentSetupParticipant.setupDocument(document); 281 } 282 283 289 private static String readString(InputStream is, String encoding) { 290 if (is == null) 291 return null; 292 BufferedReader reader= null; 293 try { 294 StringBuffer buffer= new StringBuffer (); 295 char[] part= new char[2048]; 296 int read= 0; 297 reader= new BufferedReader (new InputStreamReader (is, encoding)); 298 299 while ((read= reader.read(part)) != -1) 300 buffer.append(part, 0, read); 301 302 return buffer.toString(); 303 304 } catch (IOException ex) { 305 } finally { 307 if (reader != null) { 308 try { 309 reader.close(); 310 } catch (IOException ex) { 311 } 313 } 314 } 315 return null; 316 } 317 318 public static String readString(IStreamContentAccessor sa) throws CoreException { 319 InputStream is= sa.getContents(); 320 if (is != null) { 321 String encoding= null; 322 if (sa instanceof IEncodedStreamContentAccessor) { 323 try { 324 encoding= ((IEncodedStreamContentAccessor) sa).getCharset(); 325 } catch (Exception e) { 326 } 327 } 328 if (encoding == null) 329 encoding= ResourcesPlugin.getEncoding(); 330 return readString(is, encoding); 331 } 332 return null; 333 } 334 335 339 static byte[] getBytes(String s, String encoding) { 340 try { 341 return s.getBytes(encoding); 342 } catch (UnsupportedEncodingException e) { 343 return s.getBytes(); 344 } 345 } 346 347 353 static String [] readLines(InputStream is2, String encoding) { 354 355 BufferedReader reader= null; 356 try { 357 reader= new BufferedReader (new InputStreamReader (is2, encoding)); 358 StringBuffer sb= new StringBuffer (); 359 List list= new ArrayList (); 360 while (true) { 361 int c= reader.read(); 362 if (c == -1) 363 break; 364 sb.append((char)c); 365 if (c == '\r') { c= reader.read(); 367 if (c == -1) 368 break; 369 sb.append((char)c); 370 if (c == '\n') { 371 list.add(sb.toString()); 372 sb= new StringBuffer (); 373 } 374 } else if (c == '\n') { list.add(sb.toString()); 376 sb= new StringBuffer (); 377 } 378 } 379 if (sb.length() > 0) 380 list.add(sb.toString()); 381 return (String []) list.toArray(new String [list.size()]); 382 383 } catch (IOException ex) { 384 return null; 385 386 } finally { 387 if (reader != null) { 388 try { 389 reader.close(); 390 } catch (IOException ex) { 391 } 393 } 394 } 395 } 396 397 400 static void initAction(IAction a, ResourceBundle bundle, String prefix) { 401 402 String labelKey= "label"; String tooltipKey= "tooltip"; String imageKey= "image"; String descriptionKey= "description"; 407 if (prefix != null && prefix.length() > 0) { 408 labelKey= prefix + labelKey; 409 tooltipKey= prefix + tooltipKey; 410 imageKey= prefix + imageKey; 411 descriptionKey= prefix + descriptionKey; 412 } 413 414 a.setText(getString(bundle, labelKey, labelKey)); 415 a.setToolTipText(getString(bundle, tooltipKey, null)); 416 a.setDescription(getString(bundle, descriptionKey, null)); 417 418 String relPath= getString(bundle, imageKey, null); 419 if (relPath != null && relPath.trim().length() > 0) { 420 421 String dPath; 422 String ePath; 423 424 if (relPath.indexOf("/") >= 0) { String path= relPath.substring(1); 426 dPath= 'd' + path; 427 ePath= 'e' + path; 428 } else { 429 dPath= "dlcl16/" + relPath; ePath= "elcl16/" + relPath; } 432 433 ImageDescriptor id= JavaCompareUtilities.getImageDescriptor(dPath); if (id != null) 435 a.setDisabledImageDescriptor(id); 436 id= JavaCompareUtilities.getImageDescriptor(ePath); 437 if (id != null) { 438 a.setImageDescriptor(id); 439 a.setHoverImageDescriptor(id); 440 } 441 } 442 } 443 444 static void initToggleAction(IAction a, ResourceBundle bundle, String prefix, boolean checked) { 445 446 String tooltip= null; 447 if (checked) 448 tooltip= getString(bundle, prefix + "tooltip.checked", null); else 450 tooltip= getString(bundle, prefix + "tooltip.unchecked", null); if (tooltip == null) 452 tooltip= getString(bundle, prefix + "tooltip", null); 454 if (tooltip != null) 455 a.setToolTipText(tooltip); 456 457 String description= null; 458 if (checked) 459 description= getString(bundle, prefix + "description.checked", null); else 461 description= getString(bundle, prefix + "description.unchecked", null); if (description == null) 463 description= getString(bundle, prefix + "description", null); 465 if (description != null) 466 a.setDescription(description); 467 } 468 } 469 | Popular Tags |