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: package org.netbeans.modules.visualweb.web.ui.dt.component.table;
042:
043: import com.sun.data.provider.FieldKey;
044: import com.sun.data.provider.TableDataProvider;
045: import com.sun.data.provider.impl.ObjectArrayDataProvider;
046: import com.sun.data.provider.impl.ObjectListDataProvider;
047: import com.sun.rave.designtime.DesignBean;
048: import com.sun.rave.designtime.ext.DesignBeanExt;
049: import com.sun.rave.web.ui.component.Checkbox;
050: import java.lang.reflect.Type;
051: import java.util.ArrayList;
052: import java.util.HashMap;
053: import java.util.List;
054: import java.util.Map;
055: import java.util.ResourceBundle;
056: import java.util.Vector;
057: import javax.swing.DefaultListModel;
058: import org.openide.ErrorManager;
059: import org.openide.util.Exceptions;
060:
061: /**
062: * Data structure to hold design state of a data provider
063: * @author Winston Prakash
064: */
065: public class TableDataProviderDesignState {
066:
067: boolean dataProviderBroken = false;
068: private static int newColumnNameCount = 0;
069:
070: private DesignBean dataProviderBean;
071:
072: private DefaultListModel selectedColumnListModel = new DefaultListModel();
073: private DefaultListModel availableColumnListModel = new DefaultListModel();
074:
075: Map columnsDesignStates = null;
076:
077: private TableDataProvider tableDataProvider;
078: private ResourceBundle bundle = ResourceBundle
079: .getBundle(TableDataProviderDesignState.class.getPackage()
080: .getName()
081: + ".Bundle");
082:
083: /** Creates a new instance of TableDataProviderDesignState */
084: public TableDataProviderDesignState(DesignBean modelBean) {
085:
086: if (modelBean.getInstance() instanceof TableDataProvider) {
087: tableDataProvider = (TableDataProvider) modelBean
088: .getInstance();
089: } else if (List.class.isAssignableFrom(modelBean.getBeanInfo()
090: .getBeanDescriptor().getBeanClass())) {
091: List listObject = (List) modelBean.getInstance();
092: if (listObject == null) {
093: listObject = new ArrayList();
094: }
095: tableDataProvider = new ObjectListDataProvider(listObject);
096: if (modelBean instanceof DesignBeanExt) {
097: try {
098: java.lang.reflect.Type[] parameterTypes = ((com.sun.rave.designtime.ext.DesignBeanExt) modelBean)
099: .getTypeParameters();
100: if (parameterTypes != null
101: && (parameterTypes.length > 0)) {
102: ((com.sun.data.provider.impl.ObjectListDataProvider) tableDataProvider)
103: .setObjectType((java.lang.Class) parameterTypes[0]);
104: }
105: } catch (ClassNotFoundException exc) {
106: ErrorManager.getDefault().notify(exc);
107: }
108: }
109: } else if (modelBean.getInstance() instanceof Object[]) {
110: tableDataProvider = new ObjectArrayDataProvider(
111: (Object[]) modelBean.getInstance());
112: } else {
113: throw new IllegalArgumentException(modelBean
114: .getInstanceName()
115: + " " + bundle.getString("NOT_DATA_PROVIDER")); //NOI18N
116: }
117:
118: dataProviderBean = modelBean;
119: // Check if this is a broken DP
120: try {
121: tableDataProvider.getFieldKeys();
122: } catch (Exception exc) {
123: ErrorManager.getDefault().notify(exc);
124: dataProviderBroken = true;
125: }
126: }
127:
128: public boolean isBroken() {
129: return dataProviderBroken;
130: }
131:
132: /**
133: * Get the Data Model Bean
134: */
135: public DesignBean getDataProviderBean() {
136: return dataProviderBean;
137: }
138:
139: public String getUniqueColumnName(String baseName) {
140: int colNameCount = selectedColumnListModel.size() + 1;
141: boolean found = false;
142: String newName = baseName + colNameCount; //NOI18N
143: do {
144: if (selectedColumnListModel.contains(newName)) {
145: found = true;
146: } else if (availableColumnListModel.contains(newName)) {
147: found = true;
148: } else {
149: found = false;
150: }
151: if (found) {
152: newName = baseName + colNameCount++; //NOI18N
153: }
154: } while (found);
155: return newName;
156: }
157:
158: /**
159: * Set the available column vector
160: */
161: public void setSelectedColumnListModel(DefaultListModel listModel) {
162: selectedColumnListModel = listModel;
163: }
164:
165: /**
166: * Get the Selected table column information
167: */
168: public DefaultListModel getSelectedColumnListModel() {
169: return selectedColumnListModel;
170: }
171:
172: /**
173: * Set the available column vector
174: */
175: public void setAvailableColumnListModel(DefaultListModel listModel) {
176: availableColumnListModel = listModel;
177: }
178:
179: /**
180: * Get the Selected table column information
181: */
182: public DefaultListModel getAvailableColumnListModel() {
183: return availableColumnListModel;
184: }
185:
186: /**
187: * Add the TableColumnDesignState to the selectedColumnsDesignStates design state
188: * The name is added to the selected column list model.
189: */
190: public void addColumnDesignStates(
191: TableColumnDesignState colDesignState) {
192: if (columnsDesignStates == null)
193: columnsDesignStates = new HashMap();
194: columnsDesignStates.put(colDesignState.getName(),
195: colDesignState);
196: selectedColumnListModel.addElement(colDesignState.getName());
197: }
198:
199: /*
200: * Set the Table Column Design states
201: */
202: public void setColumnDesignStates(Map colDesignStates) {
203: columnsDesignStates = colDesignStates;
204: }
205:
206: /*
207: * Get the Table Column Design states
208: */
209: public Map getColumnDesignStates() {
210: return columnsDesignStates;
211: }
212:
213: public TableColumnDesignState getTableColumnDesignState(
214: String columnName) {
215: if (columnsDesignStates != null) {
216: TableColumnDesignState tableColumnDesignState = (TableColumnDesignState) columnsDesignStates
217: .get(columnName);
218: return tableColumnDesignState;
219: } else {
220: return null;
221: }
222: }
223:
224: /**
225: * Remove or add items from Selected Column List and Available Column List
226: */
227: public void setSelectedColumnNames(Vector selectedColumnNames) {
228: for (int i = 0; i < selectedColumnNames.size(); i++) {
229: selectedColumnListModel.addElement(selectedColumnNames
230: .get(i));
231: }
232: }
233:
234: /**
235: * Get the Selected Column
236: */
237: public Vector getSelectedColumnNames() {
238: Vector columnNames = new Vector();
239: for (int i = 0; i < selectedColumnListModel.size(); i++) {
240: //String columnName = selectedColumnListModel.getElementAt(i).toString();
241: columnNames.add(selectedColumnListModel.getElementAt(i));
242: }
243: return columnNames;
244: }
245:
246: /**
247: * Get the Selected Column
248: */
249: public Vector getAvailableColumnNames() {
250: Vector columnNames = new Vector();
251: for (int i = 0; i < availableColumnListModel.size(); i++) {
252: //String columnName = selectedColumnListModel.getElementAt(i).toString();
253: columnNames.add(availableColumnListModel.getElementAt(i));
254: }
255: return columnNames;
256: }
257:
258: /**
259: * Populate the Selected Column. All the columns of the data model are
260: * selected by defaullt
261: */
262: public void initialize() {
263: if (dataProviderBroken)
264: return;
265:
266: FieldKey[] columns = tableDataProvider.getFieldKeys();
267:
268: if ((columns != null) && (columns.length > 0)) {
269: if (columnsDesignStates == null) {
270: columnsDesignStates = new HashMap();
271: // Populate the selected column list and create corresponding TableColumnDesignState
272: for (int i = 0; i < columns.length; i++) {
273: //Skip FieldKey of type "Class" - 6309491
274: if ((tableDataProvider.getType(columns[i]) != null)
275: && tableDataProvider.getType(columns[i])
276: .toString().indexOf(
277: "java.lang.Class") == -1) {
278: String columnName = columns[i].getDisplayName();
279: selectedColumnListModel.addElement(columnName);
280: TableColumnDesignState tableColumnDesignState = new TableColumnDesignState(
281: columnName);
282: tableColumnDesignState
283: .setColumnType(tableDataProvider
284: .getType(columns[i]));
285: if (tableColumnDesignState.getColumnType()
286: .isAssignableFrom(Boolean.class)) {
287: tableColumnDesignState
288: .setChildType(Checkbox.class);
289: }
290: columnsDesignStates.put(columnName,
291: tableColumnDesignState);
292: }
293: }
294: } else {
295: // Populate the available column list and create the corresponding TableColumnDesignState
296: for (int i = 0; i < columns.length; i++) {
297: //Skip FieldKey of type "Class" - 6309491
298: if (tableDataProvider.getType(columns[i])
299: .toString().indexOf("java.lang.Class") == -1) {
300: String columnName = columns[i].getDisplayName();
301: if (!selectedColumnListModel
302: .contains(columnName)) {
303: availableColumnListModel
304: .addElement(columnName);
305: TableColumnDesignState tableColumnDesignState = new TableColumnDesignState(
306: columnName);
307: tableColumnDesignState
308: .setColumnType(tableDataProvider
309: .getType(columns[i]));
310: if (tableColumnDesignState.getColumnType()
311: .isAssignableFrom(Boolean.class)) {
312: tableColumnDesignState
313: .setChildType(Checkbox.class);
314: }
315: columnsDesignStates.put(columnName,
316: tableColumnDesignState);
317: }
318: }
319: }
320:
321: }
322: }
323:
324: }
325: }
|