1 54 55 package com.mullassery.act.gui; 56 57 import java.awt.Dimension ; 58 import java.awt.FlowLayout ; 59 import java.awt.event.ActionEvent ; 60 import java.awt.event.ActionListener ; 61 import java.io.File ; 62 import java.util.Arrays ; 63 import java.util.HashMap ; 64 import java.util.Iterator ; 65 import java.util.Vector ; 66 67 import javax.swing.Box ; 68 import javax.swing.JComponent ; 69 import javax.swing.JOptionPane ; 70 import javax.swing.JPanel ; 71 import javax.swing.JScrollPane ; 72 73 import org.apache.tools.ant.IntrospectionHelper; 74 import org.w3c.dom.Element ; 75 import org.w3c.dom.Node ; 76 import org.w3c.dom.NodeList ; 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 93 public class DataPanel extends JScrollPane { 94 protected TaskPanel top; 95 protected DataPanel par; 96 protected Element element; 97 protected HashMap childPanels; 98 protected Vector labels = new Vector (); 99 protected Vector values = new Vector (); 100 protected String fullName; 101 102 public DataPanel(TaskPanel top, DataPanel par, String name, Class clazz, Element 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 ("Element values cannot be null"); 111 } 112 final HashMap attrTypes = TaskMaster.getElementInfo(clazz); 113 filterAttributes(attrTypes); 114 115 if (attrTypes.containsKey(TaskMaster.CHILDREN) 116 && attrTypes.get(TaskMaster.CHILDREN) instanceof HashMap ) { 117 final TComboBox tcb = 118 new TComboBox((HashMap )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 [] keys = attrTypes.keySet().toArray(); 125 Arrays.sort(keys, String.CASE_INSENSITIVE_ORDER); 126 127 Object prop = null; 128 String 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 box = Box.createVerticalBox(); 143 JPanel jp = null; 144 for (int x = 0; x < labels.size(); x++) { 145 jp = new JPanel (new FlowLayout (FlowLayout.RIGHT, 2, 0)); 146 jp.add((JComponent )labels.get(x)); 147 jp.add((JComponent )values.get(x)); 148 box.add(jp); 149 } 150 setViewportView(box); 151 152 Dimension boxDim = box.getPreferredSize(); 153 setPreferredSize(new Dimension (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 attrTypes) throws ACTException { 161 File tf = ResourceUtil.getSettingsFile(); 162 Element filters = XMLUtil.findElementNode(XMLUtil.getDocumentElement(tf), "filters", true); 163 if (filters != null) { 164 NodeList attributes = filters.getElementsByTagName("attribute"); 165 for (int i = 0; i < attributes.getLength(); i++) { 166 Element attr = (Element )attributes.item(i); 167 String attrName = attr.getAttribute("name"); 168 attrTypes.remove(attrName); 169 } 170 } 171 } 172 173 DataPanel createChildPanel(String eName, Class eClass, Element val) throws ACTException { 174 if (val == null) { 175 val = element.getOwnerDocument().createElement(eName); 176 this.element.appendChild(val); 177 } 178 179 String childName = fullName + "." + eName; 180 int suffix = 2; 181 if (this.childPanels == null) { 182 childPanels = new HashMap (); 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 cp = child.childPanels; 197 if (cp != null) { 198 Iterator 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 public Element getValues() { 210 String val = null; 211 String 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 en = childPanels.keySet().iterator(); 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 ("Cannot have a DataPanel without a model element"); 233 } 234 String val = null; 235 String 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 clazz) throws ACTException { 248 if (!element.hasChildNodes()) 249 return; 250 IntrospectionHelper ih = IntrospectionHelper.getHelper(clazz); 251 NodeList nl = element.getChildNodes(); 252 for (int i = 0; i < nl.getLength(); i++) { 253 if (nl.item(i).getNodeType() == Node.ELEMENT_NODE) { 254 Element node = (Element )nl.item(i); 255 createChildPanel(node.getNodeName(), ih.getElementType(node.getNodeName()), node); 256 } 257 } 258 } 259 260 private final class ElementSelectionListener implements ActionListener { 261 private final TComboBox tcb; 262 private ElementSelectionListener(TComboBox tcb) { 263 super(); 264 this.tcb = tcb; 265 } 266 public void actionPerformed(ActionEvent e) { 267 try { 268 Class clazz = (Class ) (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 |