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 15KB

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