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.HashMap;
046: import java.util.List;
047: import java.util.Map;
048: import java.util.Set;
049: import com.sun.data.provider.DataListener;
050: import com.sun.data.provider.TableDataProvider;
051: import com.sun.data.provider.TableDataListener;
052:
053: public class MapListDataProvider {
054: } /*implements TableDataProvider {
055:
056: protected List list;
057:
058: public MapListDataProvider() {}
059:
060: public MapListDataProvider(List list) {
061: this.list = list;
062: }
063:
064: public void setList(List list) {
065: this.list = list;
066: }
067:
068: public List getList() {
069: return list;
070: }
071:
072: public Map getMap(RowKey row) {
073: return (Map)list.get(row);
074: }
075:
076: public void setMap(RowKey row, Map map) {
077: list.set(row, map);
078: }
079:
080: public Object getData(String fieldKey) {
081: return getData(cursorIndex, fieldKey);
082: }
083:
084: public Class getDataType(String fieldKey) {
085: return null;
086: }
087:
088: public boolean isReadOnly(String fieldKey) {
089: return false;
090: }
091:
092: public void setData(String fieldKey, Object data) {
093: setData(cursorIndex, fieldKey, data);
094: }
095:
096: public Object getData(RowKey row, String fieldKey) {
097: Object o = list.get(row);
098: if (o instanceof Map) {
099: Map map = (Map)o;
100: return map.get(fieldKey);
101: }
102: return o; // ignore fieldKey if the list item isn't a map
103: }
104:
105: public Class getDataType(RowKey row, String fieldKey) {
106: return null;
107: }
108:
109: public boolean isReadOnly(RowKey row, String fieldKey) {
110: return false;
111: }
112:
113: public void setData(RowKey row, String fieldKey, Object data) {
114: Object o = list.get(row);
115: if (o instanceof Map) {
116: Map map = (Map)o;
117: map.put(fieldKey, data);
118: } else {
119: HashMap map = new HashMap();
120: map.put(fieldKey, data);
121: list.set(row, map);
122: }
123: fireDataChanged(row, fieldKey, data);
124: if (row == cursorIndex) {
125: fireDataChanged(fieldKey, data);
126: }
127: }
128:
129: protected int cursorIndex = 0;
130: public int getCursorIndex() {
131: return cursorIndex;
132: }
133:
134: public void setCursorIndex(RowKey row) {
135: if (row < 0 || row >= list.size()) {
136: throw new IllegalArgumentException("Invalid row: " + row + ". Size is " +
137: list.size());
138: }
139: int oldRow = this.cursorIndex;
140: fireCursorIndexChanging(oldRow, row);
141: this.cursorIndex = row;
142: fireCursorIndexChanged(oldRow, cursorIndex);
143: }
144:
145: public void cursorNext() {
146: setCursorIndex(cursorIndex + 1);
147: }
148:
149: public void cursorPrevious() {
150: setCursorIndex(cursorIndex - 1);
151: }
152:
153: public int getRowCount() {
154: return list.size();
155: }
156:
157: public String[] getFieldKeys() {
158: Object o = list.get(cursorIndex);
159: if (o instanceof Map) {
160: Map map = (Map)o;
161: Set keys = map.keySet();
162: return (String[])keys.toArray(new String[keys.size()]);
163: }
164: return null;
165: }
166:
167: protected ArrayList dsEars = new ArrayList();
168: public void addDataListener(DataListener dsl) {
169: dsEars.add(dsl);
170: }
171:
172: public void removeDataListener(DataListener dsl) {
173: dsEars.remove(dsl);
174: }
175:
176: public DataListener[] getDataListeners() {
177: return (DataListener[])dsEars.toArray(new DataListener[dsEars.size()]);
178: }
179:
180: protected ArrayList vdsEars = new ArrayList();
181: public void addTableDataListener(TableDataListener vdsl) {
182: vdsEars.add(vdsl);
183: }
184:
185: public void removeTableDataListener(TableDataListener vdsl) {
186: vdsEars.remove(vdsl);
187: }
188:
189: public TableDataListener[] getTableDataListeners() {
190: return (TableDataListener[])vdsEars.toArray(new TableDataListener[
191: vdsEars.size()]);
192: }
193:
194: protected void fireDataChanged(String fieldKey, Object data) {
195: DataListener[] dsls = getDataListeners();
196: for (int i = 0; i < dsls.length; i++) {
197: dsls[i].dataChanged(this, fieldKey, data);
198: }
199: }
200:
201: protected void fireDataChanged(RowKey row, String fieldKey, Object data) {
202: TableDataListener[] vdsls = getTableDataListeners();
203: for (int i = 0; i < vdsls.length; i++) {
204: vdsls[i].dataChanged(this, row, fieldKey, data);
205: }
206: }
207:
208: protected void fireCursorIndexChanging(RowKey oldRow, RowKey newRow) {
209: TableDataListener[] vdsls = getTableDataListeners();
210: for (int i = 0; i < vdsls.length; i++) {
211: vdsls[i].cursorIndexChanging(this, oldRow, newRow);
212: }
213: }
214:
215: protected void fireCursorIndexChanged(RowKey oldRow, RowKey newRow) {
216: TableDataListener[] vdsls = getTableDataListeners();
217: for (int i = 0; i < vdsls.length; i++) {
218: vdsls[i].cursorIndexChanged(this, oldRow, newRow);
219: }
220: }
221: }
222: */
|