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.propertyeditors;
042:
043: import com.sun.rave.propertyeditors.util.JavaInitializer;
044: import org.netbeans.modules.visualweb.web.ui.dt.component.util.DesignMessageUtil;
045: import com.sun.rave.web.ui.model.Option;
046: import java.util.ArrayList;
047: import javax.swing.event.TableModelListener;
048: import org.netbeans.modules.visualweb.propertyeditors.TabularPropertyEditor;
049: import org.netbeans.modules.visualweb.propertyeditors.TabularPropertyModel;
050:
051: /**
052: * An editor for properties that take lists of options, such as the
053: * <code>items</code> property on all list-like components.
054: *
055: * @author gjmurphy
056: */
057: public class OptionsPropertyEditor extends TabularPropertyEditor {
058:
059: public OptionsPropertyEditor() {
060: }
061:
062: private OptionsTableModel tableModel;
063:
064: protected TabularPropertyModel getTabularPropertyModel() {
065: if (tableModel == null)
066: tableModel = new OptionsTableModel();
067: return tableModel;
068: }
069:
070: public String getJavaInitializationString() {
071: Object value = this .getValue();
072: if (!(value instanceof Option[]))
073: return null;
074: Option[] options = (Option[]) value;
075: StringBuffer buffer = new StringBuffer();
076: buffer.append("new " + Option.class.getName() + "[] {");
077: for (int i = 0; i < options.length; i++) {
078: if (i > 0)
079: buffer.append(", ");
080: buffer.append("new " + Option.class.getName() + "(");
081: buffer.append(JavaInitializer
082: .toJavaInitializationString(options[i].getValue()));
083: buffer.append(", ");
084: buffer.append(JavaInitializer
085: .toJavaInitializationString(options[i].getLabel()));
086: buffer.append(")");
087: }
088: buffer.append("}");
089: return buffer.toString();
090: }
091:
092: static final String[] columnNames = new String[] {
093: DesignMessageUtil.getMessage(OptionsPropertyEditor.class,
094: "Options.label"), //NOI18N
095: DesignMessageUtil.getMessage(OptionsPropertyEditor.class,
096: "Options.value") //NOI18N
097: };
098:
099: static class OptionsTableModel implements TabularPropertyModel {
100:
101: final int labelIndex = 0;
102: final int valueIndex = 1;
103:
104: ArrayList options;
105:
106: public void setValue(Object value) {
107: if (value instanceof Option[]) {
108: options = new ArrayList();
109: Option[] values = (Option[]) value;
110: for (int i = 0; i < values.length; i++)
111: options.add(new Option(values[i].getValue(),
112: values[i].getLabel()));
113: }
114: }
115:
116: public Object getValue() {
117: if (options == null)
118: return null;
119: return options.toArray(new Option[options.size()]);
120: }
121:
122: public void addTableModelListener(TableModelListener listener) {
123: }
124:
125: public void removeTableModelListener(TableModelListener listener) {
126: }
127:
128: public boolean isCellEditable(int rowIndex, int columnIndex) {
129: if (columnIndex < 0 || columnIndex >= columnNames.length)
130: return false;
131: if (rowIndex < 0 || rowIndex >= options.size())
132: return false;
133: return true;
134: }
135:
136: public void setValueAt(Object newValue, int rowIndex,
137: int columnIndex) {
138: if (isCellEditable(rowIndex, columnIndex)) {
139: if (columnIndex == labelIndex)
140: ((Option) options.get(rowIndex)).setLabel(newValue
141: .toString());
142: else
143: ((Option) options.get(rowIndex)).setValue(newValue);
144: }
145: }
146:
147: public Class getColumnClass(int columnIndex) {
148: if (columnIndex == labelIndex)
149: return String.class;
150: else if (options.size() > 0 && options.get(0) != null)
151: return ((Option) options.get(0)).getValue().getClass();
152: else
153: return Object.class;
154: }
155:
156: public Object getValueAt(int rowIndex, int columnIndex) {
157: if (rowIndex >= 0 && rowIndex < options.size()) {
158: if (columnIndex == labelIndex)
159: return ((Option) options.get(rowIndex)).getLabel();
160: else
161: return ((Option) options.get(rowIndex)).getValue();
162: }
163: return null;
164: }
165:
166: public int getRowCount() {
167: return options.size();
168: }
169:
170: public String getColumnName(int columnIndex) {
171: return columnNames[columnIndex];
172: }
173:
174: public int getColumnCount() {
175: return columnNames.length;
176: }
177:
178: public boolean canAddRow() {
179: return true;
180: }
181:
182: public boolean addRow() {
183: options.add(new Option(columnNames[valueIndex],
184: columnNames[labelIndex]));
185: return true;
186: }
187:
188: public boolean canMoveRow(int indexFrom, int indexTo) {
189: if (indexFrom < 0 || indexTo < 0 || indexFrom == indexTo)
190: return false;
191: if (indexFrom >= options.size()
192: || indexTo >= options.size())
193: return false;
194: return true;
195: }
196:
197: public boolean moveRow(int indexFrom, int indexTo) {
198: if (!canMoveRow(indexFrom, indexTo))
199: return false;
200: options.add(indexTo, options.remove(indexFrom));
201: return true;
202: }
203:
204: public boolean canRemoveRow(int index) {
205: if (index < 0 || index >= options.size())
206: return false;
207: return true;
208: }
209:
210: public boolean removeRow(int index) {
211: if (!canRemoveRow(index))
212: return false;
213: options.remove(index);
214: return true;
215: }
216:
217: public boolean removeAllRows() {
218: if (options.isEmpty())
219: return true;
220:
221: int numRows = options.size();
222: options.clear();
223:
224: return true;
225: }
226: }
227:
228: }
|