KickJava   Java API By Example, From Geeks To Geeks.

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


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.ActionListener JavaDoc;
19 import java.awt.event.KeyEvent JavaDoc;
20 import java.awt.event.KeyListener JavaDoc;
21 import java.awt.event.MouseAdapter JavaDoc;
22 import java.awt.event.MouseEvent JavaDoc;
23 import java.io.File JavaDoc;
24 import java.io.IOException JavaDoc;
25 import java.util.logging.Logger JavaDoc;
26
27 import javax.swing.JComponent JavaDoc;
28 import javax.swing.JFileChooser JavaDoc;
29 import javax.swing.event.ListSelectionEvent JavaDoc;
30 import javax.swing.event.ListSelectionListener JavaDoc;
31
32 import org.columba.core.desktop.ColumbaDesktop;
33 import org.columba.mail.util.MailResourceLoader;
34 import org.columba.ristretto.io.FileSource;
35 import org.columba.ristretto.message.LocalMimePart;
36 import org.columba.ristretto.message.MimeHeader;
37 import org.columba.ristretto.message.StreamableMimePart;
38
39 /**
40  * Controller for the attachment view.
41  *
42  * @author frdietz
43  * @author redsolo
44  */

45 public class AttachmentController implements KeyListener JavaDoc, ListSelectionListener JavaDoc {
46
47     private static final Logger JavaDoc LOG = Logger
48             .getLogger("org.columba.mail.gui.composer");
49
50     private AttachmentView view;
51
52     private ComposerController composerController;
53
54     private AttachmentActionListener actionListener;
55
56     private AttachmentMenu menu;
57
58     private JFileChooser JavaDoc fileChooser;
59
60     /**
61      * Creates the attachment controller.
62      *
63      * @param controller
64      * the main composer controller.
65      */

66     public AttachmentController(ComposerController controller) {
67         this.composerController = controller;
68
69         view = new AttachmentView(this);
70
71         actionListener = new AttachmentActionListener(this);
72
73         menu = new AttachmentMenu(this);
74
75         view.addPopupListener(new PopupListener());
76
77         // register Component as FocusOwner
78
// FocusManager.getInstance().registerComponent(this);
79

80         fileChooser = new JFileChooser JavaDoc();
81
82         installListener();
83
84         // view.addListSelectionListener(this);
85
}
86
87     /**
88      * Returns the action listener for this attachment controller.
89      *
90      * @return the action listener for this attachment controller.
91      */

92     public ActionListener JavaDoc getActionListener() {
93         return actionListener;
94     }
95
96     /**
97      * Installs this object as a listener to the view.
98      */

99     public void installListener() {
100         view.installListener(this);
101     }
102
103     /**
104      * Synchronizes model and view.
105      *
106      * @param b
107      * If true, model data is transferred to the view. If false, view
108      * data is saved in the model.
109      */

110     public void updateComponents(boolean b) {
111         if (b) {
112             // transfer attachments from model to view
113

114             /*
115              * clear existing attachments from the view *20031105, karlpeder*
116              * Added to avoid dupplicating attachments when switching btw. html
117              * and plain text.
118              */

119             view.clear();
120
121             // add attachments (mimeparts) from model to the view
122
for (int i = 0; i < composerController.getModel().getAttachments()
123                     .size(); i++) {
124                 StreamableMimePart p = (StreamableMimePart) composerController
125                         .getModel().getAttachments().get(i);
126                 view.add(p);
127             }
128         } else {
129             // transfer attachments from view to model
130
// clear existing attachments from the model
131
composerController.getModel().getAttachments().clear();
132
133             // add attachments (mimeparts) from view to the model
134
for (int i = 0; i < view.count(); i++) {
135                 StreamableMimePart mp = (StreamableMimePart) view.get(i);
136                 composerController.getModel().getAttachments().add(mp);
137             }
138         }
139     }
140
141     /**
142      * Removes the current selected attachments.
143      */

144     public void removeSelected() {
145         view.removeSelected();
146
147         // hide/show attachment panel
148
composerController.showAttachmentPanel();
149     }
150
151     /**
152      * Opens up a file chooser and lets the user select the files to import.
153      */

154     public void addFileAttachment() {
155         int returnValue;
156         File JavaDoc[] files;
157
158         fileChooser.setDialogTitle(MailResourceLoader.getString(
159                 "menu", "composer", "menu_message_attachFile")); //$NON-NLS-1$
160
fileChooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
161         fileChooser.setMultiSelectionEnabled(true);
162         returnValue = fileChooser.showOpenDialog(view);
163
164         if (returnValue == JFileChooser.APPROVE_OPTION) {
165             files = fileChooser.getSelectedFiles();
166
167             for (int i = 0; i < files.length; i++) {
168                 addFileAttachment(files[i]);
169             }
170         }
171
172         // show attachment panel
173
composerController.showAttachmentPanel();
174     }
175
176     /**
177      * Attaches a file to the email as an attachment. This method accepts only
178      * files and not directories.
179      *
180      * @param file
181      * the file to attach to the email.
182      */

183     public void addFileAttachment(File JavaDoc file) {
184         if (file.isFile()) {
185
186             String JavaDoc mimetype = ColumbaDesktop.getInstance().getMimeType(file);
187
188             MimeHeader header = new MimeHeader(mimetype.substring(0, mimetype
189                     .indexOf('/')), mimetype
190                     .substring(mimetype.indexOf('/') + 1));
191             header.putContentParameter("name", file.getName());
192             header.setContentDisposition("attachment");
193             header.putDispositionParameter("filename", file.getName());
194             header.setContentTransferEncoding("base64");
195
196             try {
197                 LocalMimePart mimePart = new LocalMimePart(header,
198                         new FileSource(file));
199
200                 view.add(mimePart);
201             } catch (IOException JavaDoc e) {
202                 LOG.warning("Could not add the file '" + file
203                         + "' to the attachment list, due to:" + e);
204             }
205         }
206     }
207
208     public AttachmentView getView() {
209         return view;
210     }
211
212     /** ***************** KeyListener *************************** */
213
214     /** {@inheritDoc} */
215     public void keyPressed(KeyEvent JavaDoc k) {
216         switch (k.getKeyCode()) {
217         case (KeyEvent.VK_DELETE):
218             delete();
219
220             break;
221         }
222     }
223
224     /** {@inheritDoc} */
225     public void keyReleased(KeyEvent JavaDoc k) {
226     }
227
228     /** {@inheritDoc} */
229     public void keyTyped(KeyEvent JavaDoc k) {
230     }
231
232     /** ******************** FocusOwner implementation ****************** */
233
234     /** {@inheritDoc} */
235     public void copy() {
236         // attachment controller doesn't support copy-operation
237
}
238
239     /** {@inheritDoc} */
240     public void cut() {
241         if (view.count() > 0) {
242             removeSelected();
243         }
244     }
245
246     /** {@inheritDoc} */
247     public void delete() {
248         if (view.count() > 0) {
249             removeSelected();
250         }
251     }
252
253     /** {@inheritDoc} */
254     public JComponent JavaDoc getComponent() {
255         return view;
256     }
257
258     /** {@inheritDoc} */
259     public boolean isCopyActionEnabled() {
260         // attachment controller doesn't support copy actions
261
return false;
262     }
263
264     /** {@inheritDoc} */
265     public boolean isCutActionEnabled() {
266         return (view.getSelectedValues().length > 0);
267     }
268
269     /** {@inheritDoc} */
270     public boolean isDeleteActionEnabled() {
271         return (view.getSelectedValues().length > 0);
272     }
273
274     /** {@inheritDoc} */
275     public boolean isPasteActionEnabled() {
276         // attachment controller doesn't support paste actions
277
return false;
278     }
279
280     /** {@inheritDoc} */
281     public boolean isSelectAllActionEnabled() {
282         return (view.count() > 0);
283     }
284
285     /** {@inheritDoc} */
286     public void paste() {
287         // attachment controller doesn't support paste actions
288
}
289
290     /** {@inheritDoc} */
291     public boolean isRedoActionEnabled() {
292         // attachment controller doesn't support redo operation
293
return false;
294     }
295
296     /** {@inheritDoc} */
297     public boolean isUndoActionEnabled() {
298         // attachment controller doesn't support undo operation
299
return false;
300     }
301
302     /** {@inheritDoc} */
303     public void redo() {
304         // attachment controller doesn't support redo operation
305
}
306
307     /** {@inheritDoc} */
308     public void selectAll() {
309         // view.setSelectionInterval(0, view.count() - 1);
310
}
311
312     /** {@inheritDoc} */
313     public void undo() {
314         // attachment controller doesn't support undo operation
315
}
316
317     /**
318      * ******************* ListSelectionListener interface
319      * **********************
320      */

321
322     /** {@inheritDoc} */
323     public void valueChanged(ListSelectionEvent JavaDoc arg0) {
324         // FocusManager.getInstance().updateActions();
325
}
326
327     /** ******************** MouseListener **************************** */
328     class PopupListener extends MouseAdapter JavaDoc {
329
330         /** {@inheritDoc} */
331         public void mousePressed(MouseEvent JavaDoc e) {
332             maybeShowPopup(e);
333         }
334
335         /** {@inheritDoc} */
336         public void mouseReleased(MouseEvent JavaDoc e) {
337             maybeShowPopup(e);
338         }
339
340         /**
341          * Shows the popup menu.
342          *
343          * @param e
344          * the mouse event used to get the selected attachment.
345          */

346         private void maybeShowPopup(MouseEvent JavaDoc e) {
347             if (e.isPopupTrigger()) {
348                 Object JavaDoc[] values = view.getSelectedValues();
349
350                 if (values.length == 0) {
351                     view.fixSelection(e.getX(), e.getY());
352                 }
353
354                 menu.show(e.getComponent(), e.getX(), e.getY());
355             }
356         }
357     }
358 }
359
Popular Tags