001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2007 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */
041:
042: package com.sun.data.provider.impl;
043:
044: import java.util.ArrayList;
045: import java.util.List;
046: import com.sun.data.provider.DataProviderException;
047: import com.sun.data.provider.FieldKey;
048: import com.sun.data.provider.RowKey;
049:
050: /**
051: * <p>This {@link com.sun.data.provider.TableDataProvider} implementation
052: * wraps the contents of a {@link List}. This DataProvider ignores FieldKeys
053: * entirely, and maintains a single list of objects.</p>
054: *
055: * <p>NOTE about Serializable: By default, this class uses an {@link ArrayList}
056: * as its internal data storage, which is a Serializable implementation of
057: * {@link List}. The internal storage can be swapped out using the
058: * <code>setList(List)</code> method. For this class to remain Serializable,
059: * the contained List must be a Serializable implementation. Also, and more
060: * importantly, the contents of the storage List must be Serializable as well
061: * for this class to successfully be serialized.</p>
062: *
063: * @author Joe Nuxoll
064: */
065: public class ListDataProvider extends AbstractTableDataProvider {
066:
067: /**
068: * Storage for the internal {@link List} ({@link ArrayList} by default)
069: */
070: protected List list = new ArrayList();
071:
072: /**
073: * Constructs a new ListDataProvider using the default internal storage
074: */
075: public ListDataProvider() {
076: }
077:
078: /**
079: * <p>Constructs a new ListDataProvider using the specified List as internal
080: * storage.</p>
081: *
082: * <p>NOTE about Serializable: By default, this class uses an {@link
083: * ArrayList} as its internal data storage, which is a Serializable
084: * implementation of {@link List}. The internal storage can be swapped out
085: * using the <code>setList(List)</code> method. For this class to remain
086: * Serializable, the contained List must be a Serializable implementation.
087: * Also, and more importantly, the contents of the storage List must be
088: * Serializable as well for this class to successfully be serialized.</p>
089: *
090: * @param list List to use for internal storage
091: */
092: public ListDataProvider(List list) {
093: this .list = list;
094: }
095:
096: /**
097: * @return List used for internal storage
098: */
099: public List getList() {
100: return list;
101: }
102:
103: /**
104: * <p>Sets the List to use for internal storage.</p>
105: *
106: * <p>NOTE about Serializable: By default, this class uses an {@link
107: * ArrayList} as its internal data storage, which is a Serializable
108: * implementation of {@link List}. The internal storage can be swapped out
109: * using the <code>setList(List)</code> method. For this class to remain
110: * Serializable, the contained List must be a Serializable implementation.
111: * Also, and more importantly, the contents of the storage List must be
112: * Serializable as well for this class to successfully be serialized.</p>
113: *
114: * @param list List to use for internal storage
115: */
116: public void setList(List list) {
117: this .list = list;
118: fireProviderChanged();
119: }
120:
121: /**
122: * <p>NOTE: FieldKey is ignored in this class.</p>
123: *
124: * {@inheritDoc}
125: */
126: public Object getValue(FieldKey fieldKey, RowKey row)
127: throws DataProviderException {
128:
129: if (java.beans.Beans.isDesignTime()
130: && (list == null || list.isEmpty())) {
131: // Fill the object with design time fake data
132: list = (List) AbstractDataProvider.getFakeData(list
133: .getClass());
134: }
135:
136: return list.get(getRowIndex(row)); // ignoring fieldKey
137: }
138:
139: /**
140: * <p>NOTE: FieldKey is ignored in this class.</p>
141: *
142: * {@inheritDoc}
143: */
144: public Class getType(FieldKey fieldKey)
145: throws DataProviderException {
146: Object o = getValue(fieldKey);
147: return o != null ? o.getClass() : null;
148: }
149:
150: /**
151: * <p>NOTE: FieldKey is ignored in this class.</p>
152: *
153: * <p>NOTE: This method always returns false, as the storage List can be edited
154: * at any row.</p>
155: *
156: * {@inheritDoc}
157: */
158: public boolean isReadOnly(FieldKey fieldKey)
159: throws DataProviderException {
160: return false;
161: }
162:
163: /**
164: * <p>NOTE: FieldKey is ignored in this class.</p>
165: *
166: * {@inheritDoc}
167: */
168: public void setValue(FieldKey fieldKey, RowKey row, Object value)
169: throws DataProviderException {
170:
171: Object oldValue = getValue(fieldKey, row);
172: list.set(getRowIndex(row), value); // ignoring fieldKey
173: fireValueChanged(fieldKey, row, oldValue, value);
174: if (getRowIndex(row) == getCursorIndex()) {
175: fireValueChanged(fieldKey, oldValue, value);
176: }
177: }
178:
179: /** {@inheritDoc} */
180: public int getRowCount() throws DataProviderException {
181: return list.size();
182: }
183:
184: /** {@inheritDoc} */
185: public boolean isRowAvailable(RowKey row)
186: throws DataProviderException {
187: if (row instanceof IndexRowKey) {
188: return list.size() > ((IndexRowKey) row).getIndex();
189: }
190: return false;
191: }
192:
193: public int getCursorIndex() throws DataProviderException {
194: return getRowIndex(getCursorRow());
195: }
196:
197: public int getRowIndex(RowKey row) throws DataProviderException {
198: if (row instanceof IndexRowKey) {
199: return ((IndexRowKey) row).getIndex();
200: }
201: try {
202: return Integer.parseInt(row.getRowId());
203: } catch (NumberFormatException nfx) {
204: // its not an index
205: }
206: return -1;
207: }
208:
209: /**
210: * <p>NOTE: This implementation always returns <code>false</code> from this
211: * method. To resize the data provider, access the List directly.</p>
212: *
213: * {@inheritDoc}
214: */
215: public boolean canInsertRow(RowKey beforeRow)
216: throws DataProviderException {
217: return false;
218: }
219:
220: /** {@inheritDoc} */
221: public RowKey insertRow(RowKey beforeRow)
222: throws DataProviderException {
223: return null;
224: }
225:
226: /**
227: * <p>NOTE: This implementation always returns <code>false</code> from this
228: * method. To resize the data provider, access the List directly.</p>
229: *
230: * {@inheritDoc}
231: */
232: public boolean canAppendRow() throws DataProviderException {
233: return false;
234: }
235:
236: /** {@inheritDoc} */
237: public RowKey appendRow() throws DataProviderException {
238: return null;
239: }
240:
241: /**
242: * <p>NOTE: This implementation always returns <code>true</code> from this
243: * method.</p>
244: *
245: * {@inheritDoc}
246: */
247: public boolean canRemoveRow(RowKey row)
248: throws DataProviderException {
249: return true;
250: }
251:
252: /** {@inheritDoc} */
253: public void removeRow(RowKey row) throws DataProviderException {
254: list.remove(row);
255: }
256: }
|