|
| 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 | +/* |
| 26 | + * This is the interactive constructor paint component, must call evaluate |
| 27 | + * to update the instance this component manipulates before use the output. |
| 28 | + */ |
| 29 | +public class ClassConstructorPaintComponent extends DataTextIOPaintComponent |
| 30 | + implements DataFromPointDataProvider { |
| 31 | + |
| 32 | + private Constructor displayingConstructor; |
| 33 | + //Store a reference to the object this component operates. |
| 34 | + private Object instance; |
| 35 | + |
| 36 | + |
| 37 | + public ClassConstructorPaintComponent(Constructor displayingContructor, |
| 38 | + int x, int y) { |
| 39 | + super(displayingContructor.toString(), x, y); |
| 40 | + this.displayingConstructor = displayingContructor; |
| 41 | + init(); |
| 42 | + } |
| 43 | + |
| 44 | + private void init() { |
| 45 | + |
| 46 | + // parameters take place from line 1 to length |
| 47 | + Class[] paramTypes = displayingConstructor.getParameterTypes(); |
| 48 | + for (int i = 0; i < paramTypes.length; i++) { |
| 49 | + addToPoint(i + 1, new JavaType(paramTypes[i])); |
| 50 | + } |
| 51 | + |
| 52 | + // constructed instance take line length+1 |
| 53 | + addFromPoint(this, paramTypes.length + 1, |
| 54 | + new JavaType(this.displayingConstructor.getDeclaringClass())); |
| 55 | + |
| 56 | + // prepare String |
| 57 | + StringBuilder s = new StringBuilder(); |
| 58 | + s.append(this.displayingConstructor.toString() + "\n"); |
| 59 | + for (int i = 0; i < paramTypes.length; i++) { |
| 60 | + s.append("arg" + i + " :: " + paramTypes[i].getName() + "\n"); |
| 61 | + } |
| 62 | + |
| 63 | + s.append("Constructed Instance >>>> " + "\n"); |
| 64 | + setDisplayingText(s.toString()); |
| 65 | + |
| 66 | + } |
| 67 | + |
| 68 | + /** |
| 69 | + * Calculate the input data and store it. |
| 70 | + */ |
| 71 | + public void evaluate(DataFromPoint dataFromPoint){ |
| 72 | + // prepare argument list |
| 73 | + ArrayList<DataToPoint> toPoints = getToPoints(); |
| 74 | + Object[] args = new Object[toPoints.size()]; |
| 75 | + for (int i = 0; i < toPoints.size(); i++) { |
| 76 | + DataToPoint toPoint = toPoints.get(i); |
| 77 | + try { |
| 78 | + args[i] = toPoint.fetchData(); |
| 79 | + } catch (NoSuchElementException | NoConnectingLineSegmentException |
| 80 | + | DataFromPointNoDataProviderException |
| 81 | + | DataFromPointProviderCannotProvideDataException e) { |
| 82 | + e.printStackTrace(); |
| 83 | + // TODO Handle Exception |
| 84 | + throw new IllegalStateException(); |
| 85 | + } |
| 86 | + } |
| 87 | + try { |
| 88 | + instance = displayingConstructor.newInstance(args); |
| 89 | + } catch (InstantiationException | IllegalAccessException |
| 90 | + | IllegalArgumentException | InvocationTargetException e) { |
| 91 | + e.printStackTrace(); |
| 92 | + // TODO Handle Exception |
| 93 | + throw new IllegalStateException(); |
| 94 | + } |
| 95 | + } |
| 96 | + |
| 97 | + /** |
| 98 | + * Retrieve the data stored in this component |
| 99 | + * @return the data stored |
| 100 | + */ |
| 101 | + @Override |
| 102 | + public Object provideInformationToDataFromPoint( |
| 103 | + DataFromPoint dataFromPoint) { |
| 104 | + |
| 105 | + return instance; |
| 106 | + } |
| 107 | + |
| 108 | + /** |
| 109 | + * Check whether the data is good to return from this component |
| 110 | + * @param dataFromPoint |
| 111 | + * @return the return value |
| 112 | + */ |
| 113 | + @Override |
| 114 | + public boolean canProvideInformationToDataFromPoint( |
| 115 | + DataFromPoint dataFromPoint) { |
| 116 | + |
| 117 | + return instance != null; |
| 118 | + } |
| 119 | + |
| 120 | +} |
0 commit comments