|
| 1 | +package paintcomponents.java.lazy; |
| 2 | + |
| 3 | +import java.lang.reflect.Field; |
| 4 | +import java.util.ArrayList; |
| 5 | +import java.util.NoSuchElementException; |
| 6 | + |
| 7 | +import paintcomponents.NoConnectingLineSegmentException; |
| 8 | +import paintcomponents.data.DataFromPoint; |
| 9 | +import paintcomponents.data.DataFromPointDataProvider; |
| 10 | +import paintcomponents.data.DataFromPointNoDataProviderException; |
| 11 | +import paintcomponents.data.DataFromPointProviderCannotProvideDataException; |
| 12 | +import paintcomponents.data.DataTextIOPaintComponent; |
| 13 | + |
| 14 | +public class FieldsPaintComponent extends DataTextIOPaintComponent implements DataFromPointDataProvider{ |
| 15 | + |
| 16 | + private Class displayingClass; |
| 17 | + |
| 18 | + public FieldsPaintComponent(Class displayingClass, int x, int y) { |
| 19 | + super(displayingClass.getName(), x, y); |
| 20 | + this.displayingClass = displayingClass; |
| 21 | + |
| 22 | + init(); |
| 23 | + } |
| 24 | + |
| 25 | + private void init() { |
| 26 | + /* |
| 27 | + * Line 1 class name |
| 28 | + * line 2 in instance |
| 29 | + * line 3 .. the fields |
| 30 | + */ |
| 31 | + Field[] fields = displayingClass.getFields(); |
| 32 | + |
| 33 | + //the left receiving instance |
| 34 | + addToPoint(1); |
| 35 | + |
| 36 | + for (int i = 0; i < fields.length; i++) { |
| 37 | + addFromPoint(this, i + 2); |
| 38 | + } |
| 39 | + |
| 40 | + |
| 41 | + // prepare String |
| 42 | + StringBuilder s = new StringBuilder(); |
| 43 | + s.append(this.displayingClass.toString() + "\n"); |
| 44 | + s.append(">>> Operating Instance :: " |
| 45 | + + "\n"); |
| 46 | + for (int i = 0; i < fields.length; i++) { |
| 47 | + s.append("arg" + i + " :: " + fields[i].getName() + "\n"); |
| 48 | + } |
| 49 | + |
| 50 | + setDisplayingText(s.toString()); |
| 51 | + |
| 52 | + } |
| 53 | + |
| 54 | + @Override |
| 55 | + public Object provideInformationToDataFromPoint( |
| 56 | + DataFromPoint dataFromPoint) { |
| 57 | + int index = getFromPoints().indexOf(dataFromPoint); |
| 58 | + Field[] fields = displayingClass.getFields(); |
| 59 | + Field field = fields[index]; |
| 60 | + |
| 61 | + Object operatingInstance = null; |
| 62 | + try { |
| 63 | + operatingInstance = getToPoints().get(0).fetchData(); |
| 64 | + } catch (NoSuchElementException | NoConnectingLineSegmentException |
| 65 | + | DataFromPointNoDataProviderException |
| 66 | + | DataFromPointProviderCannotProvideDataException e1) { |
| 67 | + e1.printStackTrace(); |
| 68 | + // TODO Handle Exception |
| 69 | + // Note: a static method may not contain an valid instance |
| 70 | + // throw new IllegalStateException(); |
| 71 | + } |
| 72 | + |
| 73 | + try { |
| 74 | + return field.get(operatingInstance); |
| 75 | + } catch (IllegalArgumentException | IllegalAccessException e) { |
| 76 | + // TODO Auto-generated catch block |
| 77 | + e.printStackTrace(); |
| 78 | + // TODO Handle Exception |
| 79 | + throw new IllegalStateException(); |
| 80 | + } |
| 81 | + } |
| 82 | + |
| 83 | + @Override |
| 84 | + public boolean canProvideInformationToDataFromPoint( |
| 85 | + DataFromPoint dataFromPoint) { |
| 86 | + //TODO IMPORTANT Implement this method |
| 87 | + return true; |
| 88 | + } |
| 89 | + |
| 90 | +} |
0 commit comments