KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > columba > mail > gui > message > viewer > AttachmentsViewer


1 // The contents of this file are subject to the Mozilla Public License Version
2
// 1.1
3
//(the "License"); you may not use this file except in compliance with the
4
//License. You may obtain a copy of the License at http://www.mozilla.org/MPL/
5
//
6
//Software distributed under the License is distributed on an "AS IS" basis,
7
//WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
8
//for the specific language governing rights and
9
//limitations under the License.
10
//
11
//The Original Code is "The Columba Project"
12
//
13
//The Initial Developers of the Original Code are Frederik Dietz and Timo
14
// Stich.
15
//Portions created by Frederik Dietz and Timo Stich are Copyright (C) 2003.
16
//
17
//All Rights Reserved.
18
package org.columba.mail.gui.message.viewer;
19
20 import java.awt.datatransfer.DataFlavor JavaDoc;
21 import java.awt.event.MouseAdapter JavaDoc;
22 import java.awt.event.MouseEvent JavaDoc;
23 import java.awt.event.MouseListener JavaDoc;
24 import java.io.File JavaDoc;
25 import java.io.IOException JavaDoc;
26 import java.util.List JavaDoc;
27
28 import javax.swing.Icon JavaDoc;
29 import javax.swing.ImageIcon JavaDoc;
30 import javax.swing.JComponent JavaDoc;
31 import javax.swing.JPopupMenu JavaDoc;
32 import javax.swing.TransferHandler JavaDoc;
33 import javax.swing.UIManager JavaDoc;
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 /**
56  * @author fdietz
57  *
58  */

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 JavaDoc popupListener = new PopupListener();
86         addMouseListener(popupListener);
87
88         // set double-click action for attachment viewer
89
setDoubleClickAction(new OpenAttachmentAction(mediator, this));
90     }
91
92     /**
93      * Sets the mime part. Adds icons to the view.
94      *
95      * @param collection
96      * collection containing mime parts.
97      * @return true if there was any mime parts added to the view; false
98      * otherwise.
99      */

100     private boolean setMimePartTree(MimeTree collection) {
101         String JavaDoc contentType;
102         String JavaDoc contentSubtype;
103         String JavaDoc text = null;
104         boolean output = false;
105
106         removeAll();
107
108         model.setCollection(collection);
109
110         List JavaDoc displayedMimeParts = model.getDisplayedMimeParts();
111
112         // Display resulting MimeParts
113
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             // Get Text for Icon
124
if (header.getFileName() != null) {
125                 text = header.getFileName();
126             } else {
127                 text = contentType + "/" + contentSubtype;
128             }
129
130             // Get Tooltip for Icon
131
StringBuffer JavaDoc tooltip = new StringBuffer JavaDoc();
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 JavaDoc 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     /**
165      * @see org.columba.mail.gui.message.viewer.IViewer#view(org.columba.mail.folder.IMailbox,
166      * java.lang.Object, org.columba.mail.gui.frame.MailFrameMediator)
167      */

168     public void view(IMailbox folder, Object JavaDoc uid, MailFrameMediator mediator)
169             throws Exception JavaDoc {
170
171         setLocalReference(new MailFolderCommandReference(folder,
172                 new Object JavaDoc[] { uid }));
173
174         mimePartTree = folder.getMimePartTree(uid);
175
176     }
177
178     /**
179      * @see org.columba.mail.gui.message.viewer.IViewer#updateGUI()
180      */

181     public void updateGUI() throws Exception JavaDoc {
182         if (mimePartTree == null)
183             return;
184
185         setMimePartTree(mimePartTree);
186     }
187
188     /**
189      * @see org.columba.mail.gui.message.viewer.IViewer#getView()
190      */

191     public JComponent JavaDoc getView() {
192         return this;
193     }
194
195     private JPopupMenu JavaDoc getPopupMenu() {
196         // bug #999990 (fdietz): make sure popup menu is created correctly
197
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 JavaDoc[] address = getSelectedMimePart().getAddress();
214         ref.setAddress(address);
215
216         return ref;
217     }
218
219     class PopupListener extends MouseAdapter JavaDoc {
220         public void mousePressed(MouseEvent JavaDoc e) {
221             maybeShowPopup(e);
222         }
223
224         public void mouseReleased(MouseEvent JavaDoc e) {
225             maybeShowPopup(e);
226         }
227
228         private void maybeShowPopup(MouseEvent JavaDoc 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         /** {@inheritDoc} */
241         public File JavaDoc[] createFiles(JComponent JavaDoc arg0) throws IOException JavaDoc {
242             File JavaDoc[] files = new File JavaDoc[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     /**
261      * Returns the selected mime part from the model.
262      *
263      * @return the selected mime part.
264      */

265     private StreamableMimePart getSelectedMimePart() {
266         return (StreamableMimePart) model.getDisplayedMimeParts().get(
267                 getSelectedIndex());
268     }
269
270     public Integer JavaDoc[] getSelected() {
271         StreamableMimePart mp = getSelectedMimePart();
272         return mp.getAddress();
273     }
274
275     /**
276      * Transfer handler for the attachment view.
277      *
278      * The Sun DnD integration with the the native platform, requires that the
279      * file is already exists on the disk, when a File DnD is issued. This
280      * TransferHandler will create the files locally when the
281      *
282      * @linkplain java.awt.datatransfer.Transferable#getTransferData(java.awt.datatransfer.DataFlavor)
283      * method is called. That method is the last method called before
284      * the DnD has completed. The actual extraction is done through
285      * the SaveAttachmentTemporaryCommand, and there might be
286      * problems waiting for other commands to finish before it. The
287      * method does not complete until the file has been created, ie
288      * locks up the DnD action.
289      * <p>
290      * @author redsolo
291      * @see org.columba.mail.gui.message.command.SaveAttachmentTemporaryCommand
292      * @see org.frappucino.swing.DynamicFileTransferHandler
293      */

294
295     public class AttachmentTransferHandler extends DynamicFileTransferHandler {
296
297         /**
298          * Setup the dynamic transfer handler.
299          *
300          * @param factory
301          * the factory that creates the file for the DnD action.
302          */

303         public AttachmentTransferHandler(DynamicFileFactory factory) {
304             super(factory, DynamicFileTransferHandler.LATE_GENERATION);
305         }
306
307         /**
308          * Returns the COPY action.
309          *
310          * @param c
311          * ignored.
312          * @return the
313          * @link TransferHandler#COPY COPY action.
314          */

315         public int getSourceActions(JComponent JavaDoc c) {
316             return TransferHandler.COPY;
317         }
318
319         /**
320          * Returns always false. The attachment transfer handler can only export
321          * data flavors, and not import them.
322          *
323          * @param comp
324          * ignored.
325          * @param transferFlavors
326          * ignored.
327          * @return false.
328          */

329         public boolean canImport(JComponent JavaDoc comp, DataFlavor JavaDoc[] transferFlavors) {
330             return false;
331         }
332     }
333
334     /**
335      * Imageloader using a content-type and subtype to determine the image name.
336      * <p>
337      * Automatically falls back to the default icon.
338      *
339      * @author fdietz
340      */

341     class AttachmentImageIconLoader {
342
343         /**
344          * Utility constructor.
345          */

346         private AttachmentImageIconLoader() {
347         }
348
349         /**
350          * Returns the image icon for the content type.
351          *
352          * @param contentType
353          * content type
354          * @param contentSubtype
355          * content sub type
356          * @return an Image Icon for the content type.
357          */

358         public ImageIcon JavaDoc getImageIcon(String JavaDoc contentType, String JavaDoc contentSubtype) {
359             StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
360             buf.append("gnome-");
361             buf.append(contentType);
362             buf.append("-");
363             buf.append(contentSubtype);
364             buf.append(".png");
365
366             ImageIcon JavaDoc 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 JavaDoc add(Icon JavaDoc image, String JavaDoc text, String JavaDoc tooltip) {
382         JComponent JavaDoc comp = super.add(image, text, tooltip);
383         return comp;
384     }
385
386 }
Popular Tags