KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > columba > mail > gui > config > filter > ActionList


1 // The contents of this file are subject to the Mozilla Public License Version
2
// 1.1
3
//(the "License"); you may not use this file except in compliance with the
4
//License. You may obtain a copy of the License at http://www.mozilla.org/MPL/
5
//
6
//Software distributed under the License is distributed on an "AS IS" basis,
7
//WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
8
//for the specific language governing rights and
9
//limitations under the License.
10
//
11
//The Original Code is "The Columba Project"
12
//
13
//The Initial Developers of the Original Code are Frederik Dietz and Timo
14
// Stich.
15
//Portions created by Frederik Dietz and Timo Stich are Copyright (C) 2003.
16
//
17
//All Rights Reserved.
18

19 package org.columba.mail.gui.config.filter;
20
21 import java.awt.BorderLayout JavaDoc;
22 import java.awt.Component JavaDoc;
23 import java.awt.Dimension JavaDoc;
24 import java.awt.GridBagConstraints JavaDoc;
25 import java.awt.GridBagLayout JavaDoc;
26 import java.awt.GridLayout JavaDoc;
27 import java.awt.Insets JavaDoc;
28 import java.awt.event.ActionEvent JavaDoc;
29 import java.awt.event.ActionListener JavaDoc;
30 import java.awt.event.ItemEvent JavaDoc;
31 import java.awt.event.ItemListener JavaDoc;
32 import java.util.Iterator JavaDoc;
33 import java.util.List JavaDoc;
34 import java.util.Vector JavaDoc;
35
36 import javax.swing.Box JavaDoc;
37 import javax.swing.JButton JavaDoc;
38 import javax.swing.JFrame JavaDoc;
39 import javax.swing.JPanel JavaDoc;
40 import javax.swing.JScrollPane JavaDoc;
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 JavaDoc implements ActionListener JavaDoc, ItemListener JavaDoc {
63     private IFilter filter;
64
65     private List JavaDoc list;
66
67     private JPanel JavaDoc panel;
68
69     protected GridBagLayout JavaDoc gridbag = new GridBagLayout JavaDoc();
70
71     protected GridBagConstraints JavaDoc c = new GridBagConstraints JavaDoc();
72
73     protected IFrameMediator mediator;
74
75     public ActionList(IFrameMediator mediator, IFilter filter, JFrame JavaDoc frame) {
76         super(new BorderLayout JavaDoc());
77         this.mediator = mediator;
78         this.filter = filter;
79
80         list = new Vector JavaDoc();
81         panel = new JPanel JavaDoc();
82
83         JScrollPane JavaDoc scrollPane = new JScrollPane JavaDoc(panel);
84
85         scrollPane.setPreferredSize(new Dimension JavaDoc(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 JavaDoc it = list.iterator(); it.hasNext();) {
97                 DefaultActionRow row = (DefaultActionRow) it.next();
98                 row.updateComponents(false);
99             }
100
101             // for (int i = 0; i < list.size(); i++) {
102
// DefaultActionRow row = (DefaultActionRow) list.get(i);
103
// row.updateComponents(false);
104
// }
105
}
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             // int type = action.getActionInt();
166
String JavaDoc name = action.getAction();
167             DefaultActionRow row = null;
168
169             Object JavaDoc[] args = { mediator, this, action };
170
171             try {
172                 IExtension extension = pluginHandler.getExtension(name);
173                 if ( extension != null) {
174                 String JavaDoc ui = extension.getMetadata().getAttribute("ui");
175                 IExtension uiExtension = pluginUIHandler.getExtension(ui);
176                 
177                 row = (DefaultActionRow) uiExtension.instanciateExtension(args);
178                 }
179             } catch (Exception JavaDoc ex) {
180                 ex.printStackTrace();
181
182                 // this probably means that the configuration
183
// is wrong
184
// -> change this to a sane default value
185
action.setAction("Mark Message");
186                 ((MailFilterAction) action).setMarkVariant("read");
187                 row = null;
188             }
189
190             if (row == null) {
191                 // maybe the plugin wasn't loaded correctly
192
// -> use default
193
// row = new MarkActionRow(this,action);
194
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 JavaDoc addButton = new JButton JavaDoc(ImageLoader
209                         .getIcon(IconKeys.LIST_ADD));
210                 addButton.setActionCommand("ADD");
211                 addButton.setMargin(new Insets JavaDoc(0, 0, 0, 0));
212                 addButton.addActionListener(new ActionListener JavaDoc() {
213                     public void actionPerformed(ActionEvent JavaDoc e) {
214                         add();
215                     }
216                 });
217
218                 JButton JavaDoc removeButton = new JButton JavaDoc(ImageLoader
219                         .getIcon(IconKeys.LIST_REMOVE));
220                 removeButton.setActionCommand(Integer.toString(i));
221                 removeButton.setMargin(new Insets JavaDoc(0, 0, 0, 0));
222
223                 final int index = i;
224                 removeButton.addActionListener(new ActionListener JavaDoc() {
225                     public void actionPerformed(ActionEvent JavaDoc e) {
226                         remove(index);
227                     }
228                 });
229
230                 /*
231                  * c.gridx = GridBagConstraints.REMAINDER; c.anchor =
232                  * GridBagConstraints.NORTHEAST; gridbag.setConstraints(
233                  * removeButton, c ); panel.add( removeButton );
234                  */

235                 JPanel JavaDoc buttonPanel = new JPanel JavaDoc();
236                 buttonPanel.setLayout(new GridLayout JavaDoc(0, 2, 2, 2));
237                 buttonPanel.add(removeButton);
238                 buttonPanel.add(addButton);
239
240                 c.insets = new Insets JavaDoc(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 JavaDoc box = Box.createVerticalGlue();
251         gridbag.setConstraints(box, c);
252         panel.add(box);
253
254         validate();
255         repaint();
256     }
257
258     public void actionPerformed(ActionEvent JavaDoc e) {
259         updateComponents(false);
260         update();
261     }
262
263     /**
264      * @see java.awt.event.ItemListener#itemStateChanged(java.awt.event.ItemEvent)
265      */

266     public void itemStateChanged(ItemEvent JavaDoc arg0) {
267         updateComponents(false);
268         update();
269     }
270 }
Popular Tags