KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > mullassery > act > gui > TaskAction


1 /*
2  * The Apache Software License, Version 1.1
3  *
4  * Copyright (c) 2000-2002 The Apache Software Foundation. All rights
5  * reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  *
11  * 1. Redistributions of source code must retain the above copyright
12  * notice, this list of conditions and the following disclaimer.
13  *
14  * 2. Redistributions in binary form must reproduce the above copyright
15  * notice, this list of conditions and the following disclaimer in
16  * the documentation and/or other materials provided with the
17  * distribution.
18  *
19  * 3. The end-user documentation included with the redistribution, if
20  * any, must include the following acknowlegement:
21  * "This product includes software developed by the
22  * Apache Software Foundation (http://www.apache.org/)."
23  * Alternately, this acknowlegement may appear in the software itself,
24  * if and wherever such third-party acknowlegements normally appear.
25  *
26  * 4. The names "The Jakarta Project", "Ant", and "Apache Software
27  * Foundation" must not be used to endorse or promote products derived
28  * from this software without prior written permission. For written
29  * permission, please contact apache@apache.org.
30  *
31  * 5. Products derived from this software may not be called "Apache"
32  * nor may "Apache" appear in their names without prior written
33  * permission of the Apache Group.
34  *
35  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
36  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
37  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
38  * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
39  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
40  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
41  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
42  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
43  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
44  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
45  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
46  * SUCH DAMAGE.
47  * ====================================================================
48  *
49  * This software consists of voluntary contributions made by many
50  * individuals on behalf of the Apache Software Foundation. For more
51  * information on the Apache Software Foundation, please see
52  * <http://www.apache.org/>.
53  */

54
55 package com.mullassery.act.gui;
56
57 import java.awt.BorderLayout JavaDoc;
58 import java.awt.FlowLayout JavaDoc;
59 import java.awt.GridLayout JavaDoc;
60 import java.awt.event.ActionEvent JavaDoc;
61 import java.awt.event.ActionListener JavaDoc;
62 import java.util.HashMap JavaDoc;
63
64 import javax.swing.AbstractAction JavaDoc;
65 import javax.swing.JButton JavaDoc;
66 import javax.swing.JDialog JavaDoc;
67 import javax.swing.JPanel JavaDoc;
68
69 import org.w3c.dom.Element JavaDoc;
70 import org.w3c.dom.Node JavaDoc;
71 import org.w3c.dom.NodeList JavaDoc;
72
73 import com.mullassery.act.gui.components.TBooleanField;
74 import com.mullassery.act.gui.components.TComboBox;
75 import com.mullassery.act.gui.components.TLabel;
76 import com.mullassery.act.gui.components.TTextField;
77 import com.mullassery.act.util.GUIUtil;
78
79 /**
80  *
81  * @author Abey Mullassery
82  *
83  */

84 public class TaskAction extends AbstractAction JavaDoc {
85     public static final int ADD_TASK = 0;
86
87     public static final String JavaDoc ADD_TASK_LABEL = "Add new Task";
88     public static final int DELETE_TASK = 2;
89     public static final String JavaDoc DELETE_TASK_LABEL = "Delete a Task";
90     public static final int EDIT_TASK = 1;
91     public static final String JavaDoc EDIT_TASK_LABEL = "Edit a Task";
92
93     ACT act;
94     Element JavaDoc category;
95     private int type;
96
97     TaskAction(ACT act, Element JavaDoc category, String JavaDoc display, int type) {
98         super(display);
99         this.act = act;
100         this.category = category;
101         this.type = type;
102     }
103
104     public void actionPerformed(ActionEvent JavaDoc arg0) {
105         if (type == ADD_TASK)
106             new AddTaskDialog().show();
107         else
108             new SelectTaskDialog();
109     }
110
111     private class AddTaskDialog extends JDialog JavaDoc {
112         AddTaskDialog() {
113             this(null);
114         }
115
116         AddTaskDialog(Element JavaDoc val) {
117             super(act, "Enter details of the task");
118             init(val);
119         }
120
121         private void init(final Element JavaDoc val) {
122             final boolean edit = (val != null);
123             final JPanel JavaDoc jp = new JPanel JavaDoc(new GridLayout JavaDoc(5, 2, 2, 1));
124             jp.add(new TLabel("Display Name"));
125             final TTextField dt = new TTextField();
126             if (edit)
127                 dt.setText(val.getAttribute(GUIUtil.TASK_DISPLAY));
128             jp.add(dt);
129             jp.add(new TLabel("Task Name"));
130             final TTextField t = new TTextField();
131             if (edit)
132                 t.setText(val.getAttribute(GUIUtil.TASK_NAME));
133             jp.add(t);
134             jp.add(new TLabel("Task class"));
135             final TTextField tc = new TTextField();
136             if (edit)
137                 tc.setText(val.getAttribute(GUIUtil.TASK_CLASS));
138             jp.add(tc);
139             jp.add(new TLabel("Task description"));
140             final TTextField td = new TTextField();
141             if (edit)
142                 td.setText(val.getAttribute(GUIUtil.TASK_DESCRIPTION));
143             jp.add(td);
144             jp.add(new TLabel("Show/ Hide"));
145             final TBooleanField tv = new TBooleanField();
146             if (edit
147                 && "false".equalsIgnoreCase(
148                     val.getAttribute(GUIUtil.TASK_VISIBLE)))
149                 tv.setText("false");
150             else
151                 tv.setText("true");
152             jp.add(tv);
153             getContentPane().add(jp);
154
155             final JPanel JavaDoc bb = new JPanel JavaDoc(new FlowLayout JavaDoc(FlowLayout.CENTER));
156             final JButton JavaDoc cancel = new JButton JavaDoc("Cancel");
157             cancel.addActionListener(new ActionListener JavaDoc() {
158                 public void actionPerformed(ActionEvent JavaDoc evt) {
159                     AddTaskDialog.this.dispose();
160                 }
161             });
162             bb.add(cancel);
163             final JButton JavaDoc ok = new JButton JavaDoc("OK");
164             ok.addActionListener(
165                 new EditActionListener(this, tc, td, tv, t, dt, val));
166             bb.add(ok);
167             getContentPane().add(bb, BorderLayout.SOUTH);
168
169             pack();
170             setLocationRelativeTo(act);
171             show();
172         }
173     }
174
175     private final class EditActionListener implements ActionListener JavaDoc {
176         private final TTextField dt;
177         private final JDialog JavaDoc par;
178         private final TTextField t;
179         private final TTextField tc;
180         private final TTextField td;
181         private final TBooleanField tv;
182         private final Element JavaDoc val;
183         private EditActionListener(
184             JDialog JavaDoc par,
185             TTextField tc,
186             TTextField td,
187             TBooleanField tv,
188             TTextField t,
189             TTextField dt,
190             Element JavaDoc val) {
191             super();
192             this.par = par;
193             this.tc = tc;
194             this.td = td;
195             this.tv = tv;
196             this.t = t;
197             this.dt = dt;
198             this.val = val;
199         }
200
201         public void actionPerformed(ActionEvent JavaDoc evt) {
202             final String JavaDoc tStr = t.getText(); //task name
203
final String JavaDoc tcStr = tc.getText(); //task class
204
final String JavaDoc dtStr = dt.getText(); //task display name
205
final String JavaDoc tdStr = td.getText(); //task description
206
final String JavaDoc tvStr = tv.getText(); //task visibility
207

208             if (tStr == null || tStr.length() < 1) {
209                 GUIUtil.showErrorMessage(
210                     par,
211                     "Please specify a valid task name.");
212                 return;
213             }
214
215             try {
216                 if (tcStr == null || tcStr.length() < 1) {
217                     if (!GUIUtil
218                         .getConfirmation(
219                             par,
220                             "Is the task part of the default ANT distribution (core or optional)?")) {
221                         GUIUtil.showErrorMessage(
222                             par,
223                             "Full class-name should be specified for a custom task.");
224                         return;
225                     }
226                 } else {
227                     act.getTaskMaster().addTask(tStr, tcStr);
228                 }
229                 Element JavaDoc newTask = val;
230                 if (val == null)
231                     newTask = category.getOwnerDocument().createElement("task");
232                 //TODO CHECK IF Task NAME IS VALID
233
if (dtStr != null)
234                     newTask.setAttribute(GUIUtil.TASK_DISPLAY, dtStr);
235                 if (tStr != null)
236                     newTask.setAttribute(GUIUtil.TASK_NAME, tStr);
237                 if (tcStr != null)
238                     newTask.setAttribute(GUIUtil.TASK_CLASS, tcStr);
239                 if (tdStr != null)
240                     newTask.setAttribute(GUIUtil.TASK_DESCRIPTION, tdStr);
241                 if (tvStr != null && tvStr.equalsIgnoreCase("false"))
242                     newTask.setAttribute(GUIUtil.TASK_VISIBLE, tvStr);
243                 else
244                     newTask.removeAttribute(GUIUtil.TASK_VISIBLE);
245
246                 if (val == null) {
247                     category.appendChild(newTask);
248                 }
249                 act.reStart();
250                 par.dispose();
251             } catch (Exception JavaDoc e) {
252                 GUIUtil.showErrorMessage(
253                     par,
254                     "Could not edit/ add the Task. Please fill all fields with valid values. "
255                         + e.getMessage());
256             }
257         }
258     }
259
260     private final class RemoveActionListener implements ActionListener JavaDoc {
261         private final TComboBox cb;
262         private final JDialog JavaDoc par;
263         private RemoveActionListener(JDialog JavaDoc par, TComboBox cb) {
264             super();
265             this.cb = cb;
266             this.par = par;
267         }
268         public void actionPerformed(ActionEvent JavaDoc evt) {
269             final Element JavaDoc t = (Element JavaDoc) cb.getItemValue(cb.getText());
270             if (t != null) {
271                 try {
272                     act.getTaskMaster().removeTask(
273                         t.getAttribute(GUIUtil.TASK_NAME));
274                     category.removeChild(t);
275                     act.reStart();
276                     par.dispose();
277                 } catch (Exception JavaDoc e) {
278                     GUIUtil.showErrorMessage(
279                         par,
280                         "Could not remove the Task. " + e.getMessage());
281                 }
282             }
283         }
284     }
285
286     private final class SelectEditActionListener implements ActionListener JavaDoc {
287         private final TComboBox cb;
288         private final JDialog JavaDoc par;
289         private SelectEditActionListener(JDialog JavaDoc par, TComboBox cb) {
290             super();
291             this.cb = cb;
292             this.par = par;
293         }
294         public void actionPerformed(ActionEvent JavaDoc evt) {
295             final Element JavaDoc t = (Element JavaDoc) cb.getItemValue(cb.getText());
296             if (t != null) {
297                 try {
298                     new AddTaskDialog(t).show();
299                     par.dispose();
300                 } catch (Exception JavaDoc e) {
301                     GUIUtil.showErrorMessage(
302                         par,
303                         "Could not select the Task. " + e.getMessage());
304                 }
305             }
306         }
307     }
308
309     private class SelectTaskDialog extends JDialog JavaDoc {
310
311         SelectTaskDialog() {
312             super(act, "Select the task to remove/hide");
313             init();
314         }
315
316         private void init() {
317             HashMap JavaDoc tasks = new HashMap JavaDoc();
318             int count = 0;
319             NodeList JavaDoc nl = category.getChildNodes();
320             for (int i = 0; i < nl.getLength(); i++) {
321                 if (nl.item(i).getNodeType() != Node.ELEMENT_NODE
322                     || !nl.item(i).getNodeName().equals("task")) {
323                     continue;
324                 }
325                 Element JavaDoc child = (Element JavaDoc) nl.item(i);
326                 tasks.put(child.getAttribute(GUIUtil.TASK_DISPLAY), child);
327                 count++;
328             }
329             if (count == 0) {
330                 GUIUtil.showInfoMessage(
331                     this,
332                     "No Tasks in this group to edit/ delete!");
333                 return;
334             }
335             final JPanel JavaDoc jp = new JPanel JavaDoc(new GridLayout JavaDoc(1, 2, 2, 1));
336
337             jp.add(new TLabel("Select Task"));
338             final TComboBox cb =
339                 new TComboBox(tasks, type == EDIT_TASK ? "Edit" : "Delete");
340             jp.add(cb);
341
342             getContentPane().add(jp);
343             if (type == EDIT_TASK)
344                 cb.addActionListener(new SelectEditActionListener(this, cb));
345             else
346                 cb.addActionListener(new RemoveActionListener(this, cb));
347             pack();
348             setLocationRelativeTo(act);
349             show();
350         }
351     }
352
353 }
354
Popular Tags