You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

PropertyPanel.java 16KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469
  1. /* *************************************************************************
  2. IT Mill Toolkit
  3. Development of Browser User Interfaces Made Easy
  4. Copyright (C) 2000-2006 IT Mill Ltd
  5. *************************************************************************
  6. This product is distributed under commercial license that can be found
  7. from the product package on license.pdf. Use of this product might
  8. require purchasing a commercial license from IT Mill Ltd. For guidelines
  9. on usage, see licensing-guidelines.html
  10. *************************************************************************
  11. For more information, contact:
  12. IT Mill Ltd phone: +358 2 4802 7180
  13. Ruukinkatu 2-4 fax: +358 2 4802 7181
  14. 20540, Turku email: info@itmill.com
  15. Finland company www: www.itmill.com
  16. Primary source for information and releases: www.itmill.com
  17. ********************************************************************** */
  18. package com.itmill.toolkit.demo.features;
  19. import java.beans.BeanInfo;
  20. import java.beans.IntrospectionException;
  21. import java.beans.Introspector;
  22. import java.beans.PropertyDescriptor;
  23. import java.util.Date;
  24. import java.util.HashSet;
  25. import java.util.Iterator;
  26. import java.util.LinkedList;
  27. import com.itmill.toolkit.data.*;
  28. import com.itmill.toolkit.data.Property.ValueChangeListener;
  29. import com.itmill.toolkit.data.util.*;
  30. import com.itmill.toolkit.terminal.*;
  31. import com.itmill.toolkit.ui.*;
  32. public class PropertyPanel extends Panel implements Button.ClickListener,
  33. Property.ValueChangeListener {
  34. private Select addComponent;
  35. private OrderedLayout formsLayout = new OrderedLayout();
  36. private LinkedList forms = new LinkedList();
  37. private Button setButton = new Button("Set", this);
  38. private Button discardButton = new Button("Discard changes", this);
  39. private Button showAllProperties = new Button("List of All Properties",
  40. this);
  41. private Table allProperties = new Table();
  42. private Object objectToConfigure;
  43. private BeanItem config;
  44. /** Contruct new property panel for configuring given object. */
  45. public PropertyPanel(Object objectToConfigure) {
  46. super();
  47. // Layout
  48. setCaption("Properties");
  49. addComponent(formsLayout);
  50. // Target object
  51. this.objectToConfigure = objectToConfigure;
  52. config = new BeanItem(objectToConfigure);
  53. // Control buttons
  54. OrderedLayout buttons = new OrderedLayout(
  55. OrderedLayout.ORIENTATION_VERTICAL);
  56. buttons.addComponent(setButton);
  57. buttons.addComponent(discardButton);
  58. addComponent(buttons);
  59. // Add default properties
  60. addBasicComponentProperties();
  61. if (objectToConfigure instanceof Select)
  62. addSelectProperties();
  63. if (objectToConfigure instanceof AbstractField
  64. && !(objectToConfigure instanceof Table || objectToConfigure instanceof Tree))
  65. addFieldProperties();
  66. if ((objectToConfigure instanceof AbstractComponentContainer)
  67. && !(objectToConfigure instanceof FrameWindow))
  68. addComponentContainerProperties();
  69. // The list of all properties
  70. addComponent(showAllProperties);
  71. showAllProperties.setSwitchMode(true);
  72. allProperties.setVisible(false);
  73. allProperties.addContainerProperty("Name", String.class, "");
  74. allProperties.addContainerProperty("Type", String.class, "");
  75. allProperties.addContainerProperty("R/W", String.class, "");
  76. allProperties.addContainerProperty("Demo", String.class, "");
  77. allProperties.setColumnAlignments(new String[] { Table.ALIGN_LEFT,
  78. Table.ALIGN_LEFT, Table.ALIGN_CENTER, Table.ALIGN_CENTER });
  79. allProperties.setColumnHeaderMode(Table.COLUMN_HEADER_MODE_ID);
  80. updatePropertyList();
  81. addComponent(allProperties);
  82. }
  83. /** Add a formful of properties to property panel */
  84. public void addProperties(String propertySetCaption, Form properties) {
  85. // Create new panel containing the form
  86. Panel p = new Panel();
  87. p.setCaption(propertySetCaption);
  88. p.setStyle("light");
  89. p.addComponent(properties);
  90. formsLayout.addComponent(p);
  91. // Setup buffering
  92. setButton.dependsOn(properties);
  93. discardButton.dependsOn(properties);
  94. properties.setWriteThrough(false);
  95. properties.setReadThrough(true);
  96. // Maintain property lists
  97. forms.add(properties);
  98. updatePropertyList();
  99. }
  100. /** Handle all button clicks for this panel */
  101. public void buttonClick(Button.ClickEvent event) {
  102. // Commit all changed on all forms
  103. if (event.getButton() == setButton) {
  104. for (Iterator i = forms.iterator(); i.hasNext();)
  105. ((Form) i.next()).commit();
  106. }
  107. // Discard all changed on all forms
  108. if (event.getButton() == discardButton) {
  109. for (Iterator i = forms.iterator(); i.hasNext();)
  110. ((Form) i.next()).discard();
  111. }
  112. // Show property list
  113. if (event.getButton() == showAllProperties) {
  114. allProperties.setVisible(((Boolean) showAllProperties.getValue())
  115. .booleanValue());
  116. }
  117. }
  118. /** Recreate property list contents */
  119. public void updatePropertyList() {
  120. allProperties.removeAllItems();
  121. // Collect demoed properties
  122. HashSet listed = new HashSet();
  123. for (Iterator i = forms.iterator(); i.hasNext();)
  124. listed.addAll(((Form) i.next()).getItemPropertyIds());
  125. // Resolve all properties
  126. BeanInfo info;
  127. try {
  128. info = Introspector.getBeanInfo(objectToConfigure.getClass());
  129. } catch (IntrospectionException e) {
  130. throw new RuntimeException(e.toString());
  131. }
  132. PropertyDescriptor[] pd = info.getPropertyDescriptors();
  133. // Fill the table
  134. for (int i = 0; i < pd.length; i++) {
  135. allProperties.addItem(new Object[] { pd[i].getName(),
  136. pd[i].getPropertyType().getName(),
  137. (pd[i].getWriteMethod() == null ? "R" : "R/W"),
  138. (listed.contains(pd[i].getName()) ? "x" : "") }, pd[i]);
  139. }
  140. }
  141. /** Add basic properties implemented most often by abstract component */
  142. private void addBasicComponentProperties() {
  143. // Set of properties
  144. Form set = createBeanPropertySet(new String[] { "caption", "icon",
  145. "componentError", "description", "enabled", "visible", "style",
  146. "readOnly", "immediate" });
  147. // Icon
  148. set.replaceWithSelect("icon", new Object[] { null,
  149. new ThemeResource("icon/files/file.gif") }, new Object[] {
  150. "No icon", "Sample icon" });
  151. // Component error
  152. Throwable sampleException;
  153. try {
  154. throw new NullPointerException("sample exception");
  155. } catch (NullPointerException e) {
  156. sampleException = e;
  157. }
  158. set
  159. .replaceWithSelect(
  160. "componentError",
  161. new Object[] {
  162. null,
  163. new UserError("Sample text error message."),
  164. new UserError(
  165. "<h3>Error message formatting</h3><p>Error messages can "
  166. + "contain any UIDL formatting, like: <ul><li><b>Bold"
  167. + "</b></li><li><i>Italic</i></li></ul></p>",
  168. UserError.CONTENT_UIDL,
  169. ErrorMessage.INFORMATION),
  170. new SystemError(
  171. "This is an example of exception error reposting",
  172. sampleException) },
  173. new Object[] { "No error", "Sample text error",
  174. "Sample Formatted error", "Sample System Error" });
  175. // Style
  176. String currentStyle = ((Component) objectToConfigure).getStyle();
  177. if (currentStyle == null)
  178. set.replaceWithSelect("style", new Object[] { null },
  179. new Object[] { "Default" }).setNewItemsAllowed(true);
  180. else
  181. set.replaceWithSelect("style", new Object[] { null, currentStyle },
  182. new Object[] { "Default", currentStyle })
  183. .setNewItemsAllowed(true);
  184. // Set up descriptions
  185. set
  186. .getField("caption")
  187. .setDescription(
  188. "Component caption is the title of the component. Usage of the caption is optional and the "
  189. + "exact behavior of the propery is defined by the component. Setting caption null "
  190. + "or empty disables the caption.");
  191. set
  192. .getField("enabled")
  193. .setDescription(
  194. "Enabled property controls the usage of the component. If the component is disabled (enabled=false),"
  195. + " it can not receive any events from the terminal. In most cases it makes the usage"
  196. + " of the component easier, if the component visually looks disbled (for example is grayed), "
  197. + "when it can not be used.");
  198. set
  199. .getField("icon")
  200. .setDescription(
  201. "Icon of the component selects the main icon of the component. The usage of the icon is identical "
  202. + "to caption and in most components caption and icon are kept together. Icons can be "
  203. + "loaded from any resources (see Terminal/Resources for more information). Some components "
  204. + "contain more than just the captions icon. Those icons are controlled through their "
  205. + "own properties.");
  206. set
  207. .getField("visible")
  208. .setDescription(
  209. "Visibility property says if the component is renreded or not. Invisible components are implicitly "
  210. + "disabled, as there is no visible user interface to send event.");
  211. set
  212. .getField("description")
  213. .setDescription(
  214. "Description is designed to allow easy addition of short tooltips, like this. Like the caption,"
  215. + " setting description null or empty disables the description.");
  216. set
  217. .getField("readOnly")
  218. .setDescription(
  219. "Those components that have internal state that can be written are settable to readOnly-mode,"
  220. + " where the object can only be read, not written.");
  221. set
  222. .getField("componentError")
  223. .setDescription(
  224. "IT Mill Toolkit supports extensive error reporting. One part of the error reporting are component"
  225. + " errors that can be controlled by the programmer. This example only contains couple of "
  226. + "sample errors; to get the full picture, read browse ErrorMessage-interface implementors "
  227. + "API documentation.");
  228. set
  229. .getField("immediate")
  230. .setDescription(
  231. "Not all terminals can send the events immediately to server from all action. Web is the most "
  232. + "typical environment where many events (like textfield changed) are not sent to server, "
  233. + "before they are explicitly submitted. Setting immediate property true (by default this "
  234. + "is false for most components), the programmer can assure that the application is"
  235. + " notified as soon as possible about the value change in this component.");
  236. set
  237. .getField("style")
  238. .setDescription(
  239. "Themes specify the overall looks of the user interface. In addition component can have a set of "
  240. + "styles, that can be visually very different (like datefield calendar- and text-styles), "
  241. + "but contain the same logical functionality. As a rule of thumb, theme specifies if a "
  242. + "component is blue or yellow and style determines how the component is used.");
  243. // Add created fields to property panel
  244. addProperties("Component Basics", set);
  245. }
  246. /** Add properties for selecting */
  247. private void addSelectProperties() {
  248. Form set = createBeanPropertySet(new String[] { "newItemsAllowed",
  249. "lazyLoading", "multiSelect" });
  250. addProperties("Select Properties", set);
  251. set.getField("multiSelect").setDescription(
  252. "Specified if multiple items can be selected at once.");
  253. set
  254. .getField("newItemsAllowed")
  255. .setDescription(
  256. "Select component (but not Tree or Table) can allow the user to directly "
  257. + "add new items to set of options. The new items are constrained to be "
  258. + "strings and thus feature only applies to simple lists.");
  259. Button ll = (Button) set.getField("lazyLoading");
  260. ll
  261. .setDescription("In Ajax rendering mode select supports lazy loading and filtering of options.");
  262. ll.addListener((ValueChangeListener) this);
  263. ll.setImmediate(true);
  264. if (((Boolean) ll.getValue()).booleanValue()) {
  265. set.getField("multiSelect").setVisible(false);
  266. set.getField("newItemsAllowed").setVisible(false);
  267. }
  268. if (objectToConfigure instanceof Tree
  269. || objectToConfigure instanceof Table) {
  270. set.removeItemProperty("newItemsAllowed");
  271. set.removeItemProperty("lazyLoading");
  272. }
  273. }
  274. /** Field special properties */
  275. private void addFieldProperties() {
  276. // TODO This is temporarily disabled, until bug #211 is fixed
  277. /*
  278. * Form set = new Form(new GridLayout(COLUMNS, 1));
  279. * set.addField("focus", new Button("Focus", objectToConfigure,
  280. * "focus")); set.getField("focus").setDescription( "Focus the cursor to
  281. * this field. Not all " + "components and/or terminals support this
  282. * feature."); addProperties("Field Features", set);
  283. */
  284. }
  285. /**
  286. * Add and remove some miscellaneous example component to/from component
  287. * container
  288. */
  289. private void addComponentContainerProperties() {
  290. Form set = new Form(new OrderedLayout(
  291. OrderedLayout.ORIENTATION_VERTICAL));
  292. addComponent = new Select();
  293. addComponent.setImmediate(true);
  294. addComponent.addItem("Add component to container");
  295. addComponent.setNullSelectionItemId("Add field");
  296. addComponent.addItem("Text field");
  297. addComponent.addItem("Time");
  298. addComponent.addItem("Option group");
  299. addComponent.addItem("Calendar");
  300. addComponent.addListener(this);
  301. set.addField("component adder", addComponent);
  302. set.addField("remove all components", new Button(
  303. "Remove all components", objectToConfigure,
  304. "removeAllComponents"));
  305. addProperties("ComponentContainer Features", set);
  306. }
  307. /** Value change listener for listening selections */
  308. public void valueChange(Property.ValueChangeEvent event) {
  309. // Adding components to component container
  310. if (event.getProperty() == addComponent) {
  311. String value = (String) addComponent.getValue();
  312. if (value != null) {
  313. // TextField component
  314. if (value.equals("Text field"))
  315. ((AbstractComponentContainer) objectToConfigure)
  316. .addComponent(new TextField("Test field"));
  317. // DateField time style
  318. if (value.equals("Time")) {
  319. DateField d = new DateField("Time", new Date());
  320. d
  321. .setDescription("This is a DateField-component with text-style");
  322. d.setResolution(DateField.RESOLUTION_MIN);
  323. d.setStyle("text");
  324. ((AbstractComponentContainer) objectToConfigure)
  325. .addComponent(d);
  326. }
  327. // Date field calendar style
  328. if (value.equals("Calendar")) {
  329. DateField c = new DateField("Calendar", new Date());
  330. c
  331. .setDescription("DateField-component with calendar-style and day-resolution");
  332. c.setStyle("calendar");
  333. c.setResolution(DateField.RESOLUTION_DAY);
  334. ((AbstractComponentContainer) objectToConfigure)
  335. .addComponent(c);
  336. }
  337. // Select option group style
  338. if (value.equals("Option group")) {
  339. Select s = new Select("Options");
  340. s.setDescription("Select-component with optiongroup-style");
  341. s.addItem("Linux");
  342. s.addItem("Windows");
  343. s.addItem("Solaris");
  344. s.addItem("Symbian");
  345. s.setStyle("optiongroup");
  346. ((AbstractComponentContainer) objectToConfigure)
  347. .addComponent(s);
  348. }
  349. addComponent.setValue(null);
  350. }
  351. } else if (event.getProperty() == getField("lazyLoading")) {
  352. boolean newValue = ((Boolean) event.getProperty().getValue())
  353. .booleanValue();
  354. Field multiselect = getField("multiSelect");
  355. Field newitems = getField("newItemsAllowed");
  356. if (newValue) {
  357. newitems.setValue(Boolean.FALSE);
  358. newitems.setVisible(false);
  359. multiselect.setValue(Boolean.FALSE);
  360. multiselect.setVisible(false);
  361. } else {
  362. newitems.setVisible(true);
  363. multiselect.setVisible(true);
  364. }
  365. }
  366. }
  367. /**
  368. * Helper function for creating forms from array of propety names.
  369. */
  370. protected Form createBeanPropertySet(String names[]) {
  371. Form set = new Form(new OrderedLayout(
  372. OrderedLayout.ORIENTATION_VERTICAL));
  373. for (int i = 0; i < names.length; i++) {
  374. Property p = config.getItemProperty(names[i]);
  375. if (p != null) {
  376. set.addItemProperty(names[i], p);
  377. Field f = set.getField(names[i]);
  378. if (f instanceof TextField) {
  379. if (Integer.class.equals(p.getType()))
  380. ((TextField) f).setColumns(4);
  381. else {
  382. ((TextField) f).setNullSettingAllowed(true);
  383. ((TextField) f).setColumns(24);
  384. }
  385. }
  386. }
  387. }
  388. return set;
  389. }
  390. /** Find a field from all forms */
  391. public Field getField(Object propertyId) {
  392. for (Iterator i = forms.iterator(); i.hasNext();) {
  393. Form f = (Form) i.next();
  394. Field af = f.getField(propertyId);
  395. if (af != null)
  396. return af;
  397. }
  398. return null;
  399. }
  400. }