KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > suberic > pooka > gui > SwingUIFactory


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 /**
12  * An abstract base class for PookaUIFactories.
13  */

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   /**
26    * Returns the PookaThemeManager for fonts and colors.
27    */

28   public ThemeManager getPookaThemeManager() {
29     return pookaThemeManager;
30   }
31
32   /**
33    * Creates an appropriate MessageUI object for the given MessageProxy.
34    */

35   public MessageUI createMessageUI(MessageProxy mp) throws javax.mail.MessagingException JavaDoc {
36     return createMessageUI(mp, null);
37   }
38
39   /**
40    * Creates an appropriate MessageUI object for the given MessageProxy,
41    * using the provided MessageUI as a guideline.
42    */

43   public abstract MessageUI createMessageUI(MessageProxy mp, MessageUI mui) throws javax.mail.MessagingException JavaDoc;
44
45   /**
46    * Opens the given MessageProxy in the default manner for this UI.
47    * Usually this will just be callen createMessageUI() and openMessageUI()
48    * on it. However, in some cases (Preview Panel without auto display)
49    * it may be necessary to act differently.
50    */

51   public abstract void doDefaultOpen(MessageProxy mp);
52
53   /**
54    * Creates an appropriate FolderDisplayUI object for the given
55    * FolderInfo.
56    */

57   public abstract FolderDisplayUI createFolderDisplayUI(net.suberic.pooka.FolderInfo fi);
58
59   /**
60    * Creates a ContentPanel which will be used to show messages and folders.
61    */

62   public abstract ContentPanel createContentPanel();
63
64   /**
65    * Shows an Editor Window with the given title, which allows the user
66    * to edit the given property.
67    */

68   public void showEditorWindow(String JavaDoc title, String JavaDoc property) {
69     showEditorWindow(title, property, property);
70   }
71
72   /**
73    * Shows an Editor Window with the given title, which allows the user
74    * to edit the given property, which is in turn defined by the
75    * given template.
76    */

77   public abstract void showEditorWindow(String JavaDoc title, String JavaDoc property, String JavaDoc template);
78
79   /**
80    * Returns the PropertyEditorFactory used by this component.
81    */

82   public net.suberic.util.gui.propedit.PropertyEditorFactory getEditorFactory() {
83     return editorFactory;
84   }
85
86   /**
87    * Sets the PropertyEditorFactory used by this component.
88    */

89   public void setEditorFactory(net.suberic.util.gui.propedit.PropertyEditorFactory pEditorFactory) {
90     editorFactory = pEditorFactory;
91   }
92
93
94   /**
95    * Shows a Confirm dialog.
96    */

97   public int showConfirmDialog(String JavaDoc message, String JavaDoc title, int type) {
98     String JavaDoc displayMessage = formatMessage(message);
99     final ResponseWrapper fResponseWrapper = new ResponseWrapper();
100     final String JavaDoc fDisplayMessage = displayMessage;
101     final String JavaDoc fTitle = title;
102     final int fType = type;
103     Runnable JavaDoc runMe = new Runnable JavaDoc() {
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 JavaDoc e) {
113       }
114     } else {
115       runMe.run();
116     }
117
118     return fResponseWrapper.getInt();
119   }
120
121   /**
122    * Shows a Confirm dialog with the given Object[] as the Message.
123    */

124   public int showConfirmDialog(Object JavaDoc[] messageComponents, String JavaDoc title, int type) {
125     final ResponseWrapper fResponseWrapper = new ResponseWrapper();
126     final Object JavaDoc[] fMessageComponents = messageComponents;
127     final String JavaDoc fTitle = title;
128     final int fType = type;
129     Runnable JavaDoc runMe = new Runnable JavaDoc() {
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 JavaDoc e) {
139       }
140     } else {
141       runMe.run();
142     }
143
144     return fResponseWrapper.getInt();
145   }
146
147   /**
148    * This shows an Input window.
149    */

150   public String JavaDoc showInputDialog(String JavaDoc inputMessage, String JavaDoc title) {
151     final String JavaDoc displayMessage = formatMessage(inputMessage);
152     final String JavaDoc fTitle = title;
153     final ResponseWrapper fResponseWrapper = new ResponseWrapper();
154
155     Runnable JavaDoc runMe = new Runnable JavaDoc() {
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 JavaDoc e) {
165       }
166     } else {
167       runMe.run();
168     }
169
170     return fResponseWrapper.getString();
171   }
172
173   /**
174    * Shows an Input window.
175    */

176   public String JavaDoc showInputDialog(Object JavaDoc[] inputPanels, String JavaDoc title) {
177     final String JavaDoc fTitle = title;
178     final Object JavaDoc[] fInputPanes = inputPanels;
179     final ResponseWrapper fResponseWrapper = new ResponseWrapper();
180
181     Runnable JavaDoc runMe = new Runnable JavaDoc() {
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 JavaDoc e) {
191       }
192     } else {
193       runMe.run();
194     }
195
196     return fResponseWrapper.getString();
197   }
198
199   /**
200    * Shows a status message.
201    */

202   public void showStatusMessage(String JavaDoc newMessage) {
203     final String JavaDoc msg = newMessage;
204     Runnable JavaDoc runMe = new Runnable JavaDoc() {
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   /**
217    * Clears the main status message panel.
218    */

219   public void clearStatus() {
220     Runnable JavaDoc runMe = new Runnable JavaDoc() {
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   /**
233    * Shows a message.
234    */

235   public void showMessage(String JavaDoc newMessage, String JavaDoc title) {
236     //final String displayMessage = formatMessage(newMessage);
237
final String JavaDoc displayMessage = newMessage;
238     final String JavaDoc fTitle = title;
239
240     Runnable JavaDoc runMe = new Runnable JavaDoc() {
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 JavaDoc e) {
251       }
252     } else {
253       runMe.run();
254     }
255   }
256
257   /**
258    * Creates a ProgressDialog using the given values.
259    */

260   public abstract ProgressDialog createProgressDialog(int min, int max, int initialValue, String JavaDoc title, String JavaDoc content);
261
262   /**
263    * Shows a SearchForm with the given FolderInfos selected from the list
264    * of the given allowedValues.
265    */

266   /**
267    * Shows a SearchForm with the given FolderInfos selected from the list
268    * of the given allowedValues.
269    */

270   public void showSearchForm(net.suberic.pooka.FolderInfo[] selectedFolders, java.util.Vector JavaDoc 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 JavaDoc tmpSelectedFolders = null;
280     javax.mail.search.SearchTerm JavaDoc tmpSearchTerm = null;
281
282     while (! ok ) {
283       returnValue = showConfirmDialog(new Object JavaDoc[] { 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 JavaDoc 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   /**
305    * This shows an Error Message window.
306    */

307   public void showError(String JavaDoc errorMessage, String JavaDoc title) {
308     final String JavaDoc displayErrorMessage = formatMessage(errorMessage);
309     final String JavaDoc fTitle = title;
310
311     if (showing) {
312       SwingUtilities.invokeLater(new Runnable JavaDoc() {
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   /**
323    * This shows an Error Message window.
324    */

325   public void showError(String JavaDoc errorMessage, String JavaDoc title, Exception JavaDoc e) {
326     final String JavaDoc displayErrorMessage = formatMessage(errorMessage + ": " + e.getMessage());
327     final Exception JavaDoc fE = e;
328     final String JavaDoc fTitle = title;
329     if (showing) {
330       SwingUtilities.invokeLater(new Runnable JavaDoc() {
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     //e.printStackTrace();
339
}
340
341
342   /**
343    * Shows a SearchForm with the given FolderInfos selected. The allowed
344    * values will be the list of all available Folders.
345    */

346   public void showSearchForm(net.suberic.pooka.FolderInfo[] selectedFolders) {
347     showSearchForm(selectedFolders, null);
348   }
349
350   /**
351    * This tells the factory whether or not its ui components are showing
352    * yet or not.
353    */

354   public void setShowing(boolean newValue) {
355     showing=newValue;
356   }
357
358   /**
359    * Gets the current MessageNotificationManager.
360    */

361   public MessageNotificationManager getMessageNotificationManager() {
362     return mMessageNotificationManager;
363   }
364
365
366   /**
367    * Gets the IconManager for this UI.
368    */

369   public net.suberic.util.gui.IconManager getIconManager() {
370     return mIconManager;
371   }
372
373   /**
374    * Sets the IconManager for this UI.
375    */

376   public void setIconManager(net.suberic.util.gui.IconManager pIconManager) {
377     mIconManager = pIconManager;
378   }
379
380   /**
381    * This formats a display message.
382    */

383   public String JavaDoc formatMessage(String JavaDoc message) {
384     return net.suberic.pooka.MailUtilities.wrapText(message, mMaxErrorLine, "\r\n", 5);
385   }
386
387
388   /**
389    * Creates the panels for showing an error message.
390    */

391   public Object JavaDoc[] createErrorPanel(String JavaDoc message, Exception JavaDoc e) {
392     Object JavaDoc[] returnValue = new Object JavaDoc[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   /**
400    * Calculates a Dimension which defines a reasonably sized dialog window.
401    */

402   public Dimension calculateDisplaySize(Component parentComponent, Component displayComponent) {
403     //Point parentLocation = parentComponent.getLocationOnScreen();
404
Dimension parentSize = parentComponent.getSize();
405     // width and height should be mo more than 80%
406
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   /**
417    * Returns either a properly-sized JLabel, or a JLabel inside of a
418    * properly-sized JScrollPane.
419    */

420   public Component createMessageComponent(String JavaDoc message, Component parentComponent) {
421     Component labelComponent = createLabel(message);
422     Dimension displaySize = calculateDisplaySize(parentComponent, labelComponent);
423     JScrollPane jsp = new JScrollPane(labelComponent);
424     // add on space for the scrollbar.
425
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   /**
438    * Breaks up a String into proper JLabels.
439    */

440   public Component createLabel(String JavaDoc 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 JavaDoc s, int maxll) {
449     // taken from BasicOptionPaneUI
450
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       // break up newlines
461
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       /*
480     } else if (len > maxll) {
481       Container c = Box.createVerticalBox();
482       c.setName("OptionPane.verticalBox");
483       burstStringInto(c, s, maxll);
484       addMessageComponents(container, cons, c, maxll, true );
485       */

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   /**
495    * Recursively creates new JLabel instances to represent <code>d</code>.
496    * Each JLabel instance is added to <code>c</code>.
497    */

498   private void burstStringInto(Container c, String JavaDoc d, int maxll) {
499     // Primitive line wrapping
500
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