1 package net.suberic.pooka.gui; 2 import net.suberic.util.swing.ProgressDialog; 3 import net.suberic.util.gui.IconManager; 4 import net.suberic.util.gui.propedit.PropertyEditorFactory; 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 11 14 public abstract class SwingUIFactory implements PookaUIFactory { 15 16 protected ThemeManager pookaThemeManager = null; 17 protected MessageNotificationManager mMessageNotificationManager; 18 protected IconManager mIconManager; 19 protected PropertyEditorFactory editorFactory = null; 20 21 public boolean showing = false; 22 23 protected int mMaxErrorLine = 50; 24 25 28 public ThemeManager getPookaThemeManager() { 29 return pookaThemeManager; 30 } 31 32 35 public MessageUI createMessageUI(MessageProxy mp) throws javax.mail.MessagingException { 36 return createMessageUI(mp, null); 37 } 38 39 43 public abstract MessageUI createMessageUI(MessageProxy mp, MessageUI mui) throws javax.mail.MessagingException ; 44 45 51 public abstract void doDefaultOpen(MessageProxy mp); 52 53 57 public abstract FolderDisplayUI createFolderDisplayUI(net.suberic.pooka.FolderInfo fi); 58 59 62 public abstract ContentPanel createContentPanel(); 63 64 68 public void showEditorWindow(String title, String property) { 69 showEditorWindow(title, property, property); 70 } 71 72 77 public abstract void showEditorWindow(String title, String property, String template); 78 79 82 public net.suberic.util.gui.propedit.PropertyEditorFactory getEditorFactory() { 83 return editorFactory; 84 } 85 86 89 public void setEditorFactory(net.suberic.util.gui.propedit.PropertyEditorFactory pEditorFactory) { 90 editorFactory = pEditorFactory; 91 } 92 93 94 97 public int showConfirmDialog(String message, String title, int type) { 98 String displayMessage = formatMessage(message); 99 final ResponseWrapper fResponseWrapper = new ResponseWrapper(); 100 final String fDisplayMessage = displayMessage; 101 final String fTitle = title; 102 final int fType = type; 103 Runnable runMe = new Runnable () { 104 public void run() { 105 fResponseWrapper.setInt(JOptionPane.showConfirmDialog(Pooka.getMainPanel(), fDisplayMessage, fTitle, fType)); 106 } 107 }; 108 109 if (! SwingUtilities.isEventDispatchThread()) { 110 try { 111 SwingUtilities.invokeAndWait(runMe); 112 } catch (Exception e) { 113 } 114 } else { 115 runMe.run(); 116 } 117 118 return fResponseWrapper.getInt(); 119 } 120 121 124 public int showConfirmDialog(Object [] messageComponents, String title, int type) { 125 final ResponseWrapper fResponseWrapper = new ResponseWrapper(); 126 final Object [] fMessageComponents = messageComponents; 127 final String fTitle = title; 128 final int fType = type; 129 Runnable runMe = new Runnable () { 130 public void run() { 131 fResponseWrapper.setInt(JOptionPane.showConfirmDialog(Pooka.getMainPanel(), fMessageComponents, fTitle, fType)); 132 } 133 }; 134 135 if (! SwingUtilities.isEventDispatchThread()) { 136 try { 137 SwingUtilities.invokeAndWait(runMe); 138 } catch (Exception e) { 139 } 140 } else { 141 runMe.run(); 142 } 143 144 return fResponseWrapper.getInt(); 145 } 146 147 150 public String showInputDialog(String inputMessage, String title) { 151 final String displayMessage = formatMessage(inputMessage); 152 final String fTitle = title; 153 final ResponseWrapper fResponseWrapper = new ResponseWrapper(); 154 155 Runnable runMe = new Runnable () { 156 public void run() { 157 fResponseWrapper.setString(JOptionPane.showInputDialog(Pooka.getMainPanel(), displayMessage, fTitle, JOptionPane.QUESTION_MESSAGE)); 158 } 159 }; 160 161 if (! SwingUtilities.isEventDispatchThread()) { 162 try { 163 SwingUtilities.invokeAndWait(runMe); 164 } catch (Exception e) { 165 } 166 } else { 167 runMe.run(); 168 } 169 170 return fResponseWrapper.getString(); 171 } 172 173 176 public String showInputDialog(Object [] inputPanels, String title) { 177 final String fTitle = title; 178 final Object [] fInputPanes = inputPanels; 179 final ResponseWrapper fResponseWrapper = new ResponseWrapper(); 180 181 Runnable runMe = new Runnable () { 182 public void run() { 183 fResponseWrapper.setString(JOptionPane.showInputDialog(Pooka.getMainPanel(), fInputPanes, fTitle, JOptionPane.QUESTION_MESSAGE)); 184 } 185 }; 186 187 if (! SwingUtilities.isEventDispatchThread()) { 188 try { 189 SwingUtilities.invokeAndWait(runMe); 190 } catch (Exception e) { 191 } 192 } else { 193 runMe.run(); 194 } 195 196 return fResponseWrapper.getString(); 197 } 198 199 202 public void showStatusMessage(String newMessage) { 203 final String msg = newMessage; 204 Runnable runMe = new Runnable () { 205 public void run() { 206 if (Pooka.getMainPanel() != null && Pooka.getMainPanel().getInfoPanel() != null) 207 Pooka.getMainPanel().getInfoPanel().setMessage(msg); 208 } 209 }; 210 if (SwingUtilities.isEventDispatchThread()) { 211 runMe.run(); 212 } else 213 SwingUtilities.invokeLater(runMe); 214 } 215 216 219 public void clearStatus() { 220 Runnable runMe = new Runnable () { 221 public void run() { 222 if (Pooka.getMainPanel() != null && Pooka.getMainPanel().getInfoPanel() != null) 223 Pooka.getMainPanel().getInfoPanel().clear(); 224 } 225 }; 226 if (SwingUtilities.isEventDispatchThread()) 227 runMe.run(); 228 else 229 SwingUtilities.invokeLater(runMe); 230 } 231 232 235 public void showMessage(String newMessage, String title) { 236 final String displayMessage = newMessage; 238 final String fTitle = title; 239 240 Runnable runMe = new Runnable () { 241 public void run() { 242 Component displayPanel = createMessageComponent(displayMessage, Pooka.getMainPanel()); 243 JOptionPane.showMessageDialog(Pooka.getMainPanel(), displayPanel, fTitle, JOptionPane.PLAIN_MESSAGE); 244 } 245 }; 246 247 if (! SwingUtilities.isEventDispatchThread()) { 248 try { 249 SwingUtilities.invokeAndWait(runMe); 250 } catch (Exception e) { 251 } 252 } else { 253 runMe.run(); 254 } 255 } 256 257 260 public abstract ProgressDialog createProgressDialog(int min, int max, int initialValue, String title, String content); 261 262 266 270 public void showSearchForm(net.suberic.pooka.FolderInfo[] selectedFolders, java.util.Vector allowedValues) { 271 SearchForm sf = null; 272 if (allowedValues != null) 273 sf = new SearchForm(selectedFolders, allowedValues); 274 else 275 sf = new SearchForm(selectedFolders); 276 277 boolean ok = false; 278 int returnValue = -1; 279 java.util.Vector tmpSelectedFolders = null; 280 javax.mail.search.SearchTerm tmpSearchTerm = null; 281 282 while (! ok ) { 283 returnValue = showConfirmDialog(new Object [] { sf }, Pooka.getProperty("title.search", "Search Folders"), JOptionPane.OK_CANCEL_OPTION); 284 if (returnValue == JOptionPane.OK_OPTION) { 285 tmpSelectedFolders = sf.getSelectedFolders(); 286 try { 287 tmpSearchTerm = sf.getSearchTerm(); 288 ok = true; 289 } catch (java.text.ParseException pe) { 290 showError(Pooka.getProperty("error.search.invalidDateFormat", "Invalid date format: "), pe); 291 ok = false; 292 } 293 } else { 294 ok = true; 295 } 296 } 297 298 if (returnValue == JOptionPane.OK_OPTION) { 299 FolderInfo.searchFolders(tmpSelectedFolders, tmpSearchTerm); 300 } 301 } 302 303 304 307 public void showError(String errorMessage, String title) { 308 final String displayErrorMessage = formatMessage(errorMessage); 309 final String fTitle = title; 310 311 if (showing) { 312 SwingUtilities.invokeLater(new Runnable () { 313 public void run() { 314 JOptionPane.showMessageDialog(Pooka.getMainPanel(), displayErrorMessage, fTitle, JOptionPane.ERROR_MESSAGE); 315 } 316 }); 317 } else 318 System.out.println(errorMessage); 319 320 } 321 322 325 public void showError(String errorMessage, String title, Exception e) { 326 final String displayErrorMessage = formatMessage(errorMessage + ": " + e.getMessage()); 327 final Exception fE = e; 328 final String fTitle = title; 329 if (showing) { 330 SwingUtilities.invokeLater(new Runnable () { 331 public void run() { 332 JOptionPane.showMessageDialog(Pooka.getMainPanel(), createErrorPanel(displayErrorMessage, fE), fTitle, JOptionPane.ERROR_MESSAGE); 333 } 334 }); 335 } else 336 System.out.println(errorMessage); 337 338 } 340 341 342 346 public void showSearchForm(net.suberic.pooka.FolderInfo[] selectedFolders) { 347 showSearchForm(selectedFolders, null); 348 } 349 350 354 public void setShowing(boolean newValue) { 355 showing=newValue; 356 } 357 358 361 public MessageNotificationManager getMessageNotificationManager() { 362 return mMessageNotificationManager; 363 } 364 365 366 369 public net.suberic.util.gui.IconManager getIconManager() { 370 return mIconManager; 371 } 372 373 376 public void setIconManager(net.suberic.util.gui.IconManager pIconManager) { 377 mIconManager = pIconManager; 378 } 379 380 383 public String formatMessage(String message) { 384 return net.suberic.pooka.MailUtilities.wrapText(message, mMaxErrorLine, "\r\n", 5); 385 } 386 387 388 391 public Object [] createErrorPanel(String message, Exception e) { 392 Object [] returnValue = new Object [2]; 393 returnValue[0] = message; 394 returnValue[1] = new net.suberic.util.swing.ExceptionDisplayPanel(Pooka.getProperty("error.showStackTrace", "Stack Trace"), e); 395 396 return returnValue; 397 } 398 399 402 public Dimension calculateDisplaySize(Component parentComponent, Component displayComponent) { 403 Dimension parentSize = parentComponent.getSize(); 405 int maxWidth = Math.max(30, (int) (parentSize.width * 0.8)); 407 int maxHeight = Math.max(30, (int) (parentSize.height * 0.8)); 408 409 Dimension displayPrefSize = displayComponent.getPreferredSize(); 410 411 int newWidth = Math.min(maxWidth, displayPrefSize.width); 412 int newHeight = Math.min(maxHeight, displayPrefSize.height); 413 return new Dimension(newWidth +5, newHeight+5); 414 } 415 416 420 public Component createMessageComponent(String message, Component parentComponent) { 421 Component labelComponent = createLabel(message); 422 Dimension displaySize = calculateDisplaySize(parentComponent, labelComponent); 423 JScrollPane jsp = new JScrollPane(labelComponent); 424 JScrollBar jsb = jsp.getVerticalScrollBar(); 426 if (jsb != null) { 427 displaySize = new Dimension(displaySize.width + jsb.getPreferredSize().width, displaySize.height); 428 } else { 429 jsb = new JScrollBar(JScrollBar.VERTICAL); 430 displaySize = new Dimension(displaySize.width + jsb.getPreferredSize().width, displaySize.height); 431 432 } 433 jsp.setPreferredSize(displaySize); 434 return jsp; 435 } 436 437 440 public Component createLabel(String s) { 441 Container c = Box.createVerticalBox(); 442 443 addMessageComponents(c, s, 160); 444 445 return c; 446 } 447 448 private void addMessageComponents(Container c, String s, int maxll) { 449 int nl = -1; 451 int nll = 0; 452 453 if ((nl = s.indexOf("\r\n")) >= 0) { 454 nll = 2; 455 } else if ((nl = s.indexOf('\n')) >= 0) { 456 nll = 1; 457 } 458 459 if (nl >= 0) { 460 if (nl == 0) { 462 JPanel breakPanel = new JPanel() { 463 public Dimension getPreferredSize() { 464 Font f = getFont(); 465 466 if (f != null) { 467 return new Dimension(1, f.getSize() + 2); 468 } 469 return new Dimension(0, 0); 470 } 471 }; 472 breakPanel.setName("OptionPane.break"); 473 c.add(breakPanel); 474 } else { 475 addMessageComponents(c, s.substring(0, nl), maxll); 476 } 477 addMessageComponents(c, s.substring(nl + nll), maxll); 478 479 486 } else { 487 JLabel label; 488 label = new JLabel(s, JLabel.LEADING ); 489 label.setName("OptionPane.label"); 490 c.add(label); 491 } 492 } 493 494 498 private void burstStringInto(Container c, String d, int maxll) { 499 int len = d.length(); 501 if (len <= 0) 502 return; 503 if (len > maxll) { 504 int p = d.lastIndexOf(' ', maxll); 505 if (p <= 0) 506 p = d.indexOf(' ', maxll); 507 if (p > 0 && p < len) { 508 burstStringInto(c, d.substring(0, p), maxll); 509 burstStringInto(c, d.substring(p + 1), maxll); 510 return; 511 } 512 } 513 JLabel label = new JLabel(d, JLabel.LEFT); 514 label.setName("OptionPane.label"); 515 c.add(label); 516 } 517 } 518 519 | Popular Tags |