|
1 |
| |
|
2 |
| |
|
3 |
| |
|
4 |
| |
|
5 |
| |
|
6 |
| |
|
7 |
| |
|
8 |
| |
|
9 |
| |
|
10 |
| |
|
11 |
| |
|
12 |
| |
|
13 |
| |
|
14 |
| |
|
15 |
| |
|
16 |
| |
|
17 |
| |
|
18 |
| |
|
19 |
| |
|
20 |
| |
|
21 |
| |
|
22 |
| |
|
23 |
| |
|
24 |
| |
|
25 |
| |
|
26 |
| |
|
27 |
| |
|
28 |
| |
|
29 |
| |
|
30 |
| |
|
31 |
| |
|
32 |
| |
|
33 |
| |
|
34 |
| |
|
35 |
| |
|
36 |
| |
|
37 |
| package edu.rice.cs.javalanglevels.util; |
|
38 |
| |
|
39 |
| import java.awt.EventQueue; |
|
40 |
| import java.awt.*; |
|
41 |
| import java.awt.event.*; |
|
42 |
| import java.awt.datatransfer.*; |
|
43 |
| import java.lang.reflect.Array; |
|
44 |
| |
|
45 |
| import java.io.File; |
|
46 |
| import java.io.FileOutputStream; |
|
47 |
| import java.io.FileInputStream; |
|
48 |
| import java.io.IOException; |
|
49 |
| import java.io.PrintWriter; |
|
50 |
| import java.io.StringReader; |
|
51 |
| import java.io.StringWriter; |
|
52 |
| import java.nio.channels.FileChannel; |
|
53 |
| |
|
54 |
| import javax.swing.*; |
|
55 |
| import java.text.DecimalFormat; |
|
56 |
| import java.util.ArrayList; |
|
57 |
| import java.util.List; |
|
58 |
| import java.util.HashMap; |
|
59 |
| |
|
60 |
| import edu.rice.cs.javalanglevels.tree.ModifiersAndVisibility; |
|
61 |
| |
|
62 |
| public class Utilities { |
|
63 |
| |
|
64 |
| |
|
65 |
45
| public static void copyFile(File sourceFile, File destFile) throws IOException {
|
|
66 |
45
| if (! destFile.exists()) destFile.createNewFile();
|
|
67 |
| |
|
68 |
45
| FileChannel source = null;
|
|
69 |
45
| FileChannel destination = null;
|
|
70 |
45
| try {
|
|
71 |
45
| source = new FileInputStream(sourceFile).getChannel();
|
|
72 |
45
| destination = new FileOutputStream(destFile).getChannel();
|
|
73 |
45
| destination.transferFrom(source, 0, source.size());
|
|
74 |
| } |
|
75 |
| finally { |
|
76 |
45
| if (source != null) source.close();
|
|
77 |
45
| if (destination != null) destination.close();
|
|
78 |
| } |
|
79 |
| } |
|
80 |
| |
|
81 |
| |
|
82 |
| |
|
83 |
| |
|
84 |
| |
|
85 |
0
| public static void invokeLater(Runnable task) {
|
|
86 |
0
| if (EventQueue.isDispatchThread()) {
|
|
87 |
0
| task.run();
|
|
88 |
0
| return;
|
|
89 |
| } |
|
90 |
0
| EventQueue.invokeLater(task);
|
|
91 |
| } |
|
92 |
| |
|
93 |
0
| public static void invokeAndWait(Runnable task) {
|
|
94 |
0
| if (EventQueue.isDispatchThread()) {
|
|
95 |
0
| task.run();
|
|
96 |
0
| return;
|
|
97 |
| } |
|
98 |
0
| try { EventQueue.invokeAndWait(task); }
|
|
99 |
0
| catch(Exception e) { throw new RuntimeException(e); }
|
|
100 |
| } |
|
101 |
| |
|
102 |
0
| public static void main(String[] args) { clearEventQueue(); }
|
|
103 |
| |
|
104 |
0
| public static void clearEventQueue() { Utilities.invokeAndWait(new Runnable() { public void run() { } }); }
|
|
105 |
| |
|
106 |
| |
|
107 |
| |
|
108 |
| |
|
109 |
0
| public static void show(final String msg) {
|
|
110 |
0
| Utilities.invokeAndWait(new Runnable() {
|
|
111 |
0
| public void run() {
|
|
112 |
0
| new ScrollableDialog(null,"Debug Message", "Debug Message from Utilities.show():", msg, false).show();
|
|
113 |
| } |
|
114 |
| } ); |
|
115 |
| } |
|
116 |
| |
|
117 |
| |
|
118 |
| |
|
119 |
| |
|
120 |
0
| public static void showDebug(String msg) { showMessageBox(msg, "Debug Message"); }
|
|
121 |
| |
|
122 |
| |
|
123 |
| |
|
124 |
| |
|
125 |
0
| public static void showMessageBox(final String msg, final String title) {
|
|
126 |
| |
|
127 |
0
| Utilities.invokeAndWait(new Runnable() {
|
|
128 |
0
| public void run() { new ScrollableDialog(null, title, "Message:", msg, false).show(); }
|
|
129 |
| } ); |
|
130 |
| } |
|
131 |
| |
|
132 |
0
| public static void showStackTrace(final Throwable t) {
|
|
133 |
0
| Utilities.invokeAndWait(new Runnable() {
|
|
134 |
0
| public void run() { new ScrollableDialog(null, "Stack Trace", "Stack Trace:", getStackTrace(t), false).show(); }
|
|
135 |
| } ); |
|
136 |
| } |
|
137 |
| |
|
138 |
| |
|
139 |
0
| public static String getClipboardSelection(Component c) {
|
|
140 |
0
| Clipboard cb = c.getToolkit().getSystemClipboard();
|
|
141 |
0
| if (cb == null) return null;
|
|
142 |
0
| Transferable t = cb.getContents(null);
|
|
143 |
0
| if (t == null) return null;
|
|
144 |
0
| String s = null;
|
|
145 |
0
| try {
|
|
146 |
0
| java.io.Reader r = DataFlavor.stringFlavor.getReaderForText(t);
|
|
147 |
0
| int ch;
|
|
148 |
0
| final StringBuilder sb = new StringBuilder();
|
|
149 |
0
| while ((ch=r.read()) !=-1 ) { sb.append((char)ch); }
|
|
150 |
0
| s = sb.toString();
|
|
151 |
| } |
|
152 |
| catch(UnsupportedFlavorException ufe) { } |
|
153 |
| catch(java.io.IOException ioe) { } |
|
154 |
0
| return s;
|
|
155 |
| } |
|
156 |
| |
|
157 |
| |
|
158 |
0
| public static AbstractAction createDelegateAction(String newName, final Action delegate) {
|
|
159 |
0
| return new AbstractAction(newName) {
|
|
160 |
0
| public void actionPerformed(ActionEvent ae) { delegate.actionPerformed(ae); }
|
|
161 |
| }; |
|
162 |
| } |
|
163 |
| |
|
164 |
| |
|
165 |
| |
|
166 |
| |
|
167 |
0
| public static String getStackTrace(Throwable t) {
|
|
168 |
0
| StringWriter sw = new StringWriter();
|
|
169 |
0
| PrintWriter pw = new PrintWriter(sw);
|
|
170 |
0
| t.printStackTrace(pw);
|
|
171 |
0
| return sw.toString();
|
|
172 |
| } |
|
173 |
| |
|
174 |
| |
|
175 |
| |
|
176 |
| |
|
177 |
0
| public static String getStackTrace() {
|
|
178 |
| |
|
179 |
0
| try { throw new Exception(); }
|
|
180 |
| catch (Exception e) { |
|
181 |
0
| StringWriter sw = new StringWriter();
|
|
182 |
0
| PrintWriter pw = new PrintWriter(sw);
|
|
183 |
0
| StackTraceElement[] stes = e.getStackTrace();
|
|
184 |
0
| int skip = 1;
|
|
185 |
0
| for(StackTraceElement ste: stes) {
|
|
186 |
0
| if (skip > 0) --skip;
|
|
187 |
0
| else { pw.print("at "); pw.println(ste); }
|
|
188 |
| } |
|
189 |
0
| return sw.toString();
|
|
190 |
| } |
|
191 |
| } |
|
192 |
| |
|
193 |
| |
|
194 |
| |
|
195 |
1732
| public static boolean contains(Object[] a, Object elt) {
|
|
196 |
996
| for (Object o: a) { if (o.equals(elt)) return true; }
|
|
197 |
736
| return false;
|
|
198 |
| } |
|
199 |
| |
|
200 |
| |
|
201 |
176
| public static boolean hasVisibilityModifier(String[] modifiers) {
|
|
202 |
176
| for (String s: modifiers) {
|
|
203 |
37
| if (s.equals("private") || s.equals("public") || s.equals("protected")) return true;
|
|
204 |
| } |
|
205 |
139
| return false;
|
|
206 |
| } |
|
207 |
| |
|
208 |
| |
|
209 |
66
| public static boolean isFinal(String[] modifiers) { return contains(modifiers, "final"); }
|
|
210 |
| |
|
211 |
| |
|
212 |
107
| public static boolean isStatic(String[] modifiers) { return contains(modifiers, "static"); }
|
|
213 |
| |
|
214 |
| |
|
215 |
8
| public static boolean isPublic(String[] modifiers) { return contains(modifiers, "public"); }
|
|
216 |
| |
|
217 |
| |
|
218 |
0
| public static boolean isProtected(String[] modifiers) { return contains(modifiers, "protected"); }
|
|
219 |
| |
|
220 |
| |
|
221 |
0
| public static boolean isPrivate(String[] modifiers) { return contains(modifiers, "private"); }
|
|
222 |
| |
|
223 |
| |
|
224 |
126
| public static boolean isAbstract(String[] modifiers) { return contains(modifiers, "abstract"); }
|
|
225 |
| |
|
226 |
| |
|
227 |
| |
|
228 |
0
| public static boolean isFinal(ModifiersAndVisibility mav) { return contains(mav.getModifiers(), "final"); }
|
|
229 |
| |
|
230 |
| |
|
231 |
0
| public static boolean isStatic(ModifiersAndVisibility mav) { return contains(mav.getModifiers(), "static"); }
|
|
232 |
| |
|
233 |
| |
|
234 |
1118
| public static boolean isPublic(ModifiersAndVisibility mav) { return contains(mav.getModifiers(), "public"); }
|
|
235 |
| |
|
236 |
| |
|
237 |
142
| public static boolean isProtected(ModifiersAndVisibility mav) { return contains(mav.getModifiers(), "protected"); }
|
|
238 |
| |
|
239 |
| |
|
240 |
165
| public static boolean isPrivate(ModifiersAndVisibility mav) { return contains(mav.getModifiers(), "private"); }
|
|
241 |
| |
|
242 |
| |
|
243 |
0
| public static boolean isAbstract(ModifiersAndVisibility mav) { return contains(mav.getModifiers(), "abstract"); }
|
|
244 |
| |
|
245 |
121
| public static <T> T[] catenate(T[] A, T[] B) {
|
|
246 |
| |
|
247 |
| |
|
248 |
| |
|
249 |
121
| Class<T> eltClass = (Class<T>) A.getClass().getComponentType();
|
|
250 |
121
| T[] C = (T[]) Array.newInstance(eltClass, A.length + B.length);
|
|
251 |
121
| System.arraycopy(A, 0, C, 0, A.length);
|
|
252 |
| |
|
253 |
121
| System.arraycopy(B, 0, C, A.length, B.length);
|
|
254 |
121
| return C;
|
|
255 |
| } |
|
256 |
| } |