KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ui > internal > util > Util


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
12 package org.eclipse.ui.internal.util;
13
14 import java.util.ArrayList JavaDoc;
15 import java.util.Collection JavaDoc;
16 import java.util.Collections JavaDoc;
17 import java.util.HashMap JavaDoc;
18 import java.util.HashSet JavaDoc;
19 import java.util.Iterator JavaDoc;
20 import java.util.List JavaDoc;
21 import java.util.Map JavaDoc;
22 import java.util.MissingResourceException JavaDoc;
23 import java.util.ResourceBundle JavaDoc;
24 import java.util.Set JavaDoc;
25 import java.util.SortedMap JavaDoc;
26 import java.util.SortedSet JavaDoc;
27 import java.util.StringTokenizer JavaDoc;
28 import java.util.TreeMap JavaDoc;
29 import java.util.TreeSet JavaDoc;
30
31 import org.eclipse.core.runtime.Assert;
32 import org.eclipse.core.runtime.CoreException;
33 import org.eclipse.core.runtime.IAdaptable;
34 import org.eclipse.core.runtime.IConfigurationElement;
35 import org.eclipse.core.runtime.IStatus;
36 import org.eclipse.core.runtime.Platform;
37 import org.eclipse.core.runtime.PlatformObject;
38 import org.eclipse.core.runtime.Status;
39 import org.eclipse.swt.widgets.Shell;
40 import org.eclipse.ui.IWorkbench;
41 import org.eclipse.ui.IWorkbenchWindow;
42 import org.eclipse.ui.PlatformUI;
43 import org.eclipse.ui.internal.WorkbenchPlugin;
44
45 public final class Util {
46
47     public final static SortedMap JavaDoc EMPTY_SORTED_MAP = Collections
48             .unmodifiableSortedMap(new TreeMap JavaDoc());
49
50     public final static SortedSet JavaDoc EMPTY_SORTED_SET = Collections
51             .unmodifiableSortedSet(new TreeSet JavaDoc());
52
53     public final static String JavaDoc ZERO_LENGTH_STRING = ""; //$NON-NLS-1$
54

55     /**
56      * Ensures that a string is not null. Converts null strings into empty
57      * strings, and leaves any other string unmodified. Use this to help
58      * wrap calls to methods that return null instead of the empty string.
59      * Can also help protect against implementation errors in methods that
60      * are not supposed to return null.
61      *
62      * @param input input string (may be null)
63      * @return input if not null, or the empty string if input is null
64      */

65     public static String JavaDoc safeString(String JavaDoc input) {
66         if (input != null) {
67             return input;
68         }
69
70         return ZERO_LENGTH_STRING;
71     }
72     
73     /**
74      * If it is possible to adapt the given object to the given type, this
75      * returns the adapter. Performs the following checks:
76      *
77      * <ol>
78      * <li>Returns <code>sourceObject</code> if it is an instance of the
79      * adapter type.</li>
80      * <li>If sourceObject implements IAdaptable, it is queried for adapters.</li>
81      * <li>If sourceObject is not an instance of PlatformObject (which would have
82      * already done so), the adapter manager is queried for adapters</li>
83      * </ol>
84      *
85      * Otherwise returns null.
86      *
87      * @param sourceObject
88      * object to adapt, or null
89      * @param adapterType
90      * type to adapt to
91      * @return a representation of sourceObject that is assignable to the
92      * adapter type, or null if no such representation exists
93      */

94     public static Object JavaDoc getAdapter(Object JavaDoc sourceObject, Class JavaDoc adapterType) {
95         Assert.isNotNull(adapterType);
96         if (sourceObject == null) {
97             return null;
98         }
99         if (adapterType.isInstance(sourceObject)) {
100             return sourceObject;
101         }
102
103         if (sourceObject instanceof IAdaptable) {
104             IAdaptable adaptable = (IAdaptable) sourceObject;
105
106             Object JavaDoc result = adaptable.getAdapter(adapterType);
107             if (result != null) {
108                 // Sanity-check
109
Assert.isTrue(adapterType.isInstance(result));
110                 return result;
111             }
112         }
113         
114         if (!(sourceObject instanceof PlatformObject)) {
115             Object JavaDoc result = Platform.getAdapterManager().getAdapter(sourceObject, adapterType);
116             if (result != null) {
117                 return result;
118             }
119         }
120
121         return null;
122     }
123
124     public static void assertInstance(Object JavaDoc object, Class JavaDoc c) {
125         assertInstance(object, c, false);
126     }
127
128     public static void assertInstance(Object JavaDoc object, Class JavaDoc c, boolean allowNull) {
129         if (object == null && allowNull) {
130             return;
131         }
132
133         if (object == null || c == null) {
134             throw new NullPointerException JavaDoc();
135         } else if (!c.isInstance(object)) {
136             throw new IllegalArgumentException JavaDoc();
137         }
138     }
139
140     public static int compare(boolean left, boolean right) {
141         return left == false ? (right == true ? -1 : 0) : 1;
142     }
143
144     public static int compare(Comparable JavaDoc left, Comparable JavaDoc right) {
145         if (left == null && right == null) {
146             return 0;
147         } else if (left == null) {
148             return -1;
149         } else if (right == null) {
150             return 1;
151         } else {
152             return left.compareTo(right);
153         }
154     }
155
156     public static int compare(Comparable JavaDoc[] left, Comparable JavaDoc[] right) {
157         if (left == null && right == null) {
158             return 0;
159         } else if (left == null) {
160             return -1;
161         } else if (right == null) {
162             return 1;
163         } else {
164             int l = left.length;
165             int r = right.length;
166
167             if (l != r) {
168                 return l - r;
169             } else {
170                 for (int i = 0; i < l; i++) {
171                     int compareTo = compare(left[i], right[i]);
172
173                     if (compareTo != 0) {
174                         return compareTo;
175                     }
176                 }
177
178                 return 0;
179             }
180         }
181     }
182
183     public static int compare(int left, int right) {
184         return left - right;
185     }
186
187     public static int compare(List JavaDoc left, List JavaDoc right) {
188         if (left == null && right == null) {
189             return 0;
190         } else if (left == null) {
191             return -1;
192         } else if (right == null) {
193             return 1;
194         } else {
195             int l = left.size();
196             int r = right.size();
197
198             if (l != r) {
199                 return l - r;
200             } else {
201                 for (int i = 0; i < l; i++) {
202                     int compareTo = compare((Comparable JavaDoc) left.get(i),
203                             (Comparable JavaDoc) right.get(i));
204
205                     if (compareTo != 0) {
206                         return compareTo;
207                     }
208                 }
209
210                 return 0;
211             }
212         }
213     }
214
215     public static int compare(Object JavaDoc left, Object JavaDoc right) {
216         if (left == null && right == null) {
217             return 0;
218         } else if (left == null) {
219             return -1;
220         } else if (right == null) {
221             return 1;
222         } else if (left == right) {
223             return 0;
224         } else {
225             return compare(System.identityHashCode(left), System
226                     .identityHashCode(right));
227         }
228     }
229
230     /**
231      * An optimized comparison that uses identity hash codes to perform the
232      * comparison between non- <code>null</code> objects.
233      *
234      * @param left
235      * The left-hand side of the comparison; may be <code>null</code>.
236      * @param right
237      * The right-hand side of the comparison; may be
238      * <code>null</code>.
239      * @return <code>0</code> if they are the same, <code>-1</code> if left
240      * is <code>null</code>;<code>1</code> if right is
241      * <code>null</code>. Otherwise, the left identity hash code
242      * minus the right identity hash code.
243      */

244     public static final int compareIdentity(Object JavaDoc left, Object JavaDoc right) {
245         if (left == null && right == null) {
246             return 0;
247         } else if (left == null) {
248             return -1;
249         } else if (right == null) {
250             return 1;
251         } else {
252             return System.identityHashCode(left)
253                     - System.identityHashCode(right);
254         }
255     }
256
257     public static void diff(Map JavaDoc left, Map JavaDoc right, Set JavaDoc leftOnly, Set JavaDoc different,
258             Set JavaDoc rightOnly) {
259         if (left == null || right == null || leftOnly == null
260                 || different == null || rightOnly == null) {
261             throw new NullPointerException JavaDoc();
262         }
263
264         Iterator JavaDoc iterator = left.keySet().iterator();
265
266         while (iterator.hasNext()) {
267             Object JavaDoc key = iterator.next();
268
269             if (!right.containsKey(key)) {
270                 leftOnly.add(key);
271             } else if (!Util.equals(left.get(key), right.get(key))) {
272                 different.add(key);
273             }
274         }
275
276         iterator = right.keySet().iterator();
277
278         while (iterator.hasNext()) {
279             Object JavaDoc key = iterator.next();
280
281             if (!left.containsKey(key)) {
282                 rightOnly.add(key);
283             }
284         }
285     }
286
287     public static void diff(Set JavaDoc left, Set JavaDoc right, Set JavaDoc leftOnly, Set JavaDoc rightOnly) {
288         if (left == null || right == null || leftOnly == null
289                 || rightOnly == null) {
290             throw new NullPointerException JavaDoc();
291         }
292
293         Iterator JavaDoc iterator = left.iterator();
294
295         while (iterator.hasNext()) {
296             Object JavaDoc object = iterator.next();
297
298             if (!right.contains(object)) {
299                 leftOnly.add(object);
300             }
301         }
302
303         iterator = right.iterator();
304
305         while (iterator.hasNext()) {
306             Object JavaDoc object = iterator.next();
307
308             if (!left.contains(object)) {
309                 rightOnly.add(object);
310             }
311         }
312     }
313
314     public static boolean endsWith(List JavaDoc left, List JavaDoc right, boolean equals) {
315         if (left == null || right == null) {
316             return false;
317         } else {
318             int l = left.size();
319             int r = right.size();
320
321             if (r > l || !equals && r == l) {
322                 return false;
323             }
324
325             for (int i = 0; i < r; i++) {
326                 if (!equals(left.get(l - i - 1), right.get(r - i - 1))) {
327                     return false;
328                 }
329             }
330
331             return true;
332         }
333     }
334
335     public static boolean endsWith(Object JavaDoc[] left, Object JavaDoc[] right, boolean equals) {
336         if (left == null || right == null) {
337             return false;
338         } else {
339             int l = left.length;
340             int r = right.length;
341
342             if (r > l || !equals && r == l) {
343                 return false;
344             }
345
346             for (int i = 0; i < r; i++) {
347                 if (!equals(left[l - i - 1], right[r - i - 1])) {
348                     return false;
349                 }
350             }
351
352             return true;
353         }
354     }
355
356     public static boolean equals(boolean left, boolean right) {
357         return left == right;
358     }
359
360     public static boolean equals(int left, int right) {
361         return left == right;
362     }
363
364     public static boolean equals(Object JavaDoc left, Object JavaDoc right) {
365         return left == null ? right == null : ((right != null) && left
366                 .equals(right));
367     }
368
369     /**
370      * Tests whether two arrays of objects are equal to each other. The arrays
371      * must not be <code>null</code>, but their elements may be
372      * <code>null</code>.
373      *
374      * @param leftArray
375      * The left array to compare; may be <code>null</code>, and
376      * may be empty and may contain <code>null</code> elements.
377      * @param rightArray
378      * The right array to compare; may be <code>null</code>, and
379      * may be empty and may contain <code>null</code> elements.
380      * @return <code>true</code> if the arrays are equal length and the
381      * elements at the same position are equal; <code>false</code>
382      * otherwise.
383      */

384     public static final boolean equals(final Object JavaDoc[] leftArray,
385             final Object JavaDoc[] rightArray) {
386         if (leftArray == rightArray) {
387             return true;
388         }
389
390         if (leftArray == null) {
391             return (rightArray == null);
392         } else if (rightArray == null) {
393             return false;
394         }
395
396         if (leftArray.length != rightArray.length) {
397             return false;
398         }
399
400         for (int i = 0; i < leftArray.length; i++) {
401             final Object JavaDoc left = leftArray[i];
402             final Object JavaDoc right = rightArray[i];
403             final boolean equal = (left == null) ? (right == null) : (left
404                     .equals(right));
405             if (!equal) {
406                 return false;
407             }
408         }
409
410         return true;
411     }
412
413     public static int hashCode(boolean b) {
414         return b ? Boolean.TRUE.hashCode() : Boolean.FALSE.hashCode();
415     }
416
417     public static int hashCode(int i) {
418         return i;
419     }
420
421     public static int hashCode(Object JavaDoc object) {
422         return object != null ? object.hashCode() : 0;
423     }
424
425     public static Collection JavaDoc safeCopy(Collection JavaDoc collection, Class JavaDoc c) {
426         return safeCopy(collection, c, false);
427     }
428
429     public static Collection JavaDoc safeCopy(Collection JavaDoc collection, Class JavaDoc c,
430             boolean allowNullElements) {
431         if (collection == null || c == null) {
432             throw new NullPointerException JavaDoc();
433         }
434
435         collection = Collections.unmodifiableCollection(new ArrayList JavaDoc(
436                 collection));
437         Iterator JavaDoc iterator = collection.iterator();
438
439         while (iterator.hasNext()) {
440             assertInstance(iterator.next(), c, allowNullElements);
441         }
442
443         return collection;
444     }
445
446     public static List JavaDoc safeCopy(List JavaDoc list, Class JavaDoc c) {
447         return safeCopy(list, c, false);
448     }
449
450     public static List JavaDoc safeCopy(List JavaDoc list, Class JavaDoc c, boolean allowNullElements) {
451         if (list == null || c == null) {
452             throw new NullPointerException JavaDoc();
453         }
454
455         list = Collections.unmodifiableList(new ArrayList JavaDoc(list));
456         Iterator JavaDoc iterator = list.iterator();
457
458         while (iterator.hasNext()) {
459             assertInstance(iterator.next(), c, allowNullElements);
460         }
461
462         return list;
463     }
464
465     public static Map JavaDoc safeCopy(Map JavaDoc map, Class JavaDoc keyClass, Class JavaDoc valueClass) {
466         return safeCopy(map, keyClass, valueClass, false, false);
467     }
468
469     public static Map JavaDoc safeCopy(Map JavaDoc map, Class JavaDoc keyClass, Class JavaDoc valueClass,
470             boolean allowNullKeys, boolean allowNullValues) {
471         if (map == null || keyClass == null || valueClass == null) {
472             throw new NullPointerException JavaDoc();
473         }
474
475         map = Collections.unmodifiableMap(new HashMap JavaDoc(map));
476         Iterator JavaDoc iterator = map.entrySet().iterator();
477
478         while (iterator.hasNext()) {
479             Map.Entry JavaDoc entry = (Map.Entry JavaDoc) iterator.next();
480             assertInstance(entry.getKey(), keyClass, allowNullKeys);
481             assertInstance(entry.getValue(), valueClass, allowNullValues);
482         }
483
484         return map;
485     }
486
487     public static Set JavaDoc safeCopy(Set JavaDoc set, Class JavaDoc c) {
488         return safeCopy(set, c, false);
489     }
490
491     public static Set JavaDoc safeCopy(Set JavaDoc set, Class JavaDoc c, boolean allowNullElements) {
492         if (set == null || c == null) {
493             throw new NullPointerException JavaDoc();
494         }
495
496         set = Collections.unmodifiableSet(new HashSet JavaDoc(set));
497         Iterator JavaDoc iterator = set.iterator();
498
499         while (iterator.hasNext()) {
500             assertInstance(iterator.next(), c, allowNullElements);
501         }
502
503         return set;
504     }
505
506     public static SortedMap JavaDoc safeCopy(SortedMap JavaDoc sortedMap, Class JavaDoc keyClass,
507             Class JavaDoc valueClass) {
508         return safeCopy(sortedMap, keyClass, valueClass, false, false);
509     }
510
511     public static SortedMap JavaDoc safeCopy(SortedMap JavaDoc sortedMap, Class JavaDoc keyClass,
512             Class JavaDoc valueClass, boolean allowNullKeys, boolean allowNullValues) {
513         if (sortedMap == null || keyClass == null || valueClass == null) {
514             throw new NullPointerException JavaDoc();
515         }
516
517         sortedMap = Collections.unmodifiableSortedMap(new TreeMap JavaDoc(sortedMap));
518         Iterator JavaDoc iterator = sortedMap.entrySet().iterator();
519
520         while (iterator.hasNext()) {
521             Map.Entry JavaDoc entry = (Map.Entry JavaDoc) iterator.next();
522             assertInstance(entry.getKey(), keyClass, allowNullKeys);
523             assertInstance(entry.getValue(), valueClass, allowNullValues);
524         }
525
526         return sortedMap;
527     }
528
529     public static SortedSet JavaDoc safeCopy(SortedSet JavaDoc sortedSet, Class JavaDoc c) {
530         return safeCopy(sortedSet, c, false);
531     }
532
533     public static SortedSet JavaDoc safeCopy(SortedSet JavaDoc sortedSet, Class JavaDoc c,
534             boolean allowNullElements) {
535         if (sortedSet == null || c == null) {
536             throw new NullPointerException JavaDoc();
537         }
538
539         sortedSet = Collections.unmodifiableSortedSet(new TreeSet JavaDoc(sortedSet));
540         Iterator JavaDoc iterator = sortedSet.iterator();
541
542         while (iterator.hasNext()) {
543             assertInstance(iterator.next(), c, allowNullElements);
544         }
545
546         return sortedSet;
547     }
548
549     public static boolean startsWith(List JavaDoc left, List JavaDoc right, boolean equals) {
550         if (left == null || right == null) {
551             return false;
552         } else {
553             int l = left.size();
554             int r = right.size();
555
556             if (r > l || !equals && r == l) {
557                 return false;
558             }
559
560             for (int i = 0; i < r; i++) {
561                 if (!equals(left.get(i), right.get(i))) {
562                     return false;
563                 }
564             }
565
566             return true;
567         }
568     }
569
570     public static boolean startsWith(Object JavaDoc[] left, Object JavaDoc[] right,
571             boolean equals) {
572         if (left == null || right == null) {
573             return false;
574         } else {
575             int l = left.length;
576             int r = right.length;
577
578             if (r > l || !equals && r == l) {
579                 return false;
580             }
581
582             for (int i = 0; i < r; i++) {
583                 if (!equals(left[i], right[i])) {
584                     return false;
585                 }
586             }
587
588             return true;
589         }
590     }
591
592     public static String JavaDoc translateString(ResourceBundle JavaDoc resourceBundle,
593             String JavaDoc key) {
594         return Util.translateString(resourceBundle, key, key, true, true);
595     }
596
597     public static String JavaDoc translateString(ResourceBundle JavaDoc resourceBundle,
598             String JavaDoc key, String JavaDoc string, boolean signal, boolean trim) {
599         if (resourceBundle != null && key != null) {
600             try {
601                 final String JavaDoc translatedString = resourceBundle.getString(key);
602
603                 if (translatedString != null) {
604                     return trim ? translatedString.trim() : translatedString;
605                 }
606             } catch (MissingResourceException JavaDoc eMissingResource) {
607                 if (signal) {
608                     WorkbenchPlugin.log(eMissingResource);
609                 }
610             }
611         }
612
613         return trim ? string.trim() : string;
614     }
615     
616     public static void arrayCopyWithRemoval(Object JavaDoc [] src, Object JavaDoc [] dst, int idxToRemove) {
617         if (src == null || dst == null || src.length - 1 != dst.length || idxToRemove < 0 || idxToRemove >= src.length) {
618             throw new IllegalArgumentException JavaDoc();
619         }
620         
621         if (idxToRemove == 0) {
622             System.arraycopy(src, 1, dst, 0, src.length - 1);
623         }
624         else if (idxToRemove == src.length - 1) {
625             System.arraycopy(src, 0, dst, 0, src.length - 1);
626         }
627         else {
628             System.arraycopy(src, 0, dst, 0, idxToRemove);
629             System.arraycopy(src, idxToRemove + 1, dst, idxToRemove, src.length - idxToRemove - 1);
630         }
631     }
632
633     /**
634      * Appends array2 to the end of array1 and returns the result
635      *
636      * @param array1
637      * @param array2
638      * @return
639      * @since 3.1
640      */

641     public static Object JavaDoc[] appendArray(Object JavaDoc[] array1, Object JavaDoc[] array2) {
642         Object JavaDoc[] result = new Object JavaDoc[array1.length + array2.length];
643         System.arraycopy(array1, 0, result, 0, array1.length);
644         System.arraycopy(array2, 0, result, array1.length, array2.length);
645         return result;
646     }
647     
648     private Util() {
649     }
650
651     /**
652      * Returns an interned representation of the given string
653      * @param string The string to intern
654      * @return The interned string
655      */

656     public static String JavaDoc intern(String JavaDoc string) {
657         return string == null ? null : string.intern();
658     }
659     
660     /**
661      * Returns the result of converting a list of comma-separated tokens into an array.
662      * Used as a replacement for <code>String.split(String)</code>, to allow compilation
663      * against JCL Foundation (bug 80053).
664      *
665      * @param prop the initial comma-separated string
666      * @param separator the separator characters
667      * @return the array of string tokens
668      * @since 3.1
669      */

670     public static String JavaDoc[] getArrayFromList(String JavaDoc prop, String JavaDoc separator) {
671         if (prop == null || prop.trim().equals("")) { //$NON-NLS-1$
672
return new String JavaDoc[0];
673         }
674         ArrayList JavaDoc list = new ArrayList JavaDoc();
675         StringTokenizer JavaDoc tokens = new StringTokenizer JavaDoc(prop, separator);
676         while (tokens.hasMoreTokens()) {
677             String JavaDoc token = tokens.nextToken().trim();
678             if (!token.equals("")) { //$NON-NLS-1$
679
list.add(token);
680             }
681         }
682         return list.isEmpty() ? new String JavaDoc[0] : (String JavaDoc[]) list.toArray(new String JavaDoc[list.size()]);
683     }
684
685     /**
686      * Return the window for the given shell or the currently active window if
687      * one could not be determined.
688      *
689      * @param shellToCheck
690      * the shell to search on
691      * @return the window for the given shell or the currently active window if
692      * one could not be determined
693      * @since 3.2
694      */

695     public static IWorkbenchWindow getWorkbenchWindowForShell(Shell shellToCheck) {
696         IWorkbenchWindow workbenchWindow = null;
697         while (workbenchWindow == null && shellToCheck != null) {
698             if (shellToCheck.getData() instanceof IWorkbenchWindow) {
699                 workbenchWindow = (IWorkbenchWindow) shellToCheck.getData();
700             } else {
701                 shellToCheck = (Shell) shellToCheck.getParent();
702             }
703         }
704
705         if (workbenchWindow == null) {
706             workbenchWindow = PlatformUI.getWorkbench()
707                     .getActiveWorkbenchWindow();
708         }
709
710         return workbenchWindow;
711     }
712     
713     /**
714      * Return an appropriate shell to parent dialogs on. This will be one of the
715      * workbench windows (the active one) should any exist. Otherwise
716      * <code>null</code> is returned.
717      *
718      * @return the shell to parent on or <code>null</code> if there is no
719      * appropriate shell
720      * @since 3.3
721      */

722     public static Shell getShellToParentOn() {
723         IWorkbench workbench = PlatformUI.getWorkbench();
724         IWorkbenchWindow activeWindow = workbench.getActiveWorkbenchWindow();
725         IWorkbenchWindow windowToParentOn = activeWindow == null ? (workbench
726                 .getWorkbenchWindowCount() > 0 ? workbench
727                 .getWorkbenchWindows()[0] : null) : activeWindow;
728         return windowToParentOn == null ? null : activeWindow.getShell();
729     }
730     
731     /**
732      * Splits a string at the first occurance of the delimiting char.
733      * If the source string is null then so is the result. If the source
734      * string is badly formatted then it is returned in the first array
735      * entry and the second entry is an empty string.
736      *
737      * @param src The string to be split
738      * @param delim The character to split on
739      * @return A two entry string array containing the left/right pair.
740      */

741     public static String JavaDoc[] split(String JavaDoc src, char delim) {
742         if (src == null)
743             return null;
744         
745         String JavaDoc[] splitStr = new String JavaDoc[2];
746         int delimIndex = src.indexOf(delim);
747         if (delimIndex == -1 || delimIndex == 0 || delimIndex == src.length()-1) {
748             splitStr[0] = src;
749             splitStr[1] = ZERO_LENGTH_STRING;
750             return splitStr;
751         }
752         
753         splitStr[0] = src.substring(0, delimIndex);
754         splitStr[1] = src.substring(delimIndex+1);
755         
756         return splitStr;
757     }
758     
759     /**
760      * Foundation replacement for String.replaceAll(*).
761      *
762      * @param src the starting string.
763      * @param find the string to find.
764      * @param replacement the string to replace.
765      * @return The new string.
766      * @since 3.3
767      */

768     public static String JavaDoc replaceAll(String JavaDoc src, String JavaDoc find, String JavaDoc replacement) {
769         final int len = src.length();
770         final int findLen = find.length();
771
772         int idx = src.indexOf(find);
773         if (idx < 0) {
774             return src;
775         }
776
777         StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
778         int beginIndex = 0;
779         while (idx != -1 && idx < len) {
780             buf.append(src.substring(beginIndex, idx));
781             buf.append(replacement);
782             
783             beginIndex = idx + findLen;
784             if (beginIndex < len) {
785                 idx = src.indexOf(find, beginIndex);
786             } else {
787                 idx = -1;
788             }
789         }
790         if (beginIndex<len) {
791             buf.append(src.substring(beginIndex, (idx==-1?len:idx)));
792         }
793         return buf.toString();
794     }
795     
796     /**
797      * Attempt to load the executable extension from the element/attName. If
798      * the load fails or the resulting object is not castable to the
799      * provided classSpec (if any) an error is logged and a null is returned.
800      *
801      * @param element The {@link IConfigurationElement} containing the
802      * executable extension's specification
803      * @param attName The attribute name of the executable extension
804      * @param classSpec An optional <code>Class</code> defining the type
805      * that the loaded Object must be castable to. This is optional to support
806      * code where the client has a choice of mutually non-castable types to
807      * choose from.
808      *
809      * @return The loaded object which is guaranteed to be
810      * castable to the given classSpec or null if a failure occurred
811      */

812     public static Object JavaDoc safeLoadExecutableExtension(IConfigurationElement element,
813             String JavaDoc attName, Class JavaDoc classSpec) {
814         Object JavaDoc loadedEE = null;
815         
816         // Load the handler.
817
try {
818             loadedEE = element.createExecutableExtension(attName);
819         } catch (final CoreException e) {
820             // TODO: give more info (eg plugin id)....
821
// Gather formatting info
822
final String JavaDoc classDef = element.getAttribute(attName);
823
824             final String JavaDoc message = "Class load Failure: '" + classDef + "'"; //$NON-NLS-1$//$NON-NLS-2$
825
IStatus status = new Status(IStatus.ERROR,
826                     WorkbenchPlugin.PI_WORKBENCH, 0, message, e);
827             WorkbenchPlugin.log(message, status);
828         }
829         
830         // Check the loaded object's type
831
if (classSpec != null && loadedEE != null && !classSpec.isInstance(loadedEE)) {
832             // ooops, the loaded class is not castable to the given type
833
final String JavaDoc message = "Loaded class is of incorrect type: expected(" + //$NON-NLS-1$
834
classSpec.getName() + ") got (" + loadedEE.getClass().getName() + ")"; //$NON-NLS-1$ //$NON-NLS-2$
835

836             IllegalArgumentException JavaDoc e = new IllegalArgumentException JavaDoc(message);
837             final IStatus status = new Status(IStatus.ERROR,
838                     WorkbenchPlugin.PI_WORKBENCH, 0, message, e);
839             WorkbenchPlugin.log(message, status);
840             
841             // This 'failed'
842
loadedEE = null;
843         }
844         
845         return loadedEE;
846     }
847     
848 }
849
Popular Tags