KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > edu > rice > cs > util > swing > Utilities


1 /*BEGIN_COPYRIGHT_BLOCK
2  *
3  * This file is part of DrJava. Download the current version of this project from http://www.drjava.org/
4  * or http://sourceforge.net/projects/drjava/
5  *
6  * DrJava Open Source License
7  *
8  * Copyright (C) 2001-2006 JavaPLT group at Rice University (javaplt@rice.edu). All rights reserved.
9  *
10  * Developed by: Java Programming Languages Team, Rice University, http://www.cs.rice.edu/~javaplt/
11  *
12  * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
13  * documentation files (the "Software"), to deal with the Software without restriction, including without limitation
14  * the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and
15  * to permit persons to whom the Software is furnished to do so, subject to the following conditions:
16  *
17  * - Redistributions of source code must retain the above copyright notice, this list of conditions and the
18  * following disclaimers.
19  * - Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the
20  * following disclaimers in the documentation and/or other materials provided with the distribution.
21  * - Neither the names of DrJava, the JavaPLT, Rice University, nor the names of its contributors may be used to
22  * endorse or promote products derived from this Software without specific prior written permission.
23  * - Products derived from this software may not be called "DrJava" nor use the term "DrJava" as part of their
24  * names without prior written permission from the JavaPLT group. For permission, write to javaplt@rice.edu.
25  *
26  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO
27  * THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
28  * CONTRIBUTORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
29  * CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
30  * WITH THE SOFTWARE.
31  *
32  *END_COPYRIGHT_BLOCK*/

33
34 package edu.rice.cs.util.swing;
35
36 import edu.rice.cs.drjava.ui.MainFrame;
37 import java.awt.EventQueue JavaDoc;
38 import java.awt.*;
39 import java.awt.event.*;
40 import javax.swing.*;
41 import javax.swing.border.Border JavaDoc;
42 import java.awt.datatransfer.*;
43
44 import edu.rice.cs.util.UnexpectedException;
45 import edu.rice.cs.util.StringOps;
46
47 import edu.rice.cs.drjava.ui.DrJavaErrorHandler;
48   
49 public class Utilities {
50   
51   /** True if the program is run in non-interactive test mode. */
52   public static volatile boolean TEST_MODE = false;
53   
54   /** Runs the task synchronously if the current thread is the event thread; otherwise passes it to the
55    * event thread to be run asynchronously after all events already on the queue have been processed.
56    */

57   public static void invokeLater(Runnable JavaDoc task) {
58     if (EventQueue.isDispatchThread()) {
59       task.run();
60       return;
61     }
62     EventQueue.invokeLater(task);
63   }
64   
65   public static void invokeAndWait(Runnable JavaDoc task) {
66     if (EventQueue.isDispatchThread()) {
67       task.run();
68       return;
69     }
70     try { EventQueue.invokeAndWait(task); }
71     catch(Exception JavaDoc e) { throw new UnexpectedException(e); }
72   }
73   
74   public static void main(String JavaDoc[] args) { clearEventQueue(); }
75   
76   public static void clearEventQueue() {
77     Utilities.invokeAndWait(new Runnable JavaDoc() { public void run() { } });
78   }
79   
80   /** Show a modal debug message box with an OK button regardless of TEST_MODE.
81    * @param msg string to display
82    */

83   public static void show(final String JavaDoc msg) {
84      Utilities.invokeAndWait(new Runnable JavaDoc() { public void run() { JOptionPane.showMessageDialog(null, msg); } } );
85   }
86   
87   /** Show a modal debug message box containing a backtrace for the Throwable t.
88    * @param t the Throwable to be back traced.
89    */

90   public static void showTrace(final Throwable JavaDoc t) {
91     Utilities.invokeAndWait(new Runnable JavaDoc() { public void run() { new DrJavaErrorHandler().handle(t); } } );
92   }
93   
94   /** Show a modal debug message box with an OK button when not in TEST_MODE.
95    * @param msg string to display
96    */

97   public static void showDebug(String JavaDoc msg) { showMessageBox(msg, "Debug Message"); }
98   
99   /** Show a modal message box with an OK button.
100    * @param msg string to display
101    */

102   public static void showMessageBox(final String JavaDoc msg, final String JavaDoc title) {
103     //Utilities.invokeAndWait(new Runnable() { public void run() { JOptionPane.showMessageDialog(null, msg); } } );
104
Utilities.invokeAndWait(new Runnable JavaDoc() { public void run() {
105       Utilities.TextAreaMessageDialog.showDialog(null, title, msg);
106     } } );
107   }
108
109   public static void showStackTrace(final Throwable JavaDoc t) {
110     Utilities.invokeAndWait(new Runnable JavaDoc() { public void run() {
111       JOptionPane.showMessageDialog(null, StringOps.getStackTrace(t));
112     } } );
113   }
114   
115   
116   /** Message dialog with a word-wrapping text area that allows copy & paste. */
117   public static class TextAreaMessageDialog extends JDialog {
118
119     /** Show the initialized dialog.
120      * @param comp parent component, or null
121      * @param title dialog title
122      * @param message message for the text area
123      */

124     public static void showDialog(Component comp, String JavaDoc title, String JavaDoc message) {
125       if (TEST_MODE) System.out.println(title + ": " + message);
126       else {
127         Frame frame = JOptionPane.getFrameForComponent(comp);
128         TextAreaMessageDialog dialog = new TextAreaMessageDialog(frame, comp, title, message);
129         MainFrame.setPopupLoc(dialog, frame);
130         dialog.setVisible(true);
131       }
132     }
133
134     /** Private constructor for this dialog. Only gets used in the static showDialog method.
135      * @param frame owner frame
136      * @param comp parent component
137      * @param title dialog title
138      * @param message message for the text area
139      */

140     private TextAreaMessageDialog(Frame frame, Component comp, String JavaDoc title, String JavaDoc message) {
141       super(frame, title, true);
142       setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
143
144       //buttons
145
JButton okButton = new JButton("OK");
146       okButton.addActionListener(new ActionListener() {
147         public void actionPerformed(ActionEvent e) {
148           TextAreaMessageDialog.this.dispose();
149         }
150       });
151       getRootPane().setDefaultButton(okButton);
152
153       JTextArea textArea = new JTextArea(message);
154       textArea.setEditable(false);
155       textArea.setLineWrap(true);
156       textArea.setWrapStyleWord(false);
157       textArea.setBackground(SystemColor.window);
158
159       Border JavaDoc emptyBorder = BorderFactory.createEmptyBorder(10, 10, 10, 10);
160       textArea.setBorder(emptyBorder);
161
162       Container contentPane = getContentPane();
163       contentPane.add(textArea, BorderLayout.CENTER);
164       contentPane.add(okButton, BorderLayout.SOUTH);
165
166       Dimension parentDim = (comp!=null)?(comp.getSize()):getToolkit().getScreenSize();
167       int xs = (int)parentDim.getWidth()/4;
168       int ys = (int)parentDim.getHeight()/5;
169       setSize(Math.max(xs,350), Math.max(ys, 250));
170       setLocationRelativeTo(comp);
171     }
172   }
173   
174   /** @return a string with the current clipboard selection, or null if not available. */
175   public static String JavaDoc getClipboardSelection(Component c) {
176       Clipboard cb = c.getToolkit().getSystemClipboard();
177       if (cb==null) return null;
178       Transferable t = cb.getContents(null);
179       if (t==null) return null;
180       String JavaDoc s = null;
181       try {
182         java.io.Reader JavaDoc r = DataFlavor.stringFlavor.getReaderForText(t);
183         int ch;
184         final StringBuilder JavaDoc sb = new StringBuilder JavaDoc();
185         while ((ch=r.read()) !=-1 ) { sb.append((char)ch); }
186         s = sb.toString();
187       }
188       catch(UnsupportedFlavorException ufe) { /* ignore, return null */ }
189       catch(java.io.IOException JavaDoc ioe) { /* ignore, return null */ }
190       return s;
191   }
192 }
193
Popular Tags