001: /*
002: #IFNDEF ALT_LICENSE
003: ThinWire(R) RIA Ajax Framework
004: Copyright (C) 2003-2007 Custom Credit Systems
005:
006: This library is free software; you can redistribute it and/or modify it under
007: the terms of the GNU Lesser General Public License as published by the Free
008: Software Foundation; either version 2.1 of the License, or (at your option) any
009: later version.
010:
011: This library is distributed in the hope that it will be useful, but WITHOUT ANY
012: WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
013: PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
014:
015: You should have received a copy of the GNU Lesser General Public License along
016: with this library; if not, write to the Free Software Foundation, Inc., 59
017: Temple Place, Suite 330, Boston, MA 02111-1307 USA
018:
019: Users who would rather have a commercial license, warranty or support should
020: contact the following company who invented, built and supports the technology:
021:
022: Custom Credit Systems, Richardson, TX 75081, USA.
023: email: info@thinwire.com ph: +1 (888) 644-6405
024: http://www.thinwire.com
025: #ENDIF
026: [ v1.2_RC2 ]
027: */
028: package thinwire.ui;
029:
030: import java.util.AbstractList;
031: import java.util.ArrayList;
032: import java.util.List;
033:
034: import thinwire.ui.event.ActionEvent;
035: import thinwire.ui.event.ActionListener;
036: import thinwire.ui.event.ItemChangeListener;
037: import thinwire.ui.event.ItemChangeEvent.Type;
038: import thinwire.util.ImageInfo;
039:
040: /**
041: * @author Joshua J. Gertzen
042: */
043: abstract class AbstractHierarchyComponent<HI extends AbstractHierarchyComponent.Item>
044: extends AbstractComponent implements HierarchyComponent<HI> {
045: private static class ChildList<I extends Item> extends
046: AbstractList<I> {
047: private List<I> l = new ArrayList<I>(3);
048: private Item parent;
049:
050: private ChildList(Item parent) {
051: this .parent = parent;
052: }
053:
054: public void add(int index, I item) {
055: if (item.parent != null)
056: throw new IllegalStateException(
057: "item.getParent() != null");
058: l.add(index, item);
059: modCount++;
060: item.setParent(parent);
061: AbstractHierarchyComponent hier = parent.getHierarchy();
062: if (hier != null)
063: hier.icei.fireItemChange(parent, Type.ADD, index, null,
064: item);
065: }
066:
067: public I get(int index) {
068: return l.get(index);
069: }
070:
071: public I set(int index, I item) {
072: if (item.parent != null)
073: throw new IllegalStateException(
074: "item.getParent() != null");
075: AbstractHierarchyComponent hier = parent.getHierarchy();
076: I ret = l.get(index);
077: hier.removingItem(ret);
078: l.set(index, item);
079: ret.setParent(null);
080: item.setParent(parent);
081: if (hier != null)
082: hier.icei.fireItemChange(parent, Type.SET, index, ret,
083: item);
084: return ret;
085: }
086:
087: public I remove(int index) {
088: modCount++;
089: AbstractHierarchyComponent hier = parent.getHierarchy();
090: I item = l.get(index);
091: hier.removingItem(item);
092: l.remove(index);
093: item.setParent(null);
094: if (hier != null)
095: hier.icei.fireItemChange(parent, Type.REMOVE, index,
096: item, null);
097: return item;
098: }
099:
100: public int size() {
101: return l.size();
102: }
103: }
104:
105: static abstract class Item<H extends AbstractHierarchyComponent, I extends AbstractHierarchyComponent.Item>
106: implements HierarchyComponent.Item<H, I> {
107: private String text = "";
108: private ImageInfo imageInfo = new ImageInfo(null);
109: private Object userObject;
110: private Object parent;
111: private List<I> children;
112:
113: public String getText() {
114: return text;
115: }
116:
117: public void setText(String text) {
118: String oldText = this .text;
119: if (text == null)
120: text = "";
121: this .text = text;
122: H hier = getHierarchy();
123: if (hier != null)
124: hier.firePropertyChange(this , PROPERTY_ITEM_TEXT,
125: oldText, text);
126: }
127:
128: public String getImage() {
129: return imageInfo.getName();
130: }
131:
132: public void setImage(String image) {
133: String oldImage = this .imageInfo.getName();
134: imageInfo = new ImageInfo(image);
135: H hier = getHierarchy();
136: if (hier != null)
137: hier.firePropertyChange(this , PROPERTY_ITEM_IMAGE,
138: oldImage, this .imageInfo.getName());
139: }
140:
141: public ImageInfo getImageInfo() {
142: return imageInfo;
143: }
144:
145: public Object getUserObject() {
146: return userObject;
147: }
148:
149: public void setUserObject(Object value) {
150: Object oldValue = this .userObject;
151: this .userObject = value;
152: H hier = getHierarchy();
153: if (hier != null)
154: hier.firePropertyChange(this ,
155: PROPERTY_ITEM_USER_OBJECT, oldValue,
156: this .userObject);
157: }
158:
159: public int getIndex() {
160: if (parent == null)
161: throw new IllegalStateException("getParent() == null");
162: if (parent instanceof HierarchyComponent)
163: throw new IllegalStateException(
164: "getParent() instanceof Hierarchy");
165: return ((Item) parent).getChildren().indexOf(this );
166: }
167:
168: public H getHierarchy() {
169: Object parent = this ;
170:
171: while (parent != null
172: && !(parent instanceof AbstractHierarchyComponent))
173: parent = ((I) parent).getParent();
174:
175: return (H) parent;
176: }
177:
178: public Object getParent() {
179: return parent;
180: }
181:
182: private void setParent(Object parent) {
183: this .parent = parent;
184: }
185:
186: public boolean hasChildren() {
187: return children == null ? false : children.size() > 0;
188: }
189:
190: public List<I> getChildren() {
191: if (children == null)
192: children = new ChildList<I>(this );
193: return children;
194: }
195:
196: public String toString() {
197: return "text:"
198: + text
199: + ",image:"
200: + imageInfo.getName()
201: + ",children.size():"
202: + (children == null ? 0 : children.size())
203: + ",parent:"
204: + (parent == null ? null : parent.getClass()
205: .getName()
206: + "@" + System.identityHashCode(parent));
207: }
208: }
209:
210: private HI rootItem;
211: private EventListenerImpl<ItemChangeListener> icei;
212:
213: AbstractHierarchyComponent(HI rootItem,
214: EventListenerImpl.SubTypeValidator actionValidator) {
215: super (actionValidator);
216: EventListenerImpl<ItemChangeListener> gicei = app == null ? null
217: : app.getGlobalListenerSet(ItemChangeListener.class,
218: false);
219: icei = new EventListenerImpl<ItemChangeListener>(this ,
220: ItemChangeListener.class, null, gicei);
221: this .rootItem = rootItem;
222: rootItem.setParent(this );
223: }
224:
225: void removingItem(HI item) {
226:
227: }
228:
229: public HI getRootItem() {
230: return rootItem;
231: }
232:
233: public void addItemChangeListener(ItemChangeListener listener) {
234: icei.addListener(listener);
235: }
236:
237: public void removeItemChangeListener(ItemChangeListener listener) {
238: icei.removeListener(listener);
239: }
240: }
|