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.io.Serializable;
045: import com.sun.data.provider.DataProvider;
046: import com.sun.data.provider.DataProviderException;
047: import com.sun.data.provider.FieldKey;
048: import com.sun.data.provider.RowKey;
049: import com.sun.data.provider.TableDataListener;
050: import com.sun.data.provider.TableDataProvider;
051:
052: /**
053: * The TableRowDataProvider class provides a single-row {@link DataProvider}
054: * view of a {@link TableDataProvider}. Set the 'tableDataProvider' and
055: * 'tableRow' properties to create a single-row view of the table.
056: *
057: * @author Joe Nuxoll
058: */
059: public class TableRowDataProvider extends AbstractDataProvider {
060:
061: /**
062: * Constructs a TableRowDataProvider with no tableDataProvider or tableRow
063: * setting.
064: */
065: public TableRowDataProvider() {
066: }
067:
068: /**
069: * Constructs a TableRowDataProvider with the specified tableDataProvider
070: *
071: * @param provider TableDataProvider
072: */
073: public TableRowDataProvider(TableDataProvider provider) {
074: setTableDataProvider(provider);
075: }
076:
077: /**
078: * Constructs a TableRowDataProvider with the specified tableDataProvider
079: * and tableRow.
080: *
081: * @param provider TableDataProvider
082: * @param tableRow RowKey
083: */
084: public TableRowDataProvider(TableDataProvider provider,
085: RowKey tableRow) {
086: setTableDataProvider(provider);
087: setTableRow(tableRow);
088: }
089:
090: /**
091: *
092: * @param provider TableDataProvider
093: */
094: public void setTableDataProvider(TableDataProvider provider) {
095: if (this .provider != null) {
096: this .provider.removeTableDataListener(dataEars);
097: }
098: this .provider = provider;
099: if (this .provider != null) {
100: this .provider.addTableDataListener(dataEars);
101: }
102: fireProviderChanged();
103: }
104:
105: /**
106: *
107: * @return TableDataProvider
108: */
109: public TableDataProvider getTableDataProvider() {
110: return provider;
111: }
112:
113: /**
114: *
115: * @param tableRow int
116: */
117: public void setTableRow(RowKey tableRow) {
118: this .tableRow = tableRow;
119: if (provider != null) {
120: fireProviderChanged();
121: }
122: }
123:
124: /**
125: *
126: * @return RowKey
127: */
128: public RowKey getTableRow() {
129: return tableRow;
130: }
131:
132: /**
133: *
134: * @return FieldKey[]
135: */
136: public FieldKey[] getFieldKeys() throws DataProviderException {
137: if (provider != null) {
138: return provider.getFieldKeys();
139: }
140: return FieldKey.EMPTY_ARRAY;
141: }
142:
143: /**
144: *
145: * @param fieldId String
146: * @return FieldKey
147: */
148: public FieldKey getFieldKey(String fieldId)
149: throws DataProviderException {
150: if (provider != null) {
151: return provider.getFieldKey(fieldId);
152: }
153: return null;
154: }
155:
156: /**
157: *
158: * @param fieldKey FieldKey
159: * @return Class
160: */
161: public Class getType(FieldKey fieldKey)
162: throws DataProviderException {
163: if (provider != null) {
164: return provider.getType(fieldKey);
165: }
166: return null;
167: }
168:
169: /**
170: *
171: * @param fieldKey FieldKey
172: * @return boolean
173: */
174: public boolean isReadOnly(FieldKey fieldKey)
175: throws DataProviderException {
176: if (provider != null) {
177: return provider.isReadOnly(fieldKey);
178: }
179: return true;
180: }
181:
182: /**
183: *
184: * @param fieldKey FieldKey
185: * @return Object
186: */
187: public Object getValue(FieldKey fieldKey)
188: throws DataProviderException {
189: if (provider != null) {
190: return provider.getValue(fieldKey, tableRow);
191: }
192: return null;
193: }
194:
195: /**
196: *
197: * @param fieldKey FieldKey
198: * @param value Object
199: */
200: public void setValue(FieldKey fieldKey, Object value)
201: throws DataProviderException {
202: provider.setValue(fieldKey, tableRow, value);
203: }
204:
205: private class DataEars implements TableDataListener, Serializable {
206: public void valueChanged(TableDataProvider provider,
207: FieldKey fieldKey, RowKey row, Object oldValue,
208: Object newValue) {
209: if (row == tableRow
210: || (row != null && row.equals(tableRow))) {
211: fireValueChanged(fieldKey, oldValue, newValue);
212: }
213: }
214:
215: public void rowAdded(TableDataProvider provider, RowKey row) {
216: if (row == tableRow
217: || (row != null && row.equals(tableRow))) {
218: fireProviderChanged();
219: }
220: }
221:
222: public void rowRemoved(TableDataProvider provider, RowKey row) {
223: if (row == tableRow
224: || (row != null && row.equals(tableRow))) {
225: fireProviderChanged();
226: }
227: }
228:
229: public void valueChanged(DataProvider provider,
230: FieldKey fieldKey, Object oldValue, Object newValue) {
231: RowKey row = TableRowDataProvider.this .provider
232: .getCursorRow();
233: if (row == tableRow
234: || (row != null && row.equals(tableRow))) {
235: fireValueChanged(fieldKey, oldValue, newValue);
236: }
237: }
238:
239: public void providerChanged(DataProvider provider) {
240: fireProviderChanged();
241: }
242: };
243:
244: private DataEars dataEars = new DataEars();
245: private TableDataProvider provider;
246: private RowKey tableRow;
247: }
|