Source Code Cross Referenced for Util.java in  » IDE-Eclipse » ui-workbench » org » eclipse » ui » internal » util » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » IDE Eclipse » ui workbench » org.eclipse.ui.internal.util 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /*******************************************************************************
002:         * Copyright (c) 2000, 2006 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.ui.internal.util;
011:
012:        import java.util.ArrayList;
013:        import java.util.Collection;
014:        import java.util.Collections;
015:        import java.util.HashMap;
016:        import java.util.HashSet;
017:        import java.util.Iterator;
018:        import java.util.List;
019:        import java.util.Map;
020:        import java.util.MissingResourceException;
021:        import java.util.ResourceBundle;
022:        import java.util.Set;
023:        import java.util.SortedMap;
024:        import java.util.SortedSet;
025:        import java.util.StringTokenizer;
026:        import java.util.TreeMap;
027:        import java.util.TreeSet;
028:
029:        import org.eclipse.core.runtime.Assert;
030:        import org.eclipse.core.runtime.CoreException;
031:        import org.eclipse.core.runtime.IAdaptable;
032:        import org.eclipse.core.runtime.IConfigurationElement;
033:        import org.eclipse.core.runtime.IStatus;
034:        import org.eclipse.core.runtime.Platform;
035:        import org.eclipse.core.runtime.PlatformObject;
036:        import org.eclipse.core.runtime.Status;
037:        import org.eclipse.swt.widgets.Shell;
038:        import org.eclipse.ui.IWorkbench;
039:        import org.eclipse.ui.IWorkbenchWindow;
040:        import org.eclipse.ui.PlatformUI;
041:        import org.eclipse.ui.internal.WorkbenchPlugin;
042:
043:        public final class Util {
044:
045:            public final static SortedMap EMPTY_SORTED_MAP = Collections
046:                    .unmodifiableSortedMap(new TreeMap());
047:
048:            public final static SortedSet EMPTY_SORTED_SET = Collections
049:                    .unmodifiableSortedSet(new TreeSet());
050:
051:            public final static String ZERO_LENGTH_STRING = ""; //$NON-NLS-1$
052:
053:            /**
054:             * Ensures that a string is not null. Converts null strings into empty
055:             * strings, and leaves any other string unmodified. Use this to help
056:             * wrap calls to methods that return null instead of the empty string.
057:             * Can also help protect against implementation errors in methods that
058:             * are not supposed to return null. 
059:             * 
060:             * @param input input string (may be null)
061:             * @return input if not null, or the empty string if input is null
062:             */
063:            public static String safeString(String input) {
064:                if (input != null) {
065:                    return input;
066:                }
067:
068:                return ZERO_LENGTH_STRING;
069:            }
070:
071:            /**
072:             * If it is possible to adapt the given object to the given type, this
073:             * returns the adapter. Performs the following checks:
074:             * 
075:             * <ol>
076:             * <li>Returns <code>sourceObject</code> if it is an instance of the
077:             * adapter type.</li>
078:             * <li>If sourceObject implements IAdaptable, it is queried for adapters.</li>
079:             * <li>If sourceObject is not an instance of PlatformObject (which would have
080:             * already done so), the adapter manager is queried for adapters</li>
081:             * </ol>
082:             * 
083:             * Otherwise returns null.
084:             * 
085:             * @param sourceObject
086:             *            object to adapt, or null
087:             * @param adapterType
088:             *            type to adapt to
089:             * @return a representation of sourceObject that is assignable to the
090:             *         adapter type, or null if no such representation exists
091:             */
092:            public static Object getAdapter(Object sourceObject,
093:                    Class adapterType) {
094:                Assert.isNotNull(adapterType);
095:                if (sourceObject == null) {
096:                    return null;
097:                }
098:                if (adapterType.isInstance(sourceObject)) {
099:                    return sourceObject;
100:                }
101:
102:                if (sourceObject instanceof  IAdaptable) {
103:                    IAdaptable adaptable = (IAdaptable) sourceObject;
104:
105:                    Object result = adaptable.getAdapter(adapterType);
106:                    if (result != null) {
107:                        // Sanity-check
108:                        Assert.isTrue(adapterType.isInstance(result));
109:                        return result;
110:                    }
111:                }
112:
113:                if (!(sourceObject instanceof  PlatformObject)) {
114:                    Object result = Platform.getAdapterManager().getAdapter(
115:                            sourceObject, adapterType);
116:                    if (result != null) {
117:                        return result;
118:                    }
119:                }
120:
121:                return null;
122:            }
123:
124:            public static void assertInstance(Object object, Class c) {
125:                assertInstance(object, c, false);
126:            }
127:
128:            public static void assertInstance(Object object, Class c,
129:                    boolean allowNull) {
130:                if (object == null && allowNull) {
131:                    return;
132:                }
133:
134:                if (object == null || c == null) {
135:                    throw new NullPointerException();
136:                } else if (!c.isInstance(object)) {
137:                    throw new IllegalArgumentException();
138:                }
139:            }
140:
141:            public static int compare(boolean left, boolean right) {
142:                return left == false ? (right == true ? -1 : 0) : 1;
143:            }
144:
145:            public static int compare(Comparable left, Comparable right) {
146:                if (left == null && right == null) {
147:                    return 0;
148:                } else if (left == null) {
149:                    return -1;
150:                } else if (right == null) {
151:                    return 1;
152:                } else {
153:                    return left.compareTo(right);
154:                }
155:            }
156:
157:            public static int compare(Comparable[] left, Comparable[] right) {
158:                if (left == null && right == null) {
159:                    return 0;
160:                } else if (left == null) {
161:                    return -1;
162:                } else if (right == null) {
163:                    return 1;
164:                } else {
165:                    int l = left.length;
166:                    int r = right.length;
167:
168:                    if (l != r) {
169:                        return l - r;
170:                    } else {
171:                        for (int i = 0; i < l; i++) {
172:                            int compareTo = compare(left[i], right[i]);
173:
174:                            if (compareTo != 0) {
175:                                return compareTo;
176:                            }
177:                        }
178:
179:                        return 0;
180:                    }
181:                }
182:            }
183:
184:            public static int compare(int left, int right) {
185:                return left - right;
186:            }
187:
188:            public static int compare(List left, List right) {
189:                if (left == null && right == null) {
190:                    return 0;
191:                } else if (left == null) {
192:                    return -1;
193:                } else if (right == null) {
194:                    return 1;
195:                } else {
196:                    int l = left.size();
197:                    int r = right.size();
198:
199:                    if (l != r) {
200:                        return l - r;
201:                    } else {
202:                        for (int i = 0; i < l; i++) {
203:                            int compareTo = compare((Comparable) left.get(i),
204:                                    (Comparable) right.get(i));
205:
206:                            if (compareTo != 0) {
207:                                return compareTo;
208:                            }
209:                        }
210:
211:                        return 0;
212:                    }
213:                }
214:            }
215:
216:            public static int compare(Object left, Object right) {
217:                if (left == null && right == null) {
218:                    return 0;
219:                } else if (left == null) {
220:                    return -1;
221:                } else if (right == null) {
222:                    return 1;
223:                } else if (left == right) {
224:                    return 0;
225:                } else {
226:                    return compare(System.identityHashCode(left), System
227:                            .identityHashCode(right));
228:                }
229:            }
230:
231:            /**
232:             * An optimized comparison that uses identity hash codes to perform the
233:             * comparison between non- <code>null</code> objects.
234:             * 
235:             * @param left
236:             *            The left-hand side of the comparison; may be <code>null</code>.
237:             * @param right
238:             *            The right-hand side of the comparison; may be
239:             *            <code>null</code>.
240:             * @return <code>0</code> if they are the same, <code>-1</code> if left
241:             *         is <code>null</code>;<code>1</code> if right is
242:             *         <code>null</code>. Otherwise, the left identity hash code
243:             *         minus the right identity hash code.
244:             */
245:            public static final int compareIdentity(Object left, Object right) {
246:                if (left == null && right == null) {
247:                    return 0;
248:                } else if (left == null) {
249:                    return -1;
250:                } else if (right == null) {
251:                    return 1;
252:                } else {
253:                    return System.identityHashCode(left)
254:                            - System.identityHashCode(right);
255:                }
256:            }
257:
258:            public static void diff(Map left, Map right, Set leftOnly,
259:                    Set different, Set rightOnly) {
260:                if (left == null || right == null || leftOnly == null
261:                        || different == null || rightOnly == null) {
262:                    throw new NullPointerException();
263:                }
264:
265:                Iterator iterator = left.keySet().iterator();
266:
267:                while (iterator.hasNext()) {
268:                    Object key = iterator.next();
269:
270:                    if (!right.containsKey(key)) {
271:                        leftOnly.add(key);
272:                    } else if (!Util.equals(left.get(key), right.get(key))) {
273:                        different.add(key);
274:                    }
275:                }
276:
277:                iterator = right.keySet().iterator();
278:
279:                while (iterator.hasNext()) {
280:                    Object key = iterator.next();
281:
282:                    if (!left.containsKey(key)) {
283:                        rightOnly.add(key);
284:                    }
285:                }
286:            }
287:
288:            public static void diff(Set left, Set right, Set leftOnly,
289:                    Set rightOnly) {
290:                if (left == null || right == null || leftOnly == null
291:                        || rightOnly == null) {
292:                    throw new NullPointerException();
293:                }
294:
295:                Iterator iterator = left.iterator();
296:
297:                while (iterator.hasNext()) {
298:                    Object object = iterator.next();
299:
300:                    if (!right.contains(object)) {
301:                        leftOnly.add(object);
302:                    }
303:                }
304:
305:                iterator = right.iterator();
306:
307:                while (iterator.hasNext()) {
308:                    Object object = iterator.next();
309:
310:                    if (!left.contains(object)) {
311:                        rightOnly.add(object);
312:                    }
313:                }
314:            }
315:
316:            public static boolean endsWith(List left, List right, boolean equals) {
317:                if (left == null || right == null) {
318:                    return false;
319:                } else {
320:                    int l = left.size();
321:                    int r = right.size();
322:
323:                    if (r > l || !equals && r == l) {
324:                        return false;
325:                    }
326:
327:                    for (int i = 0; i < r; i++) {
328:                        if (!equals(left.get(l - i - 1), right.get(r - i - 1))) {
329:                            return false;
330:                        }
331:                    }
332:
333:                    return true;
334:                }
335:            }
336:
337:            public static boolean endsWith(Object[] left, Object[] right,
338:                    boolean equals) {
339:                if (left == null || right == null) {
340:                    return false;
341:                } else {
342:                    int l = left.length;
343:                    int r = right.length;
344:
345:                    if (r > l || !equals && r == l) {
346:                        return false;
347:                    }
348:
349:                    for (int i = 0; i < r; i++) {
350:                        if (!equals(left[l - i - 1], right[r - i - 1])) {
351:                            return false;
352:                        }
353:                    }
354:
355:                    return true;
356:                }
357:            }
358:
359:            public static boolean equals(boolean left, boolean right) {
360:                return left == right;
361:            }
362:
363:            public static boolean equals(int left, int right) {
364:                return left == right;
365:            }
366:
367:            public static boolean equals(Object left, Object right) {
368:                return left == null ? right == null : ((right != null) && left
369:                        .equals(right));
370:            }
371:
372:            /**
373:             * Tests whether two arrays of objects are equal to each other. The arrays
374:             * must not be <code>null</code>, but their elements may be
375:             * <code>null</code>.
376:             * 
377:             * @param leftArray
378:             *            The left array to compare; may be <code>null</code>, and
379:             *            may be empty and may contain <code>null</code> elements.
380:             * @param rightArray
381:             *            The right array to compare; may be <code>null</code>, and
382:             *            may be empty and may contain <code>null</code> elements.
383:             * @return <code>true</code> if the arrays are equal length and the
384:             *         elements at the same position are equal; <code>false</code>
385:             *         otherwise.
386:             */
387:            public static final boolean equals(final Object[] leftArray,
388:                    final Object[] rightArray) {
389:                if (leftArray == rightArray) {
390:                    return true;
391:                }
392:
393:                if (leftArray == null) {
394:                    return (rightArray == null);
395:                } else if (rightArray == null) {
396:                    return false;
397:                }
398:
399:                if (leftArray.length != rightArray.length) {
400:                    return false;
401:                }
402:
403:                for (int i = 0; i < leftArray.length; i++) {
404:                    final Object left = leftArray[i];
405:                    final Object right = rightArray[i];
406:                    final boolean equal = (left == null) ? (right == null)
407:                            : (left.equals(right));
408:                    if (!equal) {
409:                        return false;
410:                    }
411:                }
412:
413:                return true;
414:            }
415:
416:            public static int hashCode(boolean b) {
417:                return b ? Boolean.TRUE.hashCode() : Boolean.FALSE.hashCode();
418:            }
419:
420:            public static int hashCode(int i) {
421:                return i;
422:            }
423:
424:            public static int hashCode(Object object) {
425:                return object != null ? object.hashCode() : 0;
426:            }
427:
428:            public static Collection safeCopy(Collection collection, Class c) {
429:                return safeCopy(collection, c, false);
430:            }
431:
432:            public static Collection safeCopy(Collection collection, Class c,
433:                    boolean allowNullElements) {
434:                if (collection == null || c == null) {
435:                    throw new NullPointerException();
436:                }
437:
438:                collection = Collections.unmodifiableCollection(new ArrayList(
439:                        collection));
440:                Iterator iterator = collection.iterator();
441:
442:                while (iterator.hasNext()) {
443:                    assertInstance(iterator.next(), c, allowNullElements);
444:                }
445:
446:                return collection;
447:            }
448:
449:            public static List safeCopy(List list, Class c) {
450:                return safeCopy(list, c, false);
451:            }
452:
453:            public static List safeCopy(List list, Class c,
454:                    boolean allowNullElements) {
455:                if (list == null || c == null) {
456:                    throw new NullPointerException();
457:                }
458:
459:                list = Collections.unmodifiableList(new ArrayList(list));
460:                Iterator iterator = list.iterator();
461:
462:                while (iterator.hasNext()) {
463:                    assertInstance(iterator.next(), c, allowNullElements);
464:                }
465:
466:                return list;
467:            }
468:
469:            public static Map safeCopy(Map map, Class keyClass, Class valueClass) {
470:                return safeCopy(map, keyClass, valueClass, false, false);
471:            }
472:
473:            public static Map safeCopy(Map map, Class keyClass,
474:                    Class valueClass, boolean allowNullKeys,
475:                    boolean allowNullValues) {
476:                if (map == null || keyClass == null || valueClass == null) {
477:                    throw new NullPointerException();
478:                }
479:
480:                map = Collections.unmodifiableMap(new HashMap(map));
481:                Iterator iterator = map.entrySet().iterator();
482:
483:                while (iterator.hasNext()) {
484:                    Map.Entry entry = (Map.Entry) iterator.next();
485:                    assertInstance(entry.getKey(), keyClass, allowNullKeys);
486:                    assertInstance(entry.getValue(), valueClass,
487:                            allowNullValues);
488:                }
489:
490:                return map;
491:            }
492:
493:            public static Set safeCopy(Set set, Class c) {
494:                return safeCopy(set, c, false);
495:            }
496:
497:            public static Set safeCopy(Set set, Class c,
498:                    boolean allowNullElements) {
499:                if (set == null || c == null) {
500:                    throw new NullPointerException();
501:                }
502:
503:                set = Collections.unmodifiableSet(new HashSet(set));
504:                Iterator iterator = set.iterator();
505:
506:                while (iterator.hasNext()) {
507:                    assertInstance(iterator.next(), c, allowNullElements);
508:                }
509:
510:                return set;
511:            }
512:
513:            public static SortedMap safeCopy(SortedMap sortedMap,
514:                    Class keyClass, Class valueClass) {
515:                return safeCopy(sortedMap, keyClass, valueClass, false, false);
516:            }
517:
518:            public static SortedMap safeCopy(SortedMap sortedMap,
519:                    Class keyClass, Class valueClass, boolean allowNullKeys,
520:                    boolean allowNullValues) {
521:                if (sortedMap == null || keyClass == null || valueClass == null) {
522:                    throw new NullPointerException();
523:                }
524:
525:                sortedMap = Collections.unmodifiableSortedMap(new TreeMap(
526:                        sortedMap));
527:                Iterator iterator = sortedMap.entrySet().iterator();
528:
529:                while (iterator.hasNext()) {
530:                    Map.Entry entry = (Map.Entry) iterator.next();
531:                    assertInstance(entry.getKey(), keyClass, allowNullKeys);
532:                    assertInstance(entry.getValue(), valueClass,
533:                            allowNullValues);
534:                }
535:
536:                return sortedMap;
537:            }
538:
539:            public static SortedSet safeCopy(SortedSet sortedSet, Class c) {
540:                return safeCopy(sortedSet, c, false);
541:            }
542:
543:            public static SortedSet safeCopy(SortedSet sortedSet, Class c,
544:                    boolean allowNullElements) {
545:                if (sortedSet == null || c == null) {
546:                    throw new NullPointerException();
547:                }
548:
549:                sortedSet = Collections.unmodifiableSortedSet(new TreeSet(
550:                        sortedSet));
551:                Iterator iterator = sortedSet.iterator();
552:
553:                while (iterator.hasNext()) {
554:                    assertInstance(iterator.next(), c, allowNullElements);
555:                }
556:
557:                return sortedSet;
558:            }
559:
560:            public static boolean startsWith(List left, List right,
561:                    boolean equals) {
562:                if (left == null || right == null) {
563:                    return false;
564:                } else {
565:                    int l = left.size();
566:                    int r = right.size();
567:
568:                    if (r > l || !equals && r == l) {
569:                        return false;
570:                    }
571:
572:                    for (int i = 0; i < r; i++) {
573:                        if (!equals(left.get(i), right.get(i))) {
574:                            return false;
575:                        }
576:                    }
577:
578:                    return true;
579:                }
580:            }
581:
582:            public static boolean startsWith(Object[] left, Object[] right,
583:                    boolean equals) {
584:                if (left == null || right == null) {
585:                    return false;
586:                } else {
587:                    int l = left.length;
588:                    int r = right.length;
589:
590:                    if (r > l || !equals && r == l) {
591:                        return false;
592:                    }
593:
594:                    for (int i = 0; i < r; i++) {
595:                        if (!equals(left[i], right[i])) {
596:                            return false;
597:                        }
598:                    }
599:
600:                    return true;
601:                }
602:            }
603:
604:            public static String translateString(ResourceBundle resourceBundle,
605:                    String key) {
606:                return Util.translateString(resourceBundle, key, key, true,
607:                        true);
608:            }
609:
610:            public static String translateString(ResourceBundle resourceBundle,
611:                    String key, String string, boolean signal, boolean trim) {
612:                if (resourceBundle != null && key != null) {
613:                    try {
614:                        final String translatedString = resourceBundle
615:                                .getString(key);
616:
617:                        if (translatedString != null) {
618:                            return trim ? translatedString.trim()
619:                                    : translatedString;
620:                        }
621:                    } catch (MissingResourceException eMissingResource) {
622:                        if (signal) {
623:                            WorkbenchPlugin.log(eMissingResource);
624:                        }
625:                    }
626:                }
627:
628:                return trim ? string.trim() : string;
629:            }
630:
631:            public static void arrayCopyWithRemoval(Object[] src, Object[] dst,
632:                    int idxToRemove) {
633:                if (src == null || dst == null || src.length - 1 != dst.length
634:                        || idxToRemove < 0 || idxToRemove >= src.length) {
635:                    throw new IllegalArgumentException();
636:                }
637:
638:                if (idxToRemove == 0) {
639:                    System.arraycopy(src, 1, dst, 0, src.length - 1);
640:                } else if (idxToRemove == src.length - 1) {
641:                    System.arraycopy(src, 0, dst, 0, src.length - 1);
642:                } else {
643:                    System.arraycopy(src, 0, dst, 0, idxToRemove);
644:                    System.arraycopy(src, idxToRemove + 1, dst, idxToRemove,
645:                            src.length - idxToRemove - 1);
646:                }
647:            }
648:
649:            /**
650:             * Appends array2 to the end of array1 and returns the result
651:             * 
652:             * @param array1
653:             * @param array2
654:             * @return
655:             * @since 3.1
656:             */
657:            public static Object[] appendArray(Object[] array1, Object[] array2) {
658:                Object[] result = new Object[array1.length + array2.length];
659:                System.arraycopy(array1, 0, result, 0, array1.length);
660:                System.arraycopy(array2, 0, result, array1.length,
661:                        array2.length);
662:                return result;
663:            }
664:
665:            private Util() {
666:            }
667:
668:            /**
669:             * Returns an interned representation of the given string
670:             * @param string The string to intern
671:             * @return The interned string
672:             */
673:            public static String intern(String string) {
674:                return string == null ? null : string.intern();
675:            }
676:
677:            /**
678:             * Returns the result of converting a list of comma-separated tokens into an array.
679:             * Used as a replacement for <code>String.split(String)</code>, to allow compilation
680:             * against JCL Foundation (bug 80053).
681:             * 
682:             * @param prop the initial comma-separated string
683:             * @param separator the separator characters
684:             * @return the array of string tokens
685:             * @since 3.1
686:             */
687:            public static String[] getArrayFromList(String prop,
688:                    String separator) {
689:                if (prop == null || prop.trim().equals("")) { //$NON-NLS-1$
690:                    return new String[0];
691:                }
692:                ArrayList list = new ArrayList();
693:                StringTokenizer tokens = new StringTokenizer(prop, separator);
694:                while (tokens.hasMoreTokens()) {
695:                    String token = tokens.nextToken().trim();
696:                    if (!token.equals("")) { //$NON-NLS-1$
697:                        list.add(token);
698:                    }
699:                }
700:                return list.isEmpty() ? new String[0] : (String[]) list
701:                        .toArray(new String[list.size()]);
702:            }
703:
704:            /**
705:             * Return the window for the given shell or the currently active window if
706:             * one could not be determined.
707:             * 
708:             * @param shellToCheck
709:             *            the shell to search on
710:             * @return the window for the given shell or the currently active window if
711:             *         one could not be determined
712:             * @since 3.2
713:             */
714:            public static IWorkbenchWindow getWorkbenchWindowForShell(
715:                    Shell shellToCheck) {
716:                IWorkbenchWindow workbenchWindow = null;
717:                while (workbenchWindow == null && shellToCheck != null) {
718:                    if (shellToCheck.getData() instanceof  IWorkbenchWindow) {
719:                        workbenchWindow = (IWorkbenchWindow) shellToCheck
720:                                .getData();
721:                    } else {
722:                        shellToCheck = (Shell) shellToCheck.getParent();
723:                    }
724:                }
725:
726:                if (workbenchWindow == null) {
727:                    workbenchWindow = PlatformUI.getWorkbench()
728:                            .getActiveWorkbenchWindow();
729:                }
730:
731:                return workbenchWindow;
732:            }
733:
734:            /**
735:             * Return an appropriate shell to parent dialogs on. This will be one of the
736:             * workbench windows (the active one) should any exist. Otherwise
737:             * <code>null</code> is returned.
738:             * 
739:             * @return the shell to parent on or <code>null</code> if there is no
740:             *         appropriate shell
741:             * @since 3.3
742:             */
743:            public static Shell getShellToParentOn() {
744:                IWorkbench workbench = PlatformUI.getWorkbench();
745:                IWorkbenchWindow activeWindow = workbench
746:                        .getActiveWorkbenchWindow();
747:                IWorkbenchWindow windowToParentOn = activeWindow == null ? (workbench
748:                        .getWorkbenchWindowCount() > 0 ? workbench
749:                        .getWorkbenchWindows()[0] : null)
750:                        : activeWindow;
751:                return windowToParentOn == null ? null : activeWindow
752:                        .getShell();
753:            }
754:
755:            /**
756:             * Splits a string at the first occurance of the delimiting char.
757:             * If the source string is null then so is the result. If the source
758:             * string is badly formatted then it is returned in the first array
759:             * entry and the second entry is an empty string.
760:             * 
761:             * @param src The string to be split
762:             * @param delim The character to split on
763:             * @return A two entry string array containing the left/right pair.
764:             */
765:            public static String[] split(String src, char delim) {
766:                if (src == null)
767:                    return null;
768:
769:                String[] splitStr = new String[2];
770:                int delimIndex = src.indexOf(delim);
771:                if (delimIndex == -1 || delimIndex == 0
772:                        || delimIndex == src.length() - 1) {
773:                    splitStr[0] = src;
774:                    splitStr[1] = ZERO_LENGTH_STRING;
775:                    return splitStr;
776:                }
777:
778:                splitStr[0] = src.substring(0, delimIndex);
779:                splitStr[1] = src.substring(delimIndex + 1);
780:
781:                return splitStr;
782:            }
783:
784:            /**
785:             * Foundation replacement for String.replaceAll(*).
786:             * 
787:             * @param src the starting string.
788:             * @param find the string to find.
789:             * @param replacement the string to replace.
790:             * @return The new string.
791:             * @since 3.3
792:             */
793:            public static String replaceAll(String src, String find,
794:                    String replacement) {
795:                final int len = src.length();
796:                final int findLen = find.length();
797:
798:                int idx = src.indexOf(find);
799:                if (idx < 0) {
800:                    return src;
801:                }
802:
803:                StringBuffer buf = new StringBuffer();
804:                int beginIndex = 0;
805:                while (idx != -1 && idx < len) {
806:                    buf.append(src.substring(beginIndex, idx));
807:                    buf.append(replacement);
808:
809:                    beginIndex = idx + findLen;
810:                    if (beginIndex < len) {
811:                        idx = src.indexOf(find, beginIndex);
812:                    } else {
813:                        idx = -1;
814:                    }
815:                }
816:                if (beginIndex < len) {
817:                    buf.append(src.substring(beginIndex,
818:                            (idx == -1 ? len : idx)));
819:                }
820:                return buf.toString();
821:            }
822:
823:            /**
824:             * Attempt to load the executable extension from the element/attName. If
825:             * the load fails or the resulting object is not castable to the
826:             * provided classSpec (if any) an error is logged and a null is returned.
827:             * 
828:             * @param element The {@link IConfigurationElement} containing the
829:             * executable extension's specification 
830:             * @param attName The attribute name of the executable extension
831:             * @param classSpec An optional <code>Class</code> defining the type
832:             * that the loaded Object must be castable to. This is optional to support
833:             * code where the client has a choice of mutually non-castable types to
834:             * choose from.
835:             * 
836:             * @return The loaded object which is guaranteed to be
837:             * castable to the given classSpec or null if a failure occurred 
838:             */
839:            public static Object safeLoadExecutableExtension(
840:                    IConfigurationElement element, String attName,
841:                    Class classSpec) {
842:                Object loadedEE = null;
843:
844:                // Load the handler.
845:                try {
846:                    loadedEE = element.createExecutableExtension(attName);
847:                } catch (final CoreException e) {
848:                    // TODO: give more info (eg plugin id)....
849:                    // Gather formatting info
850:                    final String classDef = element.getAttribute(attName);
851:
852:                    final String message = "Class load Failure: '" + classDef + "'"; //$NON-NLS-1$//$NON-NLS-2$
853:                    IStatus status = new Status(IStatus.ERROR,
854:                            WorkbenchPlugin.PI_WORKBENCH, 0, message, e);
855:                    WorkbenchPlugin.log(message, status);
856:                }
857:
858:                // Check the loaded object's type
859:                if (classSpec != null && loadedEE != null
860:                        && !classSpec.isInstance(loadedEE)) {
861:                    // ooops, the loaded class is not castable to the given type
862:                    final String message = "Loaded class is of incorrect type: expected(" + //$NON-NLS-1$
863:                            classSpec.getName()
864:                            + ") got (" + loadedEE.getClass().getName() + ")"; //$NON-NLS-1$ //$NON-NLS-2$
865:
866:                    IllegalArgumentException e = new IllegalArgumentException(
867:                            message);
868:                    final IStatus status = new Status(IStatus.ERROR,
869:                            WorkbenchPlugin.PI_WORKBENCH, 0, message, e);
870:                    WorkbenchPlugin.log(message, status);
871:
872:                    // This 'failed'
873:                    loadedEE = null;
874:                }
875:
876:                return loadedEE;
877:            }
878:
879:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.