KickJava   Java API By Example, From Geeks To Geeks.

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


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.Component JavaDoc;
59 import java.awt.Cursor JavaDoc;
60 import java.awt.FlowLayout JavaDoc;
61 import java.awt.event.ActionEvent JavaDoc;
62 import java.awt.event.ActionListener JavaDoc;
63 import java.awt.event.MouseAdapter JavaDoc;
64 import java.awt.event.MouseEvent JavaDoc;
65
66 import javax.swing.AbstractAction JavaDoc;
67 import javax.swing.JButton JavaDoc;
68 import javax.swing.JComponent JavaDoc;
69 import javax.swing.JDialog JavaDoc;
70 import javax.swing.JFrame JavaDoc;
71 import javax.swing.JOptionPane JavaDoc;
72 import javax.swing.JPanel JavaDoc;
73 import javax.swing.JPopupMenu JavaDoc;
74 import javax.swing.JTabbedPane JavaDoc;
75 import javax.swing.SwingUtilities JavaDoc;
76
77 import org.w3c.dom.Element JavaDoc;
78 import org.w3c.dom.NodeList JavaDoc;
79
80 import com.mullassery.act.ACTException;
81 import com.mullassery.act.TaskMaster;
82 import com.mullassery.act.gui.components.TFileField;
83 import com.mullassery.act.gui.components.TLabel;
84 import com.mullassery.act.util.GUIUtil;
85
86 /**
87  *
88  * @author Abey Mullassery
89  *
90  */

91 public class TaskPanel extends JDialog JavaDoc {
92     protected static int incX, incY;
93     protected TFileField baseDir;
94     protected OutputPanel op;
95     protected TaskMaster taskRunner;
96     protected DataPanel dataPanel;
97     protected Element JavaDoc taskElement;
98     protected JTabbedPane JavaDoc tPane = new JTabbedPane JavaDoc();
99
100     public TaskPanel(final JFrame JavaDoc par, final TaskMaster tr, Element JavaDoc vals)
101         throws ACTException {
102         super(par, "Task " + vals.getAttribute(GUIUtil.TASK_NAME));
103         this.getContentPane().setLayout(new BorderLayout JavaDoc());
104
105         final JPopupMenu JavaDoc popup = new JPopupMenu JavaDoc();
106         popup.add(new TabCloseAction());
107         tPane.addMouseListener(new MouseAdapter JavaDoc() {
108             public void mouseReleased(MouseEvent JavaDoc e) {
109                 showPopup(e);
110             }
111             public void mousePressed(MouseEvent JavaDoc e) {
112                 showPopup(e);
113             }
114             public void mouseClicked(MouseEvent JavaDoc e) {
115                 showPopup(e);
116             }
117             private void showPopup(MouseEvent JavaDoc e) {
118                 if (e.isPopupTrigger()
119                     && !tPane.getSelectedComponent().equals(dataPanel)) {
120                     popup.show(tPane, e.getX(), e.getY());
121                 }
122             }
123         });
124
125         this.taskRunner = tr;
126         this.taskElement = vals;
127         String JavaDoc name = taskElement.getAttribute(GUIUtil.TASK_NAME);
128         String JavaDoc displayName = name;
129
130         NodeList JavaDoc nChildren = taskElement.getElementsByTagName(name);
131         Element JavaDoc dataElement;
132         if (nChildren.getLength() > 0) {
133             dataElement = (Element JavaDoc) nChildren.item(0);
134         } else {
135             dataElement = taskElement.getOwnerDocument().createElement(name);
136             this.taskElement.appendChild(dataElement);
137         }
138
139         Class JavaDoc clazz = tr.getElementClass(name);
140         this.dataPanel =
141             new DataPanel(this, null, displayName, clazz, dataElement);
142         tPane.setSelectedIndex(0);
143
144         final JPanel JavaDoc bdp = new JPanel JavaDoc(new FlowLayout JavaDoc(FlowLayout.RIGHT, 2, 0));
145         baseDir = new TFileField(".");
146         bdp.add(new TLabel(" Base Directory "));
147         bdp.add(baseDir);
148         this.getContentPane().add(bdp, BorderLayout.NORTH);
149
150         this.getContentPane().add(tPane, BorderLayout.CENTER);
151
152         final JButton JavaDoc execute = new JButton JavaDoc("Execute");
153         execute.addActionListener(new ActionListener JavaDoc() {
154             public void actionPerformed(ActionEvent JavaDoc ae) {
155                 SwingUtilities.invokeLater(new Runnable JavaDoc() {
156                     public void run() {
157                         executeTask();
158                     }
159                 });
160             }
161         });
162         this.getContentPane().add(execute, BorderLayout.SOUTH);
163
164         setDefaultCloseOperation(DO_NOTHING_ON_CLOSE);
165         GUIUtil.centralize(this);
166         pack();
167         show();
168     }
169
170     void addToTaskPanel(String JavaDoc tabTitle, JComponent JavaDoc dp) {
171         tPane.addTab(tabTitle, dp);
172         tPane.setSelectedIndex(tPane.indexOfTab(tabTitle));
173     }
174
175     void removeFromTaskPanel(JComponent JavaDoc dp) {
176         tPane.remove(dp);
177     }
178
179     void executeTask() {
180         Cursor JavaDoc def = getCursor();
181         try {
182             final Element JavaDoc val = dataPanel.getValues();
183             if (op == null)
184                 op = new OutputPanel();
185             addToTaskPanel("Output: " + dataPanel.fullName, op);
186             pack();
187             setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));
188             taskRunner.executeTask(val.getNodeName(), getBaseDir(), val, op);
189         } catch (Exception JavaDoc ex) {
190             JOptionPane.showMessageDialog(
191                 this,
192                 "Could execute task. " + ex.getMessage(),
193                 "ACT Error",
194                 JOptionPane.ERROR_MESSAGE);
195         } finally {
196             setCursor(def);
197         }
198     }
199
200     Element JavaDoc getTaskValues() {
201         dataPanel.getValues(); //update;
202
return this.taskElement;
203     }
204
205     String JavaDoc getBaseDir() {
206         return baseDir.getText();
207     }
208
209     String JavaDoc getTaskName(){
210         return dataPanel.fullName;
211     }
212
213     public class TabCloseAction extends AbstractAction JavaDoc {
214         TabCloseAction() {
215             this.putValue(NAME, "Remove");
216             this.putValue(SHORT_DESCRIPTION, "Removes this element.");
217         }
218         
219         public void actionPerformed(ActionEvent JavaDoc e) {
220             Component JavaDoc comp = tPane.getSelectedComponent();
221             if (comp instanceof DataPanel
222                 && GUIUtil.getConfirmation(
223                     TaskPanel.this,
224                     "Do you want to delete "
225                         + tPane.getTitleAt(tPane.getSelectedIndex())
226                         + " and its children?")) {
227                 DataPanel dp = (DataPanel) comp;
228                 dp.par.removeChildPanel(dp);
229             }
230             tPane.remove(comp);
231         }
232
233     }
234 }
Popular Tags