KickJava   Java API By Example, From Geeks To Geeks.

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


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 JavaDoc;
11 import java.awt.event.WindowAdapter JavaDoc;
12 import java.util.*;
13
14
15 /**
16  * This is an implementation of PookaUIFactory which is used when Pooka
17  * is started up just to send an email.
18  */

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 JavaDoc mNewMessages = new LinkedList();
31
32   StatusDisplay mStatusPanel = null;
33
34   WindowAdapter JavaDoc mWindowAdapter = null;
35
36
37   /**
38    * Constructor.
39    */

40   public PookaMinimalUIFactory(PookaUIFactory pSource) {
41     mWindowAdapter = new WindowAdapter JavaDoc() {
42         public void windowClosed(WindowEvent JavaDoc 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   /**
66    * Constructor.
67    */

68   public PookaMinimalUIFactory() {
69     this(null);
70   }
71
72   /**
73    * Returns the ThemeManager for fonts and colors.
74    */

75   public ThemeManager getPookaThemeManager() {
76     return mThemeManager;
77   }
78
79   /**
80    * Creates an appropriate MessageUI object for the given MessageProxy.
81    */

82   public MessageUI createMessageUI(MessageProxy mp) {
83     return createMessageUI(mp, null);
84   }
85
86   /**
87    * Creates an appropriate MessageUI object for the given MessageProxy,
88    * using the provided MessageUI as a guideline.
89    *
90    * Note that this implementation ignores the templateMui component.
91    */

92   public MessageUI createMessageUI(MessageProxy mp, MessageUI templateMui) {
93     // each MessageProxy can have exactly one MessageUI.
94
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     //applyNewWindowLocation((JFrame)mui);
109
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   /**
118    * Unregisters listeners for this Factory. Should be called if this
119    * ceases to be the active UIFactory.
120    */

121   public void unregisterListeners() {
122     Iterator iter = mNewMessages.iterator();
123     while (iter.hasNext()) {
124       java.awt.Window JavaDoc current = (java.awt.Window JavaDoc) iter.next();
125       current.removeWindowListener(mWindowAdapter);
126     }
127   }
128
129   /**
130    * Opens the given MessageProxy in the default manner for this UI.
131    * Usually this will just be callen createMessageUI() and openMessageUI()
132    * on it. However, in some cases (Preview Panel without auto display)
133    * it may be necessary to act differently.
134    *
135    */

136   public void doDefaultOpen(MessageProxy mp) {
137   }
138
139   /**
140    * Creates an appropriate FolderDisplayUI object for the given
141    * FolderInfo.
142    *
143    * Returns null; this implementation doesn't allow for folder views.
144    */

145   public FolderDisplayUI createFolderDisplayUI(net.suberic.pooka.FolderInfo fi) {
146     return null;
147   }
148
149   /**
150    * Creates a JPanel which will be used to show messages and folders.
151    *
152    * This implementation returns null.
153    */

154   public ContentPanel createContentPanel() {
155     return null;
156   }
157
158   /**
159    * Creates a Toolbar for the MainPanel.
160    * This implementation returns null.
161    */

162   public ConfigurableToolbar createMainToolbar() {
163     return null;
164   }
165
166
167   /**
168    * Creates a Toolbar for the FolderPanel.
169    * This implementation returns null.
170    */

171   public ConfigurableToolbar createFolderPanelToolbar() {
172     return null;
173   }
174
175
176   /**
177    * Shows an Editor Window with the given title, which allows the user
178    * to edit the values in the properties Vector. The given properties
179    * will be shown according to the values in the templates Vector.
180    * Note that there should be an entry in the templates Vector for
181    * each entry in the properties Vector.
182    */

183   public void showEditorWindow(String JavaDoc title, String JavaDoc property, String JavaDoc 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   /**
196    * Shows an Editor Window with the given title, which allows the user
197    * to edit the given property.
198    */

199   public void showEditorWindow(String JavaDoc title, String JavaDoc property) {
200     showEditorWindow(title, property, property);
201   }
202
203   /**
204    * This formats a display message.
205    */

206   public String JavaDoc formatMessage(String JavaDoc message) {
207     return net.suberic.pooka.MailUtilities.wrapText(message, mMaxErrorLine, "\r\n", 5);
208   }
209
210   /**
211    * This shows an Confirm Dialog window. We include this so that
212    * the MessageProxy can call the method without caring abou the
213    * actual implementation of the Dialog.
214    */

215   public int showConfirmDialog(String JavaDoc messageText, String JavaDoc title, int type) {
216     String JavaDoc displayMessage = formatMessage(messageText);
217     final ResponseWrapper fResponseWrapper = new ResponseWrapper();
218     final String JavaDoc fDisplayMessage = displayMessage;
219     final String JavaDoc fTitle = title;
220     final int fType = type;
221     Runnable JavaDoc runMe = new Runnable JavaDoc() {
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 JavaDoc e) {
231       }
232     } else {
233       runMe.run();
234     }
235
236     return fResponseWrapper.getInt();
237   }
238
239
240   /**
241    * Shows a Confirm dialog with the given Object[] as the Message.
242    */

243   public int showConfirmDialog(Object JavaDoc[] messageComponents, String JavaDoc title, int type) {
244     final ResponseWrapper fResponseWrapper = new ResponseWrapper();
245     final Object JavaDoc[] fMessageComponents = messageComponents;
246     final String JavaDoc fTitle = title;
247     final int fType = type;
248     Runnable JavaDoc runMe = new Runnable JavaDoc() {
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 JavaDoc e) {
258       }
259     } else {
260       runMe.run();
261     }
262
263     return fResponseWrapper.getInt();
264   }
265
266   /**
267    * This shows an Error Message window. We include this so that
268    * the MessageProxy can call the method without caring abou the
269    * actual implementation of the Dialog.
270    */

271   public void showError(String JavaDoc errorMessage, String JavaDoc title) {
272     final String JavaDoc displayErrorMessage = formatMessage(errorMessage);
273     final String JavaDoc fTitle = title;
274
275     if (mShowing) {
276       SwingUtilities.invokeLater(new Runnable JavaDoc() {
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   /**
288    * This shows an Error Message window. We include this so that
289    * the MessageProxy can call the method without caring abou the
290    * actual implementation of the Dialog.
291    */

292   public void showError(String JavaDoc errorMessage) {
293     showError(errorMessage, Pooka.getProperty("Error", "Error"));
294   }
295
296   /**
297    * This shows an Error Message window. We include this so that
298    * the MessageProxy can call the method without caring abou the
299    * actual implementation of the Dialog.
300    */

301   public void showError(String JavaDoc errorMessage, Exception JavaDoc e) {
302     showError(errorMessage, Pooka.getProperty("Error", "Error"), e);
303   }
304
305   /**
306    * This shows an Error Message window. We include this so that
307    * the MessageProxy can call the method without caring about the
308    * actual implementation of the Dialog.
309    */

310   public void showError(String JavaDoc errorMessage, String JavaDoc title, Exception JavaDoc e) {
311     final String JavaDoc displayErrorMessage = formatMessage(errorMessage + ": " + e.getMessage());
312     final Exception JavaDoc fE = e;
313     final String JavaDoc fTitle = title;
314     if (mShowing) {
315       SwingUtilities.invokeLater(new Runnable JavaDoc() {
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     //e.printStackTrace();
324
}
325
326   /**
327    * This shows an Input window. We include this so that the
328    * MessageProxy can call the method without caring about the actual
329    * implementation of the dialog.
330    */

331   public String JavaDoc showInputDialog(String JavaDoc inputMessage, String JavaDoc title) {
332     final String JavaDoc displayMessage = formatMessage(inputMessage);
333     final String JavaDoc fTitle = title;
334     final ResponseWrapper fResponseWrapper = new ResponseWrapper();
335
336     Runnable JavaDoc runMe = new Runnable JavaDoc() {
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 JavaDoc e) {
346       }
347     } else {
348       runMe.run();
349     }
350
351     return fResponseWrapper.getString();
352   }
353
354   /**
355    * This shows an Input window. We include this so that the
356    * MessageProxy can call the method without caring about the actual
357    * implementation of the dialog.
358    */

359   public String JavaDoc showInputDialog(Object JavaDoc[] inputPanes, String JavaDoc title) {
360     final String JavaDoc fTitle = title;
361     final Object JavaDoc[] fInputPanes = inputPanes;
362     final ResponseWrapper fResponseWrapper = new ResponseWrapper();
363
364     Runnable JavaDoc runMe = new Runnable JavaDoc() {
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 JavaDoc e) {
374       }
375     } else {
376       runMe.run();
377     }
378
379     return fResponseWrapper.getString();
380
381   }
382
383   /**
384    * Returns the PropertyEditorFactory used by this component.
385    */

386   public PropertyEditorFactory getEditorFactory() {
387     return mEditorFactory;
388   }
389
390   /**
391    * Sets the PropertyEditorFactory used by this component.
392    */

393   public void setEditorFactory(net.suberic.util.gui.propedit.PropertyEditorFactory pEditorFactory) {
394     mEditorFactory = pEditorFactory;
395   }
396
397   /**
398    * Shows a message.
399    */

400   public void showMessage(String JavaDoc newMessage, String JavaDoc title) {
401     final String JavaDoc displayMessage = formatMessage(newMessage);
402     final String JavaDoc fTitle = title;
403     Runnable JavaDoc runMe = new Runnable JavaDoc() {
404         public void run() {
405           JTextArea displayPanel = new JTextArea(displayMessage);
406           displayPanel.setEditable(false);
407           java.awt.Dimension JavaDoc 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 JavaDoc(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 JavaDoc e) {
419       }
420     } else {
421       runMe.run();
422     }
423
424   }
425
426   /**
427    * Shows a status message.
428    */

429   public void showStatusMessage(String JavaDoc newMessage) {
430     final String JavaDoc msg = newMessage;
431
432     Runnable JavaDoc runMe = new Runnable JavaDoc() {
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.setStatusMessage(msg);
442
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   /**
458    * Creates a ProgressDialog using the given values.
459    */

460   public ProgressDialog createProgressDialog(int min, int max, int initialValue, String JavaDoc title, String JavaDoc content) {
461     return new ProgressDialogImpl(min, max, initialValue, title, content);
462   }
463
464   /**
465    * Clears the main status message panel.
466    */

467   public void clearStatus() {
468     Runnable JavaDoc runMe = new Runnable JavaDoc() {
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   /**
484    * Shows a SearchForm with the given FolderInfos selected from the list
485    * of the given allowedValues.
486    *
487    * Not implemented.
488    */

489   public void showSearchForm(net.suberic.pooka.FolderInfo[] selectedFolders, java.util.Vector JavaDoc allowedValues) {
490   }
491
492   /**
493    * Shows a SearchForm with the given FolderInfos selected. The allowed
494    * values will be the list of all available Folders.
495    *
496    * Not implemented.
497    */

498   public void showSearchForm(net.suberic.pooka.FolderInfo[] selectedFolders) {
499     showSearchForm(selectedFolders, null);
500   }
501
502   /**
503    * Shows an Address Selection form for the given AddressEntryTextArea.
504    */

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   /**
514    * This tells the factory whether or not its ui components are showing
515    * yet or not.
516    */

517   public void setShowing(boolean newValue) {
518     mShowing=newValue;
519   }
520
521   /**
522    * Determines the location for new windows.
523    */

524   public void applyNewWindowLocation(JFrame f, JComponent pParentComponent) {
525     f.setLocationRelativeTo(pParentComponent);
526   }
527
528   /**
529    * Creates the panels for showing an error message.
530    */

531   public Object JavaDoc[] createErrorPanel(String JavaDoc message, Exception JavaDoc e) {
532     Object JavaDoc[] returnValue = new Object JavaDoc[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   /**
540    * Finds the selected JFrame, if there is one.
541    */

542   JFrame findJFrame() {
543     Window current = KeyboardFocusManager.getCurrentKeyboardFocusManager().getFocusedWindow();
544     if (current != null && current instanceof JFrame) {
545       return (JFrame) current;
546     }
547
548     // if we haven't gotten one yet...
549
if (mNewMessages.size() > 0) {
550       Object JavaDoc first = mNewMessages.get(0);
551       if (first instanceof JFrame)
552         return (JFrame) first;
553     }
554
555     return null;
556   }
557
558   /**
559    * A window which shows status messages.
560    */

561   class StatusDisplay extends JDialog {
562
563     // the display area.
564
JLabel mDisplayLabel = null;
565
566     /**
567      * Creates a new StatusDisplay object.
568      */

569     public StatusDisplay(JFrame pParentFrame, String JavaDoc 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     /**
582      * Shows a status message.
583      */

584     public void setStatusMessage(String JavaDoc pMessage) {
585       final String JavaDoc msg = pMessage;
586
587       Runnable JavaDoc runMe = new Runnable JavaDoc() {
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     /**
602      * Clears the status panel.
603      */

604     public void clear() {
605       Runnable JavaDoc runMe = new Runnable JavaDoc() {
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   /**
619    * Gets the current MessageNotificationManager.
620    */

621   public MessageNotificationManager getMessageNotificationManager() {
622     return mMessageNotificationManager;
623   }
624
625   /**
626    * Gets the IconManager for this UI.
627    */

628   public net.suberic.util.gui.IconManager getIconManager() {
629     return mIconManager;
630   }
631
632   /**
633    * Sets the IconManager for this UI.
634    */

635   public void setIconManager(net.suberic.util.gui.IconManager pIconManager) {
636     mIconManager = pIconManager;
637   }
638
639
640 }
641
Popular Tags