KickJava   Java API By Example, From Geeks To Geeks.

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


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.Dimension JavaDoc;
58 import java.awt.FlowLayout JavaDoc;
59 import java.awt.event.ActionEvent JavaDoc;
60 import java.awt.event.ActionListener JavaDoc;
61 import java.io.File JavaDoc;
62 import java.util.Arrays JavaDoc;
63 import java.util.HashMap JavaDoc;
64 import java.util.Iterator JavaDoc;
65 import java.util.Vector JavaDoc;
66
67 import javax.swing.Box JavaDoc;
68 import javax.swing.JComponent JavaDoc;
69 import javax.swing.JOptionPane JavaDoc;
70 import javax.swing.JPanel JavaDoc;
71 import javax.swing.JScrollPane JavaDoc;
72
73 import org.apache.tools.ant.IntrospectionHelper;
74 import org.w3c.dom.Element JavaDoc;
75 import org.w3c.dom.Node JavaDoc;
76 import org.w3c.dom.NodeList JavaDoc;
77
78 import com.mullassery.act.ACTException;
79 import com.mullassery.act.TaskMaster;
80 import com.mullassery.act.gui.components.TBooleanField;
81 import com.mullassery.act.gui.components.TComboBox;
82 import com.mullassery.act.gui.components.TFileField;
83 import com.mullassery.act.gui.components.TLabel;
84 import com.mullassery.act.gui.components.TTextField;
85 import com.mullassery.act.util.ResourceUtil;
86 import com.mullassery.act.util.XMLUtil;
87
88 /**
89  *
90  * @author Abey Mullassery
91  *
92  */

93 public class DataPanel extends JScrollPane JavaDoc {
94     protected TaskPanel top;
95     protected DataPanel par;
96     protected Element JavaDoc element;
97     protected HashMap JavaDoc childPanels;
98     protected Vector JavaDoc labels = new Vector JavaDoc();
99     protected Vector JavaDoc values = new Vector JavaDoc();
100     protected String JavaDoc fullName;
101
102     public DataPanel(TaskPanel top, DataPanel par, String JavaDoc name, Class JavaDoc clazz, Element JavaDoc val)
103         throws ACTException {
104         this.top = top;
105         this.par = par;
106         this.element = val;
107         this.fullName = name;
108         this.element = val;
109         if (element == null) {
110             throw new IllegalArgumentException JavaDoc("Element values cannot be null");
111         }
112         final HashMap JavaDoc attrTypes = TaskMaster.getElementInfo(clazz);
113         filterAttributes(attrTypes);
114
115         if (attrTypes.containsKey(TaskMaster.CHILDREN)
116             && attrTypes.get(TaskMaster.CHILDREN) instanceof HashMap JavaDoc) {
117             final TComboBox tcb =
118                 new TComboBox((HashMap JavaDoc)attrTypes.get(TaskMaster.CHILDREN), "Add a Nested Element");
119             tcb.addActionListener(new ElementSelectionListener(tcb));
120             setColumnHeaderView(tcb);
121             attrTypes.remove(TaskMaster.CHILDREN);
122         }
123
124         Object JavaDoc[] keys = attrTypes.keySet().toArray();
125         Arrays.sort(keys, String.CASE_INSENSITIVE_ORDER);
126
127         Object JavaDoc prop = null;
128         String JavaDoc attrType = null;
129         for (int i = 0; i < keys.length; i++) {
130             prop = keys[i];
131             labels.add(new TLabel(prop.toString()));
132             attrType = attrTypes.get(prop).toString();
133             if (attrType.indexOf("File") > -1) {
134                 values.add(new TFileField());
135             } else if (attrType.indexOf("boolean") > -1) {
136                 values.add(new TBooleanField());
137             } else {
138                 values.add(new TTextField());
139             }
140         }
141
142         Box JavaDoc box = Box.createVerticalBox();
143         JPanel JavaDoc jp = null;
144         for (int x = 0; x < labels.size(); x++) {
145             jp = new JPanel JavaDoc(new FlowLayout JavaDoc(FlowLayout.RIGHT, 2, 0));
146             jp.add((JComponent JavaDoc)labels.get(x));
147             jp.add((JComponent JavaDoc)values.get(x));
148             box.add(jp);
149         }
150         setViewportView(box);
151
152         Dimension JavaDoc boxDim = box.getPreferredSize();
153         setPreferredSize(new Dimension JavaDoc(boxDim.width + 18, Math.min(boxDim.height + 5, 200)));
154
155         updateGUIValues();
156         top.addToTaskPanel(fullName, this);
157         createChildren(clazz);
158     }
159
160     private void filterAttributes(final HashMap JavaDoc attrTypes) throws ACTException {
161         File JavaDoc tf = ResourceUtil.getSettingsFile();
162         Element JavaDoc filters = XMLUtil.findElementNode(XMLUtil.getDocumentElement(tf), "filters", true);
163         if (filters != null) {
164             NodeList JavaDoc attributes = filters.getElementsByTagName("attribute");
165             for (int i = 0; i < attributes.getLength(); i++) {
166                 Element JavaDoc attr = (Element JavaDoc)attributes.item(i);
167                 String JavaDoc attrName = attr.getAttribute("name");
168                 attrTypes.remove(attrName);
169             }
170         }
171     }
172
173     DataPanel createChildPanel(String JavaDoc eName, Class JavaDoc eClass, Element JavaDoc val) throws ACTException {
174         if (val == null) {
175             val = element.getOwnerDocument().createElement(eName);
176             this.element.appendChild(val);
177         }
178
179         String JavaDoc childName = fullName + "." + eName;
180         int suffix = 2;
181         if (this.childPanels == null) {
182             childPanels = new HashMap JavaDoc();
183         }
184         while (childPanels.containsKey(childName)) {
185             childName = fullName + "." + eName + suffix;
186             suffix++;
187         }
188         final DataPanel ep = new DataPanel(top, this, childName, eClass, val);
189         childPanels.put(childName, ep);
190         return ep;
191     }
192
193     void removeChildPanel(DataPanel child) {
194         if (child == null || childPanels == null || !childPanels.containsValue(child))
195             return;
196         HashMap JavaDoc cp = child.childPanels;
197         if (cp != null) {
198             Iterator JavaDoc it = cp.keySet().iterator();
199             while (it.hasNext()) {
200                 child.removeChildPanel((DataPanel)cp.get(it.next()));
201             }
202         }
203         childPanels.remove(child);
204         element.removeChild(child.element);
205         top.removeFromTaskPanel(child);
206     }
207
208     //TODO: optimize to read only when updated
209
public Element JavaDoc getValues() {
210         String JavaDoc val = null;
211         String JavaDoc key = null;
212         for (int x = 0; x < labels.size(); x++) {
213             val = ((TextData)values.get(x)).getText();
214             key = ((TextData)labels.get(x)).getText();
215             if (val != null && val.length() != 0) {
216                 element.setAttribute(key, val);
217             }
218         }
219         if (childPanels != null && !childPanels.isEmpty()) {
220             Iterator JavaDoc en = childPanels.keySet().iterator(); //elements();
221
DataPanel ePanel = null;
222             while (en.hasNext()) {
223                 ePanel = (DataPanel)childPanels.get(en.next());
224                 ePanel.getValues();
225             }
226         }
227         return element;
228     }
229
230     public void updateGUIValues() {
231         if (element == null) {
232             throw new IllegalStateException JavaDoc("Cannot have a DataPanel without a model element");
233         }
234         String JavaDoc val = null;
235         String JavaDoc key = null;
236         for (int x = 0; x < labels.size(); x++) {
237             key = ((TextData)labels.get(x)).getText();
238             if (!element.hasAttribute(key))
239                 continue;
240             val = element.getAttribute(key);
241             if (val.length() != 0) {
242                 ((TextData)values.get(x)).setText(val);
243             }
244         }
245     }
246
247     public void createChildren(Class JavaDoc clazz) throws ACTException {
248         if (!element.hasChildNodes())
249             return;
250         IntrospectionHelper ih = IntrospectionHelper.getHelper(clazz);
251         NodeList JavaDoc nl = element.getChildNodes();
252         for (int i = 0; i < nl.getLength(); i++) {
253             if (nl.item(i).getNodeType() == Node.ELEMENT_NODE) {
254                 Element JavaDoc node = (Element JavaDoc)nl.item(i);
255                 createChildPanel(node.getNodeName(), ih.getElementType(node.getNodeName()), node);
256             }
257         }
258     }
259
260     private final class ElementSelectionListener implements ActionListener JavaDoc {
261         private final TComboBox tcb;
262         private ElementSelectionListener(TComboBox tcb) {
263             super();
264             this.tcb = tcb;
265         }
266         public void actionPerformed(ActionEvent JavaDoc e) {
267             try {
268                 Class JavaDoc clazz = (Class JavaDoc) (tcb.getItemValue(tcb.getText()));
269                 if (clazz != null) {
270                     createChildPanel(tcb.getText(), clazz, null);
271                 }
272             } catch (ACTException ae) {
273                 JOptionPane.showMessageDialog(
274                     DataPanel.this,
275                     "Could not create sub-element. " + ae.getMessage(),
276                     "ACT Error",
277                     JOptionPane.ERROR_MESSAGE);
278             }
279         }
280     }
281 }
Popular Tags