Docjar: A Java Source and Docuemnt Enginecom.*    java.*    javax.*    org.*    all    new    plug-in

Quick Search    Search Deep

Source code: com/port80/eclipse/jdt/actions/Util.java


1   package com.port80.eclipse.jdt.actions;
2   
3   import java.lang.reflect.InvocationTargetException;
4   import java.lang.reflect.Method;
5   
6   import org.eclipse.jface.viewers.Viewer;
7   
8   /**
9    * Static utilities
10   * 
11   * @author chrisl
12   */
13  public class Util {
14  
15    ////////////////////////////////////////////////////////////////////////
16  
17    /** 
18     * Escape pattern and replace string for Regex find and replace. 
19     */
20    public static String escapeRegex(String s) {
21      if (s == null)
22        return null;
23      StringBuffer ret = new StringBuffer();
24      char c;
25      for (int i = 0, len = s.length(); i < len; ++i) {
26        c = s.charAt(i);
27        if (c == '/')
28          ret.append("\\/");
29        else
30          ret.append(c);
31      }
32      return ret.toString();
33    }
34  
35    ////////////////////////////////////////////////////////////////////////
36  
37    public static Viewer getViewer(Object part) {
38      Method method = null;
39      Viewer viewer;
40      // Tree viewers.
41      try {
42        method = part.getClass().getMethod("getTreeViewer", new Class[0]);
43      } catch (NoSuchMethodException e) {
44        System.err.println("part=" + part);
45        e.printStackTrace();
46        method = null;
47      }
48      if (method == null) {
49        try {
50          method = part.getClass().getMethod("getTableViewer", new Class[0]);
51        } catch (NoSuchMethodException e) {
52          System.err.println("part=" + part);
53          e.printStackTrace();
54          method = null;
55        }
56      }
57      if (method == null) {
58        try {
59          method = part.getClass().getMethod("getViewer", new Class[0]);
60        } catch (NoSuchMethodException e) {
61          System.err.println("part=" + part);
62          e.printStackTrace();
63          method = null;
64        }
65      }
66      if (method == null)
67        return null;
68      try {
69        viewer = (Viewer) method.invoke(part, new Object[0]);
70      } catch (IllegalAccessException e) {
71        System.err.println("part=" + part);
72        e.printStackTrace();
73        return null;
74      } catch (InvocationTargetException e) {
75        System.err.println("part=" + part);
76        e.printStackTrace();
77        return null;
78      }
79      return viewer;
80    }
81  
82    ////////////////////////////////////////////////////////////////////////
83  
84  }