1 package net.suberic.pooka.gui; 2 3 import javax.swing.*; 4 import java.util.Vector ; 5 import java.util.List ; 6 import javax.mail.MessagingException ; 7 8 import net.suberic.pooka.*; 9 10 14 public class AttachmentPopupMenu extends JPopupMenu { 15 16 MessageProxy mProxy = null; 18 19 List 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 31 public AttachmentPopupMenu(MessageProxy pProxy, int pActionType) { 32 mProxy = pProxy; 33 mActionType = pActionType; 34 } 35 36 39 void loadAttachments(java.awt.event.ActionEvent e) throws MessagingException { 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 () { 46 public void actionPerformed(java.awt.event.ActionEvent pActionEvent) { 47 try { 48 mInfo.loadAttachmentInfo(); 49 SwingUtilities.invokeLater(new Runnable () { 50 public void run() { 51 try { 52 displayAttachments(); 53 } catch (MessagingException me) { 54 showLoadError(me); 55 } 56 } 57 }); 58 } catch (MessagingException me) { 59 showLoadError(me); 60 } 61 } 62 }, e, net.suberic.util.thread.ActionThread.PRIORITY_HIGH); 63 } else { 64 mInfo.loadAttachmentInfo(); 66 displayAttachments(); 67 } 68 } else { 69 displayAttachments(); 70 } 71 } 72 73 76 void displayAttachments() throws MessagingException { 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 label = current.getName(); 84 if (label == null || label.length() == 0) { 85 label = Pooka.getProperty("AttachmentPane.error.FileNameUnavailable", "Unavailable"); 86 } 87 javax.mail.internet.ContentType 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 118 public void showLoadError(MessagingException me) { 119 final MessagingException fme = me; 120 Runnable runMe = new Runnable () { 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 136 class AttachmentAction extends AbstractAction { 137 Attachment mAttachment; 139 140 143 public AttachmentAction(Attachment pAttachment) { 144 mAttachment = pAttachment; 145 } 146 147 public void actionPerformed(java.awt.event.ActionEvent 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 162 class SaveAllAction extends AbstractAction { 163 166 public SaveAllAction() { 167 } 168 169 public void actionPerformed(java.awt.event.ActionEvent e) { 170 AttachmentHandler ah = new AttachmentHandler(mProxy); 171 ah.saveAllAttachments(AttachmentPopupMenu.this); 172 } 173 } 174 } 175 | Popular Tags |