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 ; 9 import java.awt.event.*; 10 import javax.activation.*; 11 import javax.swing.table.AbstractTableModel ; 12 13 import net.suberic.util.thread.*; 14 import net.suberic.util.swing.*; 15 import net.suberic.pooka.*; 16 import java.awt.*; 17 18 22 public class AttachmentPane extends JPanel { 23 24 28 class AttachmentTableModel extends AbstractTableModel { 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 51 public int getColumnCount() { 52 return 2; 53 } 54 55 58 public Object getValueAt(int row, int column) { 59 java.util.List v = null; 60 try { 61 v = msg.getAttachments(); 62 63 if (v != null && row < v.size()) { 64 if (column == 0) { 65 String 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 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 82 return null; 83 } 84 85 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 getColumnName(int columnIndex) { 101 if (columnIndex >= 0 && columnIndex < columnNames.size()) 102 return (String )columnNames.get(columnIndex); 103 else 104 return null; 105 } 106 } 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 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 (this, ActionEvent.ACTION_PERFORMED, actionCommand)); 135 136 } 137 } 138 } else if (e.isPopupTrigger()) { 139 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 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 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 207 public JPanel getDisplayPanel() { 208 return displayPanel; 209 } 210 211 214 public Attachment getSelectedAttachment() { 215 return getTableModel().getAttachmentAtRow(getTable().getSelectedRow()); 216 } 217 218 222 public void openSelectedAttachment() { 223 Attachment attachment = getSelectedAttachment(); 224 AttachmentHandler ah = new AttachmentHandler(message); 225 ah.openAttachment(attachment); 226 } 227 228 231 public void openWith() { 232 Attachment attachment = getSelectedAttachment(); 233 AttachmentHandler ah = new AttachmentHandler(message); 234 ah.openWith(attachment); 235 } 236 237 242 public void saveAttachment() { 243 Attachment attachment = getSelectedAttachment(); 244 AttachmentHandler ah = new AttachmentHandler(message); 245 ah.saveAttachment(attachment, this); 246 } 247 248 253 public void saveAllAttachments() { 254 AttachmentHandler ah = new AttachmentHandler(message); 255 ah.saveAllAttachments(this); 256 } 257 258 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 274 public JTable getTable() { 275 return table; 276 } 277 278 281 protected JPopupMenu createPopupMenu() { 282 net.suberic.util.gui.ConfigurablePopupMenu popupMenu = new net.suberic.util.gui.ConfigurablePopupMenu(); 283 String 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 etwo) { 295 if (Pooka.isDebug()) 296 System.out.println("error setting theme: " + etwo); 297 } 298 } 299 return popupMenu; 300 } 301 302 305 protected void createKeyBindings() { 306 String 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 321 public Action getActionByName(String actionName) { 322 Action[] actionList = getActions(); 323 for (int i = 0; i < actionList.length; i++) { 324 if (actionName.equals((String )actionList[i].getValue(Action.NAME))) { 325 return actionList[i]; 326 } 327 } 328 return null; 329 330 } 331 332 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 371 public void showError(String message, Exception 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 384 public void showError(String message, String title, Exception 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 395 class OpenAction extends AbstractAction { 396 OpenAction() { 397 super("file-open"); 398 } 399 400 public void actionPerformed(ActionEvent 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 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 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 e) { 431 saveAllAttachments(); 432 } 433 } 434 435 class RemoveAction extends AbstractAction { 436 RemoveAction() { 437 super("file-remove"); 438 } 439 440 public void actionPerformed(ActionEvent e) { 441 removeAttachment(); 442 } 443 } 444 445 } 446 447 448 | Popular Tags |