1 package org.columba.mail.gui.message.viewer; 19 20 import java.awt.datatransfer.DataFlavor ; 21 import java.awt.event.MouseAdapter ; 22 import java.awt.event.MouseEvent ; 23 import java.awt.event.MouseListener ; 24 import java.io.File ; 25 import java.io.IOException ; 26 import java.util.List ; 27 28 import javax.swing.Icon ; 29 import javax.swing.ImageIcon ; 30 import javax.swing.JComponent ; 31 import javax.swing.JPopupMenu ; 32 import javax.swing.TransferHandler ; 33 import javax.swing.UIManager ; 34 35 import org.columba.core.command.CommandProcessor; 36 import org.columba.core.gui.menu.ExtendablePopupMenu; 37 import org.columba.core.gui.menu.MenuXMLDecoder; 38 import org.columba.core.resourceloader.ImageLoader; 39 import org.columba.mail.command.IMailFolderCommandReference; 40 import org.columba.mail.command.MailFolderCommandReference; 41 import org.columba.mail.folder.IMailbox; 42 import org.columba.mail.gui.frame.MailFrameMediator; 43 import org.columba.mail.gui.message.IMessageController; 44 import org.columba.mail.gui.message.action.OpenAttachmentAction; 45 import org.columba.mail.gui.message.action.SaveAsAttachmentAction; 46 import org.columba.mail.gui.message.command.SaveAttachmentTemporaryCommand; 47 import org.columba.ristretto.message.MimeHeader; 48 import org.columba.ristretto.message.MimeTree; 49 import org.columba.ristretto.message.MimeType; 50 import org.columba.ristretto.message.StreamableMimePart; 51 import org.frapuccino.iconpanel.IconPanel; 52 import org.frapuccino.swing.DynamicFileFactory; 53 import org.frapuccino.swing.DynamicFileTransferHandler; 54 55 59 60 public class AttachmentsViewer extends IconPanel implements ICustomViewer { 61 62 private MimeTree mimePartTree; 63 64 private ExtendablePopupMenu menu; 65 66 private AttachmentModel model; 67 68 private IMessageController mediator; 69 70 private MailFolderCommandReference ref; 71 72 public AttachmentsViewer(IMessageController mediator) { 73 super(); 74 75 this.mediator = mediator; 76 77 model = new AttachmentModel(); 78 79 setOpaque(true); 80 setBackground(UIManager.getColor("List.background")); 81 82 setDragEnabled(true); 83 setTransferHandler(new AttachmentTransferHandler(new FileGenerator())); 84 85 MouseListener popupListener = new PopupListener(); 86 addMouseListener(popupListener); 87 88 setDoubleClickAction(new OpenAttachmentAction(mediator, this)); 90 } 91 92 100 private boolean setMimePartTree(MimeTree collection) { 101 String contentType; 102 String contentSubtype; 103 String text = null; 104 boolean output = false; 105 106 removeAll(); 107 108 model.setCollection(collection); 109 110 List displayedMimeParts = model.getDisplayedMimeParts(); 111 112 for (int i = 0; i < displayedMimeParts.size(); i++) { 114 StreamableMimePart mp = (StreamableMimePart) displayedMimeParts 115 .get(i); 116 117 MimeHeader header = mp.getHeader(); 118 MimeType type = header.getMimeType(); 119 120 contentType = type.getType(); 121 contentSubtype = type.getSubtype(); 122 123 if (header.getFileName() != null) { 125 text = header.getFileName(); 126 } else { 127 text = contentType + "/" + contentSubtype; 128 } 129 130 StringBuffer tooltip = new StringBuffer (); 132 tooltip.append("<html><body>"); 133 134 if (header.getFileName() != null) { 135 tooltip.append(header.getFileName()); 136 tooltip.append(" - "); 137 } 138 139 tooltip.append("<i>"); 140 141 if (header.getContentDescription() != null) { 142 tooltip.append(header.getContentDescription()); 143 } else { 144 tooltip.append(contentType); 145 tooltip.append("/"); 146 tooltip.append(contentSubtype); 147 } 148 149 tooltip.append("</i></body></html>"); 150 151 ImageIcon icon = null; 152 153 icon = new AttachmentImageIconLoader().getImageIcon(type.getType(), 154 type.getSubtype()); 155 156 add(icon, text, tooltip.toString()); 157 158 output = true; 159 } 160 161 return output; 162 } 163 164 168 public void view(IMailbox folder, Object uid, MailFrameMediator mediator) 169 throws Exception { 170 171 setLocalReference(new MailFolderCommandReference(folder, 172 new Object [] { uid })); 173 174 mimePartTree = folder.getMimePartTree(uid); 175 176 } 177 178 181 public void updateGUI() throws Exception { 182 if (mimePartTree == null) 183 return; 184 185 setMimePartTree(mimePartTree); 186 } 187 188 191 public JComponent getView() { 192 return this; 193 } 194 195 private JPopupMenu getPopupMenu() { 196 if (menu == null) { 198 menu = new ExtendablePopupMenu("mail.attachmentviewer"); 199 menu.add(new OpenAttachmentAction(mediator, 200 this)); 201 menu.add(new SaveAsAttachmentAction(mediator, 202 this)); 203 } 204 205 return menu; 206 } 207 208 public void setLocalReference(MailFolderCommandReference ref) { 209 this.ref = ref; 210 } 211 212 public MailFolderCommandReference getLocalReference() { 213 Integer [] address = getSelectedMimePart().getAddress(); 214 ref.setAddress(address); 215 216 return ref; 217 } 218 219 class PopupListener extends MouseAdapter { 220 public void mousePressed(MouseEvent e) { 221 maybeShowPopup(e); 222 } 223 224 public void mouseReleased(MouseEvent e) { 225 maybeShowPopup(e); 226 } 227 228 private void maybeShowPopup(MouseEvent e) { 229 if (e.isPopupTrigger()) { 230 231 if (countSelected() >= 1) { 232 getPopupMenu().show(e.getComponent(), e.getX(), e.getY()); 233 } 234 } 235 } 236 } 237 238 private class FileGenerator implements DynamicFileFactory { 239 240 241 public File [] createFiles(JComponent arg0) throws IOException { 242 File [] files = new File [1]; 243 244 IMailFolderCommandReference ref = mediator.getSelectedReference(); 245 ref.setAddress(getSelected()); 246 247 SaveAttachmentTemporaryCommand command = new SaveAttachmentTemporaryCommand( 248 ref); 249 250 CommandProcessor.getInstance().addOp(command); 251 252 command.waitForCommandToComplete(); 253 254 files[0] = command.getTempAttachmentFile(); 255 256 return files; 257 } 258 } 259 260 265 private StreamableMimePart getSelectedMimePart() { 266 return (StreamableMimePart) model.getDisplayedMimeParts().get( 267 getSelectedIndex()); 268 } 269 270 public Integer [] getSelected() { 271 StreamableMimePart mp = getSelectedMimePart(); 272 return mp.getAddress(); 273 } 274 275 294 295 public class AttachmentTransferHandler extends DynamicFileTransferHandler { 296 297 303 public AttachmentTransferHandler(DynamicFileFactory factory) { 304 super(factory, DynamicFileTransferHandler.LATE_GENERATION); 305 } 306 307 315 public int getSourceActions(JComponent c) { 316 return TransferHandler.COPY; 317 } 318 319 329 public boolean canImport(JComponent comp, DataFlavor [] transferFlavors) { 330 return false; 331 } 332 } 333 334 341 class AttachmentImageIconLoader { 342 343 346 private AttachmentImageIconLoader() { 347 } 348 349 358 public ImageIcon getImageIcon(String contentType, String contentSubtype) { 359 StringBuffer buf = new StringBuffer (); 360 buf.append("gnome-"); 361 buf.append(contentType); 362 buf.append("-"); 363 buf.append(contentSubtype); 364 buf.append(".png"); 365 366 ImageIcon icon = ImageLoader.getMimetypeIcon(buf.toString()); 367 368 if (icon == null) { 369 icon = ImageLoader.getMimetypeIcon("gnome-" + contentType 370 + ".png"); 371 } 372 373 if (icon == null) { 374 icon = ImageLoader.getMimetypeIcon("gnome-text.png"); 375 } 376 377 return icon; 378 } 379 } 380 381 public JComponent add(Icon image, String text, String tooltip) { 382 JComponent comp = super.add(image, text, tooltip); 383 return comp; 384 } 385 386 } | Popular Tags |