1 package net.suberic.pooka.gui; 2 import net.suberic.util.gui.propedit.PropertyEditorFactory; 3 import net.suberic.util.gui.ConfigurableToolbar; 4 import net.suberic.util.gui.IconManager; 5 import net.suberic.util.swing.*; 6 import net.suberic.pooka.*; 7 import net.suberic.pooka.gui.search.*; 8 import javax.swing.*; 9 import java.awt.*; 10 import java.awt.event.WindowEvent ; 11 import java.awt.event.WindowAdapter ; 12 import java.util.*; 13 14 15 19 public class PookaMinimalUIFactory implements PookaUIFactory { 20 21 PropertyEditorFactory mEditorFactory = null; 22 ThemeManager mThemeManager = null; 23 MessageNotificationManager mMessageNotificationManager; 24 IconManager mIconManager = IconManager.getIconManager(Pooka.getResources(), "IconManager._default"); 25 26 public boolean mShowing = false; 27 28 int mMaxErrorLine = 50; 29 30 java.util.List mNewMessages = new LinkedList(); 31 32 StatusDisplay mStatusPanel = null; 33 34 WindowAdapter mWindowAdapter = null; 35 36 37 40 public PookaMinimalUIFactory(PookaUIFactory pSource) { 41 mWindowAdapter = new WindowAdapter () { 42 public void windowClosed(WindowEvent we) { 43 Window window = we.getWindow(); 44 if (window instanceof NewMessageFrame) { 45 mNewMessages.remove(window); 46 if (mNewMessages.isEmpty()) { 47 System.exit(0); 48 } 49 } 50 } 51 }; 52 53 if (pSource != null) { 54 mEditorFactory = new PropertyEditorFactory(Pooka.getResources(), pSource.getIconManager(), Pooka.getPookaManager().getHelpBroker()); 55 mThemeManager = new ThemeManager("Pooka.theme", Pooka.getResources()); 56 mMessageNotificationManager = pSource.getMessageNotificationManager(); 57 mMessageNotificationManager.setMainPanel(null); 58 } else { 59 mThemeManager = new ThemeManager("Pooka.theme", Pooka.getResources()); 60 mMessageNotificationManager = new MessageNotificationManager(); 61 mEditorFactory = new PropertyEditorFactory(Pooka.getResources(), mIconManager, Pooka.getPookaManager().getHelpBroker()); 62 } 63 } 64 65 68 public PookaMinimalUIFactory() { 69 this(null); 70 } 71 72 75 public ThemeManager getPookaThemeManager() { 76 return mThemeManager; 77 } 78 79 82 public MessageUI createMessageUI(MessageProxy mp) { 83 return createMessageUI(mp, null); 84 } 85 86 92 public MessageUI createMessageUI(MessageProxy mp, MessageUI templateMui) { 93 if (mp.getMessageUI() != null) 95 return mp.getMessageUI(); 96 97 MessageUI mui; 98 if (mp instanceof NewMessageProxy) { 99 NewMessageFrame nmf = new NewMessageFrame((NewMessageProxy) mp); 100 mNewMessages.add(nmf); 101 nmf.addWindowListener(mWindowAdapter); 102 mui = nmf; 103 } else 104 mui = new ReadMessageFrame(mp); 105 106 mp.setMessageUI(mui); 107 108 if (templateMui != null && templateMui instanceof JComponent) 110 applyNewWindowLocation((JFrame)mui, (JComponent)templateMui); 111 else 112 applyNewWindowLocation((JFrame)mui, null); 113 114 return mui; 115 } 116 117 121 public void unregisterListeners() { 122 Iterator iter = mNewMessages.iterator(); 123 while (iter.hasNext()) { 124 java.awt.Window current = (java.awt.Window ) iter.next(); 125 current.removeWindowListener(mWindowAdapter); 126 } 127 } 128 129 136 public void doDefaultOpen(MessageProxy mp) { 137 } 138 139 145 public FolderDisplayUI createFolderDisplayUI(net.suberic.pooka.FolderInfo fi) { 146 return null; 147 } 148 149 154 public ContentPanel createContentPanel() { 155 return null; 156 } 157 158 162 public ConfigurableToolbar createMainToolbar() { 163 return null; 164 } 165 166 167 171 public ConfigurableToolbar createFolderPanelToolbar() { 172 return null; 173 } 174 175 176 183 public void showEditorWindow(String title, String property, String template) { 184 JFrame jf = (JFrame)getEditorFactory().createEditorWindow(title, property, template); 185 jf.pack(); 186 Component currentFocusedComponent = KeyboardFocusManager.getCurrentKeyboardFocusManager().getFocusOwner(); 187 if (currentFocusedComponent != null && currentFocusedComponent instanceof JComponent) { 188 applyNewWindowLocation(jf, ((JComponent) currentFocusedComponent)); 189 } else { 190 applyNewWindowLocation(jf, null); 191 } 192 jf.setVisible(true); 193 } 194 195 199 public void showEditorWindow(String title, String property) { 200 showEditorWindow(title, property, property); 201 } 202 203 206 public String formatMessage(String message) { 207 return net.suberic.pooka.MailUtilities.wrapText(message, mMaxErrorLine, "\r\n", 5); 208 } 209 210 215 public int showConfirmDialog(String messageText, String title, int type) { 216 String displayMessage = formatMessage(messageText); 217 final ResponseWrapper fResponseWrapper = new ResponseWrapper(); 218 final String fDisplayMessage = displayMessage; 219 final String fTitle = title; 220 final int fType = type; 221 Runnable runMe = new Runnable () { 222 public void run() { 223 fResponseWrapper.setInt(JOptionPane.showConfirmDialog(KeyboardFocusManager.getCurrentKeyboardFocusManager().getFocusOwner(), fDisplayMessage, fTitle, fType)); 224 } 225 }; 226 227 if (! SwingUtilities.isEventDispatchThread()) { 228 try { 229 SwingUtilities.invokeAndWait(runMe); 230 } catch (Exception e) { 231 } 232 } else { 233 runMe.run(); 234 } 235 236 return fResponseWrapper.getInt(); 237 } 238 239 240 243 public int showConfirmDialog(Object [] messageComponents, String title, int type) { 244 final ResponseWrapper fResponseWrapper = new ResponseWrapper(); 245 final Object [] fMessageComponents = messageComponents; 246 final String fTitle = title; 247 final int fType = type; 248 Runnable runMe = new Runnable () { 249 public void run() { 250 fResponseWrapper.setInt(JOptionPane.showConfirmDialog(KeyboardFocusManager.getCurrentKeyboardFocusManager().getFocusOwner(), fMessageComponents, fTitle, fType)); 251 } 252 }; 253 254 if (! SwingUtilities.isEventDispatchThread()) { 255 try { 256 SwingUtilities.invokeAndWait(runMe); 257 } catch (Exception e) { 258 } 259 } else { 260 runMe.run(); 261 } 262 263 return fResponseWrapper.getInt(); 264 } 265 266 271 public void showError(String errorMessage, String title) { 272 final String displayErrorMessage = formatMessage(errorMessage); 273 final String fTitle = title; 274 275 if (mShowing) { 276 SwingUtilities.invokeLater(new Runnable () { 277 public void run() { 278 JOptionPane.showMessageDialog(KeyboardFocusManager.getCurrentKeyboardFocusManager().getFocusOwner(), displayErrorMessage, fTitle, JOptionPane.ERROR_MESSAGE); 279 } 280 }); 281 } else 282 System.out.println(errorMessage); 283 284 285 } 286 287 292 public void showError(String errorMessage) { 293 showError(errorMessage, Pooka.getProperty("Error", "Error")); 294 } 295 296 301 public void showError(String errorMessage, Exception e) { 302 showError(errorMessage, Pooka.getProperty("Error", "Error"), e); 303 } 304 305 310 public void showError(String errorMessage, String title, Exception e) { 311 final String displayErrorMessage = formatMessage(errorMessage + ": " + e.getMessage()); 312 final Exception fE = e; 313 final String fTitle = title; 314 if (mShowing) { 315 SwingUtilities.invokeLater(new Runnable () { 316 public void run() { 317 JOptionPane.showMessageDialog(KeyboardFocusManager.getCurrentKeyboardFocusManager().getFocusOwner(), createErrorPanel(displayErrorMessage, fE), fTitle, JOptionPane.ERROR_MESSAGE); 318 } 319 }); 320 } else 321 System.out.println(errorMessage); 322 323 } 325 326 331 public String showInputDialog(String inputMessage, String title) { 332 final String displayMessage = formatMessage(inputMessage); 333 final String fTitle = title; 334 final ResponseWrapper fResponseWrapper = new ResponseWrapper(); 335 336 Runnable runMe = new Runnable () { 337 public void run() { 338 fResponseWrapper.setString(JOptionPane.showInputDialog(KeyboardFocusManager.getCurrentKeyboardFocusManager().getFocusOwner(), displayMessage, fTitle, JOptionPane.QUESTION_MESSAGE)); 339 } 340 }; 341 342 if (! SwingUtilities.isEventDispatchThread()) { 343 try { 344 SwingUtilities.invokeAndWait(runMe); 345 } catch (Exception e) { 346 } 347 } else { 348 runMe.run(); 349 } 350 351 return fResponseWrapper.getString(); 352 } 353 354 359 public String showInputDialog(Object [] inputPanes, String title) { 360 final String fTitle = title; 361 final Object [] fInputPanes = inputPanes; 362 final ResponseWrapper fResponseWrapper = new ResponseWrapper(); 363 364 Runnable runMe = new Runnable () { 365 public void run() { 366 fResponseWrapper.setString(JOptionPane.showInputDialog(KeyboardFocusManager.getCurrentKeyboardFocusManager().getFocusOwner(), fInputPanes, fTitle, JOptionPane.QUESTION_MESSAGE)); 367 } 368 }; 369 370 if (! SwingUtilities.isEventDispatchThread()) { 371 try { 372 SwingUtilities.invokeAndWait(runMe); 373 } catch (Exception e) { 374 } 375 } else { 376 runMe.run(); 377 } 378 379 return fResponseWrapper.getString(); 380 381 } 382 383 386 public PropertyEditorFactory getEditorFactory() { 387 return mEditorFactory; 388 } 389 390 393 public void setEditorFactory(net.suberic.util.gui.propedit.PropertyEditorFactory pEditorFactory) { 394 mEditorFactory = pEditorFactory; 395 } 396 397 400 public void showMessage(String newMessage, String title) { 401 final String displayMessage = formatMessage(newMessage); 402 final String fTitle = title; 403 Runnable runMe = new Runnable () { 404 public void run() { 405 JTextArea displayPanel = new JTextArea(displayMessage); 406 displayPanel.setEditable(false); 407 java.awt.Dimension dpSize = displayPanel.getPreferredSize(); 408 JScrollPane scrollPane = new JScrollPane(displayPanel, JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED, JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED); 409 scrollPane.setPreferredSize(new java.awt.Dimension (Math.min(dpSize.width + 10, 500), Math.min(dpSize.height + 10, 300))); 410 411 JOptionPane.showMessageDialog(KeyboardFocusManager.getCurrentKeyboardFocusManager().getFocusOwner(), scrollPane, fTitle, JOptionPane.PLAIN_MESSAGE); 412 } 413 }; 414 415 if (! SwingUtilities.isEventDispatchThread()) { 416 try { 417 SwingUtilities.invokeAndWait(runMe); 418 } catch (Exception e) { 419 } 420 } else { 421 runMe.run(); 422 } 423 424 } 425 426 429 public void showStatusMessage(String newMessage) { 430 final String msg = newMessage; 431 432 Runnable runMe = new Runnable () { 433 public void run() { 434 synchronized(this) { 435 if (mStatusPanel == null) { 436 JFrame currentFrame = findJFrame(); 437 mStatusPanel = new StatusDisplay(currentFrame, msg); 438 mStatusPanel.pack(); 439 mStatusPanel.setLocationRelativeTo(currentFrame); 440 mStatusPanel.setTitle("Message Status"); 441 mStatusPanel.setVisible(true); 443 } else { 444 mStatusPanel.setStatusMessage(msg); 445 } 446 } 447 } 448 }; 449 450 if (SwingUtilities.isEventDispatchThread()) 451 runMe.run(); 452 else 453 SwingUtilities.invokeLater(runMe); 454 455 } 456 457 460 public ProgressDialog createProgressDialog(int min, int max, int initialValue, String title, String content) { 461 return new ProgressDialogImpl(min, max, initialValue, title, content); 462 } 463 464 467 public void clearStatus() { 468 Runnable runMe = new Runnable () { 469 public void run() { 470 synchronized(this) { 471 if (mStatusPanel != null) { 472 mStatusPanel.clear(); 473 } 474 } 475 } 476 }; 477 if (SwingUtilities.isEventDispatchThread()) 478 runMe.run(); 479 else 480 SwingUtilities.invokeLater(runMe); 481 } 482 483 489 public void showSearchForm(net.suberic.pooka.FolderInfo[] selectedFolders, java.util.Vector allowedValues) { 490 } 491 492 498 public void showSearchForm(net.suberic.pooka.FolderInfo[] selectedFolders) { 499 showSearchForm(selectedFolders, null); 500 } 501 502 505 public void showAddressWindow(AddressEntryTextArea aeta) { 506 JFrame jf = new JFrame(Pooka.getProperty("AddressBookTable.title", "Choose Address")); 507 jf.getContentPane().add(new AddressBookSelectionPanel(aeta, jf)); 508 jf.pack(); 509 applyNewWindowLocation(jf, aeta); 510 jf.setVisible(true); 511 } 512 513 517 public void setShowing(boolean newValue) { 518 mShowing=newValue; 519 } 520 521 524 public void applyNewWindowLocation(JFrame f, JComponent pParentComponent) { 525 f.setLocationRelativeTo(pParentComponent); 526 } 527 528 531 public Object [] createErrorPanel(String message, Exception e) { 532 Object [] returnValue = new Object [2]; 533 returnValue[0] = message; 534 returnValue[1] = new net.suberic.util.swing.ExceptionDisplayPanel(Pooka.getProperty("error.showStackTrace", "Stack Trace"), e); 535 536 return returnValue; 537 } 538 539 542 JFrame findJFrame() { 543 Window current = KeyboardFocusManager.getCurrentKeyboardFocusManager().getFocusedWindow(); 544 if (current != null && current instanceof JFrame) { 545 return (JFrame) current; 546 } 547 548 if (mNewMessages.size() > 0) { 550 Object first = mNewMessages.get(0); 551 if (first instanceof JFrame) 552 return (JFrame) first; 553 } 554 555 return null; 556 } 557 558 561 class StatusDisplay extends JDialog { 562 563 JLabel mDisplayLabel = null; 565 566 569 public StatusDisplay(JFrame pParentFrame, String pMessage) { 570 super(pParentFrame); 571 mDisplayLabel = new JLabel(); 572 mDisplayLabel.setLayout(new FlowLayout(FlowLayout.CENTER)); 573 mDisplayLabel.setPreferredSize(new Dimension(300,60)); 574 mDisplayLabel.setText(pMessage); 575 JPanel displayPanel = new JPanel(); 576 displayPanel.setBorder(BorderFactory.createEtchedBorder()); 577 displayPanel.add(mDisplayLabel); 578 this.getContentPane().add(displayPanel); 579 } 580 581 584 public void setStatusMessage(String pMessage) { 585 final String msg = pMessage; 586 587 Runnable runMe = new Runnable () { 588 public void run() { 589 mDisplayLabel.setText(msg); 590 mDisplayLabel.repaint(); 591 } 592 }; 593 594 if (SwingUtilities.isEventDispatchThread()) { 595 runMe.run(); 596 } else { 597 SwingUtilities.invokeLater(runMe); 598 } 599 } 600 601 604 public void clear() { 605 Runnable runMe = new Runnable () { 606 public void run() { 607 StatusDisplay.this.dispose(); 608 } 609 }; 610 if (SwingUtilities.isEventDispatchThread()) { 611 runMe.run(); 612 } else { 613 SwingUtilities.invokeLater(runMe); 614 } 615 } 616 } 617 618 621 public MessageNotificationManager getMessageNotificationManager() { 622 return mMessageNotificationManager; 623 } 624 625 628 public net.suberic.util.gui.IconManager getIconManager() { 629 return mIconManager; 630 } 631 632 635 public void setIconManager(net.suberic.util.gui.IconManager pIconManager) { 636 mIconManager = pIconManager; 637 } 638 639 640 } 641 | Popular Tags |