1 package SnowMailClient.view.attachments; 2 3 import SnowMailClient.utils.*; 4 import snow.utils.gui.*; 5 import snow.utils.storage.*; 6 import SnowMailClient.model.*; 7 import SnowMailClient.view.html.*; 8 import SnowMailClient.*; 9 import SnowMailClient.model.multipart.*; 10 import SnowMailClient.view.*; 11 import SnowMailClient.Language.Language; 12 13 import java.awt.*; 14 import java.awt.image.*; 15 import java.awt.event.*; 16 import javax.swing.*; 17 import javax.swing.tree.*; 18 import javax.swing.event.*; 19 import javax.swing.filechooser.*; 20 import java.io.*; 21 import javax.imageio.*; 22 import java.util.*; 23 24 30 public class MimeTreePanel extends JPanel 31 { 32 final private JButton addBT = new JButton(Language.translate("Add"), Icons.PlusIcon.shared10); 33 final private JButton removeBT = new JButton(Language.translate("Remove"),Icons.CrossIcon.shared10); 34 final private JButton saveBT = new JButton(Language.translate("Save")); 35 36 final private JTree tree = new JTree(); 37 final private JLabel statusLabel = new JLabel(); 38 39 private MailMessage message; 40 private int fontSize = UIManager.getFont("Label.font").getSize(); 41 42 private boolean hasChanged = false; 43 44 public MimeTreePanel() 45 { 46 super(new BorderLayout(0,0)); 47 48 JPanel northPanel = new EFCNBackgroundPanel(new FlowLayout(FlowLayout.LEFT,0,0), 49 EFCNBackgroundPanel.ApplyVerticalHighLight, 50 EFCNBackgroundPanel.MediumGradientStrength, 51 EFCNBackgroundPanel.PanelBackground 52 ); 53 northPanel.add(new JLabel(Language.translate("Attachments"))); 54 add(northPanel, BorderLayout.NORTH); 55 northPanel.setBorder(BorderFactory.createEmptyBorder(fontSize/2,fontSize,fontSize/2,fontSize/2)); 56 57 JScrollPane jsp = new JScrollPane(tree); 58 tree.setCellRenderer(new MimeTreeRenderer()); 59 jsp.setBorder(null); 60 add(jsp, BorderLayout.CENTER); 61 jsp.setOpaque(false); 62 jsp.getViewport().setOpaque(false); 63 tree.setOpaque(false); 64 65 JPanel controlPanel = new JPanel(); 68 add(controlPanel, BorderLayout.SOUTH); 69 controlPanel.add(addBT); 70 controlPanel.add(removeBT); 71 controlPanel.add(saveBT); 72 73 GUIUtils.setSmallDimensions(addBT); 74 GUIUtils.setSmallDimensions(removeBT); 75 GUIUtils.setSmallDimensions(saveBT); 76 77 78 addBT.addActionListener(new ActionListener() 79 { 80 public void actionPerformed(ActionEvent e) 81 { 82 addAttachmentAction(); 83 } 84 }); 85 86 removeBT.addActionListener(new ActionListener() 87 { 88 public void actionPerformed(ActionEvent e) 89 { 90 removeSelectedAttachmentsAction(); 91 } 92 }); 93 94 saveBT.addActionListener(new ActionListener() 95 { 96 public void actionPerformed(ActionEvent e) 97 { 98 if(message==null) return; 99 if(tree.getSelectionPath()==null) return; 100 101 MimePart selPart = (MimePart) tree.getSelectionPath().getLastPathComponent(); 102 103 saveMimePart(selPart); 104 } 105 }); 106 107 108 tree.addTreeSelectionListener(new TreeSelectionListener() 109 { 110 public void valueChanged(TreeSelectionEvent e) 111 { 112 int nsel = tree.getSelectionCount(); 113 saveBT.setEnabled(nsel==1); 114 removeBT.setEnabled(nsel==1); 115 } 116 }); 117 118 tree.addMouseListener(new MouseAdapter() 119 { 120 @Override public void mousePressed(MouseEvent e) 121 { 122 if(e.isPopupTrigger()) showTreePopup(e); } 124 @Override public void mouseReleased(MouseEvent e) 125 { 126 if(e.isPopupTrigger()) showTreePopup(e); } 128 129 }); 130 131 addBT.setEnabled(false); 132 saveBT.setEnabled(false); 133 removeBT.setEnabled(false); 134 135 tree.setVisible(false); 136 } 138 AttachmentBar attachmentsQuickPanel = null; 139 public void setAttachementsQuickPanel(AttachmentBar panel) 140 { 141 attachmentsQuickPanel = panel; 142 } 143 144 private void updateQuickPanel() 145 { 146 attachmentsQuickPanel.setMessage(this, message); 147 } 148 149 150 152 public void setMessage(MailMessage message) 153 { 154 if(!SwingUtilities.isEventDispatchThread()) 155 { 156 new Throwable ("Must be called from the EDT").printStackTrace(); 157 } 158 159 163 164 this.message = message; 165 166 updateQuickPanel(); 167 168 170 if(message!=null) 171 { 172 this.setIsEditable(message.isEditable()); 173 174 addBT.setEnabled(message.isEditable()); 175 tree.setVisible(true); 176 177 MimeTreeModel treeModel = message.getMimeTree(); 179 tree.setModel( treeModel ); 180 181 182 MimePart rootPart = treeModel.getRootPart(); 183 184 if(rootPart.getChildCount()>0) 185 { 186 TreePath tp = new TreePath(rootPart.getPartAt(0).getPath()); 188 tree.makeVisible(tp); 189 } 190 191 tree.updateUI(); 192 } 193 else 194 { 195 setIsEditable(false); 196 addBT.setEnabled(false); 197 tree.setVisible(false); 198 } 199 hasChanged = false; 200 } 201 202 204 public boolean getHasChanged() { return hasChanged; } 205 public void setHasChanged() { hasChanged = true; } 206 207 208 private void showTreePopup(MouseEvent e) 209 { 210 JPopupMenu popup = new JPopupMenu("Mime popup"); 211 TreePath path = tree.getPathForLocation(e.getX(),e.getY()); 212 if(path!=null) 213 { 214 final MimePart node = (MimePart) path.getLastPathComponent(); 215 String descr = node.getTextForTreeRepresentation(); 216 if(SnowMailClientApp.debug) descr+=" ["+node.getContentTYPE()+"]"; 218 popup.add(descr); 219 popup.addSeparator(); 220 221 if(node.lookIfContentIsAnImage()) 222 { 223 JMenuItem jmViewImage = new JMenuItem(Language.translate("View image")); 224 popup.add(jmViewImage); 225 jmViewImage.addActionListener(new ActionListener() 226 { 227 public void actionPerformed(ActionEvent ee) 228 { 229 new ImageViewer(SnowMailClientApp.getInstance(), node); 230 } 231 }); 232 } 233 234 if(node.getContentTYPE()==MimePart.ContentType.TEXT) 235 { 236 237 JMenuItem jmViewText = new JMenuItem(Language.translate("View")); 238 popup.add(jmViewText); 239 jmViewText.addActionListener(new ActionListener() 240 { 241 public void actionPerformed(ActionEvent ee) 242 { 243 HTMLSecureViewer hmv = new HTMLSecureViewer(SnowMailClientApp.getInstance(), 244 ""+message.getFromAddress()); 245 246 if(node.lookIfContentIsHTML()) 247 { 248 String cont =""; 249 try 250 { 251 HTMLFromMIME htmm = new HTMLFromMIME(message.getMimeTree()); 252 cont = htmm.getHTMLCodeWithLocalizedLinks(); 253 } 254 catch(Exception e) 255 { 256 cont = Language.translate("Error")+": "+e.getMessage(); 257 } 258 hmv.setHTMLSource(cont); 259 } 260 else 261 { 262 hmv.setText(node.getBodyAsText()); 263 } 264 265 hmv.setSize(400, 500); 266 hmv.setLocationRelativeTo(SnowMailClientApp.getInstance()); 267 hmv.setVisible(true); 268 } 269 }); 270 271 294 295 } 296 297 if(node.getContentTYPE()==MimePart.ContentType.MESSAGE) 298 { 299 JMenuItem jmCreateAsNewMessage = new JMenuItem(Language.translate("Create as new message")); 301 popup.add(jmCreateAsNewMessage); 302 jmCreateAsNewMessage.addActionListener(new ActionListener() 303 { 304 public void actionPerformed(ActionEvent ee) 305 { 306 MailMessage mess = new MailMessage(); 307 mess.parse(node.getBodyAsText()); 308 SnowMailClientApp.getInstance().getFolderView().getMailFolder().addMessage(mess); 309 } 310 }); 311 } 312 313 JMenuItem jmViewSendFormat = new JMenuItem(Language.translate("View as to be send")); 314 if(SnowMailClientApp.debug) 315 { 316 popup.add(jmViewSendFormat); 317 } 318 jmViewSendFormat.addActionListener(new ActionListener() 319 { 320 public void actionPerformed(ActionEvent ee) 321 { 322 try 323 { 324 byte[] cont = node.getContent_For_Sending(1,12345,67890); 325 String mess = new String (cont); 327 ViewTextDialog vtd = new ViewTextDialog("MimeSendFormatView", Language.translate("Sent content"), false); 328 vtd.setText(mess); 329 vtd.setVisible(true); 330 } 331 catch(Exception ex) 332 { 333 ex.printStackTrace(); 334 } 335 } 336 }); 337 338 JMenuItem jmSave = new JMenuItem(Language.translate("Save to file")); 340 popup.add(jmSave); 341 jmSave.addActionListener(new ActionListener() 342 { 343 public void actionPerformed(ActionEvent ee) 344 { 345 saveMimePart(node); 346 } 347 }); 348 349 popup.show(tree, e.getX(), e.getY()); 350 } 351 } 352 353 354 355 private void setIsEditable(boolean is) 356 { 357 addBT.setVisible( is); 358 saveBT.setVisible( !is); 360 } 361 362 364 public void addAttachmentAction() 365 { 366 if(message==null) return; 368 MimeTreeModel treeModel = message.getMimeTree(); 369 370 String last = SnowMailClientApp.getInstance().getProperties().getProperty( 371 "Attachment_last_file", System.getProperty("user.home")); 372 373 JFileChooser fileChooser = new JFileChooser(last); 374 fileChooser.setDialogTitle(Language.translate("Choose a file to attach to the mail message")); 375 FileChooserImageAccessoryPanel imageAccessory = new FileChooserImageAccessoryPanel(fileChooser); 376 fileChooser.setAccessory(imageAccessory); 377 int rep = fileChooser.showOpenDialog(this); 378 File file = fileChooser.getSelectedFile(); 379 if(file!=null) 380 { 381 SnowMailClientApp.getInstance().getProperties().put( 382 "Attachment_last_file", file.getAbsolutePath()); 383 384 byte[] fileContent = null; 385 386 long size = file.length(); 387 388 if(size>1e5) { 390 try 392 { 393 BufferedImage bim = ImageIO.read(file); 394 String dimstr = bim.getWidth()+" x "+bim.getHeight(); 395 396 String [] choices = new String []{ 397 "send that image", 398 "1/2 sides", 399 "1/4 sides", 400 "1/8 sides", 401 "cancel"}; 402 int rep2 = JOptionPane.showOptionDialog( 403 this, 404 Language.translate( 405 "The image %1 has size %2, (dimensions %3)." 406 +"\nSuch a big file is not recommanded for sending as mail attachment." 407 +"\nDo you want to send a reduced copy of the image instead of the original ?", 408 file.getName(), MailMessageUtils.formatSize(file.length()), dimstr 409 ), 410 Language.translate("Big image warning"), 411 JOptionPane.YES_NO_CANCEL_OPTION, 412 JOptionPane.QUESTION_MESSAGE, 413 null, choices, choices[0]); 414 415 if(rep2==4 || rep2==-1) return; double fact = 1; 417 if(rep2==1) fact = 2; 418 else if(rep2==2) fact = 4; 419 else if(rep2==3) fact = 8; 420 421 if(fact>1) 422 { 423 BufferedImage im2 = ImageUtils.convertToBufferedImage( bim.getScaledInstance( (int)(bim.getWidth()/fact), 425 (int)(bim.getHeight()/fact), bim.SCALE_SMOOTH)); 426 ByteArrayOutputStream bos = new ByteArrayOutputStream(); 427 ImageIO.write(im2, "JPEG", bos); 428 fileContent = bos.toByteArray(); 429 } 430 else 431 { 432 fileContent = FileUtils.getFileContent(file); 433 } 434 } 436 catch(Exception e) 437 { 438 int conf = JOptionPane.showConfirmDialog(this, 440 Language.translate( 441 "The file you're trying to send is of size %" 442 +"\nSending big files is time consuming for you and for the recipient(s)." 443 +"\nDo you want to continue adding this attachment ?", 444 MailMessageUtils.formatSize(size)) ); 445 if(conf!=JOptionPane.OK_OPTION) 446 { 447 return; 448 } 449 } 450 451 } 453 MimePart p = new MimePart(); 454 455 456 try 457 { 458 if(fileContent==null) 459 { 460 fileContent = FileUtils.getFileContent(file); 461 } 462 463 p.setContentAsAttachment( 464 MimeUtils.guessContentType(file.getName()), 465 MimeUtils.guessContentSubType(file.getName()), 466 fileContent, 467 file.getName()); 468 469 treeModel.addAttachment(p); 470 tree.updateUI(); hasChanged = true; 472 473 updateQuickPanel(); 474 } 475 catch(Exception e) 476 { 477 e.printStackTrace(); 478 } 479 } 480 } 481 482 public void deleteMimePart(MimePart mp) 483 { 484 MimeTreeModel treeModel = message.getMimeTree(); 485 treeModel.removePart(mp); 486 hasChanged = true; 487 updateQuickPanel(); 488 } 489 490 private void removeSelectedAttachmentsAction() 491 { 492 if(message==null) return; 493 494 MimeTreeModel treeModel = message.getMimeTree(); 495 MimePart selPart = (MimePart) tree.getSelectionPath().getLastPathComponent(); 496 497 if(!selPart.isLeaf()) 498 { 499 JOptionPane.showMessageDialog( 500 this, "Only leafs can be stored", "Cannot store Mime part", JOptionPane.ERROR_MESSAGE); 501 return; 502 } 503 504 if(selPart.isRoot()) 505 { 506 JOptionPane.showMessageDialog( 507 this, "Cannot remove the root", "Cannot remove Mime part", JOptionPane.ERROR_MESSAGE); 508 return; 509 } 510 511 treeModel.removePart(selPart); 512 hasChanged = true; 513 514 updateQuickPanel(); 515 } 516 517 public void saveMimePart(MimePart selPart) 518 { 519 520 if(!selPart.isLeaf()) 521 { 522 JOptionPane.showMessageDialog( 523 this, "Only leafs can be stored", "Cannot store Mime part", JOptionPane.ERROR_MESSAGE); 524 return; 525 } 526 527 if(selPart.getByteContent()==null) 528 { 529 JOptionPane.showMessageDialog( 530 this, "Content is null", "Cannot store Mime part", JOptionPane.ERROR_MESSAGE); 531 return; 532 } 533 534 535 String last = SnowMailClientApp.getInstance().getProperties().getProperty( 536 "Attachment_save_file", 537 System.getProperty("user.home")); 538 539 JFileChooser fileChooser = new JFileChooser(last); 540 fileChooser.setSelectedFile(new File(last, selPart.getName())); 541 int rep = fileChooser.showSaveDialog(this); 542 if(rep==JFileChooser.APPROVE_OPTION) 543 { 544 File file = fileChooser.getSelectedFile(); 545 if(file!=null) 546 { 547 SnowMailClientApp.getInstance().getProperties().put( 548 "Attachment_save_file", file.getAbsolutePath()); 549 550 try 551 { 552 byte[] cont = selPart.getByteContent(); 553 FileUtils.saveToFile(cont, file); 554 } 555 catch(Exception e) 556 { 557 e.printStackTrace(); 558 } 559 560 } 561 } 562 } 563 564 565 } | Popular Tags |