001: /*
002: * This file is part of the Echo Web Application Framework (hereinafter "Echo").
003: * Copyright (C) 2002-2005 NextApp, Inc.
004: *
005: * Version: MPL 1.1/GPL 2.0/LGPL 2.1
006: *
007: * The contents of this file are subject to the Mozilla Public License Version
008: * 1.1 (the "License"); you may not use this file except in compliance with
009: * the License. You may obtain a copy of the License at
010: * http://www.mozilla.org/MPL/
011: *
012: * Software distributed under the License is distributed on an "AS IS" basis,
013: * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
014: * for the specific language governing rights and limitations under the
015: * License.
016: *
017: * Alternatively, the contents of this file may be used under the terms of
018: * either the GNU General Public License Version 2 or later (the "GPL"), or
019: * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
020: * in which case the provisions of the GPL or the LGPL are applicable instead
021: * of those above. If you wish to allow use of your version of this file only
022: * under the terms of either the GPL or the LGPL, and not to allow others to
023: * use your version of this file under the terms of the MPL, indicate your
024: * decision by deleting the provisions above and replace them with the notice
025: * and other provisions required by the GPL or the LGPL. If you do not delete
026: * the provisions above, a recipient may use your version of this file under
027: * the terms of any one of the MPL, the GPL or the LGPL.
028: */
029:
030: package nextapp.echo2.app.list;
031:
032: import java.util.ArrayList;
033: import java.util.List;
034:
035: /**
036: * Default <code>ListModel</code> implementation.
037: */
038: public class DefaultListModel extends AbstractListModel {
039:
040: private List items = new ArrayList();
041:
042: /**
043: * Creates a new <code>DefaultSelectListModel</code> with the given
044: * content.
045: */
046: public DefaultListModel() {
047: super ();
048: }
049:
050: /**
051: * Creates a new <code>DefaultSelectListModel</code> containing the
052: * specified items
053: *
054: * @param itemArray the initial items
055: */
056: public DefaultListModel(Object[] itemArray) {
057: this ();
058:
059: for (int i = 0; i < itemArray.length; ++i) {
060: add(itemArray[i]);
061: }
062: }
063:
064: /**
065: * Adds an item at the end of the model.
066: *
067: * @param item the item to add
068: */
069: public void add(Object item) {
070: items.add(item);
071: int index = items.size() - 1;
072: fireIntervalAdded(index, index);
073: }
074:
075: /**
076: * Inserts an item at the specified index.
077: *
078: * @param item the item
079: * @param index the index
080: */
081: public void add(int index, Object item) {
082: items.add(index, item);
083: fireIntervalAdded(index, index);
084: }
085:
086: /**
087: * Returns the item at the specified index in the list.
088: *
089: * @param index
090: * @return the item
091: */
092: public Object get(int index) {
093: return items.get(index);
094: }
095:
096: /**
097: * Returns the index of the specified item.
098: *
099: * @param item the item
100: * @return the index
101: */
102: public int indexOf(Object item) {
103: return items.indexOf(item);
104: }
105:
106: /**
107: * Removes the item at the specified index from the model.
108: *
109: * @param index the index
110: */
111: public void remove(int index) {
112: items.remove(index);
113: fireIntervalRemoved(index, index);
114: }
115:
116: /**
117: * Removes the specified item from the model.
118: *
119: * @param item the item
120: */
121: public void remove(Object item) {
122: int index = items.indexOf(item);
123: items.remove(item);
124: fireIntervalRemoved(index, index);
125: }
126:
127: /**
128: * Removes all items from the model.
129: */
130: public void removeAll() {
131: int size = items.size();
132: if (size > 0) {
133: items.clear();
134: fireIntervalRemoved(0, size - 1);
135: }
136: }
137:
138: /**
139: * Returns the length of the list.
140: *
141: * @return the length
142: */
143: public int size() {
144: return items.size();
145: }
146: }
|