001: //The contents of this file are subject to the Mozilla Public License Version 1.1
002: //(the "License"); you may not use this file except in compliance with the
003: //License. You may obtain a copy of the License at http://www.mozilla.org/MPL/
004: //
005: //Software distributed under the License is distributed on an "AS IS" basis,
006: //WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
007: //for the specific language governing rights and
008: //limitations under the License.
009: //
010: //The Original Code is "The Columba Project"
011: //
012: //The Initial Developers of the Original Code are Frederik Dietz and Timo Stich.
013: //Portions created by Frederik Dietz and Timo Stich are Copyright (C) 2003.
014: //
015: //All Rights Reserved.
016: package org.columba.mail.gui.composer;
017:
018: import java.awt.event.KeyEvent;
019: import java.awt.event.KeyListener;
020: import java.awt.event.MouseAdapter;
021:
022: import javax.swing.DefaultListModel;
023: import javax.swing.ImageIcon;
024: import javax.swing.UIManager;
025:
026: import org.columba.core.resourceloader.ImageLoader;
027: import org.columba.ristretto.message.MimeHeader;
028: import org.columba.ristretto.message.MimePart;
029: import org.columba.ristretto.message.MimeType;
030: import org.frapuccino.iconpanel.IconPanel;
031:
032: /**
033: * Attachment view. Used in the composer to show a list of attachments. Is part
034: * of a controller-view framework together with AttachmentController.
035: *
036: * @author frdietz
037: * @author redsolo
038: */
039:
040: public class AttachmentView extends IconPanel {
041:
042: /** Reference to the controller controlling this view */
043: private AttachmentController attachmentController;
044:
045: private AttachmentImageIconLoader attachmentIconLoader;
046:
047: /** Underlying data model for the list view */
048: private DefaultListModel listModel;
049:
050: /**
051: * Default constructor. Sets up the view and stores a reference to the
052: * controller for later use.
053: *
054: * @param controller
055: * Reference to the controller of this view
056: */
057: public AttachmentView(AttachmentController controller) {
058: super ();
059:
060: attachmentController = controller;
061:
062: listModel = new DefaultListModel();
063:
064: setOpaque(true);
065:
066: setBackground(UIManager.getColor("List.background"));
067:
068: attachmentIconLoader = new AttachmentImageIconLoader();
069:
070: setDoubleClickAction(new OpenAttachmentAction(this ));
071:
072: //TODO Let the AttachmentViewer get the focus so that this works
073: addKeyListener(new KeyListener() {
074:
075: public void keyPressed(KeyEvent e) {
076: if (e.getKeyCode() == KeyEvent.VK_DELETE) {
077: removeSelected();
078: }
079:
080: }
081:
082: public void keyReleased(KeyEvent e) {
083:
084: }
085:
086: public void keyTyped(KeyEvent e) {
087: }
088: });
089:
090: //setModel(listModel);
091:
092: //setCellRenderer(new ListRenderer());
093: }
094:
095: /**
096: * Installs the attachment controller as listener
097: *
098: * @param c
099: * Controller of this view
100: */
101: public void installListener(AttachmentController c) {
102: addKeyListener(c);
103: }
104:
105: /**
106: * Adds a popup listener
107: *
108: * @param a
109: * Listener to add
110: */
111: public void addPopupListener(MouseAdapter a) {
112: addMouseListener(a);
113: }
114:
115: /**
116: * Adds an attachment to be displayed in the view
117: *
118: * @param mp
119: * Attachment to add
120: */
121: public void add(MimePart mp) {
122: listModel.addElement(mp);
123:
124: MimeHeader header = mp.getHeader();
125:
126: MimeType mimeType = header.getMimeType();
127:
128: ImageIcon icon = attachmentIconLoader.getImageIcon(mimeType
129: .getType(), mimeType.getSubtype());
130:
131: String text = header.getFileName();
132: if (text == null || text.length() == 0) {
133: if (header.getContentDescription() != null) {
134: text = header.getContentDescription();
135: } else {
136: text = mimeType.toString();
137: }
138: }
139:
140: // Get Tooltip for Icon
141: StringBuffer tooltip = new StringBuffer();
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 getImageIcon(String contentType,
255: String contentSubtype) {
256: StringBuffer buf = new StringBuffer();
257: buf.append("gnome-");
258: buf.append(contentType);
259: buf.append("-");
260: buf.append(contentSubtype);
261: buf.append(".png");
262:
263: ImageIcon icon = ImageLoader
264: .getMimetypeIcon(buf.toString());
265:
266: if (icon == null) {
267: icon = ImageLoader.getMimetypeIcon("gnome-"
268: + contentType + ".png");
269: }
270:
271: if (icon == null) {
272: icon = ImageLoader.getMimetypeIcon("gnome-text.png");
273: }
274:
275: return icon;
276: }
277: }
278: }
|