KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > columba > mail > gui > composer > AttachmentView


1 //The contents of this file are subject to the Mozilla Public License Version 1.1
2
//(the "License"); you may not use this file except in compliance with the
3
//License. You may obtain a copy of the License at http://www.mozilla.org/MPL/
4
//
5
//Software distributed under the License is distributed on an "AS IS" basis,
6
//WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
7
//for the specific language governing rights and
8
//limitations under the License.
9
//
10
//The Original Code is "The Columba Project"
11
//
12
//The Initial Developers of the Original Code are Frederik Dietz and Timo Stich.
13
//Portions created by Frederik Dietz and Timo Stich are Copyright (C) 2003.
14
//
15
//All Rights Reserved.
16
package org.columba.mail.gui.composer;
17
18 import java.awt.event.KeyEvent JavaDoc;
19 import java.awt.event.KeyListener JavaDoc;
20 import java.awt.event.MouseAdapter JavaDoc;
21
22 import javax.swing.DefaultListModel JavaDoc;
23 import javax.swing.ImageIcon JavaDoc;
24 import javax.swing.UIManager JavaDoc;
25
26 import org.columba.core.resourceloader.ImageLoader;
27 import org.columba.ristretto.message.MimeHeader;
28 import org.columba.ristretto.message.MimePart;
29 import org.columba.ristretto.message.MimeType;
30 import org.frapuccino.iconpanel.IconPanel;
31
32 /**
33  * Attachment view. Used in the composer to show a list of attachments. Is part
34  * of a controller-view framework together with AttachmentController.
35  *
36  * @author frdietz
37  * @author redsolo
38  */

39
40 public class AttachmentView extends IconPanel {
41
42     /** Reference to the controller controlling this view */
43     private AttachmentController attachmentController;
44     
45     private AttachmentImageIconLoader attachmentIconLoader;
46
47     /** Underlying data model for the list view */
48     private DefaultListModel JavaDoc listModel;
49
50     /**
51      * Default constructor. Sets up the view and stores a reference to the
52      * controller for later use.
53      *
54      * @param controller
55      * Reference to the controller of this view
56      */

57     public AttachmentView(AttachmentController controller) {
58         super();
59
60         attachmentController = controller;
61
62         listModel = new DefaultListModel JavaDoc();
63
64         setOpaque(true);
65
66         setBackground(UIManager.getColor("List.background"));
67
68         attachmentIconLoader = new AttachmentImageIconLoader();
69         
70         setDoubleClickAction(new OpenAttachmentAction(this));
71         
72         //TODO Let the AttachmentViewer get the focus so that this works
73
addKeyListener( new KeyListener JavaDoc() {
74
75             public void keyPressed(KeyEvent JavaDoc e) {
76                 if( e.getKeyCode() == KeyEvent.VK_DELETE) {
77                     removeSelected();
78                 }
79                 
80             }
81
82             public void keyReleased(KeyEvent JavaDoc e) {
83                 
84             }
85
86             public void keyTyped(KeyEvent JavaDoc e) {
87             }});
88         
89         //setModel(listModel);
90

91         //setCellRenderer(new ListRenderer());
92
}
93
94     /**
95      * Installs the attachment controller as listener
96      *
97      * @param c
98      * Controller of this view
99      */

100     public void installListener(AttachmentController c) {
101         addKeyListener(c);
102     }
103
104     /**
105      * Adds a popup listener
106      *
107      * @param a
108      * Listener to add
109      */

110     public void addPopupListener(MouseAdapter JavaDoc a) {
111         addMouseListener(a);
112     }
113
114     /**
115      * Adds an attachment to be displayed in the view
116      *
117      * @param mp
118      * Attachment to add
119      */

120     public void add(MimePart mp) {
121         listModel.addElement(mp);
122
123         MimeHeader header = mp.getHeader();
124
125         MimeType mimeType = header.getMimeType();
126
127         ImageIcon JavaDoc icon = attachmentIconLoader.getImageIcon(mimeType
128                 .getType(), mimeType.getSubtype());
129
130         String JavaDoc text = header.getFileName();
131         if( text==null || text.length() == 0 ) {
132             if( header.getContentDescription() != null) {
133                 text = header.getContentDescription();
134             } else {
135                 text = mimeType.toString();
136             }
137         }
138         
139         
140         // Get Tooltip for Icon
141
StringBuffer JavaDoc tooltip = new StringBuffer JavaDoc();
142         tooltip.append("<html><body>");
143
144         if (mp.getHeader().getFileName() != null) {
145             tooltip.append(header.getFileName());
146             tooltip.append(" - ");
147         }
148
149         tooltip.append("<i>");
150
151         if (header.getContentDescription() != null) {
152             tooltip.append(header.getContentDescription());
153         } else {
154             tooltip.append(mimeType.getType());
155             tooltip.append("/");
156             tooltip.append(mimeType.getSubtype());
157         }
158
159         tooltip.append("</i></body></html>");
160         add(icon, text, tooltip.toString());
161     }
162
163     public void removeSelected() {
164         // remove from model
165
int[] indices = getSelectedIndices();
166
167         for (int i = 0; i < indices.length; i++) {
168             listModel.removeElementAt(indices[i]);
169
170         }
171
172         // remove from view
173
super.removeSelected();
174     }
175
176     /**
177      * Clears the view, i.e. removes all attachments.
178      */

179     public void clear() {
180         // clear model
181
listModel.clear();
182         
183         // clear view
184
removeAll();
185     }
186
187     /**
188      * Gets an attachment from the view by index
189      *
190      * @param index
191      * Index of attachment (zero based)
192      * @return The specified attachment
193      */

194     public MimePart get(int index) {
195         return (MimePart) listModel.get(index);
196     }
197
198     /**
199      * Gets number of attachments currently displayed in the view.
200      *
201      * @return the number of attachments currently displayed in the view.
202      */

203     public int count() {
204         return listModel.size();
205     }
206
207     /**
208      * Selects the list item that is at the position in the component/list.
209      *
210      * @param x
211      * the x pos.
212      * @param y
213      * the y pos.
214      */

215     public void fixSelection(int x, int y) {
216         /*
217          * int index = locationToIndex(new Point(x, y));
218          *
219          * setSelectedIndex(index);
220          */

221     }
222
223     /**
224      * @return Returns the controller for the view.
225      */

226     public AttachmentController getController() {
227         return attachmentController;
228     }
229     
230     /**
231      * Imageloader using a content-type and subtype to determine the image name.
232      * <p>
233      * Automatically falls back to the default icon.
234      *
235      * @author fdietz
236      */

237     class AttachmentImageIconLoader {
238
239         /**
240          * Utility constructor.
241          */

242         private AttachmentImageIconLoader() {
243         }
244
245         /**
246          * Returns the image icon for the content type.
247          *
248          * @param contentType
249          * content type
250          * @param contentSubtype
251          * content sub type
252          * @return an Image Icon for the content type.
253          */

254         public ImageIcon JavaDoc getImageIcon(String JavaDoc contentType, String JavaDoc contentSubtype) {
255             StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
256             buf.append("gnome-");
257             buf.append(contentType);
258             buf.append("-");
259             buf.append(contentSubtype);
260             buf.append(".png");
261
262             ImageIcon JavaDoc icon = ImageLoader.getMimetypeIcon(buf.toString());
263
264             if (icon == null) {
265                 icon = ImageLoader.getMimetypeIcon("gnome-"
266                         + contentType + ".png");
267             }
268
269             if (icon == null) {
270                 icon = ImageLoader.getMimetypeIcon("gnome-text.png");
271             }
272
273             return icon;
274         }
275     }
276 }
Popular Tags