KickJava   Java API By Example, From Geeks To Geeks.

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


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.text.TextAction JavaDoc;
11 import java.util.*;
12 import javax.swing.text.JTextComponent JavaDoc;
13 import javax.swing.event.*;
14 import java.io.File JavaDoc;
15 import javax.swing.plaf.metal.MetalTheme JavaDoc;
16
17
18 /**
19  * A top-level window for displaying a message.
20  */

21 public abstract class MessageFrame extends JFrame implements MessageUI, ThemeSupporter, ThemeListener {
22
23   protected MessageProxy msg;
24   protected MessageDisplayPanel messageDisplay;
25
26   protected ConfigurableToolbar toolbar;
27   protected ConfigurableKeyBinding keyBindings;
28   protected ConfigurableMenuBar menuBar;
29
30   protected javax.swing.plaf.metal.MetalTheme JavaDoc currentTheme = null;
31
32   /**
33    * Creates a MessageFrame from the given Message.
34    */

35
36   public MessageFrame(MessageProxy newMsgProxy) {
37     super(Pooka.getProperty("Pooka.messageInternalFrame.messageTitle.newMessage", "New Message"));
38
39     msg=newMsgProxy;
40
41     this.getContentPane().setLayout(new BorderLayout());
42
43     java.net.URL JavaDoc standardUrl = this.getClass().getResource(Pooka.getProperty("Pooka.standardIcon", "images/PookaIcon.gif"));
44     if (standardUrl != null) {
45       ImageIcon standardIcon = new ImageIcon(standardUrl);
46       setIconImage(standardIcon.getImage());
47     }
48
49     this.addFocusListener(new FocusAdapter() {
50         public void focusGained(FocusEvent e) {
51           if (getMessageDisplay() != null)
52             getMessageDisplay().requestFocusInWindow();
53         }
54       });
55
56
57   }
58
59   protected MessageFrame() {
60     this.getContentPane().setLayout(new BorderLayout());
61
62     java.net.URL JavaDoc standardUrl = this.getClass().getResource(Pooka.getProperty("Pooka.standardIcon", "images/PookaIcon.gif"));
63     if (standardUrl != null) {
64       ImageIcon standardIcon = new ImageIcon(standardUrl);
65       setIconImage(standardIcon.getImage());
66     }
67
68     this.addFocusListener(new FocusAdapter() {
69         public void focusGained(FocusEvent e) {
70           if (getMessageDisplay() != null)
71             getMessageDisplay().requestFocusInWindow();
72         }
73       });
74
75   }
76
77   /**
78    * this method is expected to do all the implementation-specific
79    * duties.
80    */

81
82   protected abstract void configureMessageFrame();
83
84   /**
85    * Configures the InterfaceStyle for this component.
86    */

87   public void configureInterfaceStyle() {
88     Runnable JavaDoc runMe = new Runnable JavaDoc() {
89         public void run() {
90           try {
91             Pooka.getUIFactory().getPookaThemeManager().updateUI(MessageFrame.this, MessageFrame.this);
92             getMessageDisplay().setDefaultFont();
93             getMessageDisplay().sizeToDefault();
94             MessageFrame.this.setSize(MessageFrame.this.getPreferredSize());
95           } catch (Exception JavaDoc e) {
96           }
97         }
98       };
99
100     if (! SwingUtilities.isEventDispatchThread()) {
101       SwingUtilities.invokeLater(runMe);
102     } else {
103       runMe.run();
104     }
105   }
106
107   /**
108    * Gets the currently configured Theme.
109    */

110   public MetalTheme JavaDoc getCurrentTheme() {
111     return currentTheme;
112   }
113
114   /**
115    * Sets the Theme that this component is currently using.
116    */

117   public void setCurrentTheme(MetalTheme JavaDoc newTheme) {
118     if (currentTheme != null && currentTheme instanceof ConfigurableMetalTheme) {
119       ((ConfigurableMetalTheme)currentTheme).removeThemeListener(this);
120     }
121     currentTheme = newTheme;
122
123     if (currentTheme != null && currentTheme instanceof ConfigurableMetalTheme) {
124       ((ConfigurableMetalTheme)currentTheme).addThemeListener(this);
125     }
126   }
127
128   /**
129    * Called when the specifics of a Theme change.
130    */

131   public void themeChanged(ConfigurableMetalTheme theme) {
132     // we should really only be getting messages from our own current themes,
133
// but, hey, it never hurts to check.
134
if (currentTheme != null && currentTheme == theme) {
135       SwingUtilities.invokeLater(new Runnable JavaDoc() {
136           public void run() {
137             try {
138               Pooka.getUIFactory().getPookaThemeManager().updateUI(MessageFrame.this, MessageFrame.this, true);
139               getMessageDisplay().setDefaultFont();
140               getMessageDisplay().sizeToDefault();
141               MessageFrame.this.setSize(MessageFrame.this.getPreferredSize());
142             } catch (Exception JavaDoc e) {
143             }
144
145           }
146         });
147     }
148   }
149
150   /**
151    * This opens the MessageFrame.
152    */

153   public void openMessageUI() {
154     this.setVisible(true);
155   }
156
157   /**
158    * This closes the MessageFrame.
159    */

160   public void closeMessageUI() {
161     this.dispose();
162   }
163
164   /**
165    * Attaches the window to a MessagePanel.
166    */

167   public abstract void attachWindow();
168
169   /**
170    * This shows an Confirm Dialog window. We include this so that
171    * the MessageProxy can call the method without caring about the
172    * actual implementation of the Dialog.
173    */

174   public int showConfirmDialog(String JavaDoc pMessageText, String JavaDoc pTitle, int pType) {
175     final String JavaDoc messageText = pMessageText;
176     final String JavaDoc title = pTitle;
177     final int type = pType;
178     final ResponseWrapper fResponseWrapper = new ResponseWrapper();
179
180     Runnable JavaDoc runMe = new Runnable JavaDoc() {
181         public void run() {
182           fResponseWrapper.setInt(JOptionPane.showConfirmDialog(MessageFrame.this, messageText, title, type));
183         }
184       };
185
186     if (! SwingUtilities.isEventDispatchThread()) {
187       try {
188         SwingUtilities.invokeAndWait(runMe);
189       } catch (Exception JavaDoc e) {
190       }
191     } else {
192       runMe.run();
193     }
194
195     return fResponseWrapper.getInt();
196   }
197
198   /**
199    * This shows an Confirm Dialog window. We include this so that
200    * the MessageProxy can call the method without caring about the
201    * actual implementation of the Dialog.
202    */

203   public int showConfirmDialog(String JavaDoc pMessageText, String JavaDoc pTitle, int pOptionType, int pIconType) {
204     final String JavaDoc messageText = pMessageText;
205     final String JavaDoc title = pTitle;
206     final int optionType = pOptionType;
207     final int iconType = pIconType;
208     final ResponseWrapper fResponseWrapper = new ResponseWrapper();
209
210     Runnable JavaDoc runMe = new Runnable JavaDoc() {
211         public void run() {
212           fResponseWrapper.setInt(JOptionPane.showConfirmDialog(MessageFrame.this, messageText, title, optionType, iconType));
213         }
214       };
215
216     if (! SwingUtilities.isEventDispatchThread()) {
217       try {
218         SwingUtilities.invokeAndWait(runMe);
219       } catch (Exception JavaDoc e) {
220       }
221     } else {
222       runMe.run();
223     }
224
225     return fResponseWrapper.getInt();
226   }
227
228   /**
229    * This shows an Error Message window. We include this so that
230    * the MessageProxy can call the method without caring about the
231    * actual implementation of the Dialog.
232    */

233   public void showError(String JavaDoc pErrorMessage, String JavaDoc pTitle) {
234     final String JavaDoc errorMessage = pErrorMessage;
235     final String JavaDoc title = pTitle;
236
237     Runnable JavaDoc runMe = new Runnable JavaDoc() {
238         public void run() {
239           JOptionPane.showMessageDialog(MessageFrame.this, errorMessage, title, JOptionPane.ERROR_MESSAGE);
240         }
241       };
242
243     if (! SwingUtilities.isEventDispatchThread()) {
244       try {
245         SwingUtilities.invokeAndWait(runMe);
246       } catch (Exception JavaDoc e) {
247       }
248     } else {
249       runMe.run();
250     }
251
252   }
253
254   /**
255    * This shows an Error Message window. We include this so that
256    * the MessageProxy can call the method without caring about the
257    * actual implementation of the Dialog.
258    */

259   public void showError(String JavaDoc errorMessage) {
260     showError(errorMessage, Pooka.getProperty("Error", "Error"));
261   }
262
263   /**
264    * This shows an Error Message window. We include this so that
265    * the MessageProxy can call the method without caring about the
266    * actual implementation of the Dialog.
267    */

268   public void showError(String JavaDoc errorMessage, Exception JavaDoc e) {
269     showError(errorMessage, Pooka.getProperty("Error", "Error"), e);
270   }
271
272   /**
273    * This shows an Error Message window. We include this so that
274    * the MessageProxy can call the method without caring about the
275    * actual implementation of the Dialog.
276    */

277   public void showError(String JavaDoc errorMessage, String JavaDoc title, Exception JavaDoc e) {
278     showError(errorMessage + e.getMessage(), title);
279     e.printStackTrace();
280   }
281
282   /**
283    * This formats a display message.
284    */

285   public String JavaDoc formatMessage(String JavaDoc message) {
286     return Pooka.getUIFactory().formatMessage(message);
287   }
288
289   /**
290    * This shows an Input window. We include this so that the
291    * MessageProxy can call the method without caring about the actual
292    * implementation of the dialog.
293    */

294   public String JavaDoc showInputDialog(String JavaDoc pInputMessage, String JavaDoc pTitle) {
295     final String JavaDoc inputMessage = pInputMessage;
296     final String JavaDoc title = pTitle;
297
298     final ResponseWrapper fResponseWrapper = new ResponseWrapper();
299     Runnable JavaDoc runMe = new Runnable JavaDoc() {
300         public void run() {
301           fResponseWrapper.setString(JOptionPane.showInputDialog(MessageFrame.this, inputMessage, title, JOptionPane.QUESTION_MESSAGE));
302         }
303       };
304
305     if (! SwingUtilities.isEventDispatchThread()) {
306       try {
307         SwingUtilities.invokeAndWait(runMe);
308       } catch (Exception JavaDoc e) {
309       }
310     } else {
311       runMe.run();
312     }
313
314     return fResponseWrapper.getString();
315   }
316
317   /**
318    * This shows an Input window. We include this so that the
319    * MessageProxy can call the method without caring about the actual
320    * implementation of the dialog.
321    */

322   public String JavaDoc showInputDialog(Object JavaDoc[] pInputPanes, String JavaDoc pTitle) {
323     final Object JavaDoc[] inputPanes = pInputPanes;
324     final String JavaDoc title = pTitle;
325
326     final ResponseWrapper fResponseWrapper = new ResponseWrapper();
327     Runnable JavaDoc runMe = new Runnable JavaDoc() {
328         public void run() {
329           fResponseWrapper.setString(JOptionPane.showInputDialog(MessageFrame.this, inputPanes, title, JOptionPane.QUESTION_MESSAGE));
330         }
331       };
332
333     if (! SwingUtilities.isEventDispatchThread()) {
334       try {
335         SwingUtilities.invokeAndWait(runMe);
336       } catch (Exception JavaDoc e) {
337       }
338     } else {
339       runMe.run();
340     }
341
342     return fResponseWrapper.getString();
343   }
344
345   /**
346    * This shows a Message window. We include this so that the
347    * MessageProxy can call the method without caring about the actual
348    * implementation of the dialog.
349    */

350   public void showMessageDialog(String JavaDoc pErrorMessage, String JavaDoc pTitle) {
351     final String JavaDoc errorMessage = pErrorMessage;
352     final String JavaDoc title = pTitle;
353
354     Runnable JavaDoc runMe = new Runnable JavaDoc() {
355         public void run() {
356           JOptionPane.showMessageDialog(MessageFrame.this, errorMessage, title, JOptionPane.PLAIN_MESSAGE);
357         }
358       };
359
360     if (! SwingUtilities.isEventDispatchThread()) {
361       try {
362         SwingUtilities.invokeAndWait(runMe);
363       } catch (Exception JavaDoc e) {
364       }
365     } else {
366       runMe.run();
367     }
368
369   }
370
371   /**
372    * A convenience method to set the PreferredSize and Size of the
373    * component to that of the current preferred width.
374    */

375   public void resizeByWidth() {
376     //int width = (int)messageDisplay.getPreferredSize().getWidth();
377
//this.setPreferredSize(new Dimension(width, width));
378
this.setSize(this.getPreferredSize());
379   }
380
381   /**
382    * Creates a ProgressDialog using the given values.
383    */

384   public ProgressDialog createProgressDialog(int min, int max, int initialValue, String JavaDoc title, String JavaDoc content) {
385     return new ProgressDialogImpl(min, max, initialValue, title, content);
386   }
387
388   /**
389    * Shows the current display of the encryption status.
390    */

391   public net.suberic.pooka.gui.crypto.CryptoStatusDisplay getCryptoStatusDisplay() {
392     return getMessageDisplay().getCryptoStatusDisplay();
393   }
394
395
396   /**
397    * As specified by interface net.suberic.pooka.gui.MessageUI.
398    *
399    * This implementation sets the cursor to either Cursor.WAIT_CURSOR
400    * if busy, or Cursor.DEFAULT_CURSOR if not busy.
401    */

402   public void setBusy(boolean newValue) {
403     final boolean fNewValue = newValue;
404     Runnable JavaDoc runMe = new Runnable JavaDoc() {
405         public void run() {
406           if (fNewValue)
407             setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));
408           else
409             setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR));
410         }
411       };
412
413     if (SwingUtilities.isEventDispatchThread()) {
414       runMe.run();
415     } else {
416       SwingUtilities.invokeLater(runMe);
417     }
418   }
419
420   /**
421    * Refreshes the display.
422    */

423   public void refreshDisplay() throws MessagingException {
424     if (messageDisplay instanceof ReadMessageDisplayPanel)
425       ((ReadMessageDisplayPanel)messageDisplay).resetEditorText();
426   }
427
428   /**
429    * As specified by interface net.suberic.pooka.UserProfileContainer.
430    *
431    * This implementation returns the DefaultProfile of the associated
432    * MessageProxy if the MessageFrame is not editable. If the
433    * MessageFrame is editable, it returns the currently selected
434    * UserProfile object.
435    */

436
437   public UserProfile getDefaultProfile() {
438     return getMessageProxy().getDefaultProfile();
439   }
440
441   public MessageDisplayPanel getMessageDisplay() {
442     return messageDisplay;
443   }
444
445   public MessageProxy getMessageProxy() {
446     return msg;
447   }
448
449   public void setMessageProxy(MessageProxy newValue) {
450     msg = newValue;
451   }
452
453   public String JavaDoc getMessageText() {
454     return getMessageDisplay().getMessageText();
455   }
456
457   public String JavaDoc getMessageContentType() {
458     return getMessageDisplay().getMessageContentType();
459   }
460
461   public AttachmentPane getAttachmentPanel() {
462     return getMessageDisplay().getAttachmentPanel();
463   }
464
465   public ConfigurableToolbar getToolbar() {
466     return toolbar;
467   }
468
469   public ConfigurableKeyBinding getKeyBindings() {
470     return keyBindings;
471   }
472
473   //------- Actions ----------//
474

475   public Action JavaDoc[] getActions() {
476     return defaultActions;
477   }
478
479   public Action JavaDoc[] getDefaultActions() {
480     return defaultActions;
481   }
482
483   //-----------actions----------------
484

485   // The actions supported by the window itself.
486

487   public Action JavaDoc[] defaultActions = {
488     new CloseAction(),
489     new AttachAction()
490   };
491
492   class CloseAction extends AbstractAction {
493
494     CloseAction() {
495       super("file-close");
496     }
497
498     public void actionPerformed(ActionEvent e) {
499       closeMessageUI();
500     }
501   }
502
503   public class AttachAction extends AbstractAction {
504     AttachAction() {
505       super("window-detach");
506     }
507
508     public void actionPerformed(ActionEvent e) {
509       attachWindow();
510     }
511   }
512 }
513
514
515
516
517
518
Popular Tags