PageRenderTime 46ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/src/fwt/java/Prop.java

https://bitbucket.org/bedlaczech/fan-1.0
Java | 505 lines | 333 code | 111 blank | 61 comment | 41 complexity | cc181e9edd114fb681550f01e261d77b MD5 | raw file
Possible License(s): CC-BY-SA-3.0
  1. //
  2. // Copyright (c) 2008, Brian Frank and Andy Frank
  3. // Licensed under the Academic Free License version 3.0
  4. //
  5. // History:
  6. // 20 Jul 08 Brian Frank Creation
  7. //
  8. package fan.fwt;
  9. import fan.sys.*;
  10. import fan.sys.List;
  11. import fan.gfx.Size;
  12. import fan.gfx.Halign;
  13. import fan.gfx.Valign;
  14. import org.eclipse.swt.*;
  15. import org.eclipse.swt.graphics.Color;
  16. import org.eclipse.swt.graphics.Font;
  17. import org.eclipse.swt.graphics.Image;
  18. import org.eclipse.swt.graphics.Point;
  19. import org.eclipse.swt.widgets.Widget;
  20. import org.eclipse.swt.widgets.Control;
  21. import org.eclipse.swt.widgets.ToolItem;
  22. /**
  23. * Props are used to manage a field's value and how they
  24. * bind to the SWT getter/setters. Each Prop is responsible
  25. * for caching the value until the widget can be mounted and
  26. * mapped to a SWT control.
  27. */
  28. public abstract class Prop
  29. {
  30. //////////////////////////////////////////////////////////////////////////
  31. // Construction
  32. //////////////////////////////////////////////////////////////////////////
  33. public Prop(WidgetPeer peer)
  34. {
  35. this.peer = peer;
  36. }
  37. public abstract void syncToControl();
  38. public abstract void syncFromControl();
  39. //////////////////////////////////////////////////////////////////////////
  40. // BoolProp
  41. //////////////////////////////////////////////////////////////////////////
  42. public static abstract class BoolProp extends Prop
  43. {
  44. public BoolProp(WidgetPeer peer, boolean def)
  45. {
  46. super(peer);
  47. this.val = def;
  48. }
  49. public void syncToControl() { set(val); }
  50. public void syncFromControl() { val = get(); }
  51. public boolean get()
  52. {
  53. Widget w = peer.control;
  54. return w == null ? val : get(w);
  55. }
  56. public void set(boolean v)
  57. {
  58. Widget w = peer.control;
  59. if (w == null)
  60. val = v;
  61. else
  62. set(w, v);
  63. }
  64. public abstract boolean get(Widget w);
  65. public abstract void set(Widget w, boolean v);
  66. protected boolean val;
  67. }
  68. //////////////////////////////////////////////////////////////////////////
  69. // IntProp
  70. //////////////////////////////////////////////////////////////////////////
  71. public static abstract class IntProp extends Prop
  72. {
  73. public IntProp(WidgetPeer peer, int def) { this(peer, def, false); }
  74. public IntProp(WidgetPeer peer, int def, boolean negIsNull)
  75. {
  76. super(peer);
  77. this.val = Long.valueOf(def);
  78. this.negIsNull = negIsNull;
  79. }
  80. public void syncToControl() { set(val); }
  81. public void syncFromControl() { val = get(); }
  82. public Long get()
  83. {
  84. Widget w = peer.control;
  85. if (w == null) return val;
  86. int i = get(w);
  87. if (negIsNull && i < 0) return null;
  88. return Long.valueOf(i);
  89. }
  90. public void set(Long v)
  91. {
  92. Widget w = peer.control;
  93. if (w == null)
  94. val = v;
  95. else
  96. set(w, v.intValue());
  97. }
  98. public abstract int get(Widget w);
  99. public abstract void set(Widget w, int v);
  100. protected Long val;
  101. protected boolean negIsNull;
  102. }
  103. //////////////////////////////////////////////////////////////////////////
  104. // StrProp
  105. //////////////////////////////////////////////////////////////////////////
  106. public static abstract class StrProp extends Prop
  107. {
  108. public StrProp(WidgetPeer peer, String def)
  109. {
  110. super(peer);
  111. this.val = def;
  112. }
  113. public void syncToControl() { set(val); }
  114. public void syncFromControl() { val = get(); }
  115. public String get()
  116. {
  117. Widget w = peer.control;
  118. return w == null ? val : get(w);
  119. }
  120. public void set(String v)
  121. {
  122. Widget w = peer.control;
  123. if (w == null)
  124. val = v;
  125. else
  126. set(w, v == null ? null : v);
  127. }
  128. public abstract String get(Widget w);
  129. public abstract void set(Widget w, String v);
  130. protected String val;
  131. }
  132. //////////////////////////////////////////////////////////////////////////
  133. // PosProp
  134. //////////////////////////////////////////////////////////////////////////
  135. public static class PosProp extends Prop
  136. {
  137. public PosProp(WidgetPeer peer) { super(peer); }
  138. public void syncToControl() { set(val); }
  139. public void syncFromControl() { val = get(); }
  140. public fan.gfx.Point get()
  141. {
  142. if (peer.control instanceof Control)
  143. {
  144. Control c = (Control)peer.control;
  145. val = WidgetPeer.point(c.getLocation());
  146. }
  147. else if (peer.control instanceof ToolItem)
  148. {
  149. ToolItem ti = (ToolItem)peer.control;
  150. val = WidgetPeer.point(ti.getBounds());
  151. }
  152. return val;
  153. }
  154. public void set(fan.gfx.Point v)
  155. {
  156. val = v;
  157. if (peer.control instanceof Control)
  158. {
  159. Control c = (Control)peer.control;
  160. c.setLocation((int)v.x, (int)v.y);
  161. }
  162. }
  163. protected fan.gfx.Point val = fan.gfx.Point.defVal;
  164. }
  165. //////////////////////////////////////////////////////////////////////////
  166. // SizeProp
  167. //////////////////////////////////////////////////////////////////////////
  168. public static class SizeProp extends Prop
  169. {
  170. public SizeProp(WidgetPeer peer) { super(peer); }
  171. public void syncToControl() { set(val); }
  172. public void syncFromControl() { val = get(); }
  173. public Size get()
  174. {
  175. if (peer.control instanceof Control)
  176. {
  177. Control c = (Control)peer.control;
  178. val = WidgetPeer.size(c.getSize());
  179. }
  180. else if (peer.control instanceof ToolItem)
  181. {
  182. ToolItem ti = (ToolItem)peer.control;
  183. val = WidgetPeer.size(ti.getBounds());
  184. }
  185. return val;
  186. }
  187. public void set(Size v)
  188. {
  189. val = v;
  190. if (peer.control instanceof Control)
  191. {
  192. Control c = (Control)peer.control;
  193. c.setSize((int)v.w, (int)v.h);
  194. }
  195. }
  196. protected Size val = Size.defVal;
  197. }
  198. //////////////////////////////////////////////////////////////////////////
  199. // ItemsProp
  200. //////////////////////////////////////////////////////////////////////////
  201. public static abstract class ItemsProp extends Prop
  202. {
  203. public ItemsProp(WidgetPeer peer)
  204. {
  205. super(peer);
  206. }
  207. public void syncToControl() { set(val); }
  208. public void syncFromControl() {}
  209. public List get() { return val; }
  210. public void set(List v)
  211. {
  212. val = v.ro();
  213. Widget w = peer.control;
  214. if (w != null)
  215. set(w, val.toStrings());
  216. }
  217. public abstract void set(Widget w, String[] v);
  218. protected List val = new List(Sys.ObjType).ro();
  219. }
  220. //////////////////////////////////////////////////////////////////////////
  221. // IntsProp
  222. //////////////////////////////////////////////////////////////////////////
  223. public static abstract class IntsProp extends Prop
  224. {
  225. public IntsProp(WidgetPeer peer)
  226. {
  227. super(peer);
  228. }
  229. public void syncToControl() { set(val); }
  230. public void syncFromControl() { val = get(); }
  231. public List get()
  232. {
  233. Widget w = peer.control;
  234. if (w == null) return val;
  235. int[] ints = get(w);
  236. val = new List(Sys.IntType);
  237. for (int i=0; i<ints.length; ++i) val.add(Long.valueOf(ints[i]));
  238. return val;
  239. }
  240. public void set(List v)
  241. {
  242. Widget w = peer.control;
  243. val = v == null ? null : v.ro();
  244. if (w != null)
  245. set(w, val == null ? null : val.toInts());
  246. }
  247. public abstract int[] get(Widget w);
  248. public abstract void set(Widget w, int[] v);
  249. protected List val = new List(Sys.IntType).ro();
  250. }
  251. //////////////////////////////////////////////////////////////////////////
  252. // CursorProp
  253. //////////////////////////////////////////////////////////////////////////
  254. public static class CursorProp extends Prop
  255. {
  256. public CursorProp(WidgetPeer peer) { super(peer); }
  257. public void syncToControl() { set(val); }
  258. public void syncFromControl() { val = get(); }
  259. public Cursor get() { return val; }
  260. public void set(Cursor v)
  261. {
  262. val = v;
  263. if (peer.control instanceof Control)
  264. {
  265. ((Control)peer.control).setCursor(Fwt.get().cursor(val));
  266. }
  267. }
  268. protected Cursor val = null;
  269. }
  270. //////////////////////////////////////////////////////////////////////////
  271. // ColorProp
  272. //////////////////////////////////////////////////////////////////////////
  273. public static abstract class ColorProp extends Prop
  274. {
  275. public ColorProp(WidgetPeer peer)
  276. {
  277. super(peer);
  278. }
  279. public void syncToControl() { set(val); }
  280. public void syncFromControl() {}
  281. public fan.gfx.Color get() { return val; }
  282. public void set(fan.gfx.Color v)
  283. {
  284. val = v;
  285. Widget w = peer.control;
  286. if (w != null)
  287. set(w, Fwt.get().color(val));
  288. }
  289. public abstract void set(Widget w, Color v);
  290. protected fan.gfx.Color val;
  291. }
  292. //////////////////////////////////////////////////////////////////////////
  293. // ImageProp
  294. //////////////////////////////////////////////////////////////////////////
  295. public static abstract class ImageProp extends Prop
  296. {
  297. public ImageProp(WidgetPeer peer)
  298. {
  299. super(peer);
  300. }
  301. public void syncToControl() { set(val); }
  302. public void syncFromControl() {}
  303. public fan.gfx.Image get() { return val; }
  304. public void set(fan.gfx.Image v)
  305. {
  306. val = v;
  307. Widget w = peer.control;
  308. if (w != null)
  309. set(w, Fwt.get().image(val));
  310. }
  311. public abstract void set(Widget w, Image v);
  312. protected fan.gfx.Image val;
  313. }
  314. //////////////////////////////////////////////////////////////////////////
  315. // FontProp
  316. //////////////////////////////////////////////////////////////////////////
  317. public static abstract class FontProp extends Prop
  318. {
  319. public FontProp(WidgetPeer peer)
  320. {
  321. super(peer);
  322. }
  323. public void syncToControl() { set(val); }
  324. public void syncFromControl() {}
  325. public fan.gfx.Font get() { return val; }
  326. public void set(fan.gfx.Font v)
  327. {
  328. val = v;
  329. Widget w = peer.control;
  330. if (w != null)
  331. set(w, Fwt.get().font(val));
  332. }
  333. public abstract void set(Widget w, Font v);
  334. protected fan.gfx.Font val;
  335. }
  336. //////////////////////////////////////////////////////////////////////////
  337. // KeyProp
  338. //////////////////////////////////////////////////////////////////////////
  339. public static abstract class KeyProp extends Prop
  340. {
  341. public KeyProp(WidgetPeer peer)
  342. {
  343. super(peer);
  344. }
  345. public void syncToControl() { set(val); }
  346. public void syncFromControl() {}
  347. public fan.fwt.Key get() { return val; }
  348. public void set(fan.fwt.Key v)
  349. {
  350. val = v;
  351. Widget w = peer.control;
  352. if (w != null)
  353. set(w, WidgetPeer.accelerator(val));
  354. }
  355. public abstract void set(Widget w, int v);
  356. protected fan.fwt.Key val;
  357. }
  358. //////////////////////////////////////////////////////////////////////////
  359. // HalignProp
  360. //////////////////////////////////////////////////////////////////////////
  361. public static abstract class HalignProp extends Prop
  362. {
  363. public HalignProp(WidgetPeer peer, Halign def)
  364. {
  365. super(peer);
  366. this.val = def;
  367. }
  368. public void syncToControl() { set(val); }
  369. public void syncFromControl() {}
  370. public Halign get() { return val; }
  371. public void set(Halign v)
  372. {
  373. val = v;
  374. Widget w = peer.control;
  375. if (w != null)
  376. set(w, WidgetPeer.style(val));
  377. }
  378. public abstract void set(Widget w, int v);
  379. protected Halign val;
  380. }
  381. //////////////////////////////////////////////////////////////////////////
  382. // CustomProp
  383. //////////////////////////////////////////////////////////////////////////
  384. public static abstract class Custom extends Prop
  385. {
  386. public Custom(WidgetPeer peer) { super(peer); }
  387. public abstract Object get();
  388. public abstract void set(Object val);
  389. }
  390. //////////////////////////////////////////////////////////////////////////
  391. // Fields
  392. //////////////////////////////////////////////////////////////////////////
  393. final WidgetPeer peer;
  394. }