123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456 |
- /* *************************************************************************
-
- IT Mill Toolkit
-
- Development of Browser User Interfaces Made Easy
-
- Copyright (C) 2000-2006 IT Mill Ltd
-
- *************************************************************************
-
- This product is distributed under commercial license that can be found
- from the product package on license.pdf. Use of this product might
- require purchasing a commercial license from IT Mill Ltd. For guidelines
- on usage, see licensing-guidelines.html
-
- *************************************************************************
-
- For more information, contact:
-
- IT Mill Ltd phone: +358 2 4802 7180
- Ruukinkatu 2-4 fax: +358 2 4802 7181
- 20540, Turku email: info@itmill.com
- Finland company www: www.itmill.com
-
- Primary source for information and releases: www.itmill.com
-
- ********************************************************************** */
-
- package com.itmill.toolkit.demo.features;
-
- import java.beans.BeanInfo;
- import java.beans.IntrospectionException;
- import java.beans.Introspector;
- import java.beans.PropertyDescriptor;
- import java.util.Date;
- import java.util.HashSet;
- import java.util.Iterator;
- import java.util.LinkedList;
-
- import com.itmill.toolkit.data.*;
- import com.itmill.toolkit.data.util.*;
- import com.itmill.toolkit.terminal.*;
- import com.itmill.toolkit.ui.*;
-
- public class PropertyPanel
- extends Panel
- implements Button.ClickListener, Property.ValueChangeListener {
-
- private Select addComponent;
- private OrderedLayout formsLayout = new OrderedLayout();
- private LinkedList forms = new LinkedList();
- private Button setButton = new Button("Set", this);
- private Button discardButton = new Button("Discard changes", this);
- private Button showAllProperties =
- new Button("List of All Properties", this);
- private Table allProperties = new Table();
- private Object objectToConfigure;
- private BeanItem config;
- protected static final int COLUMNS = 3;
-
- /** Contruct new property panel for configuring given object. */
- public PropertyPanel(Object objectToConfigure) {
- super();
-
- // Layout
- setCaption("Properties");
- addComponent(formsLayout);
-
- // Target object
- this.objectToConfigure = objectToConfigure;
- config = new BeanItem(objectToConfigure);
-
- // Control buttons
- OrderedLayout buttons =
- new OrderedLayout(OrderedLayout.ORIENTATION_HORIZONTAL);
- buttons.addComponent(setButton);
- buttons.addComponent(discardButton);
- addComponent(buttons);
-
- // Add default properties
- addBasicComponentProperties();
- if (objectToConfigure instanceof Select)
- addSelectProperties();
- if (objectToConfigure instanceof AbstractField
- && !(objectToConfigure instanceof Table
- || objectToConfigure instanceof Tree))
- addFieldProperties();
- if ((objectToConfigure instanceof AbstractComponentContainer)
- && !(objectToConfigure instanceof FrameWindow))
- addComponentContainerProperties();
-
- // The list of all properties
- addComponent(showAllProperties);
- showAllProperties.setSwitchMode(true);
- allProperties.setVisible(false);
- allProperties.addContainerProperty("Name", String.class, "");
- allProperties.addContainerProperty("Type", String.class, "");
- allProperties.addContainerProperty("R/W", String.class, "");
- allProperties.addContainerProperty("Demo", String.class, "");
- allProperties.setColumnAlignments(
- new String[] {
- Table.ALIGN_LEFT,
- Table.ALIGN_LEFT,
- Table.ALIGN_CENTER,
- Table.ALIGN_CENTER });
- allProperties.setColumnHeaderMode(Table.COLUMN_HEADER_MODE_ID);
- updatePropertyList();
- addComponent(allProperties);
- }
-
- /** Add a formful of properties to property panel */
- public void addProperties(String propertySetCaption, Form properties) {
-
- // Create new panel containing the form
- Panel p = new Panel();
- p.setWidth(600);
- p.setCaption(propertySetCaption);
- p.setStyle("light");
- p.addComponent(properties);
- formsLayout.addComponent(p);
-
- // Setup buffering
- setButton.dependsOn(properties);
- discardButton.dependsOn(properties);
- properties.setWriteThrough(false);
- properties.setReadThrough(true);
-
- // Maintain property lists
- forms.add(properties);
- updatePropertyList();
- }
-
- /** Handle all button clicks for this panel */
- public void buttonClick(Button.ClickEvent event) {
-
- // Commit all changed on all forms
- if (event.getButton() == setButton) {
- for (Iterator i = forms.iterator(); i.hasNext();)
- ((Form) i.next()).commit();
- }
-
- // Discard all changed on all forms
- if (event.getButton() == discardButton) {
- for (Iterator i = forms.iterator(); i.hasNext();)
- ((Form) i.next()).discard();
- }
-
- // Show property list
- if (event.getButton() == showAllProperties) {
- allProperties.setVisible(
- ((Boolean) showAllProperties.getValue()).booleanValue());
- }
- }
-
- /** Recreate property list contents */
- public void updatePropertyList() {
-
- allProperties.removeAllItems();
-
- // Collect demoed properties
- HashSet listed = new HashSet();
- for (Iterator i = forms.iterator(); i.hasNext();)
- listed.addAll(((Form) i.next()).getItemPropertyIds());
-
- // Resolve all properties
- BeanInfo info;
- try {
- info = Introspector.getBeanInfo(objectToConfigure.getClass());
- } catch (IntrospectionException e) {
- throw new RuntimeException(e.toString());
- }
- PropertyDescriptor[] pd = info.getPropertyDescriptors();
-
- // Fill the table
- for (int i = 0; i < pd.length; i++) {
- allProperties.addItem(
- new Object[] {
- pd[i].getName(),
- pd[i].getPropertyType().getName(),
- (pd[i].getWriteMethod() == null ? "R" : "R/W"),
- (listed.contains(pd[i].getName()) ? "x" : "")},
- pd[i]);
- }
- }
-
- /** Add basic properties implemented most often by abstract component */
- private void addBasicComponentProperties() {
-
- // Set of properties
- Form set =
- createBeanPropertySet(
- new String[] {
- "caption",
- "icon",
- "componentError",
- "description",
- "enabled",
- "visible",
- "style",
- "readOnly",
- "immediate" });
-
- // Icon
- set.replaceWithSelect(
- "icon",
- new Object[] { null, new ThemeResource("icon/files/file.gif")},
- new Object[] { "No icon", "Sample icon" });
-
- // Component error
- Throwable sampleException;
- try {
- throw new NullPointerException("sample exception");
- } catch (NullPointerException e) {
- sampleException = e;
- }
- set.replaceWithSelect(
- "componentError",
- new Object[] {
- null,
- new UserError("Sample text error message."),
- new UserError(
- "<h3>Error message formatting</h3><p>Error messages can "
- + "contain any UIDL formatting, like: <ul><li><b>Bold"
- + "</b></li><li><i>Italic</i></li></ul></p>",
- UserError.CONTENT_UIDL,
- ErrorMessage.INFORMATION),
- new SystemError(
- "This is an example of exception error reposting",
- sampleException)},
- new Object[] {
- "No error",
- "Sample text error",
- "Sample Formatted error",
- "Sample System Error" });
-
- // Style
- String currentStyle = ((Component)objectToConfigure).getStyle();
- if (currentStyle == null)
- set
- .replaceWithSelect(
- "style",
- new Object[] { null },
- new Object[] { "Default" })
- .setNewItemsAllowed(true);
- else
- set
- .replaceWithSelect(
- "style",
- new Object[] { null, currentStyle },
- new Object[] { "Default", currentStyle })
- .setNewItemsAllowed(true);
-
- // Set up descriptions
- set.getField("caption").setDescription(
- "Component caption is the title of the component. Usage of the caption is optional and the "
- + "exact behavior of the propery is defined by the component. Setting caption null "
- + "or empty disables the caption.");
- set.getField("enabled").setDescription(
- "Enabled property controls the usage of the component. If the component is disabled (enabled=false),"
- + " it can not receive any events from the terminal. In most cases it makes the usage"
- + " of the component easier, if the component visually looks disbled (for example is grayed), "
- + "when it can not be used.");
- set.getField("icon").setDescription(
- "Icon of the component selects the main icon of the component. The usage of the icon is identical "
- + "to caption and in most components caption and icon are kept together. Icons can be "
- + "loaded from any resources (see Terminal/Resources for more information). Some components "
- + "contain more than just the captions icon. Those icons are controlled through their "
- + "own properties.");
- set.getField("visible").setDescription(
- "Visibility property says if the component is renreded or not. Invisible components are implicitly "
- + "disabled, as there is no visible user interface to send event.");
- set.getField("description").setDescription(
- "Description is designed to allow easy addition of short tooltips, like this. Like the caption,"
- + " setting description null or empty disables the description.");
- set.getField("readOnly").setDescription(
- "Those components that have internal state that can be written are settable to readOnly-mode,"
- + " where the object can only be read, not written.");
- set.getField("componentError").setDescription(
- "IT Mill Toolkit supports extensive error reporting. One part of the error reporting are component"
- + " errors that can be controlled by the programmer. This example only contains couple of "
- + "sample errors; to get the full picture, read browse ErrorMessage-interface implementors "
- + "API documentation.");
- set.getField("immediate").setDescription(
- "Not all terminals can send the events immediately to server from all action. Web is the most "
- + "typical environment where many events (like textfield changed) are not sent to server, "
- + "before they are explicitly submitted. Setting immediate property true (by default this "
- + "is false for most components), the programmer can assure that the application is"
- + " notified as soon as possible about the value change in this component.");
- set.getField("style").setDescription(
- "Themes specify the overall looks of the user interface. In addition component can have a set of "
- + "styles, that can be visually very different (like datefield calendar- and text-styles), "
- + "but contain the same logical functionality. As a rule of thumb, theme specifies if a "
- + "component is blue or yellow and style determines how the component is used.");
-
- // Add created fields to property panel
- addProperties("Component Basics", set);
- }
-
- /** Add properties for selecting */
- private void addSelectProperties() {
- Form set =
- createBeanPropertySet(
- new String[] { "multiSelect", "newItemsAllowed" });
- addProperties("Select Properties", set);
-
- set.getField("multiSelect").setDescription(
- "Specified if multiple items can be selected at once.");
- set.getField("newItemsAllowed").setDescription(
- "Select component (but not Tree or Table) can allow the user to directly "
- + "add new items to set of options. The new items are constrained to be "
- + "strings and thus feature only applies to simple lists.");
- if (objectToConfigure instanceof Tree
- || objectToConfigure instanceof Table)
- set.removeItemProperty("newItemsAllowed");
- }
-
- /** Field special properties */
- private void addFieldProperties() {
- // TODO This is temporarily disabled, until bug #211 is fixed
- /*
- Form set = new Form(new GridLayout(COLUMNS, 1));
- set.addField("focus", new Button("Focus", objectToConfigure, "focus"));
- set.getField("focus").setDescription(
- "Focus the cursor to this field. Not all "
- + "components and/or terminals support this feature.");
- addProperties("Field Features", set);
- */
- }
-
- /** Add and remove some miscellaneous example component to/from component container */
- private void addComponentContainerProperties() {
- Form set = new Form(new GridLayout(COLUMNS, 1));
-
- addComponent = new Select();
- addComponent.setImmediate(true);
- addComponent.addItem("Add component to container");
- addComponent.setNullSelectionItemId("Add field");
- addComponent.addItem("Text field");
- addComponent.addItem("Time");
- addComponent.addItem("Option group");
- addComponent.addItem("Calendar");
- addComponent.addListener(this);
-
- set.addField("component adder", addComponent);
- set.addField(
- "remove all components",
- new Button(
- "Remove all components",
- objectToConfigure,
- "removeAllComponents"));
-
- addProperties("ComponentContainer Features", set);
- }
-
- /** Value change listener for listening selections */
- public void valueChange(Property.ValueChangeEvent event) {
-
- // Adding components to component container
- if (event.getProperty() == addComponent) {
- String value = (String) addComponent.getValue();
-
- if (value != null) {
- // TextField component
- if (value.equals("Text field"))
- (
- (
- AbstractComponentContainer) objectToConfigure)
- .addComponent(
- new TextField("Test field"));
-
- // DateField time style
- if (value.equals("Time")) {
- DateField d = new DateField("Time", new Date());
- d.setDescription(
- "This is a DateField-component with text-style");
- d.setResolution(DateField.RESOLUTION_MIN);
- d.setStyle("text");
- (
- (
- AbstractComponentContainer) objectToConfigure)
- .addComponent(
- d);
- }
-
- // Date field calendar style
- if (value.equals("Calendar")) {
- DateField c = new DateField("Calendar", new Date());
- c.setDescription(
- "DateField-component with calendar-style and day-resolution");
- c.setStyle("calendar");
- c.setResolution(DateField.RESOLUTION_DAY);
- (
- (
- AbstractComponentContainer) objectToConfigure)
- .addComponent(
- c);
- }
-
- // Select option group style
- if (value.equals("Option group")) {
- Select s = new Select("Options");
- s.setDescription("Select-component with optiongroup-style");
- s.addItem("Linux");
- s.addItem("Windows");
- s.addItem("Solaris");
- s.addItem("Symbian");
- s.setStyle("optiongroup");
-
- (
- (
- AbstractComponentContainer) objectToConfigure)
- .addComponent(
- s);
- }
-
- addComponent.setValue(null);
- }
- }
- }
-
- /** Helper function for creating forms from array of propety names.
- */
- protected Form createBeanPropertySet(String names[]) {
-
- Form set = new Form(new GridLayout(COLUMNS, 1));
-
- for (int i = 0; i < names.length; i++) {
- Property p = config.getItemProperty(names[i]);
- if (p != null) {
- set.addItemProperty(names[i], p);
- Field f = set.getField(names[i]);
- if (f instanceof TextField) {
- if (Integer.class.equals(p.getType()))
- ((TextField)f).setColumns(4);
- else {
- ((TextField)f).setNullSettingAllowed(true);
- ((TextField)f).setColumns(24);
- }
- }
- }
- }
-
- return set;
- }
-
- /** Find a field from all forms */
- public Field getField(Object propertyId) {
- for (Iterator i = forms.iterator(); i.hasNext();) {
- Form f = (Form) i.next();
- Field af = f.getField(propertyId);
- if (af != null)
- return af;
- }
- return null;
- }
- }
|