KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > suberic > pooka > gui > AttachmentPane


1 package net.suberic.pooka.gui;
2
3 import javax.swing.*;
4 import javax.mail.internet.*;
5 import javax.mail.*;
6 import java.util.*;
7 import java.io.*;
8 import java.awt.event.ActionEvent JavaDoc;
9 import java.awt.event.*;
10 import javax.activation.*;
11 import javax.swing.table.AbstractTableModel JavaDoc;
12
13 import net.suberic.util.thread.*;
14 import net.suberic.util.swing.*;
15 import net.suberic.pooka.*;
16 import java.awt.*;
17
18 /**
19  * This class basically creates a visual list of the parts of a
20  * MimeMessage.
21  */

22 public class AttachmentPane extends JPanel {
23
24   /**
25    * The AttachmentTableModel displays the MessageProxy's attachments
26    * list as a JTable.
27    */

28   class AttachmentTableModel extends AbstractTableModel JavaDoc {
29     MessageProxy msg;
30     Vector columnNames;
31
32     public AttachmentTableModel(MessageProxy newMsg) {
33       msg=newMsg;
34       columnNames = new Vector();
35       columnNames.add(Pooka.getProperty("AttachmentPane.header.name", "Filename"));
36       columnNames.add(Pooka.getProperty("AttachmentPane.header.type", "Type"));
37     }
38
39     public int getRowCount() {
40       try {
41         return msg.getAttachments().size();
42       } catch (MessagingException me) {
43         return 0;
44       }
45     }
46
47     /**
48      * As of now, we just have two columns: file name and file type.
49      * Maybe in the future we'll have an icon, too.
50      */

51     public int getColumnCount() {
52       return 2;
53     }
54
55     /**
56      * This gets the displayed value for each column in the table.
57      */

58     public Object JavaDoc getValueAt(int row, int column) {
59       java.util.List JavaDoc v = null;
60       try {
61         v = msg.getAttachments();
62
63         if (v != null && row < v.size()) {
64           if (column == 0) {
65             String JavaDoc name = (((Attachment)v.get(row)).getName());
66             if (name != null)
67               return name;
68             else
69               return Pooka.getProperty("AttachmentPane.error.FileNameUnavailable", "Unavailable");
70           } else if (column == 1) {
71
72             String JavaDoc contentType = ((Attachment)v.get(row)).getMimeType().toString();
73             if (contentType.indexOf(';') != -1)
74               contentType = contentType.substring(0, contentType.indexOf(';'));
75             return contentType;
76           }
77         }
78       } catch (MessagingException me) {
79       }
80       // if it's not a valid request, just return null.
81

82       return null;
83     }
84
85     /**
86      * A convenience method to return a particular Attachment.
87      *
88      * Returns null if there is no entry at that row.
89      */

90     public Attachment getAttachmentAtRow(int row) {
91       try {
92         if ((row < msg.getAttachments().size()) && (row >= 0))
93           return (Attachment)msg.getAttachments().get(row);
94       } catch (MessagingException me) {
95       }
96
97       return null;
98     }
99
100     public String JavaDoc getColumnName(int columnIndex) {
101       if (columnIndex >= 0 && columnIndex < columnNames.size())
102         return (String JavaDoc)columnNames.get(columnIndex);
103       else
104         return null;
105     }
106   } // AttachmentTableModel
107

108   JTable table;
109   AttachmentTableModel tableModel;
110   MessageProxy message;
111   JPanel displayPanel;
112   Action[] defaultActions;
113
114   public AttachmentPane (MessageProxy msg) {
115     super();
116
117     message=msg;
118     defaultActions = createDefaultActions();
119
120     tableModel = new AttachmentTableModel(message);
121
122     table = new JTable(tableModel);
123
124     tableModel.addTableModelListener(table);
125
126     table.addMouseListener(new MouseAdapter() {
127         public void mousePressed(MouseEvent e) {
128           if (e.getClickCount() == 2) {
129             Attachment selectedAttachment = getSelectedAttachment();
130             String JavaDoc actionCommand = Pooka.getProperty("AttachmentPane.2xClickAction", "file-open");
131             if (selectedAttachment != null) {
132               Action clickAction = getActionByName(actionCommand);
133               if (clickAction != null) {
134                 clickAction.actionPerformed(new ActionEvent JavaDoc(this, ActionEvent.ACTION_PERFORMED, actionCommand));
135
136               }
137             }
138           } else if (e.isPopupTrigger()) {
139             // see if anything is selected
140
int rowIndex = getTable().rowAtPoint(e.getPoint());
141             if (rowIndex != -1) {
142               if (! getTable().isRowSelected(rowIndex)) {
143                 getTable().setRowSelectionInterval(rowIndex, rowIndex);
144               }
145               createPopupMenu().show(getTable(), e.getX(), e.getY());
146             }
147
148           }
149
150         }
151
152         public void mouseReleased(MouseEvent e) {
153           if (e.isPopupTrigger()) {
154             // see if anything is selected
155
int rowIndex = getTable().rowAtPoint(e.getPoint());
156             if (rowIndex != -1) {
157               if (! getTable().isRowSelected(rowIndex)) {
158                 getTable().setRowSelectionInterval(rowIndex, rowIndex);
159               }
160               createPopupMenu().show(getTable(), e.getX(), e.getY());
161             }
162
163           }
164
165         }
166       });
167
168     JScrollPane jsp = new JScrollPane(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED, JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
169     jsp.getViewport().add(table);
170     table.addNotify();
171     this.add(jsp);
172
173     // the width will be resized; the only important part here is
174
// the height.
175

176     Dimension prefSize = this.getPreferredSize();
177     int defaultHeight = Integer.parseInt(Pooka.getProperty("Pooka.attachmentPanel.vsize", "100"));
178     if (prefSize.getHeight() > defaultHeight) {
179       this.setPreferredSize(new Dimension((int)prefSize.getWidth(), defaultHeight));
180     }
181     Dimension jspPrefSize = jsp.getPreferredSize();
182     if (jspPrefSize.getHeight() > defaultHeight - 15) {
183       jsp.setPreferredSize(new Dimension((int)prefSize.getWidth(), defaultHeight - 15));
184     }
185
186     this.addFocusListener(new FocusAdapter() {
187         public void focusGained(FocusEvent e) {
188           if (getTable() != null) {
189             if (getSelectedAttachment() == null) {
190               getTable().setRowSelectionInterval(0,0);
191             }
192             getTable().requestFocusInWindow();
193           }
194         }
195       });
196
197     createKeyBindings();
198
199     table.setTransferHandler(new net.suberic.pooka.gui.dnd.AttachmentTransferHandler());
200     table.setDragEnabled(true);
201   }
202
203   /**
204    * Returns the display panel for the AttachmentPane. This will normally
205    * contain just the AttachmentTable.
206    */

207   public JPanel getDisplayPanel() {
208     return displayPanel;
209   }
210
211   /**
212    * getSelectedAttachment() will return the selected Attachment.
213    */

214   public Attachment getSelectedAttachment() {
215     return getTableModel().getAttachmentAtRow(getTable().getSelectedRow());
216   }
217
218   /**
219    * This opens up the selected Attachment using the default handler
220    * for the Attachment's Mime type.
221    */

222   public void openSelectedAttachment() {
223     Attachment attachment = getSelectedAttachment();
224     AttachmentHandler ah = new AttachmentHandler(message);
225     ah.openAttachment(attachment);
226   }
227
228   /**
229    * This opens the Attachment with the program of the user's choice.
230    */

231   public void openWith() {
232     Attachment attachment = getSelectedAttachment();
233     AttachmentHandler ah = new AttachmentHandler(message);
234     ah.openWith(attachment);
235   }
236
237   /**
238    * This opens up a JFileChooser to let the user choose under what
239    * name and where the selected Attachment should be saved. It then
240    * calls saveFileAs() to save the file.
241    */

242   public void saveAttachment() {
243     Attachment attachment = getSelectedAttachment();
244     AttachmentHandler ah = new AttachmentHandler(message);
245     ah.saveAttachment(attachment, this);
246   }
247
248   /**
249    * This opens up a JFileChooser to let the user choose under what
250    * name and where the selected Attachment should be saved. It then
251    * calls saveFileAs() to save the file.
252    */

253   public void saveAllAttachments() {
254     AttachmentHandler ah = new AttachmentHandler(message);
255     ah.saveAllAttachments(this);
256   }
257
258   /**
259    * This removes the Attachment from the message.
260    */

261   public void removeAttachment() {
262     int selectedIndex = getTable().getSelectedRow();
263     Attachment attachmentToRemove = getSelectedAttachment();
264     ((NewMessageProxy)message).detachFile(attachmentToRemove);
265   }
266
267   public AttachmentTableModel getTableModel() {
268     return tableModel;
269   }
270
271   /**
272    * Gets the table with all of the attachment entries.
273    */

274   public JTable getTable() {
275     return table;
276   }
277
278   /**
279    * Creates the popup menu for this component.
280    */

281   protected JPopupMenu createPopupMenu() {
282     net.suberic.util.gui.ConfigurablePopupMenu popupMenu = new net.suberic.util.gui.ConfigurablePopupMenu();
283     String JavaDoc key;
284     if (message instanceof NewMessageProxy)
285       key = "AttachmentPane.NewMsgActions";
286     else
287       key = "AttachmentPane.Actions";
288     popupMenu.configureComponent(key, Pooka.getResources());
289     popupMenu.setActive(getActions());
290     MessageUI mui = ((MessageProxy)message).getMessageUI();
291     if (mui instanceof net.suberic.util.swing.ThemeSupporter) {
292       try {
293         Pooka.getUIFactory().getPookaThemeManager().updateUI((net.suberic.util.swing.ThemeSupporter) mui, popupMenu, true);
294       } catch (Exception JavaDoc etwo) {
295         if (Pooka.isDebug())
296           System.out.println("error setting theme: " + etwo);
297       }
298     }
299     return popupMenu;
300   }
301
302   /**
303    * Creates the ConfigurableKeyBindings for this component.
304    */

305   protected void createKeyBindings() {
306     String JavaDoc key;
307     if (message instanceof NewMessageProxy)
308       key = "AttachmentPane.newMsgKeyBindings";
309     else
310       key = "AttachmentPane.keyBindings";
311
312     net.suberic.util.gui.ConfigurableKeyBinding keyBindings = new net.suberic.util.gui.ConfigurableKeyBinding(getTable(), key, Pooka.getResources());
313     keyBindings.setActive(getActions());
314
315   }
316
317
318   /**
319    * Returns the given Action.
320    */

321   public Action getActionByName(String JavaDoc actionName) {
322     Action[] actionList = getActions();
323     for (int i = 0; i < actionList.length; i++) {
324       if (actionName.equals((String JavaDoc)actionList[i].getValue(Action.NAME))) {
325         return actionList[i];
326       }
327     }
328     return null;
329
330   }
331
332   /**
333    * Creates the default actions for this pane.
334    */

335   public Action[] createDefaultActions() {
336     if (message instanceof NewMessageProxy)
337       return new Action[] {
338         new RemoveAction()
339       };
340     else {
341       ActionThread storeThread = message.getFolderInfo().getParentStore().getStoreThread();
342       return new Action[] {
343         new ActionWrapper(new OpenAction(), storeThread),
344         new ActionWrapper(new OpenWithAction(), storeThread),
345         new ActionWrapper(new SaveAsAction(), storeThread),
346         new ActionWrapper(new SaveAllAction(), storeThread)
347       };
348     }
349   }
350
351   public Action[] getActions() {
352     return getDefaultActions();
353   }
354
355   public Action[] getDefaultActions() {
356     return defaultActions;
357   }
358
359   public MessageUI getMessageUI() {
360     return message.getMessageUI();
361   }
362
363   public MessageProxy getMessageProxy() {
364     return message;
365   }
366
367   /**
368    * Shows an error message, either on the MessageUI if there is one, or
369    * if not, on the main Pooka frame.
370    */

371   public void showError(String JavaDoc message, Exception JavaDoc ioe) {
372     MessageUI mui = getMessageUI();
373     if (mui != null) {
374       mui.showError(message,ioe);
375     } else {
376       Pooka.getUIFactory().showError(message,ioe);
377     }
378   }
379
380   /**
381    * Shows an error message, either on the MessageUI if there is one, or
382    * if not, on the main Pooka frame.
383    */

384   public void showError(String JavaDoc message, String JavaDoc title, Exception JavaDoc ioe) {
385     MessageUI mui = getMessageUI();
386     if (mui != null) {
387       mui.showError(message, title, ioe);
388     } else {
389       Pooka.getUIFactory().showError(message, title, ioe);
390     }
391   }
392
393   //------------------------------------//
394

395   class OpenAction extends AbstractAction {
396     OpenAction() {
397       super("file-open");
398     }
399
400     public void actionPerformed(ActionEvent JavaDoc e) {
401       openSelectedAttachment();
402     }
403   }
404
405   class OpenWithAction extends AbstractAction {
406     OpenWithAction() {
407       super("file-open-with");
408     }
409
410     public void actionPerformed(ActionEvent JavaDoc e) {
411       openWith();
412     }
413   }
414
415   class SaveAsAction extends AbstractAction {
416     SaveAsAction() {
417       super("file-save-as");
418     }
419
420     public void actionPerformed(ActionEvent JavaDoc e) {
421       saveAttachment();
422     }
423   }
424
425   class SaveAllAction extends AbstractAction {
426     SaveAllAction() {
427       super("file-save-all");
428     }
429
430     public void actionPerformed(ActionEvent JavaDoc e) {
431       saveAllAttachments();
432     }
433   }
434
435   class RemoveAction extends AbstractAction {
436     RemoveAction() {
437       super("file-remove");
438     }
439
440     public void actionPerformed(ActionEvent JavaDoc e) {
441       removeAttachment();
442     }
443   }
444
445 }
446
447
448
Popular Tags