KickJava   Java API By Example, From Geeks To Geeks.

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


1 package net.suberic.pooka.gui;
2
3 import javax.swing.*;
4 import java.util.Vector JavaDoc;
5 import java.util.List JavaDoc;
6 import javax.mail.MessagingException JavaDoc;
7
8 import net.suberic.pooka.*;
9
10 /**
11  * This is a popup menu that allows access to the Message's attachments
12  * directly from the FolderPanel.
13  */

14 public class AttachmentPopupMenu extends JPopupMenu {
15   
16   // the root MessageProxy
17
MessageProxy mProxy = null;
18
19   // the Attachments
20
List JavaDoc mAttachments = null;
21
22   public static int OPEN = 0;
23   public static int OPEN_WITH = 5;
24   public static int SAVE = 10;
25
26   int mActionType = OPEN;
27
28   /**
29    * Creates a new AttachmentPopupMenu from the given MessageProxy.
30    */

31   public AttachmentPopupMenu(MessageProxy pProxy, int pActionType) {
32     mProxy = pProxy;
33     mActionType = pActionType;
34   }
35
36   /**
37    * Loads the attachments for this MessageProxy.
38    */

39   void loadAttachments(java.awt.event.ActionEvent JavaDoc e) throws MessagingException JavaDoc {
40     final MessageInfo mInfo = mProxy.getMessageInfo();
41     if (! mInfo.hasLoadedAttachments()) {
42       this.setLabel(Pooka.getProperty("AttachmentPopupMenu.notloaded.title", "Loading attachments..."));
43       FolderInfo fi = mInfo.getFolderInfo();
44       if (fi != null) {
45     fi.getFolderThread().addToQueue(new javax.swing.AbstractAction JavaDoc() {
46         public void actionPerformed(java.awt.event.ActionEvent JavaDoc pActionEvent) {
47           try {
48         mInfo.loadAttachmentInfo();
49         SwingUtilities.invokeLater(new Runnable JavaDoc() {
50             public void run() {
51               try {
52             displayAttachments();
53               } catch (MessagingException JavaDoc me) {
54             showLoadError(me);
55               }
56             }
57           });
58           } catch (MessagingException JavaDoc me) {
59         showLoadError(me);
60           }
61         }
62       }, e, net.suberic.util.thread.ActionThread.PRIORITY_HIGH);
63       } else {
64     // just do it here.
65
mInfo.loadAttachmentInfo();
66     displayAttachments();
67       }
68     } else {
69       displayAttachments();
70     }
71   }
72
73   /**
74    * Actually populates the PopupMenu with attachments and actions.
75    */

76   void displayAttachments() throws MessagingException JavaDoc {
77     mAttachments = mProxy.getAttachments();
78
79     for (int i = 0; i < mAttachments.size(); i++) {
80       Attachment current = (Attachment) mAttachments.get(i);
81       JMenuItem item = new JMenuItem();
82       item.setAction(new AttachmentAction(current));
83       String JavaDoc label = current.getName();
84       if (label == null || label.length() == 0) {
85     label = Pooka.getProperty("AttachmentPane.error.FileNameUnavailable", "Unavailable");
86       }
87       javax.mail.internet.ContentType JavaDoc mimeType = current.getMimeType();
88       if (mimeType != null) {
89     item.setText(label + " (" + mimeType.getBaseType() + ")");
90       } else {
91     item.setText(label);
92       }
93       this.add(item);
94     }
95
96     JMenuItem saveAllItem = new JMenuItem();
97     saveAllItem.setAction(new SaveAllAction());
98     saveAllItem.setText(Pooka.getProperty("AttachmentPane.Actions.SaveAll.Label", "Save All"));
99     this.add(saveAllItem);
100
101     if (mActionType == SAVE) {
102       this.setLabel(Pooka.getProperty("AttachmentPopupMenu.save.title", "Save Attachment"));
103     } else if (mActionType == OPEN_WITH) {
104       this.setLabel(Pooka.getProperty("AttachmentPopupMenu.openWith.title", "Open Attachment With..."));
105     } else {
106       this.setLabel(Pooka.getProperty("AttachmentPopupMenu.open.title", "Open Attachment"));
107     }
108  
109     this.pack();
110     this.setSize(this.getMinimumSize());
111     if (this.isVisible())
112       this.revalidate();
113   }
114
115   /**
116    * Shows a load error for this PopupMenu.
117    */

118   public void showLoadError(MessagingException JavaDoc me) {
119     final MessagingException JavaDoc fme = me;
120     Runnable JavaDoc runMe = new Runnable JavaDoc() {
121     public void run() {
122       setLabel(Pooka.getProperty("AttachmentPopupMenu.errorloading.title", "Error loading attachments"));
123       fme.printStackTrace();
124     }
125       };
126
127     if (SwingUtilities.isEventDispatchThread())
128       runMe.run();
129     else
130       SwingUtilities.invokeLater(runMe);
131   }
132   
133   /**
134    * An Action that will do the appropriate action for an Attachment.
135    */

136   class AttachmentAction extends AbstractAction {
137     // the Attachment that will be acted upon
138
Attachment mAttachment;
139
140     /**
141      * Creates a new AttachmentAction for this Attachment.
142      */

143     public AttachmentAction(Attachment pAttachment) {
144       mAttachment = pAttachment;
145     }
146
147     public void actionPerformed(java.awt.event.ActionEvent JavaDoc e) {
148       AttachmentHandler ah = new AttachmentHandler(mProxy);
149       if (mActionType == SAVE) {
150     ah.saveAttachment(mAttachment, AttachmentPopupMenu.this);
151       } else if (mActionType == OPEN_WITH) {
152     ah.openWith(mAttachment);
153       } else {
154     ah.openAttachment(mAttachment);
155       }
156     }
157   }
158
159   /**
160    * An Action that will save all the attachments on a message.
161    */

162   class SaveAllAction extends AbstractAction {
163     /**
164      * Creates a new SaveAllAction for this Attachment.
165      */

166     public SaveAllAction() {
167     }
168
169     public void actionPerformed(java.awt.event.ActionEvent JavaDoc e) {
170       AttachmentHandler ah = new AttachmentHandler(mProxy);
171       ah.saveAllAttachments(AttachmentPopupMenu.this);
172     }
173   }
174 }
175
Popular Tags