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.util.swing; |
38 |
| |
39 |
| import java.awt.*; |
40 |
| import java.awt.event.*; |
41 |
| import javax.swing.*; |
42 |
| import java.awt.datatransfer.*; |
43 |
| import java.beans.PropertyChangeListener; |
44 |
| import java.beans.PropertyChangeEvent; |
45 |
| |
46 |
| import edu.rice.cs.plt.lambda.LambdaUtil; |
47 |
| import edu.rice.cs.util.UnexpectedException; |
48 |
| import edu.rice.cs.util.StringOps; |
49 |
| |
50 |
| public class Utilities { |
51 |
| |
52 |
| |
53 |
| public static volatile boolean TEST_MODE = false; |
54 |
| |
55 |
| public static final String JGOODIES_PACKAGE = "com.jgoodies.looks"; |
56 |
| |
57 |
| |
58 |
| |
59 |
| |
60 |
6122
| public static void invokeLater(Runnable task) {
|
61 |
6122
| if (EventQueue.isDispatchThread()) {
|
62 |
2354
| task.run();
|
63 |
2354
| return;
|
64 |
| } |
65 |
3768
| EventQueue.invokeLater(task);
|
66 |
| } |
67 |
| |
68 |
2596
| public static void invokeAndWait(Runnable task) {
|
69 |
2596
| if (EventQueue.isDispatchThread()) {
|
70 |
405
| task.run();
|
71 |
405
| return;
|
72 |
| } |
73 |
2191
| try { EventQueue.invokeAndWait(task); }
|
74 |
0
| catch(Exception e) { throw new UnexpectedException(e); }
|
75 |
| } |
76 |
| |
77 |
| |
78 |
| |
79 |
| |
80 |
| |
81 |
| |
82 |
605
| public static void clearEventQueue() { clearEventQueue(true); }
|
83 |
| |
84 |
| |
85 |
| |
86 |
| |
87 |
| |
88 |
| |
89 |
| |
90 |
605
| public static void clearEventQueue(boolean newEvents) {
|
91 |
605
| assert ! EventQueue.isDispatchThread();
|
92 |
605
| final EventQueue q = Toolkit.getDefaultToolkit().getSystemEventQueue();
|
93 |
605
| do {
|
94 |
| |
95 |
626
| try { EventQueue.invokeAndWait(LambdaUtil.NO_OP); }
|
96 |
0
| catch (Exception e) { throw new UnexpectedException(e); }
|
97 |
626
| } while (newEvents && (null != q.peekEvent()));
|
98 |
| } |
99 |
| |
100 |
| |
101 |
| |
102 |
| |
103 |
0
| public static void show(final String msg) {
|
104 |
0
| Utilities.invokeAndWait(new Runnable() { public void run() {
|
105 |
0
| new edu.rice.cs.drjava.ui.DrJavaScrollableDialog(null,
|
106 |
| "Debug Message", |
107 |
| "Debug Message from Utilities.show():", |
108 |
| msg, |
109 |
| false).show(); } } ); |
110 |
| } |
111 |
| |
112 |
| |
113 |
| |
114 |
| |
115 |
0
| public static void showDebug(String msg) { showMessageBox(msg, "Debug Message"); }
|
116 |
| |
117 |
| |
118 |
| |
119 |
| |
120 |
0
| public static void showMessageBox(final String msg, final String title) {
|
121 |
0
| if (TEST_MODE) System.out.println(title + ": " + msg); else {
|
122 |
| |
123 |
0
| Utilities.invokeAndWait(new Runnable() { public void run() {
|
124 |
0
| new edu.rice.cs.drjava.ui.DrJavaScrollableDialog(null,
|
125 |
| title, |
126 |
| "Message:", |
127 |
| msg, |
128 |
| false).show(); |
129 |
| } } ); |
130 |
| } |
131 |
| } |
132 |
| |
133 |
0
| public static void showStackTrace(final Throwable t) {
|
134 |
0
| Utilities.invokeAndWait(new Runnable() { public void run() {
|
135 |
0
| new edu.rice.cs.drjava.ui.DrJavaScrollableDialog(null,
|
136 |
| "Stack Trace", |
137 |
| "Stack Trace:", |
138 |
| StringOps.getStackTrace(t), |
139 |
| false).show(); |
140 |
| } } ); |
141 |
| } |
142 |
| |
143 |
| |
144 |
0
| public static String getClipboardSelection(Component c) {
|
145 |
0
| Clipboard cb = c.getToolkit().getSystemClipboard();
|
146 |
0
| if (cb == null) return null;
|
147 |
0
| Transferable t = cb.getContents(null);
|
148 |
0
| if (t == null) return null;
|
149 |
0
| String s = null;
|
150 |
0
| try {
|
151 |
0
| java.io.Reader r = DataFlavor.stringFlavor.getReaderForText(t);
|
152 |
0
| int ch;
|
153 |
0
| final StringBuilder sb = new StringBuilder();
|
154 |
0
| while ((ch=r.read()) !=-1 ) { sb.append((char)ch); }
|
155 |
0
| s = sb.toString();
|
156 |
| } |
157 |
| catch(UnsupportedFlavorException ufe) { } |
158 |
| catch(java.io.IOException ioe) { } |
159 |
0
| return s;
|
160 |
| } |
161 |
| |
162 |
| |
163 |
0
| public static AbstractAction createDelegateAction(String newName, final Action delegate) {
|
164 |
0
| return new AbstractAction(newName) {
|
165 |
0
| public void actionPerformed(ActionEvent ae) { delegate.actionPerformed(ae); }
|
166 |
| }; |
167 |
| } |
168 |
| |
169 |
| |
170 |
114
| public static boolean isPlasticLaf() {
|
171 |
114
| LookAndFeel laf = UIManager.getLookAndFeel();
|
172 |
114
| return laf != null && laf.getClass().getName().startsWith(JGOODIES_PACKAGE);
|
173 |
| } |
174 |
| |
175 |
| |
176 |
| |
177 |
0
| public static boolean isPlasticLaf(String name) {
|
178 |
0
| return name != null && name.startsWith(JGOODIES_PACKAGE);
|
179 |
| } |
180 |
| |
181 |
| |
182 |
| |
183 |
| |
184 |
| |
185 |
| |
186 |
| |
187 |
| |
188 |
418
| public static void setPopupLoc(Window popup, Component owner) {
|
189 |
418
| Rectangle frameRect = popup.getBounds();
|
190 |
| |
191 |
418
| Point ownerLoc = null;
|
192 |
418
| Dimension ownerSize = null;
|
193 |
418
| if (owner != null && owner.isVisible()) {
|
194 |
0
| ownerLoc = owner.getLocation();
|
195 |
0
| ownerSize = owner.getSize();
|
196 |
| } |
197 |
| else { |
198 |
| |
199 |
| |
200 |
418
| GraphicsDevice[] dev = GraphicsEnvironment.getLocalGraphicsEnvironment().getScreenDevices();
|
201 |
418
| Rectangle rec = dev[0].getDefaultConfiguration().getBounds();
|
202 |
418
| ownerLoc = rec.getLocation();
|
203 |
418
| ownerSize = rec.getSize();
|
204 |
| } |
205 |
| |
206 |
| |
207 |
418
| Point loc = new Point(ownerLoc.x + (ownerSize.width - frameRect.width) / 2,
|
208 |
| ownerLoc.y + (ownerSize.height - frameRect.height) / 2); |
209 |
418
| frameRect.setLocation(loc);
|
210 |
| |
211 |
| |
212 |
418
| GraphicsConfiguration gcBest = null;
|
213 |
418
| int gcBestArea = -1;
|
214 |
418
| GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
|
215 |
418
| GraphicsDevice[] gs = ge.getScreenDevices();
|
216 |
418
| for (GraphicsDevice gd: gs) {
|
217 |
418
| GraphicsConfiguration gc = gd.getDefaultConfiguration();
|
218 |
418
| Rectangle isect = frameRect.intersection(gc.getBounds());
|
219 |
418
| int gcArea = isect.width*isect.height;
|
220 |
418
| if (gcArea > gcBestArea) {
|
221 |
418
| gcBest = gc;
|
222 |
418
| gcBestArea = gcArea;
|
223 |
| } |
224 |
| } |
225 |
| |
226 |
| |
227 |
418
| Rectangle screenRect = gcBest.getBounds();
|
228 |
418
| Dimension screenSize = screenRect.getSize();
|
229 |
418
| Dimension frameSize = popup.getSize();
|
230 |
| |
231 |
0
| if (frameSize.height > screenSize.height) frameSize.height = screenSize.height;
|
232 |
0
| if (frameSize.width > screenSize.width) frameSize.width = screenSize.width;
|
233 |
| |
234 |
418
| frameRect.setSize(frameSize);
|
235 |
| |
236 |
| |
237 |
418
| loc = new Point(ownerLoc.x + (ownerSize.width - frameRect.width) / 2,
|
238 |
| ownerLoc.y + (ownerSize.height - frameRect.height) / 2); |
239 |
418
| frameRect.setLocation(loc);
|
240 |
| |
241 |
| |
242 |
0
| if (frameRect.x < screenRect.x) frameRect.x = screenRect.x;
|
243 |
418
| if (frameRect.x + frameRect.width > screenRect.x + screenRect.width)
|
244 |
0
| frameRect.x = screenRect.x + screenRect.width - frameRect.width;
|
245 |
| |
246 |
0
| if (frameRect.y < screenRect.y) frameRect.y = screenRect.y;
|
247 |
418
| if (frameRect.y + frameRect.height > screenRect.y + screenRect.height)
|
248 |
0
| frameRect.y = screenRect.y + screenRect.height - frameRect.height;
|
249 |
| |
250 |
418
| popup.setSize(frameRect.getSize());
|
251 |
418
| popup.setLocation(frameRect.getLocation());
|
252 |
| } |
253 |
| |
254 |
| |
255 |
| |
256 |
| |
257 |
| |
258 |
76
| public static PropertyChangeListener enableDisableWith(Action observable, final Action observer) {
|
259 |
76
| PropertyChangeListener pcl = new PropertyChangeListener() {
|
260 |
0
| public void propertyChange(PropertyChangeEvent e) {
|
261 |
0
| if (e.getPropertyName().equals("enabled")) { observer.setEnabled((Boolean)e.getNewValue()); }
|
262 |
| } |
263 |
| }; |
264 |
76
| observable.addPropertyChangeListener(pcl);
|
265 |
76
| return pcl;
|
266 |
| } |
267 |
| |
268 |
| |
269 |
| |
270 |
| |
271 |
0
| public static int getComponentIndex(Component component) {
|
272 |
0
| if (component != null && component.getParent() != null) {
|
273 |
0
| Container c = component.getParent();
|
274 |
0
| for (int i = 0; i < c.getComponentCount(); i++) {
|
275 |
0
| if (c.getComponent(i) == component)
|
276 |
0
| return i;
|
277 |
| } |
278 |
| } |
279 |
| |
280 |
0
| return -1;
|
281 |
| } |
282 |
| } |