1 package org.columba.mail.gui.composer; 17 18 import java.awt.event.ActionListener ; 19 import java.awt.event.KeyEvent ; 20 import java.awt.event.KeyListener ; 21 import java.awt.event.MouseAdapter ; 22 import java.awt.event.MouseEvent ; 23 import java.io.File ; 24 import java.io.IOException ; 25 import java.util.logging.Logger ; 26 27 import javax.swing.JComponent ; 28 import javax.swing.JFileChooser ; 29 import javax.swing.event.ListSelectionEvent ; 30 import javax.swing.event.ListSelectionListener ; 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 45 public class AttachmentController implements KeyListener , ListSelectionListener { 46 47 private static final Logger 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 fileChooser; 59 60 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 80 fileChooser = new JFileChooser (); 81 82 installListener(); 83 84 } 86 87 92 public ActionListener getActionListener() { 93 return actionListener; 94 } 95 96 99 public void installListener() { 100 view.installListener(this); 101 } 102 103 110 public void updateComponents(boolean b) { 111 if (b) { 112 114 119 view.clear(); 120 121 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 composerController.getModel().getAttachments().clear(); 132 133 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 144 public void removeSelected() { 145 view.removeSelected(); 146 147 composerController.showAttachmentPanel(); 149 } 150 151 154 public void addFileAttachment() { 155 int returnValue; 156 File [] files; 157 158 fileChooser.setDialogTitle(MailResourceLoader.getString( 159 "menu", "composer", "menu_message_attachFile")); 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 composerController.showAttachmentPanel(); 174 } 175 176 183 public void addFileAttachment(File file) { 184 if (file.isFile()) { 185 186 String 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 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 213 214 215 public void keyPressed(KeyEvent k) { 216 switch (k.getKeyCode()) { 217 case (KeyEvent.VK_DELETE): 218 delete(); 219 220 break; 221 } 222 } 223 224 225 public void keyReleased(KeyEvent k) { 226 } 227 228 229 public void keyTyped(KeyEvent k) { 230 } 231 232 233 234 235 public void copy() { 236 } 238 239 240 public void cut() { 241 if (view.count() > 0) { 242 removeSelected(); 243 } 244 } 245 246 247 public void delete() { 248 if (view.count() > 0) { 249 removeSelected(); 250 } 251 } 252 253 254 public JComponent getComponent() { 255 return view; 256 } 257 258 259 public boolean isCopyActionEnabled() { 260 return false; 262 } 263 264 265 public boolean isCutActionEnabled() { 266 return (view.getSelectedValues().length > 0); 267 } 268 269 270 public boolean isDeleteActionEnabled() { 271 return (view.getSelectedValues().length > 0); 272 } 273 274 275 public boolean isPasteActionEnabled() { 276 return false; 278 } 279 280 281 public boolean isSelectAllActionEnabled() { 282 return (view.count() > 0); 283 } 284 285 286 public void paste() { 287 } 289 290 291 public boolean isRedoActionEnabled() { 292 return false; 294 } 295 296 297 public boolean isUndoActionEnabled() { 298 return false; 300 } 301 302 303 public void redo() { 304 } 306 307 308 public void selectAll() { 309 } 311 312 313 public void undo() { 314 } 316 317 321 322 323 public void valueChanged(ListSelectionEvent arg0) { 324 } 326 327 328 class PopupListener extends MouseAdapter { 329 330 331 public void mousePressed(MouseEvent e) { 332 maybeShowPopup(e); 333 } 334 335 336 public void mouseReleased(MouseEvent e) { 337 maybeShowPopup(e); 338 } 339 340 346 private void maybeShowPopup(MouseEvent e) { 347 if (e.isPopupTrigger()) { 348 Object [] 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 |