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