| 1 package com.sshtools.ui.awt.options; 2 3 import java.awt.BorderLayout ; 4 import java.awt.Button ; 5 import java.awt.Color ; 6 import java.awt.Component ; 7 import java.awt.Dialog ; 8 import java.awt.Dimension ; 9 import java.awt.FlowLayout ; 10 import java.awt.Frame ; 11 import java.awt.GridBagConstraints ; 12 import java.awt.GridBagLayout ; 13 import java.awt.Image ; 14 import java.awt.Insets ; 15 import java.awt.Label ; 16 import java.awt.Panel ; 17 import java.awt.ScrollPane ; 18 import java.awt.TextField ; 19 import java.awt.event.ActionEvent ; 20 import java.awt.event.ActionListener ; 21 import java.awt.event.WindowAdapter ; 22 import java.awt.event.WindowEvent ; 23 import java.io.PrintWriter ; 24 import java.io.StringWriter ; 25 import java.util.StringTokenizer ; 26 import java.util.Vector ; 27 28 import com.sshtools.ui.awt.ImageCanvas; 29 import com.sshtools.ui.awt.MultilineLabel; 30 import com.sshtools.ui.awt.UIUtil; 31 32 public class OptionDialog extends Panel { 33 34 public static String INFORMATION_ICON = "/images/information-48x48.png"; public static String WARNING_ICON = "/images/warning-48x48.png"; public static String QUESTION_ICON = "/images/question-48x48.png"; public static String ERROR_ICON = "/images/error-48x48.png"; 39 40 public static final int INFORMATION = 0; 41 42 public static final int QUESTION = 1; 43 44 public static final int WARNING = 2; 45 46 public static final int ERROR = 3; 47 48 public static final int UNCATEGORISED = 99; 49 50 public static boolean useDialogForPrompt = true; 51 52 public static final Option CHOICE_YES = new Option(Messages.getString("OptionDialog.yes")); 54 public static final Option CHOICE_SHOW = new Option(Messages.getString("OptionDialog.show")); 56 public static final Option CHOICE_HIDE = new Option(Messages.getString("OptionDialog.hide")); 58 public static final Option CHOICE_NO = new Option(Messages.getString("OptionDialog.no")); 60 public static final Option CHOICE_OK = new Option(Messages.getString("OptionDialog.ok")); 62 public static final Option CHOICE_YES_TO_ALL = new Option(Messages.getString("OptionDialog.yesToAll")); 64 public static final Option CHOICE_CANCEL = new Option(Messages.getString("OptionDialog.cancel")); 66 public static final Option CHOICES_YES_NO[] = { CHOICE_YES, CHOICE_NO }; 67 68 public static final Option CHOICES_OK_CANCEL[] = { CHOICE_OK, CHOICE_CANCEL }; 69 70 public static final Option CHOICES_OK[] = { CHOICE_OK }; 71 72 private Object lock_; 73 private boolean dismissed; 74 private Option selected; 75 private OptionCallback callback; 76 private Dialog dialog; 77 78 public OptionDialog(int type, Object text, Option choices[], OptionCallback callback) { 79 this(type, text, choices, callback, null); 80 } 81 82 public OptionDialog(int type, Object text, Option choices[], OptionCallback callback, Component buttonBarAccessory) { 83 super(new BorderLayout ()); 84 lock_ = new Object (); 85 dismissed = false; 86 this.callback = callback; 87 Panel titlePanel = new Panel (new FlowLayout (0)); 88 Image icon = null; 89 switch (type) { 90 case 0: 91 icon = UIUtil.loadImage(getClass(), INFORMATION_ICON); 93 break; 94 case 2: 95 icon = UIUtil.loadImage(getClass(), WARNING_ICON); 97 break; 98 case 1: 99 icon = UIUtil.loadImage(getClass(), QUESTION_ICON); 101 break; 102 case 99: 103 break; 104 default: 105 icon = UIUtil.loadImage(getClass(), ERROR_ICON); 106 break; 107 } 108 if (icon != null) { 109 UIUtil.waitFor(icon, this); 110 } 111 Panel textPanel = new Panel (new GridBagLayout ()); 112 GridBagConstraints gbc = new GridBagConstraints (); 113 gbc.anchor = GridBagConstraints.WEST; 114 gbc.fill = GridBagConstraints.NONE; 115 if (text instanceof Component ) { 116 UIUtil.gridBagAdd(textPanel, (Component ) text, gbc, 0); 117 } else { 118 StringTokenizer st = new StringTokenizer (String.valueOf(text), "\n"); while (st.hasMoreTokens()) { 120 String n = st.nextToken().trim(); 121 if (n.length() > 0) { 122 UIUtil.gridBagAdd(textPanel, new Label (n), gbc, 0); 123 } 124 } 125 } 126 add(textPanel, "Center"); Panel choicePanel = new Panel (new FlowLayout (buttonBarAccessory == null ? FlowLayout.CENTER : FlowLayout.RIGHT)); 128 OptionWrapper choice = new OptionWrapper(); 129 choice.idx = -1; 130 for (int i = 0; choices != null && i < choices.length; i++) { 131 Button b = new Button (choices[i].getText()) { 134 public Dimension getMinimumSize() { 135 return new Dimension (60, super.getMinimumSize().height); 136 } 137 138 public Dimension getPreferredSize() { 139 return getMinimumSize(); 140 } 141 }; 142 choicePanel.add(b); 143 b.addActionListener(new BlockingActionListener(choices[i])); 144 } 145 if (icon != null) { 146 add(new ImageCanvas(icon), "West"); } 148 if(buttonBarAccessory != null) { 149 Panel p= new Panel (new GridBagLayout ()); 150 GridBagConstraints gbc2 = new GridBagConstraints (); 151 gbc2.anchor = GridBagConstraints.WEST; 152 gbc2.fill = GridBagConstraints.HORIZONTAL; 153 gbc2.insets = new Insets (2, 4, 2, 2); 154 UIUtil.gridBagAdd(p, buttonBarAccessory, gbc2, GridBagConstraints.RELATIVE); 155 gbc2.anchor = GridBagConstraints.EAST; 156 gbc2.weightx = 1.0; 157 UIUtil.gridBagAdd(p, choicePanel, gbc2, GridBagConstraints.REMAINDER); 158 add(p, BorderLayout.SOUTH); 159 } 160 else { 161 add(choicePanel, "South"); } 163 } 164 165 public static void main(String [] args) { 166 Frame f = new Frame (); 167 OptionDialog.error(f, "Test", "This is a test error", new Exception ("Test")); } 169 170 public void choice(Option choice) { 171 selected = choice; 172 if (dialog != null) 173 dialog.dispose(); 174 else 175 synchronized (lock_) { 176 dismissed = true; 177 lock_.notify(); 178 } 179 } 180 181 public Option dialogPrompt(Component parent, String title) { 182 java.awt.Frame f = parent == null ? null : (parent instanceof Frame ? (Frame ) parent : UIUtil.getFrameAncestor(parent)); 183 if (f == null) { 184 f = UIUtil.getSharedFrame(); 185 } 186 dialog = new Dialog (f, title, true); 187 dialog.addWindowListener(new WindowAdapter () { 188 public void windowClosing(WindowEvent evt) { 189 dialog.dispose(); 190 } 191 }); 192 dialog.setLayout(new BorderLayout ()); 193 dialog.add(this); 194 dialog.pack(); 195 Dimension s = dialog.getSize(); 196 dialog.setSize(s.width + 16, s.height + 16); 197 UIUtil.positionComponent(UIUtil.CENTER, dialog); 198 dialog.setResizable(false); 199 dialog.setVisible(true); 200 return selected; 201 } 202 203 public static char[] promptForAuthentication(Component parent, String title) { 204 return promptForAuthentication(parent, title, Messages.getString("OptionDialog.password")); } 206 207 public static char[] promptForAuthentication(Component parent, String title, String label) { 208 String t = promptForText(parent, title, "", null, '*', label); return t != null ? t.toCharArray() : null; 210 } 211 212 public static String promptForText(Component parent, String title, String defaultText, Component accessory, char echoCharacter, 213 String label) { 214 return promptForText(parent, title, defaultText, accessory, echoCharacter, label, -1, "South"); } 216 217 public static String promptForText(Component parent, String title, String defaultText, Component accessory, char echoCharacter, 218 String label, int textWidth, String accesoryPosition) { 219 Panel p = new Panel (new BorderLayout ()); 220 Panel middle = new Panel (new FlowLayout ()); 221 middle.add(new Label (label)); 222 TextField text = new TextField (defaultText, textWidth == -1 ? 15 : textWidth); 223 middle.add(text); 224 p.add(middle, "Center"); if (echoCharacter != ' ') 226 text.setEchoChar(echoCharacter); 227 text.requestFocus(); 228 final OptionDialog dialog = new OptionDialog(QUESTION, p, CHOICES_OK_CANCEL, null); 229 text.addActionListener(new ActionListener () { 230 public void actionPerformed(ActionEvent evt) { 231 dialog.choice(CHOICE_OK); 232 } 233 }); 234 if (accessory != null) 235 p.add(accessory, accesoryPosition); 236 if (dialog.dialogPrompt(parent, title) == CHOICE_OK) 237 return text.getText(); 238 else 239 return null; 240 } 241 242 public static Option prompt(Component parent, int type, String title, Object text, Option choices[]) { 243 return prompt(parent, type, title, text, choices, null); 244 } 245 246 public static Option prompt(Component parent, int type, String title, Object text, Option choices[], OptionCallback callback) { 247 return prompt(parent, type, title, text, choices, callback, null); 248 } 249 250 public static Option prompt(Component parent, int type, String title, Object text, Option choices[], OptionCallback callback, Component buttonBarAccesory) { 251 return new OptionDialog(type, text, choices, callback, buttonBarAccesory).dialogPrompt(parent, title); 252 } 253 254 261 public static void error(Component parent, String title, Throwable exception) { 262 error(parent, title, null, exception); 263 } 264 265 272 public static void error(Component parent, String title, String message) { 273 error(parent, title, message, null); 274 } 275 276 284 public static void error(Component parent, String title, String mesg, Throwable exception) { 285 error(parent, title, mesg, exception, null); 286 } 287 288 296 public static Option error(Component parent, String title, String mesg, Throwable exception, Option[] options) { 297 boolean details = false; 298 if (exception != null) { 299 exception.printStackTrace(); 300 } 301 while (true) { 302 Vector optlist = new Vector (); 303 int detailsIdx = -1; 304 if (options != null) { 305 for (int i = 0; i < options.length; i++) { 306 optlist.addElement(options[i]); 307 } 308 } 309 if (exception != null) { 310 detailsIdx = optlist.size(); 311 if (details) { 312 optlist.addElement(CHOICE_HIDE); 313 } else { 314 optlist.addElement(CHOICE_SHOW); 315 } 316 } 317 if (options == null) { 318 optlist.addElement(CHOICE_OK); 319 } 320 Option[] opts = new Option[optlist.size()]; 321 for (int i = 0; i < optlist.size(); i++) { 322 opts[i] = (Option) optlist.elementAt(i); 323 } 324 325 StringBuffer buf = new StringBuffer (); 326 if (mesg != null) { 327 buf.append(mesg + "\n"); } 329 appendException(exception, 0, buf, details); 330 Component message; 331 if (details) { 332 MultilineLabel text = new MultilineLabel(buf.toString()); 333 message = new ScrollPane (ScrollPane.SCROLLBARS_AS_NEEDED); 334 message.setSize(new Dimension (520, 400)); 335 ((ScrollPane ) message).add(text); 336 337 } else { 338 message = new MultilineLabel(buf.toString()); 339 } 340 Option opt = prompt(parent, ERROR, title, message, opts); 341 if (opt == CHOICE_HIDE || opt == CHOICE_SHOW) { 342 details = !details; 343 } else { 344 if (options != null) { 345 return opt; 346 } else { 347 return null; 348 } 349 } 350 } 351 } 352 353 protected static void appendException(Throwable exception, int level, StringBuffer buf, boolean details) { 354 try { 355 if (((exception != null) && (exception.getMessage() != null)) && (exception.getMessage().length() > 0)) { 356 if (details && (level > 0)) { 357 buf.append("\n \nCaused by ...\n"); } 359 buf.append(exception.getMessage()); 360 } 361 if (details) { 362 if (exception != null) { 363 if ((exception.getMessage() != null) && (exception.getMessage().length() == 0)) { 364 buf.append("\n \nCaused by ..."); } else { 366 buf.append("\n \n"); } 368 } 369 StringWriter sw = new StringWriter (); 370 if (exception != null) { 371 exception.printStackTrace(new PrintWriter (sw)); 372 } 373 buf.append(sw.toString()); 374 } 375 try { 376 java.lang.reflect.Method method = exception.getClass().getMethod("getCause", new Class [] {}); Throwable cause = (Throwable ) method.invoke(exception, (Object []) null); 378 if (cause != null) { 379 appendException(cause, level + 1, buf, details); 380 } 381 } catch (Exception e) { 382 } 383 } catch (Throwable ex) { 384 } 385 } 386 387 public static void info(Component parent, String title, String message) { 388 prompt(parent, 0, title, message, CHOICES_OK); 389 } 390 391 static class OptionWrapper { 392 int idx; 393 394 OptionWrapper() { 395 } 396 } 397 398 class BlockingActionListener implements ActionListener { 399 public void actionPerformed(ActionEvent e) { 400 if (callback != null && !callback.allowClose(choice)) 401 return; 402 selected = choice; 403 if (dialog != null) 404 dialog.dispose(); 405 else 406 synchronized (lock_) { 407 dismissed = true; 408 lock_.notify(); 409 } 410 } 411 412 Option choice; 413 414 BlockingActionListener(Option choice) { 415 this.choice = choice; 416 } 417 } 418 } | Popular Tags |