KickJava   Java API By Example, From Geeks To Geeks.

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


1 package net.suberic.pooka.gui;
2 import net.suberic.pooka.*;
3 import javax.mail.*;
4 import javax.mail.internet.MimeMessage JavaDoc;
5 import javax.mail.event.*;
6 import java.awt.*;
7 import java.awt.event.*;
8 import javax.swing.*;
9 import javax.swing.table.*;
10 import javax.swing.text.TextAction JavaDoc;
11 import java.util.*;
12 import net.suberic.util.gui.*;
13 import net.suberic.util.event.*;
14 import net.suberic.util.thread.*;
15 import net.suberic.util.swing.*;
16
17 /**
18  * This is a JPanel which contains a FolderDisplayPanel for showing
19  * the messages in the current folder. It also can be updated dynamically
20  * with a new folder if the selected folder changes.
21  */

22
23 public class PreviewFolderPanel extends JPanel implements FolderDisplayUI {
24
25   private PreviewContentPanel contentPanel;
26
27   private FolderDisplayPanel folderDisplay = null;
28   private FolderInfo displayedFolder = null;
29
30   //private FolderStatusBar folderStatusBar = null;
31
//private ConfigurableToolbar toolbar;
32

33   ConfigurableKeyBinding keyBindings;
34
35   private boolean enabled;
36
37   private Action JavaDoc[] defaultActions;
38
39   /**
40    * Creates an empty PreviewFolderPanel.
41    */

42   public PreviewFolderPanel(PreviewContentPanel newContentPanel) {
43     contentPanel = newContentPanel;
44
45     this.setSize(new java.awt.Dimension JavaDoc(newContentPanel.getSize().width, Integer.parseInt(Pooka.getProperty("Pooka.previewPanel.folderSize", "200"))));
46     this.setPreferredSize(new java.awt.Dimension JavaDoc(newContentPanel.getSize().width, Integer.parseInt(Pooka.getProperty("Pooka.contentPanel.dividerLocation", "200"))));
47
48     folderDisplay = new FolderDisplayPanel();
49
50   }
51
52   /**
53    * Creates a new PreviewFolderPanel for the given Folder.
54    */

55   public PreviewFolderPanel(PreviewContentPanel newContentPanel, FolderInfo folder) {
56     contentPanel = newContentPanel;
57
58     displayedFolder = folder;
59     folderDisplay = new FolderDisplayPanel(folder, false);
60
61     configurePanel();
62   }
63
64   /**
65    * Creates a new PreviewFolderPanel for the given Folder.
66    */

67   public PreviewFolderPanel(PreviewContentPanel newContentPanel, FolderInternalFrame fif) {
68     contentPanel = newContentPanel;
69
70     displayedFolder = fif.getFolderInfo();
71     folderDisplay = fif.getFolderDisplay();
72
73     configurePanel();
74   }
75
76   /**
77    * Configures the PreviewFolderPanel.
78    */

79   void configurePanel() {
80
81     this.setSize(contentPanel.getSize());
82
83     //folderStatusBar = new FolderStatusBar(displayedFolder);
84

85     this.setLayout(new java.awt.BorderLayout JavaDoc());
86
87     /*
88     if (displayedFolder != null) {
89       if (displayedFolder.isOutboxFolder()) {
90         toolbar = new ConfigurableToolbar("OutboxWindowToolbar", Pooka.getResources());
91       } else {
92         toolbar = new ConfigurableToolbar("FolderWindowToolbar", Pooka.getResources());
93       }
94     }
95     */

96
97     //this.add("North", toolbar);
98
this.add("Center", folderDisplay);
99     //this.add("South", folderStatusBar);
100

101     defaultActions = new Action JavaDoc[] {
102       new ActionWrapper(new ExpungeAction(), getFolderInfo().getFolderThread()),
103       new NextMessageAction(),
104       new PreviousMessageAction(),
105       new GotoMessageAction(),
106       new SearchAction(),
107       new SelectAllAction(),
108       new PreviewMessageAction()
109     };
110
111     keyBindings = new ConfigurableKeyBinding(this, "PreviewFolderWindow.keyBindings", Pooka.getResources());
112
113     keyBindings.setActive(getActions());
114
115     // if the PreviewFolderPanel itself gets the focus, pass it on to
116
// the folderDisplay
117

118     this.addFocusListener(new FocusAdapter() {
119         public void focusGained(FocusEvent e) {
120           if (folderDisplay != null) {
121             folderDisplay.requestFocusInWindow();
122           }
123         }
124       });
125
126     getFolderDisplay().getMessageTable().getSelectionModel().addListSelectionListener(new javax.swing.event.ListSelectionListener JavaDoc() {
127         public void valueChanged(javax.swing.event.ListSelectionEvent JavaDoc e) {
128           if (enabled) {
129             if (keyBindings != null) {
130               keyBindings.setActive(getActions());
131             }
132           }
133         }
134       });
135   }
136
137   /**
138    * Searches the underlying FolderInfo's messages for messages matching
139    * the search term.
140    */

141   public void searchFolder() {
142     getFolderInfo().showSearchFolder();
143   }
144
145   /**
146    * Opens the display for the given Folder.
147    *
148    * As defined in interface net.suberic.pooka.gui.FolderDisplayUI.
149    */

150   public void openFolderDisplay() {
151     openFolderDisplay(true);
152   }
153
154   /**
155    * Opens the display for the given Folder. Note that for this
156    * implementation, selected is ignored.
157    *
158    * As defined in interface net.suberic.pooka.gui.FolderDisplayUI.
159    */

160   public void openFolderDisplay(boolean selected) {
161     Runnable JavaDoc runMe = new Runnable JavaDoc() {
162         public void run() {
163           if (displayedFolder != null)
164             contentPanel.showFolder(displayedFolder.getFolderID());
165           setEnabled(true);
166         }
167       };
168     if (SwingUtilities.isEventDispatchThread())
169       runMe.run();
170     else
171       SwingUtilities.invokeLater(runMe);
172
173   }
174
175   /**
176    * Closes the display for the given Folder.
177    *
178    * As defined in interface net.suberic.pooka.gui.FolderDisplayUI.
179    */

180   public void closeFolderDisplay() {
181     contentPanel.removePreviewPanel(getFolderInfo().getFolderID());
182     folderDisplay.removeMessageTable();
183     if (displayedFolder != null && displayedFolder.getFolderDisplayUI() == this)
184       displayedFolder.setFolderDisplayUI(null);
185     displayedFolder = null;
186     setEnabled(false);
187   }
188
189   /**
190    * This expunges all the messages marked as deleted in the folder.
191    */

192   public void expungeMessages() {
193     try {
194       getFolderInfo().getFolder().expunge();
195     } catch (MessagingException me) {
196       showError(Pooka.getProperty("error.Message.ExpungeErrorMessage", "Error: could not expunge messages.") +"\n" + me.getMessage());
197     }
198   }
199
200   /**
201    * Gets the FolderInfo for the currently displayed Folder.
202    *
203    * As defined in interface net.suberic.pooka.gui.FolderDisplayUI.
204    */

205   public FolderInfo getFolderInfo() {
206     return displayedFolder;
207   }
208
209   /**
210    * Sets the FolderInfo to be displayed by this PreviewFolderPanel.
211    */

212   public void setFolderInfo(FolderInfo fi) {
213     getFolderDisplay().removeMessageTable();
214     getFolderDisplay().setFolderInfo(fi);
215     getFolderDisplay().createMessageTable();
216
217     displayedFolder = fi;
218
219   }
220
221   /**
222    * Sets the panel enabled or disabled.
223    *
224    * As defined in interface net.suberic.pooka.gui.FolderDisplayUI.
225    */

226   public void setEnabled(boolean newValue) {
227     enabled = newValue;
228
229     if (keyBindings != null)
230       keyBindings.setActive(getActions());
231   }
232
233   /**
234    * As specified by interface net.suberic.pooka.gui.FolderDisplayUI.
235    *
236    * This skips to the given message.
237    */

238   public int selectMessage(int messageNumber) {
239     return getFolderDisplay().selectMessage(messageNumber);
240   }
241
242   /**
243    * As specified by interface net.suberic.pooka.gui.FolderDisplayUI.
244    *
245    * This makes the given row visible.
246    */

247   public void makeSelectionVisible(int messageNumber) {
248     getFolderDisplay().makeSelectionVisible(messageNumber);
249   }
250
251   public int selectNextMessage() {
252     return getFolderDisplay().selectNextMessage();
253   }
254
255   public int selectPreviousMessage() {
256     return getFolderDisplay().selectPreviousMessage();
257   }
258
259   public int selectNextUnreadMessage() {
260     return getFolderDisplay().selectNextUnreadMessage();
261   }
262
263   /**
264    * As specified by interface net.suberic.pooka.gui.FolderDisplayUI.
265    *
266    * This resets the FolderTableModel in the MessageTable.
267    */

268   public void resetFolderTableModel(FolderTableModel ftm) {
269     getFolderDisplay().resetFolderTableModel(ftm);
270   }
271
272   /**
273    * As specified by interface net.suberic.pooka.gui.FolderDisplayUI.
274    *
275    */

276   public void showStatusMessage(String JavaDoc msg) {
277     Pooka.getUIFactory().showStatusMessage(getFolderInfo().getFolderID() + ": " + msg);
278   }
279
280   /**
281    * As specified by interface net.suberic.pooka.gui.FolderDisplayUI.
282    *
283    */

284   public void clearStatusMessage() {
285     Pooka.getUIFactory().clearStatus();
286   }
287
288   /**
289    * Sets the busy property of the panel.
290    *
291    * As defined in interface net.suberic.pooka.gui.FolderDisplayUI.
292    */

293   public void setBusy(boolean newValue) {
294     final boolean newV = newValue;
295     Runnable JavaDoc runMe = new Runnable JavaDoc() {
296         public void run() {
297           if (newV)
298             setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));
299           else
300             setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR));
301         }
302       };
303
304     if (SwingUtilities.isEventDispatchThread())
305       runMe.run();
306     else
307       SwingUtilities.invokeLater(runMe);
308
309   }
310
311   /**
312    * Shows an Input dialog for this panel.
313    *
314    * As defined in interface net.suberic.pooka.gui.FolderDisplayUI.
315    */

316   public String JavaDoc showInputDialog(String JavaDoc pInputMessage, String JavaDoc pTitle) {
317     final String JavaDoc inputMessage = pInputMessage;
318     final String JavaDoc title = pTitle;
319     final ResponseWrapper fResponseWrapper = new ResponseWrapper();
320
321     Runnable JavaDoc runMe = new Runnable JavaDoc() {
322         public void run() {
323           fResponseWrapper.setString(JOptionPane.showInputDialog(PreviewFolderPanel.this, inputMessage, title, JOptionPane.QUESTION_MESSAGE));
324         }
325       };
326
327     if (! SwingUtilities.isEventDispatchThread()) {
328       try {
329         SwingUtilities.invokeAndWait(runMe);
330       } catch (Exception JavaDoc e) {
331       }
332     } else {
333       runMe.run();
334     }
335
336     return fResponseWrapper.getString();
337   }
338
339   /**
340    * Shows an Input Dialog with the given Input Panels and title.
341    *
342    * As defined in interface net.suberic.pooka.gui.MessageUI.
343    */

344   public String JavaDoc showInputDialog(Object JavaDoc[] pInputPanels, String JavaDoc pTitle) {
345     final Object JavaDoc[] inputPanels= pInputPanels;
346     final String JavaDoc title = pTitle;
347     final ResponseWrapper fResponseWrapper = new ResponseWrapper();
348
349     Runnable JavaDoc runMe = new Runnable JavaDoc() {
350         public void run() {
351           fResponseWrapper.setString(JOptionPane.showInputDialog(PreviewFolderPanel.this, inputPanels, title, JOptionPane.QUESTION_MESSAGE));
352         }
353       };
354
355     if (! SwingUtilities.isEventDispatchThread()) {
356       try {
357         SwingUtilities.invokeAndWait(runMe);
358       } catch (Exception JavaDoc e) {
359       }
360     } else {
361       runMe.run();
362     }
363
364     return fResponseWrapper.getString();
365   }
366
367   /**
368    * Shows a Confirm dialog.
369    *
370    * As defined in interface net.suberic.pooka.gui.MessageUI.
371    */

372   public int showConfirmDialog(String JavaDoc message, String JavaDoc title, int optionType, int messageType) {
373     return JOptionPane.showConfirmDialog(this, message, title, messageType);
374   }
375
376   /**
377    * Gets the default UserProfile for this component.
378    *
379    * As defined in interface net.suberic.pooka.UserProfileContainer.
380    */

381   public UserProfile getDefaultProfile() {
382     if (getFolderInfo() != null)
383       return getFolderInfo().getDefaultProfile();
384     else return null;
385   }
386
387   /**
388    * Shows an Error with the given parameters.
389    *
390    * As defined in interface net.suberic.pooka.gui.ErrorHandler.
391    */

392   public void showError(String JavaDoc errorMessage) {
393     showError(errorMessage, Pooka.getProperty("Error", "Error"));
394   }
395
396   /**
397    * Shows an Error with the given parameters.
398    *
399    * As defined in interface net.suberic.pooka.gui.ErrorHandler.
400    */

401   public void showError(String JavaDoc errorMessage, Exception JavaDoc e) {
402     showError(errorMessage, Pooka.getProperty("Error", "Error"), e);
403   }
404
405   /**
406    * Shows an Error with the given parameters.
407    *
408    * As defined in interface net.suberic.pooka.gui.ErrorHandler.
409    */

410   public void showError(String JavaDoc errorMessage, String JavaDoc title) {
411     final String JavaDoc errorMsg = errorMessage;
412     final String JavaDoc realTitle = title;
413     Runnable JavaDoc runMe = new Runnable JavaDoc() {
414         public void run() {
415
416           JOptionPane.showMessageDialog(PreviewFolderPanel.this, errorMsg, realTitle, JOptionPane.ERROR_MESSAGE);
417         }
418       };
419
420     if (SwingUtilities.isEventDispatchThread()) {
421       runMe.run();
422     } else {
423       SwingUtilities.invokeLater(runMe);
424     }
425
426   }
427
428   /**
429    * Shows an Error with the given parameters.
430    *
431    * As defined in interface net.suberic.pooka.gui.ErrorHandler.
432    */

433   public void showError(String JavaDoc errorMessage, String JavaDoc title, Exception JavaDoc e) {
434     showError(errorMessage + e.getMessage(), title);
435     e.printStackTrace();
436   }
437
438   /**
439    * This formats a display message.
440    */

441   public String JavaDoc formatMessage(String JavaDoc message) {
442     return Pooka.getUIFactory().formatMessage(message);
443   }
444
445   // MessageLoadedListener
446

447   /**
448    *
449    * Defined in net.suberic.pooka.event.MessageLoadedListener.
450    */

451   public void handleMessageLoaded(net.suberic.pooka.event.MessageLoadedEvent e) {
452     /*
453     if (getFolderStatusBar() != null && getFolderStatusBar().getTracker() != null)
454       getFolderStatusBar().getTracker().handleMessageLoaded(e);
455     */

456   }
457
458   // ConnectionListener
459

460   /**
461    *
462    */

463   public void closed(ConnectionEvent e) {
464
465   }
466
467   /**
468    *
469    */

470   public void disconnected(ConnectionEvent e) {
471
472   }
473
474   /**
475    *
476    */

477   public void opened(ConnectionEvent e) {
478
479   }
480
481   // MessageCountListener
482
/**
483    *
484    */

485   public void messagesAdded(MessageCountEvent e) {
486     //getFolderStatusBar().messagesAdded(e);
487
}
488
489   /**
490    * Called in response to a messagesRemoved event. Should always be
491    * called on the parent FolderThread.
492    */

493   public void messagesRemoved(MessageCountEvent e) {
494     //getFolderStatusBar().messagesRemoved(e);
495

496     Runnable JavaDoc updateAdapter = new Runnable JavaDoc() {
497         public void run() {
498           Pooka.getMainPanel().refreshActiveMenus();
499         }
500       };
501     if (SwingUtilities.isEventDispatchThread())
502       updateAdapter.run();
503     else
504       SwingUtilities.invokeLater(updateAdapter);
505
506   }
507
508   // MessageChangedListener
509
public void messageChanged(MessageChangedEvent e) {
510     /*
511     if (getFolderStatusBar() != null)
512       getFolderStatusBar().messageChanged(e);
513     */

514     if (getFolderDisplay() != null)
515       getFolderDisplay().moveSelectionOnRemoval(e);
516
517     final MessageInfo mi = getFolderInfo().getMessageInfo(e.getMessage());
518     if (mi != null) {
519       SwingUtilities.invokeLater(new Runnable JavaDoc() {
520           public void run() {
521             // really, all we should do here is update the individual
522
// row.
523
// getFolderDisplay().repaint();
524
getFolderDisplay().repaintMessage(mi.getMessageProxy());
525           }
526         });
527     }
528   }
529
530   /**
531    * Calls getFolderDisplay().removeRows(removedProxies).
532    * This is the preferred way to remove rows from the FolderTableModel.
533    */

534   public void removeRows(java.util.Vector JavaDoc removedProxies) {
535     getFolderDisplay().removeRows(removedProxies);
536   }
537
538   /**
539    * Returns the Toolbar for this FolderPanel.
540    */

541   /*
542   public ConfigurableToolbar getToolbar() {
543     return toolbar;
544   }
545   */

546
547   /**
548    * Gets the folderStatusBar.
549    */

550   /*
551   public FolderStatusBar getFolderStatusBar() {
552     return folderStatusBar;
553   }
554   */

555
556   /**
557    * Gets the currently available Actions for this component.
558    *
559    * As defined in interface net.suberic.pooka.gui.ActionContainer.
560    */

561   public Action JavaDoc[] getActions() {
562     if (enabled) {
563       Action JavaDoc[] returnValue = defaultActions;
564
565       if (getFolderDisplay() != null) {
566         if (returnValue == null) {
567           returnValue = getFolderDisplay().getActions();
568         } else {
569           returnValue = TextAction.augmentList(returnValue, getFolderDisplay().getActions());
570         }
571       }
572
573       return returnValue;
574
575     } else {
576       return null;
577     }
578   }
579
580   public FolderDisplayPanel getFolderDisplay() {
581     return folderDisplay;
582   }
583
584   public class ExpungeAction extends AbstractAction {
585
586     ExpungeAction() {
587       super("message-expunge");
588     }
589
590     public void actionPerformed(ActionEvent e) {
591       // we can't have a normal action wrapper because the underlying
592
// folder (and therefore folder thread) can change...
593

594       if (displayedFolder != null) {
595         displayedFolder.getFolderThread().addToQueue(new AbstractAction() {
596             public void actionPerformed(ActionEvent e) {
597               expungeMessages();
598
599             }
600           }, e);
601       }
602
603     }
604   }
605
606   public class NextMessageAction extends AbstractAction {
607
608     NextMessageAction() {
609       super("message-next");
610     }
611
612     public void actionPerformed(ActionEvent e) {
613       selectNextMessage();
614     }
615   }
616
617   public class PreviousMessageAction extends AbstractAction {
618
619     PreviousMessageAction() {
620       super("message-previous");
621     }
622
623     public void actionPerformed(ActionEvent e) {
624       selectPreviousMessage();
625     }
626   }
627
628   public class NextUnreadMessageAction extends AbstractAction {
629
630     NextUnreadMessageAction() {
631       super("message-next-unread");
632     }
633
634     public void actionPerformed(ActionEvent e) {
635       selectNextUnreadMessage();
636     }
637   }
638
639   public class GotoMessageAction extends AbstractAction {
640
641     GotoMessageAction() {
642       super("message-goto");
643     }
644
645     public void actionPerformed(ActionEvent e) {
646       //getFolderStatusBar().activateGotoDialog();
647
}
648   }
649
650   public class SearchAction extends AbstractAction {
651
652     SearchAction() {
653       super("folder-search");
654     }
655
656     public void actionPerformed(ActionEvent e) {
657       searchFolder();
658     }
659   }
660
661   public class SelectAllAction extends AbstractAction {
662
663     SelectAllAction() {
664       super("select-all");
665     }
666
667     public void actionPerformed(ActionEvent e) {
668       getFolderDisplay().selectAll();
669     }
670   }
671
672   public class PreviewMessageAction extends AbstractAction {
673     PreviewMessageAction() {
674       super("message-preview");
675     }
676
677     public void actionPerformed(ActionEvent e) {
678       contentPanel.refreshCurrentMessage();
679     }
680   }
681
682 }
683
684
Popular Tags