1 package org.columba.mail.gui.composer; 17 18 import java.awt.event.KeyEvent ; 19 import java.awt.event.KeyListener ; 20 import java.awt.event.MouseAdapter ; 21 22 import javax.swing.DefaultListModel ; 23 import javax.swing.ImageIcon ; 24 import javax.swing.UIManager ; 25 26 import org.columba.core.resourceloader.ImageLoader; 27 import org.columba.ristretto.message.MimeHeader; 28 import org.columba.ristretto.message.MimePart; 29 import org.columba.ristretto.message.MimeType; 30 import org.frapuccino.iconpanel.IconPanel; 31 32 39 40 public class AttachmentView extends IconPanel { 41 42 43 private AttachmentController attachmentController; 44 45 private AttachmentImageIconLoader attachmentIconLoader; 46 47 48 private DefaultListModel listModel; 49 50 57 public AttachmentView(AttachmentController controller) { 58 super(); 59 60 attachmentController = controller; 61 62 listModel = new DefaultListModel (); 63 64 setOpaque(true); 65 66 setBackground(UIManager.getColor("List.background")); 67 68 attachmentIconLoader = new AttachmentImageIconLoader(); 69 70 setDoubleClickAction(new OpenAttachmentAction(this)); 71 72 addKeyListener( new KeyListener () { 74 75 public void keyPressed(KeyEvent e) { 76 if( e.getKeyCode() == KeyEvent.VK_DELETE) { 77 removeSelected(); 78 } 79 80 } 81 82 public void keyReleased(KeyEvent e) { 83 84 } 85 86 public void keyTyped(KeyEvent e) { 87 }}); 88 89 91 } 93 94 100 public void installListener(AttachmentController c) { 101 addKeyListener(c); 102 } 103 104 110 public void addPopupListener(MouseAdapter a) { 111 addMouseListener(a); 112 } 113 114 120 public void add(MimePart mp) { 121 listModel.addElement(mp); 122 123 MimeHeader header = mp.getHeader(); 124 125 MimeType mimeType = header.getMimeType(); 126 127 ImageIcon icon = attachmentIconLoader.getImageIcon(mimeType 128 .getType(), mimeType.getSubtype()); 129 130 String text = header.getFileName(); 131 if( text==null || text.length() == 0 ) { 132 if( header.getContentDescription() != null) { 133 text = header.getContentDescription(); 134 } else { 135 text = mimeType.toString(); 136 } 137 } 138 139 140 StringBuffer tooltip = new StringBuffer (); 142 tooltip.append("<html><body>"); 143 144 if (mp.getHeader().getFileName() != null) { 145 tooltip.append(header.getFileName()); 146 tooltip.append(" - "); 147 } 148 149 tooltip.append("<i>"); 150 151 if (header.getContentDescription() != null) { 152 tooltip.append(header.getContentDescription()); 153 } else { 154 tooltip.append(mimeType.getType()); 155 tooltip.append("/"); 156 tooltip.append(mimeType.getSubtype()); 157 } 158 159 tooltip.append("</i></body></html>"); 160 add(icon, text, tooltip.toString()); 161 } 162 163 public void removeSelected() { 164 int[] indices = getSelectedIndices(); 166 167 for (int i = 0; i < indices.length; i++) { 168 listModel.removeElementAt(indices[i]); 169 170 } 171 172 super.removeSelected(); 174 } 175 176 179 public void clear() { 180 listModel.clear(); 182 183 removeAll(); 185 } 186 187 194 public MimePart get(int index) { 195 return (MimePart) listModel.get(index); 196 } 197 198 203 public int count() { 204 return listModel.size(); 205 } 206 207 215 public void fixSelection(int x, int y) { 216 221 } 222 223 226 public AttachmentController getController() { 227 return attachmentController; 228 } 229 230 237 class AttachmentImageIconLoader { 238 239 242 private AttachmentImageIconLoader() { 243 } 244 245 254 public ImageIcon getImageIcon(String contentType, String contentSubtype) { 255 StringBuffer buf = new StringBuffer (); 256 buf.append("gnome-"); 257 buf.append(contentType); 258 buf.append("-"); 259 buf.append(contentSubtype); 260 buf.append(".png"); 261 262 ImageIcon icon = ImageLoader.getMimetypeIcon(buf.toString()); 263 264 if (icon == null) { 265 icon = ImageLoader.getMimetypeIcon("gnome-" 266 + contentType + ".png"); 267 } 268 269 if (icon == null) { 270 icon = ImageLoader.getMimetypeIcon("gnome-text.png"); 271 } 272 273 return icon; 274 } 275 } 276 } | Popular Tags |