KickJava   Java API By Example, From Geeks To Geeks.

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


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.util.StringTokenizer JavaDoc;
58
59 import javax.swing.BorderFactory JavaDoc;
60 import javax.swing.JMenu JavaDoc;
61 import javax.swing.JMenuItem JavaDoc;
62 import javax.swing.JSeparator JavaDoc;
63
64 import org.w3c.dom.Element JavaDoc;
65 import org.w3c.dom.Node JavaDoc;
66 import org.w3c.dom.NodeList JavaDoc;
67
68 import com.mullassery.act.util.DebugUtil;
69 import com.mullassery.act.util.GUIUtil;
70 import com.mullassery.act.util.ResourceUtil;
71 import com.mullassery.act.util.XMLUtil;
72
73 /**
74  *
75  * @author Abey Mullassery
76  *
77  */

78 public class SettingsManager {
79     public static Element JavaDoc actSettingsElement;
80
81     private ACT act;
82     // private boolean dirty;
83

84     /**
85      * @param act
86      */

87     public SettingsManager(ACT act) {
88         this.act = act;
89     }
90     
91     /**
92      * @param menu
93      */

94     private int addTaskItems(JMenu JavaDoc menu, Element JavaDoc n) {
95         int added = 0;
96         if (n == null || !n.hasChildNodes())
97             return added;
98         String JavaDoc display = null;
99         String JavaDoc name = null;
100         String JavaDoc visible = null;
101         String JavaDoc clazz = null;
102         Element JavaDoc node = null;
103         JMenuItem JavaDoc menuItem = null;
104         NodeList JavaDoc nTasks = n.getChildNodes();
105         DebugUtil.debug(
106             "\t" + n.getNodeName() + " has children : " + nTasks.getLength());
107         for (int i = 0; i < nTasks.getLength(); i++) {
108             try {
109                 if (nTasks.item(i).getNodeType() != Node.ELEMENT_NODE
110                     || !nTasks.item(i).getNodeName().equals("task")) {
111                     continue;
112                 }
113                 node = (Element JavaDoc) nTasks.item(i);
114                 name = node.getAttribute(GUIUtil.TASK_NAME);
115                 display = node.getAttribute(GUIUtil.TASK_DISPLAY);
116                 if (display == null || display.length() < 1) {
117                     display = name;
118                     node.setAttribute(GUIUtil.TASK_DISPLAY, display);
119                 } else if (name == null || name.length() < 1) {
120                     name = display.toLowerCase();
121                     node.setAttribute(GUIUtil.TASK_NAME, name);
122                 }
123                 visible = node.getAttribute(GUIUtil.TASK_VISIBLE);
124                 clazz = node.getAttribute(GUIUtil.TASK_CLASS);
125                 if (clazz != null && clazz.length() > 0)
126                     act.getTaskMaster().addTask(name, clazz);
127
128                 if (name != null
129                     && name.length() > 0
130                     && !visible.equalsIgnoreCase("FALSE")) {
131                     menuItem = new JMenuItem JavaDoc(new OpenTaskAction(act, node));
132                     if (!act.getTaskMaster().getAllTasks().containsKey(name)) {
133                         menuItem.setEnabled(false);
134                         menuItem.setToolTipText(
135                             getWrappedText(
136                                 "<b>Cannot use this task since some of its dependencies"
137                                     + "are NOT available in the CLASSPATH.</b><br><br>"
138                                     + "["
139                                     + node.getAttribute(GUIUtil.TASK_DESCRIPTION)
140                                     + "]",
141                                 40));
142                     } else {
143                         menuItem.setToolTipText(
144                             getWrappedText(
145                                 "<b>[</b>"
146                                     + node.getAttribute(GUIUtil.TASK_DESCRIPTION)
147                                     + "<b>]</b>",
148                                 40));
149                     }
150                     menu.add(menuItem);
151                     added++;
152                 }
153             } catch (Throwable JavaDoc t) {
154                 continue;
155             }
156         }
157         menu.add(new JSeparator JavaDoc());
158         JMenu JavaDoc editMenu = new JMenu JavaDoc("Add/ Edit/ Delete");
159         menu.add(editMenu);
160         editMenu.add(
161             new JMenuItem JavaDoc(
162                 new TaskAction(
163                     act,
164                     n,
165                     TaskAction.ADD_TASK_LABEL,
166                     TaskAction.ADD_TASK)));
167         editMenu.add(
168             new JMenuItem JavaDoc(
169                 new TaskAction(
170                     act,
171                     n,
172                     TaskAction.EDIT_TASK_LABEL,
173                     TaskAction.EDIT_TASK)));
174         editMenu.add(
175             new JMenuItem JavaDoc(
176                 new TaskAction(
177                     act,
178                     n,
179                     TaskAction.DELETE_TASK_LABEL,
180                     TaskAction.DELETE_TASK)));
181         return added;
182     }
183
184     private String JavaDoc getWrappedText(String JavaDoc str, int lineLength) {
185         StringBuffer JavaDoc sb = new StringBuffer JavaDoc("<html>");
186         StringTokenizer JavaDoc st = new StringTokenizer JavaDoc(str, " \t\n\r\f", true);
187         int count = sb.length();
188         while (st.hasMoreTokens()) {
189             sb.append(st.nextToken());
190             if (sb.length() >= count + lineLength) {
191                 sb.append("<br>");
192                 count += lineLength;
193             }
194         }
195         return sb.toString();
196     }
197
198     public Element JavaDoc getSettings() {
199         try {
200             if (actSettingsElement != null)
201                 return actSettingsElement;
202             actSettingsElement =
203                 XMLUtil.getDocumentElement(ResourceUtil.getSettingsFile());
204             return actSettingsElement;
205         } catch (Exception JavaDoc e) {
206             try {
207                 return XMLUtil.loadDocument(
208                     this.getClass().getResourceAsStream("act-settings.xml"),
209                     "act-settings");
210             } catch (Exception JavaDoc ex) {
211                 return null;
212             }
213         }
214     }
215
216     public Element JavaDoc getMergedSettings() {
217         return null;
218     }
219
220     public JMenu JavaDoc getTaskMenu() {
221         JMenu JavaDoc top = new JMenu JavaDoc("Select a task to Execute");
222         top.setBorder(BorderFactory.createEtchedBorder());
223         Element JavaDoc settings = getSettings();
224         if (settings == null || !settings.hasChildNodes()) {
225             top.setText("No Tasks. Settings file not found!");
226             return top;
227         }
228
229         addGroups(top, settings);
230         return top;
231     }
232
233     private int addGroups(JMenu JavaDoc jm, Element JavaDoc group) {
234         NodeList JavaDoc nGroups = group.getChildNodes();
235         DebugUtil.debug(
236             "\t"
237                 + group.getNodeName()
238                 + " has children : "
239                 + nGroups.getLength());
240         int children = 0;
241         //sub groups
242
for (int i = 0; i < nGroups.getLength(); i++) {
243             try {
244                 if (nGroups.item(i).getNodeType() != Node.ELEMENT_NODE
245                     || !nGroups.item(i).getNodeName().equals("task-group")) {
246                     continue;
247                 }
248                 Element JavaDoc node = (Element JavaDoc) nGroups.item(i);
249                 String JavaDoc name = node.getAttribute(GUIUtil.TASK_DISPLAY);
250                 String JavaDoc visible = node.getAttribute(GUIUtil.TASK_VISIBLE);
251                 if (name != null
252                     && name.length() > 0
253                     && !visible.equalsIgnoreCase("FALSE")) {
254                     DebugUtil.debug(
255                         "Adding group " + name + " to " + jm.getText());
256                     JMenu JavaDoc groupMenu = new JMenu JavaDoc(name);
257                     jm.add(groupMenu);
258                     children += addGroups(groupMenu, node);
259                 }
260             } catch (Throwable JavaDoc t) {
261                 continue;
262             }
263         }
264         children += addTaskItems(jm, group);
265         jm.setText(jm.getText() + " (" + children + ")");
266         return children;
267     }
268
269     public void saveSettings() {
270         if (actSettingsElement != null) {
271             try {
272                 XMLUtil.saveElement(
273                     actSettingsElement,
274                     ResourceUtil.getSettingsFile(),
275                     false);
276             } catch (Exception JavaDoc e) { //GUIUtil.showErrorMessage(this, "Error while saving.." + e);
277
}
278         }
279     }
280 }
281
Popular Tags