KickJava   Java API By Example, From Geeks To Geeks.

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


1 package net.suberic.pooka.gui;
2 import net.suberic.pooka.*;
3 import net.suberic.util.gui.*;
4 import net.suberic.util.swing.*;
5 import javax.mail.*;
6 import javax.mail.internet.*;
7 import java.awt.*;
8 import java.awt.event.*;
9 import javax.swing.*;
10 import javax.swing.plaf.basic.*;
11 import javax.swing.text.TextAction JavaDoc;
12 import java.util.*;
13 import javax.swing.text.JTextComponent JavaDoc;
14 import javax.swing.event.*;
15 import java.io.File JavaDoc;
16 import javax.swing.plaf.metal.MetalTheme JavaDoc;
17
18 /**
19  * An InternalFrame which can display messages.
20  *
21  * This class should be used in conjunction with a MessagePanel.
22  */

23 public abstract class MessageInternalFrame extends JInternalFrame implements MessageUI, ThemeSupporter, ThemeListener {
24
25   protected MessagePanel parentContainer;
26
27   protected MessageProxy msg;
28   protected MessageDisplayPanel messageDisplay;
29
30   protected ConfigurableToolbar toolbar;
31   protected ConfigurableKeyBinding keyBindings;
32   protected boolean addedToDesktop = false;
33
34   protected PookaUIFactory uiFactory;
35
36   protected javax.swing.plaf.metal.MetalTheme JavaDoc currentTheme = null;
37
38   /**
39    * Creates a MessageInternalFrame from the given Message.
40    */

41   public MessageInternalFrame(MessagePanel newParentContainer, MessageProxy newMsgProxy) {
42     super(Pooka.getProperty("Pooka.messageInternalFrame.messageTitle.newMessage", "New Message"), true, true, true, true);
43
44     parentContainer = newParentContainer;
45     msg=newMsgProxy;
46
47     this.getContentPane().setLayout(new BorderLayout());
48
49     this.addFocusListener(new FocusAdapter() {
50         public void focusGained(FocusEvent e) {
51           if (getMessageDisplay() != null)
52             getMessageDisplay().requestFocusInWindow();
53         }
54       });
55
56     FocusTraversalPolicy ftp = new LayoutFocusTraversalPolicy() {
57         public Component JavaDoc getInitialComponent(JInternalFrame jif) {
58           if (jif instanceof MessageInternalFrame) {
59             return ((MessageInternalFrame) jif).getMessageDisplay();
60           }
61
62           return super.getInitialComponent(jif);
63         }
64       };
65     this.setFocusTraversalPolicy(ftp);
66
67     if (getUI() instanceof BasicInternalFrameUI) {
68       ((BasicInternalFrameUI) getUI()).getNorthPane().addMouseListener(new MouseAdapter() {
69
70           public void mousePressed(MouseEvent evt) {
71             if (evt.getButton() == MouseEvent.BUTTON2) {
72               try {
73                 Object JavaDoc messagePanel = SwingUtilities.getAncestorOfClass(Class.forName("net.suberic.pooka.gui.MessagePanel"), MessageInternalFrame.this);
74                 if (messagePanel != null) {
75                   ((MessagePanel) messagePanel).unselectAndMoveToBack(MessageInternalFrame.this);
76                   evt.consume();
77                 }
78               } catch (Exception JavaDoc e) {
79                 getLogger().log(java.util.logging.Level.FINE, "exception lowering MessageInternalFrame", e);
80               }
81             }
82           }
83         });
84     }
85   }
86
87   /**
88    * Creates a MessageInternalFrame from the given Message.
89    */

90
91   protected MessageInternalFrame() {
92     super(Pooka.getProperty("Pooka.messageInternalFrame.messageTitle.newMessage", "New Message"), true, true, true, true);
93     this.getContentPane().setLayout(new BorderLayout());
94
95     if (getUI() instanceof BasicInternalFrameUI) {
96       ((BasicInternalFrameUI) getUI()).getNorthPane().addMouseListener(new MouseAdapter() {
97
98           public void mouseClicked(MouseEvent evt) {
99             if (evt.getButton() == MouseEvent.BUTTON2) {
100               try {
101                 Object JavaDoc messagePanel = SwingUtilities.getAncestorOfClass(Class.forName("net.suberic.pooka.gui.MessagePanel"), MessageInternalFrame.this);
102                 if (messagePanel != null) {
103                   ((MessagePanel) messagePanel).unselectAndMoveToBack(MessageInternalFrame.this);
104                   evt.consume();
105                 }
106               } catch (Exception JavaDoc e) {
107                 getLogger().log(java.util.logging.Level.FINE, "exception lowering MessageInternalFrame", e);
108               }
109             }
110           }
111
112         });
113     }
114   }
115
116   /**
117    * this method is expected to do all the implementation-specific
118    * duties.
119    */

120
121   protected abstract void configureMessageInternalFrame() throws MessagingException;
122
123   /**
124    * Configures the InterfaceStyle for this component.
125    */

126   public void configureInterfaceStyle() {
127     Runnable JavaDoc runMe = new Runnable JavaDoc() {
128         public void run() {
129           try {
130             Pooka.getUIFactory().getPookaThemeManager().updateUI(MessageInternalFrame.this, MessageInternalFrame.this);
131             getMessageDisplay().setDefaultFont();
132             getMessageDisplay().sizeToDefault();
133             MessageInternalFrame.this.setSize(MessageInternalFrame.this.getPreferredSize());
134             if (getUI() instanceof BasicInternalFrameUI) {
135               ((BasicInternalFrameUI) getUI()).getNorthPane().addMouseListener(new MouseAdapter() {
136
137                   public void mouseClicked(MouseEvent evt) {
138                     if (evt.getButton() == MouseEvent.BUTTON2) {
139                       try {
140                         Object JavaDoc messagePanel = SwingUtilities.getAncestorOfClass(Class.forName("net.suberic.pooka.gui.MessagePanel"), MessageInternalFrame.this);
141                         if (messagePanel != null) {
142                           ((MessagePanel) messagePanel).unselectAndMoveToBack(MessageInternalFrame.this);
143                           evt.consume();
144                         }
145                       } catch (Exception JavaDoc e) {
146                         getLogger().log(java.util.logging.Level.FINE, "exception lowering MessageInternalFrame", e);
147                       }
148                     }
149                   }
150
151                 });
152             }
153           } catch (Exception JavaDoc e) {
154           }
155         }
156       };
157
158     if (! SwingUtilities.isEventDispatchThread()) {
159       SwingUtilities.invokeLater(runMe);
160     } else {
161       runMe.run();
162     }
163   }
164
165   /**
166    * Gets the currently configured Theme.
167    */

168   public MetalTheme JavaDoc getCurrentTheme() {
169     return currentTheme;
170   }
171
172   /**
173    * Sets the Theme that this component is currently using.
174    */

175   public void setCurrentTheme(MetalTheme JavaDoc newTheme) {
176     if (currentTheme != null && currentTheme instanceof ConfigurableMetalTheme) {
177       ((ConfigurableMetalTheme)currentTheme).removeThemeListener(this);
178     }
179     currentTheme = newTheme;
180
181     if (currentTheme != null && currentTheme instanceof ConfigurableMetalTheme) {
182       ((ConfigurableMetalTheme)currentTheme).addThemeListener(this);
183     }
184   }
185
186   /**
187    * Called when the specifics of a Theme change.
188    */

189   public void themeChanged(ConfigurableMetalTheme theme) {
190     // we should really only be getting messages from our own current themes,
191
// but, hey, it never hurts to check.
192
if (currentTheme != null && currentTheme == theme) {
193       SwingUtilities.invokeLater(new Runnable JavaDoc() {
194           public void run() {
195             try {
196               Pooka.getUIFactory().getPookaThemeManager().updateUI(MessageInternalFrame.this, MessageInternalFrame.this, true);
197               getMessageDisplay().setDefaultFont();
198               getMessageDisplay().sizeToDefault();
199               MessageInternalFrame.this.setSize(MessageInternalFrame.this.getPreferredSize());
200
201             } catch (Exception JavaDoc e) {
202             }
203
204           }
205         });
206     }
207   }
208
209   /**
210    * This opens the MessageInternalFrame by calling
211    * getParentContainer().openMessageInternalFrame(getMessageProxy());
212    */

213   public void openMessageUI() {
214     getParentContainer().openMessageWindow(getMessageProxy(), !addedToDesktop);
215     addedToDesktop = true;
216   }
217
218   /**
219    * This closes the MessageInternalFrame.
220    */

221   public void closeMessageUI() {
222     try {
223       this.setClosed(true);
224     } catch (java.beans.PropertyVetoException JavaDoc e) {
225     }
226   }
227
228   /**
229    * This detaches this window from the MessagePanel, instead making it
230    * a top-level MessageFrame.
231    */

232   public abstract void detachWindow();
233
234
235   /**
236    * This shows an Confirm Dialog window. We include this so that
237    * the MessageProxy can call the method without caring about the
238    * actual implementation of the Dialog.
239    */

240   public int showConfirmDialog(String JavaDoc pMessageText, String JavaDoc pTitle, int pType) {
241     final String JavaDoc messageText = pMessageText;
242     final String JavaDoc title = pTitle;
243     final int type = pType;
244     final ResponseWrapper fResponseWrapper = new ResponseWrapper();
245
246     Runnable JavaDoc runMe = new Runnable JavaDoc() {
247         public void run() {
248           fResponseWrapper.setInt(JOptionPane.showInternalConfirmDialog((JDesktopPane)Pooka.getMainPanel().getContentPanel(), messageText, title, type));
249         }
250       };
251
252     if (! SwingUtilities.isEventDispatchThread()) {
253       try {
254         SwingUtilities.invokeAndWait(runMe);
255       } catch (Exception JavaDoc e) {
256       }
257     } else {
258       runMe.run();
259     }
260
261     return fResponseWrapper.getInt();
262   }
263
264   /**
265    * This shows an Confirm Dialog window. We include this so that
266    * the MessageProxy can call the method without caring about the
267    * actual implementation of the Dialog.
268    */

269   public int showConfirmDialog(String JavaDoc pMessageText, String JavaDoc pTitle, int pOptionType, int pIconType) {
270     final String JavaDoc messageText = pMessageText;
271     final String JavaDoc title = pTitle;
272     final int optionType = pOptionType;
273     final int iconType = pIconType;
274     final ResponseWrapper fResponseWrapper = new ResponseWrapper();
275
276     Runnable JavaDoc runMe = new Runnable JavaDoc() {
277         public void run() {
278           fResponseWrapper.setInt(JOptionPane.showInternalConfirmDialog((JDesktopPane)Pooka.getMainPanel().getContentPanel(), messageText, title, optionType));
279         }
280       };
281
282     if (! SwingUtilities.isEventDispatchThread()) {
283       try {
284         SwingUtilities.invokeAndWait(runMe);
285       } catch (Exception JavaDoc e) {
286       }
287     } else {
288       runMe.run();
289     }
290
291     return fResponseWrapper.getInt();
292   }
293
294   /**
295    * This shows an Error Message window. We include this so that
296    * the MessageProxy can call the method without caring about the
297    * actual implementation of the Dialog.
298    */

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

308   public void showError(String JavaDoc errorMessage) {
309     Pooka.getUIFactory().showError(errorMessage);
310   }
311
312   /**
313    * This shows an Error Message window. We include this so that
314    * the MessageProxy can call the method without caring about the
315    * actual implementation of the Dialog.
316    */

317   public void showError(String JavaDoc errorMessage, Exception JavaDoc e) {
318     Pooka.getUIFactory().showError(errorMessage, e);
319   }
320
321   /**
322    * This shows an Error Message window. We include this so that
323    * the MessageProxy can call the method without caring about the
324    * actual implementation of the Dialog.
325    */

326   public void showError(String JavaDoc errorMessage, String JavaDoc title, Exception JavaDoc e) {
327     Pooka.getUIFactory().showError(errorMessage, title, e);
328   }
329
330   /**
331    * This formats a display message.
332    */

333   public String JavaDoc formatMessage(String JavaDoc message) {
334     return Pooka.getUIFactory().formatMessage(message);
335   }
336
337   /**
338    * This shows a Message window. We include this so that
339    * the MessageProxy can call the method without caring about the
340    * actual implementation of the Dialog.
341    */

342   public void showMessageDialog(String JavaDoc pMessage, String JavaDoc pTitle) {
343     final String JavaDoc message = pMessage;
344     final String JavaDoc title = pTitle;
345
346     Runnable JavaDoc runMe = new Runnable JavaDoc() {
347         public void run() {
348           JOptionPane.showInternalMessageDialog((JDesktopPane)Pooka.getMainPanel().getContentPanel(), message, title, JOptionPane.PLAIN_MESSAGE);
349         }
350       };
351
352     if (! SwingUtilities.isEventDispatchThread()) {
353       try {
354         SwingUtilities.invokeAndWait(runMe);
355       } catch (Exception JavaDoc e) {
356       }
357     } else {
358       runMe.run();
359     }
360   }
361
362   /**
363    * This shows an Input window. We include this so that the
364    * MessageProxy can call the method without caring about the actual
365    * implementation of the dialog.
366    */

367   public String JavaDoc showInputDialog(String JavaDoc inputMessage, String JavaDoc title) {
368     return Pooka.getUIFactory().showInputDialog(inputMessage, title);
369   }
370
371   /**
372    * This shows an Input window. We include this so that the
373    * MessageProxy can call the method without caring about the actual
374    * implementation of the dialog.
375    */

376   public String JavaDoc showInputDialog(Object JavaDoc[] pInputPanes, String JavaDoc pTitle) {
377     final Object JavaDoc[] inputPanes = pInputPanes;
378     final String JavaDoc title = pTitle;
379     final ResponseWrapper fResponseWrapper = new ResponseWrapper();
380
381     Runnable JavaDoc runMe = new Runnable JavaDoc() {
382         public void run() {
383           fResponseWrapper.setString(JOptionPane.showInternalInputDialog((MessagePanel)Pooka.getMainPanel().getContentPanel(), inputPanes, title, JOptionPane.QUESTION_MESSAGE));
384
385         }
386       };
387
388     if (! SwingUtilities.isEventDispatchThread()) {
389       try {
390         SwingUtilities.invokeAndWait(runMe);
391       } catch (Exception JavaDoc e) {
392       }
393     } else {
394       runMe.run();
395     }
396
397     return fResponseWrapper.getString();
398   }
399
400   /**
401    * A convenience method to set the PreferredSize and Size of the
402    * component to that of the current preferred width.
403    */

404   public void resizeByWidth() {
405     /*
406       int width = (int)messageDisplay.getPreferredSize().getWidth();
407       this.setPreferredSize(new Dimension(width, width));
408     */

409     this.setSize(this.getPreferredSize());
410   }
411
412   /**
413    * Creates a ProgressDialog using the given values.
414    */

415   public ProgressDialog createProgressDialog(int min, int max, int initialValue, String JavaDoc title, String JavaDoc content) {
416     return new ProgressInternalDialog(min, max, initialValue, title, content, getParentContainer());
417   }
418
419   /**
420    * Shows the current display of the encryption status.
421    */

422   public net.suberic.pooka.gui.crypto.CryptoStatusDisplay getCryptoStatusDisplay() {
423     return getMessageDisplay().getCryptoStatusDisplay();
424   }
425
426
427
428   /**
429    * As specified by interface net.suberic.pooka.gui.MessageUI.
430    *
431    * This implementation sets the cursor to either Cursor.WAIT_CURSOR
432    * if busy, or Cursor.DEFAULT_CURSOR if not busy.
433    */

434   public void setBusy(boolean newValue) {
435     final boolean fNewValue = newValue;
436     Runnable JavaDoc runMe = new Runnable JavaDoc() {
437         public void run() {
438           if (fNewValue)
439             setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));
440           else
441             setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR));
442         }
443       };
444
445     if (SwingUtilities.isEventDispatchThread()) {
446       runMe.run();
447     } else {
448       SwingUtilities.invokeLater(runMe);
449     }
450   }
451
452   /**
453    * Refreshes the display.
454    */

455   public void refreshDisplay() throws MessagingException {
456     if (messageDisplay instanceof ReadMessageDisplayPanel)
457       ((ReadMessageDisplayPanel)messageDisplay).resetEditorText();
458   }
459
460
461   /**
462    * As specified by interface net.suberic.pooka.UserProfileContainer.
463    *
464    * This implementation returns the DefaultProfile of the associated
465    * MessageProxy if the MessageInternalFrame is not editable. If the
466    * MessageInternalFrame is editable, it returns the currently selected
467    * UserProfile object.
468    */

469
470   public UserProfile getDefaultProfile() {
471     return getMessageProxy().getDefaultProfile();
472   }
473
474   public MessageDisplayPanel getMessageDisplay() {
475     return messageDisplay;
476   }
477
478   public MessageProxy getMessageProxy() {
479     return msg;
480   }
481
482   public void setMessageProxy(MessageProxy newValue) {
483     msg = newValue;
484   }
485
486   public String JavaDoc getMessageText() {
487     return getMessageDisplay().getMessageText();
488   }
489
490   public String JavaDoc getMessageContentType() {
491     return getMessageDisplay().getMessageContentType();
492   }
493
494   public AttachmentPane getAttachmentPanel() {
495     return getMessageDisplay().getAttachmentPanel();
496   }
497
498   public MessagePanel getParentContainer() {
499     return parentContainer;
500   }
501
502   public ConfigurableToolbar getToolbar() {
503     return toolbar;
504   }
505
506   public ConfigurableKeyBinding getKeyBindings() {
507     return keyBindings;
508   }
509
510   public java.util.logging.Logger JavaDoc getLogger() {
511     return java.util.logging.Logger.getLogger("Pooka.debug.gui");
512   }
513
514   //------- Actions ----------//
515

516   public Action JavaDoc[] getActions() {
517     return defaultActions;
518   }
519
520   public Action JavaDoc[] getDefaultActions() {
521     return defaultActions;
522   }
523
524   //-----------actions----------------
525

526   // The actions supported by the window itself.
527

528   public Action JavaDoc[] defaultActions = {
529     new CloseAction(),
530     new DetachAction()
531   };
532
533   class CloseAction extends AbstractAction {
534
535     CloseAction() {
536       super("file-close");
537     }
538
539     public void actionPerformed(ActionEvent e) {
540       closeMessageUI();
541     }
542   }
543
544   class DetachAction extends AbstractAction {
545     DetachAction() {
546       super("window-detach");
547     }
548
549     public void actionPerformed(ActionEvent e) {
550       detachWindow();
551     }
552   }
553 }
554
555
556
557
558
559
Popular Tags