001: /* ListModelArray.java
002:
003: {{IS_NOTE
004: Purpose:
005:
006: Description:
007:
008: History:
009: Mon Feb 26 17:02:14 2007, Created by henrichen
010: }}IS_NOTE
011:
012: Copyright (C) 2007 Potix Corporation. All Rights Reserved.
013:
014: {{IS_RIGHT
015: This program is distributed under GPL Version 2.0 in the hope that
016: it will be useful, but WITHOUT ANY WARRANTY.
017: }}IS_RIGHT
018: */
019: package org.zkoss.zul;
020:
021: import org.zkoss.zul.event.ListDataEvent;
022: import org.zkoss.zk.ui.UiException;
023:
024: import org.zkoss.lang.Objects;
025: import org.zkoss.util.ArraysX;
026: import java.util.Comparator;
027: import java.util.Arrays;
028: import java.util.List;
029:
030: /**
031: * <p>This is the {@link ListModel} as an Object array to be used with {@link Listbox}.
032: * Change the contents of this model as an Object array would cause the associated Listbox to
033: * change accordingly.</p>
034: *
035: * @author Henri Chen
036: * @see ListModel
037: * @see ListModelList
038: * @see ListModelMap
039: */
040: public class ListModelArray extends AbstractListModel implements
041: ListModelExt, java.io.Serializable {
042: private static final long serialVersionUID = 20070226L;
043:
044: protected final Object[] _array;
045:
046: /**
047: * Creates an instance which accepts a "live" Object array as its inner array.
048: * @param array the inner array storage
049: * @deprecated As of release 2.4.0, replaced by {@link #ListModelArray(Object[],boolean)}
050: */
051: public static ListModelArray instance(Object[] array) {
052: return new ListModelArray(array, true);
053: }
054:
055: /**
056: * Constructor
057: *
058: * @param array the array to represent
059: * @param live whether to have a 'live' {@link ListModel} on top of
060: * the specified array.
061: * If false, the content of the specified array is copied.
062: * If true, this object is a 'facade' of the specified array,
063: * i.e., when you add or remove items from this {@link ListModelArray},
064: * the inner "live" array would be changed accordingly.
065: *
066: * However, it is not a good idea to modify <code>array</code>
067: * if it is passed to this method with live is true,
068: * since {@link Listbox} is not smart enough to hanle it.
069: * Instead, modify it thru this object.
070: * @since 2.4.0
071: */
072: public ListModelArray(Object[] array, boolean live) {
073: _array = live ? array : (Object[]) ArraysX.clone(array);
074: }
075:
076: /**
077: * Constructor.
078: * It mades a copy of the specified array (i.e., not live).
079: * @param src the source array used to initialize this ListModelArray.
080: */
081: public ListModelArray(Object[] src) {
082: _array = (Object[]) ArraysX.clone(src);
083: }
084:
085: /**
086: * Constructor.
087: * @param size the array size.
088: */
089: public ListModelArray(int size) {
090: _array = new Object[size];
091: }
092:
093: /**
094: * Constructor.
095: * It mades a copy of the specified list (i.e., not live).
096: * @since 2.4.1
097: */
098: public ListModelArray(List list) {
099: _array = list.toArray(new Object[list.size()]);
100: }
101:
102: /** Get the value of this ListModelArray at specified index.
103: * @param index the array index to be get value.
104: */
105: public Object get(int index) {
106: return getElementAt(index);
107: }
108:
109: /** Change content of the Array at specified index.
110: * @param index the array index to be set the new value.
111: */
112: public void set(int index, Object value) {
113: _array[index] = value;
114: fireEvent(ListDataEvent.CONTENTS_CHANGED, index, index);
115: }
116:
117: /**
118: * Get the inner real Object[].
119: * @since 2.4.0
120: */
121: public Object[] getInnerArray() {
122: return _array;
123: }
124:
125: /** Returns the index of the specified element.
126: */
127: public int indexOf(Object elm) {
128: for (int j = 0; j < _array.length; ++j) {
129: if (Objects.equals(elm, _array[j])) {
130: return j;
131: }
132: }
133: return -1;
134: }
135:
136: //-- ListModel --//
137: public int getSize() {
138: return _array.length;
139: }
140:
141: public Object getElementAt(int j) {
142: return _array[j];
143: }
144:
145: //-- ListModelExt --//
146: /** Sorts the data.
147: *
148: * @param cmpr the comparator.
149: * @param ascending whether to sort in the ascending order.
150: * It is ignored since this implementation uses cmprt to compare.
151: */
152: public void sort(Comparator cmpr, final boolean ascending) {
153: Arrays.sort(_array, cmpr);
154: fireEvent(ListDataEvent.CONTENTS_CHANGED, -1, -1);
155: }
156:
157: //Object//
158: public boolean equals(Object o) {
159: return _array
160: .equals(o instanceof ListModelArray ? ((ListModelArray) o)._array
161: : o);
162: }
163:
164: public int hashCode() {
165: return _array.hashCode();
166: }
167:
168: public String toString() {
169: return Objects.toString(_array);
170: }
171: }
|