001: /*
002: * Copyright 2006 Ethan Nicholas. All rights reserved.
003: * Use is subject to license terms.
004: */
005: package jaxx.runtime.swing;
006:
007: import java.beans.*;
008: import java.util.*;
009: import javax.swing.event.*;
010:
011: // This needs to be split into two classes, Item and TreeItem
012: /** An item in a component such as <code>JComboBox</code> or <code>JTree</code>. The <code>Item</code>
013: * class corresponds to the <code><item></code> tag in JAXX source files.
014: */
015: public class Item {
016: public static final String LABEL_PROPERTY = "label";
017: public static final String VALUE_PROPERTY = "value";
018: public static final String SELECTED_PROPERTY = "selected";
019:
020: private String id;
021: private String label;
022: private Object value;
023: private boolean selected;
024: private List children;
025: private Item parent;
026: private PropertyChangeSupport propertyChangeSupport;
027:
028: /** Creates a new Item. This should only be called from compiled JAXX files.
029: *
030: *@param id the item's ID
031: *@param label the string that should be used to represent the item visually
032: *@param value the item's actual value
033: *@param selected <code>true</code> if the item should be selected by default
034: */
035: public Item(String id, String label, Object value, boolean selected) {
036: this .id = id;
037: this .label = label;
038: this .value = value;
039: this .selected = selected;
040: }
041:
042: /** Returns this item's ID.
043: *
044: *@return the JAXX ID attribute
045: */
046: public String getId() {
047: return id;
048: }
049:
050: /** Returns the string that should be used to represent the item at display time. If <code>null</code>,
051: * <code>String.valueOf(getValue())</code> will be used instead.
052: *
053: *@return this item's display string
054: *@see #setLabel
055: */
056: public String getLabel() {
057: return label;
058: }
059:
060: /** Sets the item's display string. If <code>null, String.valueOf(getValue())</code> will be used instead.
061: *
062: *@param label the new display string
063: *@see #getLabel
064: */
065: public void setLabel(String label) {
066: String oldLabel = this .label;
067: this .label = label;
068: firePropertyChange(LABEL_PROPERTY, oldLabel, label);
069: }
070:
071: /** Returns the item's actual value as it appears in the component's model. The <code>Item</code> itself is not
072: * visible from the model, only the value.
073: *
074: *@return the item's value
075: *@see #setValue
076: */
077: public Object getValue() {
078: return value;
079: }
080:
081: /** Sets the item's value as it appears in the component's model. The <code>Item</code> itself is not
082: * visible from the model, only the value.
083: *
084: *@param value the new value
085: *@see #getValue
086: */
087: public void setValue(Object value) {
088: Object oldValue = this .value;
089: this .value = value;
090: firePropertyChange(VALUE_PROPERTY, oldValue, value);
091: }
092:
093: /** Returns <code>true</code> if this item is currently selected. This is a bound property.
094: *
095: *@see #setSelected
096: */
097: public boolean isSelected() {
098: return selected;
099: }
100:
101: /** Sets the item's selection state. This is a bound property.
102: *
103: *@param selected the new selection state
104: *@see #isSelected
105: */
106: public void setSelected(boolean selected) {
107: boolean oldSelected = this .selected;
108: this .selected = selected;
109: firePropertyChange(SELECTED_PROPERTY, Boolean
110: .valueOf(oldSelected), Boolean.valueOf(selected));
111: }
112:
113: /** Adds a new child node (Items can be nested in trees).
114: *
115: *@param item the new child item
116: */
117: public void addChild(Item item) {
118: if (children == null)
119: children = new ArrayList/*<Item>*/();
120: children.add(item);
121: item.parent = this ;
122: }
123:
124: /** Returns a list of this item's children.
125: *
126: *@return a list of all nested child nodes
127: */
128: public List/*<Item>*/getChildren() {
129: if (children == null)
130: children = new ArrayList/*<Item>*/();
131: return children;
132: }
133:
134: /** Returns the <code>Item</code> containing this <code>Item</code>, or <code>null</code> for a top-level
135: * <code>Item</code>.
136: */
137: public Item getParent() {
138: return parent;
139: }
140:
141: private PropertyChangeSupport getPropertyChangeSupport() {
142: if (propertyChangeSupport == null)
143: propertyChangeSupport = new SwingPropertyChangeSupport(this );
144: return propertyChangeSupport;
145: }
146:
147: public void addPropertyChangeListener(
148: PropertyChangeListener listener) {
149: getPropertyChangeSupport().addPropertyChangeListener(listener);
150: }
151:
152: public void addPropertyChangeListener(String property,
153: PropertyChangeListener listener) {
154: getPropertyChangeSupport().addPropertyChangeListener(property,
155: listener);
156: }
157:
158: public void removePropertyChangeListener(
159: PropertyChangeListener listener) {
160: getPropertyChangeSupport().removePropertyChangeListener(
161: listener);
162: }
163:
164: public void removePropertyChangeListener(String property,
165: PropertyChangeListener listener) {
166: getPropertyChangeSupport().removePropertyChangeListener(
167: property, listener);
168: }
169:
170: protected void firePropertyChange(String propertyName,
171: Object oldValue, Object newValue) {
172: if (propertyChangeSupport != null)
173: getPropertyChangeSupport().firePropertyChange(propertyName,
174: oldValue, newValue);
175: }
176:
177: public String toString() {
178: return getClass().getName() + "[" + value + "]";
179: }
180: }
|