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

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