1 19 package org.columba.mail.gui.config.filter; 20 21 import java.awt.BorderLayout ; 22 import java.awt.Component ; 23 import java.awt.Dimension ; 24 import java.awt.GridBagConstraints ; 25 import java.awt.GridBagLayout ; 26 import java.awt.GridLayout ; 27 import java.awt.Insets ; 28 import java.awt.event.ActionEvent ; 29 import java.awt.event.ActionListener ; 30 import java.awt.event.ItemEvent ; 31 import java.awt.event.ItemListener ; 32 import java.util.Iterator ; 33 import java.util.List ; 34 import java.util.Vector ; 35 36 import javax.swing.Box ; 37 import javax.swing.JButton ; 38 import javax.swing.JFrame ; 39 import javax.swing.JPanel ; 40 import javax.swing.JScrollPane ; 41 42 import org.columba.api.gui.frame.IFrameMediator; 43 import org.columba.api.plugin.IExtension; 44 import org.columba.api.plugin.IExtensionHandler; 45 import org.columba.api.plugin.PluginHandlerNotFoundException; 46 import org.columba.core.filter.Filter; 47 import org.columba.core.filter.FilterAction; 48 import org.columba.core.filter.FilterActionList; 49 import org.columba.core.filter.IFilter; 50 import org.columba.core.filter.IFilterAction; 51 import org.columba.core.filter.IFilterActionList; 52 import org.columba.core.logging.Logging; 53 import org.columba.core.plugin.PluginManager; 54 import org.columba.core.resourceloader.IconKeys; 55 import org.columba.core.resourceloader.ImageLoader; 56 import org.columba.mail.filter.MailFilterAction; 57 import org.columba.mail.gui.config.filter.plugins.DefaultActionRow; 58 import org.columba.mail.gui.config.filter.plugins.MarkActionRow; 59 import org.columba.mail.plugin.IExtensionHandlerKeys; 60 61 62 public class ActionList extends JPanel implements ActionListener , ItemListener { 63 private IFilter filter; 64 65 private List list; 66 67 private JPanel panel; 68 69 protected GridBagLayout gridbag = new GridBagLayout (); 70 71 protected GridBagConstraints c = new GridBagConstraints (); 72 73 protected IFrameMediator mediator; 74 75 public ActionList(IFrameMediator mediator, IFilter filter, JFrame frame) { 76 super(new BorderLayout ()); 77 this.mediator = mediator; 78 this.filter = filter; 79 80 list = new Vector (); 81 panel = new JPanel (); 82 83 JScrollPane scrollPane = new JScrollPane (panel); 84 85 scrollPane.setPreferredSize(new Dimension (500, 50)); 86 add(scrollPane, BorderLayout.CENTER); 87 88 update(); 89 } 90 91 public void initComponents() { 92 } 93 94 public void updateComponents(boolean b) { 95 if (!b) { 96 for (Iterator it = list.iterator(); it.hasNext();) { 97 DefaultActionRow row = (DefaultActionRow) it.next(); 98 row.updateComponents(false); 99 } 100 101 } 106 } 107 108 public void add() { 109 boolean allowed = true; 110 111 IFilterActionList actionList = filter.getFilterActionList(); 112 for (int i = 0; i < actionList.getChildCount(); i++) { 113 IFilterAction action = actionList.get(i); 114 115 116 if ((action.equals("move")) || (action.equals("delete"))) { 117 allowed = false; 118 break; 119 } 120 } 121 122 if (allowed) { 123 updateComponents(false); 124 actionList.addEmptyAction(); 125 } 126 127 update(); 128 } 129 130 public void remove(int i) { 131 IFilterActionList actionList = filter.getFilterActionList(); 132 133 if (actionList.getChildCount() > 1) { 134 updateComponents(false); 135 136 actionList.remove(i); 137 update(); 138 } 139 } 140 141 public void update() { 142 panel.removeAll(); 143 list.clear(); 144 145 panel.setLayout(gridbag); 146 147 IExtensionHandler pluginHandler = null; 148 IExtensionHandler pluginUIHandler = null; 149 try { 150 pluginHandler = PluginManager 151 .getInstance().getExtensionHandler(IExtensionHandlerKeys.ORG_COLUMBA_MAIL_FILTERACTION); 152 pluginUIHandler =PluginManager 153 .getInstance().getExtensionHandler(IExtensionHandlerKeys.ORG_COLUMBA_MAIL_FILTERACTIONUI); 154 } catch (PluginHandlerNotFoundException ex) { 155 if (Logging.DEBUG) { 156 ex.printStackTrace(); 157 } 158 } 159 160 IFilterActionList actionList = filter.getFilterActionList(); 161 162 for (int i = 0; i < actionList.getChildCount(); i++) { 163 IFilterAction action = actionList.get(i); 164 165 String name = action.getAction(); 167 DefaultActionRow row = null; 168 169 Object [] args = { mediator, this, action }; 170 171 try { 172 IExtension extension = pluginHandler.getExtension(name); 173 if ( extension != null) { 174 String ui = extension.getMetadata().getAttribute("ui"); 175 IExtension uiExtension = pluginUIHandler.getExtension(ui); 176 177 row = (DefaultActionRow) uiExtension.instanciateExtension(args); 178 } 179 } catch (Exception ex) { 180 ex.printStackTrace(); 181 182 action.setAction("Mark Message"); 186 ((MailFilterAction) action).setMarkVariant("read"); 187 row = null; 188 } 189 190 if (row == null) { 191 row = new MarkActionRow(mediator, this, action); 195 } 196 197 if (row != null) { 198 c.fill = GridBagConstraints.NONE; 199 c.gridx = GridBagConstraints.RELATIVE; 200 c.gridy = i; 201 c.weightx = 1.0; 202 c.anchor = GridBagConstraints.NORTHWEST; 203 gridbag.setConstraints(row.getContentPane(), c); 204 205 list.add(row); 206 panel.add(row.getContentPane()); 207 208 JButton addButton = new JButton (ImageLoader 209 .getIcon(IconKeys.LIST_ADD)); 210 addButton.setActionCommand("ADD"); 211 addButton.setMargin(new Insets (0, 0, 0, 0)); 212 addButton.addActionListener(new ActionListener () { 213 public void actionPerformed(ActionEvent e) { 214 add(); 215 } 216 }); 217 218 JButton removeButton = new JButton (ImageLoader 219 .getIcon(IconKeys.LIST_REMOVE)); 220 removeButton.setActionCommand(Integer.toString(i)); 221 removeButton.setMargin(new Insets (0, 0, 0, 0)); 222 223 final int index = i; 224 removeButton.addActionListener(new ActionListener () { 225 public void actionPerformed(ActionEvent e) { 226 remove(index); 227 } 228 }); 229 230 235 JPanel buttonPanel = new JPanel (); 236 buttonPanel.setLayout(new GridLayout (0, 2, 2, 2)); 237 buttonPanel.add(removeButton); 238 buttonPanel.add(addButton); 239 240 c.insets = new Insets (2, 2, 2, 2); 241 c.gridx = GridBagConstraints.REMAINDER; 242 c.anchor = GridBagConstraints.NORTHEAST; 243 gridbag.setConstraints(buttonPanel, c); 244 panel.add(buttonPanel); 245 } 246 } 247 248 c.weighty = 1.0; 249 250 Component box = Box.createVerticalGlue(); 251 gridbag.setConstraints(box, c); 252 panel.add(box); 253 254 validate(); 255 repaint(); 256 } 257 258 public void actionPerformed(ActionEvent e) { 259 updateComponents(false); 260 update(); 261 } 262 263 266 public void itemStateChanged(ItemEvent arg0) { 267 updateComponents(false); 268 update(); 269 } 270 } | Popular Tags |