|
| 1 | +package paintcomponents.java.interactive; |
| 2 | + |
| 3 | +import java.lang.reflect.Constructor; |
| 4 | +import java.lang.reflect.InvocationTargetException; |
| 5 | +import java.util.ArrayList; |
| 6 | +import java.util.Arrays; |
| 7 | +import java.util.NoSuchElementException; |
| 8 | + |
| 9 | +import javax.swing.JOptionPane; |
| 10 | + |
| 11 | +import org.w3c.dom.DOMException; |
| 12 | +import org.w3c.dom.Document; |
| 13 | +import org.w3c.dom.Element; |
| 14 | + |
| 15 | +import paintcomponents.NoConnectingLineSegmentException; |
| 16 | +import paintcomponents.data.DataFromPoint; |
| 17 | +import paintcomponents.data.DataFromPointDataProvider; |
| 18 | +import paintcomponents.data.DataFromPointNoDataProviderException; |
| 19 | +import paintcomponents.data.DataFromPointProviderCannotProvideDataException; |
| 20 | +import paintcomponents.data.DataTextIOPaintComponent; |
| 21 | +import paintcomponents.data.DataToPoint; |
| 22 | +import typesystem.JavaType; |
| 23 | +import ui.PaintPanel; |
| 24 | + |
| 25 | +public class ClassConstructorPaintComponent extends DataTextIOPaintComponent |
| 26 | + implements DataFromPointDataProvider { |
| 27 | + |
| 28 | + private Constructor displayingConstructor; |
| 29 | + private Object instance; |
| 30 | + private Object returnVal; |
| 31 | + |
| 32 | + |
| 33 | + public ClassConstructorPaintComponent(Constructor displayingContructor, |
| 34 | + Object instance, int x, int y) { |
| 35 | + super(displayingContructor.toString(), x, y); |
| 36 | + this.instance = instance; |
| 37 | + this.displayingConstructor = displayingContructor; |
| 38 | + init(); |
| 39 | + } |
| 40 | + |
| 41 | + private void init() { |
| 42 | + |
| 43 | + // parameters take place from line 1 to length |
| 44 | + Class[] paramTypes = displayingConstructor.getParameterTypes(); |
| 45 | + for (int i = 0; i < paramTypes.length; i++) { |
| 46 | + addToPoint(i + 1, new JavaType(paramTypes[i])); |
| 47 | + } |
| 48 | + |
| 49 | + // prepare String |
| 50 | + StringBuilder s = new StringBuilder(); |
| 51 | + s.append(this.displayingConstructor.toString() + "\n"); |
| 52 | + for (int i = 0; i < paramTypes.length; i++) { |
| 53 | + s.append("arg" + i + " :: " + paramTypes[i].getName() + "\n"); |
| 54 | + } |
| 55 | + |
| 56 | + setDisplayingText(s.toString()); |
| 57 | + |
| 58 | + } |
| 59 | + |
| 60 | + /** |
| 61 | + * Calculate the input data and store it. |
| 62 | + */ |
| 63 | + public void evaluate(DataFromPoint dataFromPoint){ |
| 64 | + // prepare argument list |
| 65 | + ArrayList<DataToPoint> toPoints = getToPoints(); |
| 66 | + Object[] args = new Object[toPoints.size()]; |
| 67 | + for (int i = 0; i < toPoints.size(); i++) { |
| 68 | + DataToPoint toPoint = toPoints.get(i); |
| 69 | + try { |
| 70 | + args[i] = toPoint.fetchData(); |
| 71 | + } catch (NoSuchElementException | NoConnectingLineSegmentException |
| 72 | + | DataFromPointNoDataProviderException |
| 73 | + | DataFromPointProviderCannotProvideDataException e) { |
| 74 | + e.printStackTrace(); |
| 75 | + // TODO Handle Exception |
| 76 | + throw new IllegalStateException(); |
| 77 | + } |
| 78 | + } |
| 79 | + try { |
| 80 | + returnVal = displayingConstructor.newInstance(args); |
| 81 | + } catch (InstantiationException | IllegalAccessException |
| 82 | + | IllegalArgumentException | InvocationTargetException e) { |
| 83 | + e.printStackTrace(); |
| 84 | + // TODO Handle Exception |
| 85 | + // TODO Handle Exception |
| 86 | + throw new IllegalStateException(); |
| 87 | + } |
| 88 | + } |
| 89 | + |
| 90 | + /** |
| 91 | + * Retrieve the data stored in this component |
| 92 | + * @return the data stored |
| 93 | + */ |
| 94 | + @Override |
| 95 | + public Object provideInformationToDataFromPoint( |
| 96 | + DataFromPoint dataFromPoint) { |
| 97 | + |
| 98 | + return returnVal; |
| 99 | + } |
| 100 | + |
| 101 | + /** |
| 102 | + * Check whether the data is good to return from this component |
| 103 | + * @return the return value |
| 104 | + */ |
| 105 | + @Override |
| 106 | + public boolean canProvideInformationToDataFromPoint( |
| 107 | + DataFromPoint dataFromPoint) { |
| 108 | + |
| 109 | + return returnVal != null; |
| 110 | + } |
| 111 | + |
| 112 | + @Override |
| 113 | + public void saveToElement(Element rootElement, Document doc) { |
| 114 | + super.saveToElement(rootElement, doc); |
| 115 | + // build the structure |
| 116 | + Element main = doc.createElement("classconstructorcomponent"); |
| 117 | + Element className = doc.createElement("classname"); |
| 118 | + Element constructorInfoElem = doc |
| 119 | + .createElement("constructorinfo"); |
| 120 | + |
| 121 | + main.appendChild(className); |
| 122 | + main.appendChild(constructorInfoElem); |
| 123 | + rootElement.appendChild(main); |
| 124 | + |
| 125 | + // store the class name in the classname element |
| 126 | + className.setTextContent(displayingConstructor.getDeclaringClass().getName()); |
| 127 | + |
| 128 | + // this approach connot deal with arrays and primitives |
| 129 | + // //store a list of constructor types in constructorParamType element |
| 130 | + // Class[] parameterTypes = displayingConstructor.getParameterTypes(); |
| 131 | + // for (Class type : parameterTypes) { |
| 132 | + // Element typeElem = doc.createElement("typename"); |
| 133 | + // typeElem.appendChild(doc.createTextNode(type.getName())); |
| 134 | + // constructorParamType.appendChild(typeElem); |
| 135 | + // } |
| 136 | + |
| 137 | + /* Index approach */ |
| 138 | + constructorInfoElem |
| 139 | + .setAttribute("index", |
| 140 | + Integer.toString(Arrays.asList(this.displayingConstructor |
| 141 | + .getDeclaringClass().getConstructors()) |
| 142 | + .indexOf(displayingConstructor))); |
| 143 | + |
| 144 | + } |
| 145 | + |
| 146 | + public ClassConstructorPaintComponent(Element rootElement, |
| 147 | + PaintPanel panel) { |
| 148 | + super(rootElement, panel); |
| 149 | + Element main = (Element) rootElement |
| 150 | + .getElementsByTagName("classconstructorcomponent").item(0); |
| 151 | + Element classNameElem = (Element) main |
| 152 | + .getElementsByTagName("classname").item(0); |
| 153 | + Element constructorInfoElem = (Element) main |
| 154 | + .getElementsByTagName("constructorinfo").item(0); |
| 155 | + |
| 156 | + |
| 157 | + |
| 158 | + String className = classNameElem.getTextContent(); |
| 159 | + |
| 160 | + //index appproach |
| 161 | + try { |
| 162 | + Class consClass = Class.forName(className); |
| 163 | + this.displayingConstructor = consClass.getConstructors()[Integer.parseInt(constructorInfoElem.getAttribute("index"))]; |
| 164 | + this.setDisplayingText(displayingConstructor.toString()); |
| 165 | + init(); |
| 166 | + linkPoints(rootElement); |
| 167 | + |
| 168 | + } catch (ClassNotFoundException | DOMException | SecurityException e) { |
| 169 | + JOptionPane.showMessageDialog(panel, e.toString()); |
| 170 | + e.printStackTrace(); |
| 171 | + } |
| 172 | + |
| 173 | + |
| 174 | + /* type approach */ |
| 175 | +// NodeList types = constructorParamTypesElem |
| 176 | +// .getElementsByTagName("typename"); |
| 177 | +// Class[] paramTypes = new Class[types.getLength()]; |
| 178 | +// |
| 179 | +// for (int i = 0; i < types.getLength(); i++) { |
| 180 | +// Element typeElem = (Element) types.item(i); |
| 181 | +// String typeName = typeElem.getTextContent(); |
| 182 | +// try { |
| 183 | +// paramTypes[i] = Class.forName(typeName); |
| 184 | +// } catch (ClassNotFoundException e) { |
| 185 | +// JOptionPane.showMessageDialog(panel, e.toString()); |
| 186 | +// e.printStackTrace(); |
| 187 | +// } |
| 188 | +// } |
| 189 | +// |
| 190 | +// try { |
| 191 | +// Class consClass = Class.forName(className); |
| 192 | +// this.displayingConstructor = consClass.getConstructor(paramTypes); |
| 193 | +// this.setDisplayingText(displayingConstructor.toString()); |
| 194 | +// init(); |
| 195 | +// linkPoints(rootElement); |
| 196 | +// |
| 197 | +// } catch (ClassNotFoundException | DOMException | NoSuchMethodException |
| 198 | +// | SecurityException e) { |
| 199 | +// JOptionPane.showMessageDialog(panel, e.toString()); |
| 200 | +// e.printStackTrace(); |
| 201 | +// } |
| 202 | + |
| 203 | + } |
| 204 | + |
| 205 | +} |
0 commit comments