1 package org.catacomb.druid.build; 2 3 import org.catacomb.interlish.annotation.ControlPoint; 4 import org.catacomb.interlish.annotation.Editable; 5 import org.catacomb.interlish.annotation.IOPoint; 6 import org.catacomb.interlish.content.*; 7 import org.catacomb.interlish.structure.*; 8 import org.catacomb.report.E; 9 10 11 import java.lang.annotation.*; 12 import java.lang.reflect.Field; 13 14 15 public class AnnotationConnector { 16 17 TargetStore targetStore; 18 19 20 public AnnotationConnector(TargetStore tgs) { 21 targetStore = tgs; 22 } 23 24 25 26 public void annotationConnect(Object ctrl) { 27 28 int nanot = 0; 29 for (Field fld : ctrl.getClass().getFields()) { 30 Annotation[] aa = fld.getDeclaredAnnotations(); 31 if (aa != null && aa.length > 0) { 32 nanot += aa.length; 33 for (Annotation ant : aa) { 34 35 if (ant instanceof IOPoint) { 36 ioPointConnect(ctrl, fld, ((IOPoint)ant).xid()); 37 38 } else if (ant instanceof Editable) { 39 editableConnect(ctrl, fld, ((Editable)ant).xid()); 40 41 } else if (ant instanceof ControlPoint) { 42 controlConnect(ctrl, fld, ((ControlPoint)ant).xid()); 43 44 } else { 45 E.warning("unhandled annotation " + ant); 46 } 47 48 } 49 } 50 } 51 52 53 int nta = 0; 54 for (Field fld : ctrl.getClass().getDeclaredFields()) { 55 Annotation[] aa = fld.getDeclaredAnnotations(); 56 if (aa != null) { 57 nta += aa.length; 58 } 59 } 60 if (nta > nanot) { 61 E.shortError("Class " + ctrl.getClass() + " has unused annotations\n " + 62 "(anotatiosn of private fields) - shoud these be public fields?"); 63 } 64 65 66 } 67 68 69 private void editableConnect(Object ctrl, Field fld, String guiID) { 70 Object fval = null; 71 72 try { 73 fval = fld.get(ctrl); 74 } catch (Exception ex) { 75 E.warning("cant get field " + fld + " on " + ctrl); 76 } 77 78 if (fval == null) { 79 E.warning("all editable fields should be set in the constructor - " + 80 "not so for " + fld.getName() + " on " + ctrl); 81 } else { 82 83 Object cpt = targetStore.get(guiID); 84 if (cpt == null) { 85 E.error("No such cpt in target store : " + guiID); 86 87 } else { 88 89 setEditable(fval, cpt); 90 91 } 92 } 93 } 94 95 96 private void setEditable(Object fval, Object edtr) { 97 if (fval instanceof StringValue && edtr instanceof StringValueEditor) { 98 ((StringValueEditor)edtr).setStringValue((StringValue)fval); 99 100 } else if (fval instanceof BooleanValue && edtr instanceof BooleanValueEditor) { 101 ((BooleanValueEditor)edtr).setBooleanValue((BooleanValue)fval); 102 103 } else if (fval instanceof IntegerValue && edtr instanceof IntegerValueEditor) { 104 ((IntegerValueEditor)edtr).setIntegerValue((IntegerValue)fval); 105 106 } else if (fval instanceof DoubleValue && edtr instanceof DoubleValueEditor) { 107 ((DoubleValueEditor)edtr).setDoubleValue((DoubleValue)fval); 108 109 } else if (fval instanceof ColorValue && edtr instanceof ColorValueEditor) { 110 ((ColorValueEditor)edtr).setColorValue((ColorValue)fval); 111 112 } else { 113 E.error("cant connect val to editor " + fval + " " + edtr); 114 } 115 } 116 117 118 119 private void controlConnect(Object ctrl, Field fld, String guiID) { 120 Object cpt = targetStore.get(guiID); 121 122 if (cpt == null) { 123 E.linkToWarning("No such cpt in target store : " + guiID + " when connecting " + 124 "controller ", ctrl); 125 targetStore.printAvailable(); 126 127 } else { 128 Object subctrl = null; 129 if (cpt instanceof Druid) { 130 subctrl = ((Druid)cpt).getController(); 131 } else if (cpt instanceof Controller) { 132 subctrl = cpt; 133 } else { 134 E.error("cant control connect " + cpt + " " + cpt.getClass().getName()); 135 } 136 if (subctrl != null) { 137 try { 138 fld.set(ctrl, subctrl); 139 140 } catch (Exception ex) { 141 E.error("cant set gui cpt in controller: " + guiID + " cpt is " + cpt 142 + " but field needs " + fld.getType()); 143 } 144 } 145 } 146 } 147 148 149 private void ioPointConnect(Object ctrl, Field fld, String guiID) { 150 Object cpt = targetStore.get(guiID); 151 152 153 if (cpt == null) { 154 E.linkToWarning("No such cpt in target store : " + guiID + " when connecting " + 155 "controller ", ctrl); 156 targetStore.printAvailable(); 157 158 } else { 159 160 try { 161 fld.set(ctrl, cpt); 162 163 } catch (Exception ex) { 164 E.error("cant set gui cpt in controller: " + guiID + " cpt is " + cpt 165 + " but field needs " + fld.getType()); 166 } 167 } 168 169 } 170 171 172 173 174 175 }