KickJava   Java API By Example, From Geeks To Geeks.

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


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.io.File JavaDoc;
58
59 import javax.swing.JFileChooser JavaDoc;
60 import javax.swing.JPopupMenu JavaDoc;
61 import javax.swing.filechooser.FileFilter JavaDoc;
62
63 import org.w3c.dom.Element JavaDoc;
64 import org.w3c.dom.NodeList JavaDoc;
65
66 import com.mullassery.act.util.GUIUtil;
67 import com.mullassery.act.util.XMLUtil;
68
69 /**
70  *
71  * @author Abey Mullassery
72  *
73  */

74 public class TaskManager {
75     private ACT act;
76     private final JFileChooser JavaDoc jfc;
77     private File JavaDoc lastFile = new File JavaDoc(".");
78     /**
79      *
80      */

81     public TaskManager(ACT act) {
82         this.act = act;
83         jfc = new JFileChooser JavaDoc();
84         jfc.setMultiSelectionEnabled(false);
85         jfc.setAcceptAllFileFilterUsed(true);
86         jfc.addChoosableFileFilter(new FileFilter JavaDoc() {
87             public boolean accept(File JavaDoc f) {
88                 return (
89                     f.isDirectory()
90                         || (f.isFile() && f.getName().endsWith(".xml")));
91             }
92             public String JavaDoc getDescription() {
93                 return "ACT task list file (*.xml)";
94             }
95         });
96     }
97
98     //TODO Open and save ant build files
99

100     public void openTask() {
101         jfc.setDialogTitle("Open a saved task");
102         jfc.setCurrentDirectory(lastFile);
103         int ret = jfc.showOpenDialog(act);
104         if (ret == JFileChooser.APPROVE_OPTION) {
105             File JavaDoc f = jfc.getSelectedFile();
106             setLastFile(f);
107             try {
108                 Element JavaDoc el = XMLUtil.loadDocument(f, GUIUtil.TASK_LIST);
109                 if (el != null && el.hasChildNodes()) {
110                     NodeList JavaDoc nl = el.getElementsByTagName(GUIUtil.TASK);
111                     Element JavaDoc node = null;
112                     JPopupMenu JavaDoc jp = new JPopupMenu JavaDoc("Select task");
113                     for (int i = 0; i < nl.getLength(); i++) {
114                         node = (Element JavaDoc) nl.item(i);
115                         try {
116                             if (!act
117                                 .getTaskMaster()
118                                 .getAllTasks()
119                                 .containsKey(
120                                     node.getAttribute(GUIUtil.TASK_NAME))) {
121                                 act.getTaskMaster().addTask(
122                                     node.getAttribute(GUIUtil.TASK_NAME),
123                                     node.getAttribute(GUIUtil.TASK_CLASS));
124                             }
125                             jp.add(new OpenTaskAction(act, node, false));
126                         } catch (Throwable JavaDoc t) {
127                             GUIUtil.showErrorMessage(
128                                 act,
129                                 "This task was not loaded/ included during startup. "
130                                     + "Task might have been renamed/ hidden/ deleted in the settings.");
131                         }
132                     }
133                     jp.pack();
134                     jp.show(act, 30, 30);
135                 } else {
136                     GUIUtil.showErrorMessage(
137                         act,
138                         "No tasks to load.\n"
139                             + "You might have selected an empty or invalid ACT task list file");
140                 }
141             } catch (Exception JavaDoc e) {
142                 GUIUtil.showErrorMessage(act, "Error loading tasks.." + e);
143             }
144         }
145     }
146
147     public boolean saveTask(TaskPanel focusedTP) {
148         if (focusedTP == null) {
149             GUIUtil.showErrorMessage(act, "No Task available to save!");
150             return false;
151         }
152         Element JavaDoc root = null;
153         File JavaDoc f = null;
154         do {
155             jfc.setDialogTitle(
156                 "Save " + focusedTP.getTaskName() + " task to..");
157             jfc.setCurrentDirectory(lastFile);
158             if (jfc.showSaveDialog(act) != JFileChooser.APPROVE_OPTION) {
159                 return false;
160             }
161             f = jfc.getSelectedFile();
162             if (!f.exists()) {
163                 String JavaDoc fStr = f.getAbsolutePath();
164                 if (fStr.indexOf('.') == -1) {
165                     //not considering files with extentsion
166
f = new File JavaDoc(fStr + ".xml");
167                 }
168             } else if (!f.canWrite()) {
169                 GUIUtil.showErrorMessage(
170                     focusedTP,
171                     "The xml file selected is not writable."
172                         + "\nIt might be accessed by other programs or must be read-only.");
173                 continue;
174             }
175             try {
176                 if (f.length() < 1) {
177                     root =
178                         XMLUtil
179                             .getDocumentBuilder()
180                             .newDocument()
181                             .createElement(
182                             GUIUtil.TASK_LIST);
183                 } else {
184                     root = XMLUtil.loadDocument(f, null);
185                 }
186             } catch (Exception JavaDoc e) {
187             }
188             if (root == null
189                 || !root.getNodeName().equals(GUIUtil.TASK_LIST)) {
190                 GUIUtil.showErrorMessage(
191                     focusedTP,
192                     "The xml file selected is not a valid ACT task list file."
193                         + "\nChoose another or create new.");
194                 continue;
195             }
196             setLastFile(f);
197         } while (root == null);
198
199         try {
200             Element JavaDoc vals =
201                 (Element JavaDoc) root.getOwnerDocument().importNode(
202                     focusedTP.getTaskValues(),
203                     true);
204             //if no attributes nor sub elements present
205
if (!vals.hasChildNodes()
206                 && !vals.hasAttributes()
207                 && !GUIUtil.getConfirmation(
208                     focusedTP,
209                     "The Task has no propertes set. Do you still want to save?")) {
210                 return true;
211             }
212             String JavaDoc saveName = focusedTP.getTaskName();
213             if (GUIUtil
214                 .getConfirmation(
215                     focusedTP,
216                     "Would you like to save the task ("
217                         + saveName
218                         + ") by some other name?")) {
219                 do {
220                     saveName = GUIUtil.getInput(focusedTP, "Save task as..");
221                     if (saveName == null || saveName.length() == 0) {
222                         GUIUtil.showErrorMessage(
223                             focusedTP,
224                             "Cannot save task without a name.");
225                         saveName = null;
226                         continue;
227                     }
228                     NodeList JavaDoc nl = root.getElementsByTagName(GUIUtil.TASK);
229                     Element JavaDoc node = null;
230                     for (int i = 0; i < nl.getLength(); i++) {
231                         node = (Element JavaDoc) nl.item(i);
232                         if (node
233                             .getAttribute(GUIUtil.TASK_DISPLAY)
234                             .equals(saveName.trim())) {
235                             if (GUIUtil
236                                 .getConfirmation(
237                                     focusedTP,
238                                     "The name entered already exists in the file. "
239                                         + "\nWould you like to overwrite?")) {
240                                 root.removeChild(node);
241                             } else {
242                                 saveName = null;
243                                 continue;
244                             }
245                         }
246                     }
247                 } while (saveName == null);
248             }
249             vals.setAttribute(GUIUtil.TASK_DISPLAY, saveName.trim());
250             root.appendChild(vals);
251             XMLUtil.saveElement(root, f, false);
252         } catch (Exception JavaDoc e) {
253             GUIUtil.showErrorMessage(
254                 focusedTP,
255                 "Error while saving.\n" + e.getMessage());
256             return false;
257         }
258         return true;
259     }
260
261     //TODO: REMOVE DUPLICATION
262
private void setLastFile(File JavaDoc f) {
263         lastFile = f;
264         //saving to open next time
265
if (!lastFile.isDirectory())
266             lastFile = lastFile.getParentFile();
267     }
268
269 }
270
Popular Tags