1 33 34 package edu.rice.cs.util.swing; 35 36 import edu.rice.cs.drjava.ui.MainFrame; 37 import java.awt.EventQueue ; 38 import java.awt.*; 39 import java.awt.event.*; 40 import javax.swing.*; 41 import javax.swing.border.Border ; 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 52 public static volatile boolean TEST_MODE = false; 53 54 57 public static void invokeLater(Runnable task) { 58 if (EventQueue.isDispatchThread()) { 59 task.run(); 60 return; 61 } 62 EventQueue.invokeLater(task); 63 } 64 65 public static void invokeAndWait(Runnable task) { 66 if (EventQueue.isDispatchThread()) { 67 task.run(); 68 return; 69 } 70 try { EventQueue.invokeAndWait(task); } 71 catch(Exception e) { throw new UnexpectedException(e); } 72 } 73 74 public static void main(String [] args) { clearEventQueue(); } 75 76 public static void clearEventQueue() { 77 Utilities.invokeAndWait(new Runnable () { public void run() { } }); 78 } 79 80 83 public static void show(final String msg) { 84 Utilities.invokeAndWait(new Runnable () { public void run() { JOptionPane.showMessageDialog(null, msg); } } ); 85 } 86 87 90 public static void showTrace(final Throwable t) { 91 Utilities.invokeAndWait(new Runnable () { public void run() { new DrJavaErrorHandler().handle(t); } } ); 92 } 93 94 97 public static void showDebug(String msg) { showMessageBox(msg, "Debug Message"); } 98 99 102 public static void showMessageBox(final String msg, final String title) { 103 Utilities.invokeAndWait(new Runnable () { public void run() { 105 Utilities.TextAreaMessageDialog.showDialog(null, title, msg); 106 } } ); 107 } 108 109 public static void showStackTrace(final Throwable t) { 110 Utilities.invokeAndWait(new Runnable () { public void run() { 111 JOptionPane.showMessageDialog(null, StringOps.getStackTrace(t)); 112 } } ); 113 } 114 115 116 117 public static class TextAreaMessageDialog extends JDialog { 118 119 124 public static void showDialog(Component comp, String title, String 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 140 private TextAreaMessageDialog(Frame frame, Component comp, String title, String message) { 141 super(frame, title, true); 142 setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE); 143 144 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 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 175 public static String 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 s = null; 181 try { 182 java.io.Reader r = DataFlavor.stringFlavor.getReaderForText(t); 183 int ch; 184 final StringBuilder sb = new StringBuilder (); 185 while ((ch=r.read()) !=-1 ) { sb.append((char)ch); } 186 s = sb.toString(); 187 } 188 catch(UnsupportedFlavorException ufe) { } 189 catch(java.io.IOException ioe) { } 190 return s; 191 } 192 } 193 | Popular Tags |