KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > internal > ui > compare > JavaCompareUtilities


1 /*******************************************************************************
2  * Copyright (c) 2000, 2006 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.jdt.internal.ui.compare;
12
13 import java.io.BufferedReader JavaDoc;
14 import java.io.IOException JavaDoc;
15 import java.io.InputStream JavaDoc;
16 import java.io.InputStreamReader JavaDoc;
17 import java.io.UnsupportedEncodingException JavaDoc;
18 import java.util.ArrayList JavaDoc;
19 import java.util.List JavaDoc;
20 import java.util.MissingResourceException JavaDoc;
21 import java.util.ResourceBundle JavaDoc;
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 JavaDoc getString(ResourceBundle JavaDoc bundle, String JavaDoc key, String JavaDoc dfltValue) {
66         
67         if (bundle != null) {
68             try {
69                 return bundle.getString(key);
70             } catch (MissingResourceException JavaDoc x) {
71                 // NeedWork
72
}
73         }
74         return dfltValue;
75     }
76     
77     static String JavaDoc getString(ResourceBundle JavaDoc bundle, String JavaDoc key) {
78         return getString(bundle, key, key);
79     }
80     
81     static int getInteger(ResourceBundle JavaDoc bundle, String JavaDoc key, int dfltValue) {
82         
83         if (bundle != null) {
84             try {
85                 String JavaDoc s= bundle.getString(key);
86                 if (s != null)
87                     return Integer.parseInt(s);
88             } catch (NumberFormatException JavaDoc x) {
89                 // NeedWork
90
} catch (MissingResourceException JavaDoc x) {
91                 // NeedWork
92
}
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"); //$NON-NLS-1$
102
case IJavaElement.FIELD:
103             return getImageDescriptor("obj16/compare_field.gif"); //$NON-NLS-1$
104
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     /**
145      * Returns a name for the given Java element that uses the same conventions
146      * as the JavaNode name of a corresponding element.
147      */

148     static String JavaDoc getJavaElementID(IJavaElement je) {
149         
150         if (je instanceof IMember && ((IMember)je).isBinary())
151             return null;
152             
153         StringBuffer JavaDoc sb= new StringBuffer JavaDoc();
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 JavaDoc 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     /**
194      * Returns a name which identifies the given typed name.
195      * The type is encoded as a single character at the beginning of the string.
196      */

197     static String JavaDoc buildID(int type, String JavaDoc name) {
198         StringBuffer JavaDoc sb= new StringBuffer JavaDoc();
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 JavaDoc 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 JavaDoc key, boolean dflt) {
246         if (cc != null) {
247             Object JavaDoc value= cc.getProperty(key);
248             if (value instanceof Boolean JavaDoc)
249                 return ((Boolean JavaDoc) 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     /**
284      * Reads the contents of the given input stream into a string.
285      * The function assumes that the input stream uses the platform's default encoding
286      * (<code>ResourcesPlugin.getEncoding()</code>).
287      * Returns null if an error occurred.
288      */

289     private static String JavaDoc readString(InputStream JavaDoc is, String JavaDoc encoding) {
290         if (is == null)
291             return null;
292         BufferedReader JavaDoc reader= null;
293         try {
294             StringBuffer JavaDoc buffer= new StringBuffer JavaDoc();
295             char[] part= new char[2048];
296             int read= 0;
297             reader= new BufferedReader JavaDoc(new InputStreamReader JavaDoc(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 JavaDoc ex) {
305             // NeedWork
306
} finally {
307             if (reader != null) {
308                 try {
309                     reader.close();
310                 } catch (IOException JavaDoc ex) {
311                     // silently ignored
312
}
313             }
314         }
315         return null;
316     }
317     
318     public static String JavaDoc readString(IStreamContentAccessor sa) throws CoreException {
319         InputStream JavaDoc is= sa.getContents();
320         if (is != null) {
321             String JavaDoc encoding= null;
322             if (sa instanceof IEncodedStreamContentAccessor) {
323                 try {
324                     encoding= ((IEncodedStreamContentAccessor) sa).getCharset();
325                 } catch (Exception JavaDoc e) {
326                 }
327             }
328             if (encoding == null)
329                 encoding= ResourcesPlugin.getEncoding();
330             return readString(is, encoding);
331         }
332         return null;
333     }
334
335     /**
336      * Returns the contents of the given string as an array of bytes
337      * in the platform's default encoding.
338      */

339     static byte[] getBytes(String JavaDoc s, String JavaDoc encoding) {
340         try {
341             return s.getBytes(encoding);
342         } catch (UnsupportedEncodingException JavaDoc e) {
343             return s.getBytes();
344         }
345     }
346     
347     /**
348      * Breaks the contents of the given input stream into an array of strings.
349      * The function assumes that the input stream uses the platform's default encoding
350      * (<code>ResourcesPlugin.getEncoding()</code>).
351      * Returns null if an error occurred.
352      */

353     static String JavaDoc[] readLines(InputStream JavaDoc is2, String JavaDoc encoding) {
354         
355         BufferedReader JavaDoc reader= null;
356         try {
357             reader= new BufferedReader JavaDoc(new InputStreamReader JavaDoc(is2, encoding));
358             StringBuffer JavaDoc sb= new StringBuffer JavaDoc();
359             List JavaDoc list= new ArrayList JavaDoc();
360             while (true) {
361                 int c= reader.read();
362                 if (c == -1)
363                     break;
364                 sb.append((char)c);
365                 if (c == '\r') { // single CR or a CR followed by LF
366
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 JavaDoc();
373                     }
374                 } else if (c == '\n') { // a single LF
375
list.add(sb.toString());
376                     sb= new StringBuffer JavaDoc();
377                 }
378             }
379             if (sb.length() > 0)
380                 list.add(sb.toString());
381             return (String JavaDoc[]) list.toArray(new String JavaDoc[list.size()]);
382
383         } catch (IOException JavaDoc ex) {
384             return null;
385
386         } finally {
387             if (reader != null) {
388                 try {
389                     reader.close();
390                 } catch (IOException JavaDoc ex) {
391                     // silently ignored
392
}
393             }
394         }
395     }
396     
397     /*
398      * Initialize the given Action from a ResourceBundle.
399      */

400     static void initAction(IAction a, ResourceBundle JavaDoc bundle, String JavaDoc prefix) {
401         
402         String JavaDoc labelKey= "label"; //$NON-NLS-1$
403
String JavaDoc tooltipKey= "tooltip"; //$NON-NLS-1$
404
String JavaDoc imageKey= "image"; //$NON-NLS-1$
405
String JavaDoc descriptionKey= "description"; //$NON-NLS-1$
406

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 JavaDoc relPath= getString(bundle, imageKey, null);
419         if (relPath != null && relPath.trim().length() > 0) {
420             
421             String JavaDoc dPath;
422             String JavaDoc ePath;
423             
424             if (relPath.indexOf("/") >= 0) { //$NON-NLS-1$
425
String JavaDoc path= relPath.substring(1);
426                 dPath= 'd' + path;
427                 ePath= 'e' + path;
428             } else {
429                 dPath= "dlcl16/" + relPath; //$NON-NLS-1$
430
ePath= "elcl16/" + relPath; //$NON-NLS-1$
431
}
432             
433             ImageDescriptor id= JavaCompareUtilities.getImageDescriptor(dPath); // we set the disabled image first (see PR 1GDDE87)
434
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 JavaDoc bundle, String JavaDoc prefix, boolean checked) {
445
446         String JavaDoc tooltip= null;
447         if (checked)
448             tooltip= getString(bundle, prefix + "tooltip.checked", null); //$NON-NLS-1$
449
else
450             tooltip= getString(bundle, prefix + "tooltip.unchecked", null); //$NON-NLS-1$
451
if (tooltip == null)
452             tooltip= getString(bundle, prefix + "tooltip", null); //$NON-NLS-1$
453

454         if (tooltip != null)
455             a.setToolTipText(tooltip);
456             
457         String JavaDoc description= null;
458         if (checked)
459             description= getString(bundle, prefix + "description.checked", null); //$NON-NLS-1$
460
else
461             description= getString(bundle, prefix + "description.unchecked", null); //$NON-NLS-1$
462
if (description == null)
463             description= getString(bundle, prefix + "description", null); //$NON-NLS-1$
464

465         if (description != null)
466             a.setDescription(description);
467     }
468 }
469
Popular Tags