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.designtime.DesignBean;
044: import com.sun.rave.designtime.DesignProperty;
045: import com.sun.rave.propertyeditors.util.JavaInitializer;
046: import org.netbeans.modules.visualweb.web.ui.dt.component.util.DesignMessageUtil;
047: import com.sun.rave.web.ui.model.MultipleSelectOptionsList;
048: import com.sun.rave.web.ui.model.Option;
049: import java.util.ArrayList;
050: import javax.swing.event.TableModelEvent;
051: import javax.swing.event.TableModelListener;
052: import org.netbeans.modules.visualweb.propertyeditors.TabularPropertyEditor;
053: import org.netbeans.modules.visualweb.propertyeditors.TabularPropertyModel;
054:
055: /**
056: * An editor for properties that specify the options that are "selected" among
057: * a list of options. An option is "selected" if the value of its
058: * <code>value</code> property is equal to one of the objects in the selected
059: * property.
060: *
061: * @author gjmurphy
062: */
063: public class SelectedValuesPropertyEditor extends TabularPropertyEditor {
064:
065: public SelectedValuesPropertyEditor() {
066: }
067:
068: private SelectedValuesTableModel tableModel;
069:
070: protected TabularPropertyModel getTabularPropertyModel() {
071: if (tableModel == null)
072: tableModel = new SelectedValuesTableModel();
073: tableModel.setDesignProperty(this .getDesignProperty());
074: return tableModel;
075: }
076:
077: public String getJavaInitializationString() {
078: StringBuffer buffer = new StringBuffer();
079: Object value = this .getValue();
080: if (value instanceof Object[]) {
081: buffer.append("new Object[] {");
082: Object[] values = (Object[]) value;
083: for (int i = 0; i < values.length; i++) {
084: if (i > 0)
085: buffer.append(", ");
086: buffer.append(JavaInitializer
087: .toJavaInitializationString(values[i]));
088: }
089: buffer.append("}");
090: } else {
091: buffer.append(JavaInitializer
092: .toJavaInitializationString(value));
093: }
094: return buffer.toString();
095: }
096:
097: public String getAsText() {
098: StringBuffer buffer = new StringBuffer();
099: Object value = this .getValue();
100: if (value instanceof Object[]) {
101: Object[] values = (Object[]) value;
102: for (int i = 0; i < values.length; i++) {
103: if (i > 0)
104: buffer.append(", ");
105: buffer.append(values[i].toString());
106: }
107: } else if (value != null) {
108: buffer.append(value.toString());
109: }
110: return buffer.toString();
111: }
112:
113: static final String[] columnNames = new String[] {
114: DesignMessageUtil.getMessage(OptionsPropertyEditor.class,
115: "Options.label"), //NOI18N
116: DesignMessageUtil.getMessage(OptionsPropertyEditor.class,
117: "Options.value"), //NOI18N
118: DesignMessageUtil.getMessage(OptionsPropertyEditor.class,
119: "Options.selected") //NOI18N
120: };
121:
122: class SelectedValuesTableModel implements TabularPropertyModel {
123:
124: final int labelIndex = 0;
125: final int valueIndex = 1;
126: final int selectedIndex = 2;
127:
128: DesignProperty designProperty;
129: // List of the component options from which selections are made
130: ArrayList options;
131: // List of boolean values that reflected selected state of the options
132: ArrayList selected;
133: // If true, component allows multiple selections
134: boolean isMultiple;
135:
136: public void setValue(Object value) {
137: options = new ArrayList();
138: selected = new ArrayList();
139: if (designProperty != null) {
140: DesignBean bean = designProperty.getDesignBean();
141: Option[] opts = (Option[]) bean.getProperty("options")
142: .getValue();
143: DesignProperty multipleProperty = bean
144: .getProperty("multiple");
145: if ((multipleProperty != null && Boolean.TRUE
146: .equals(multipleProperty.getValue()))
147: || bean.getInstance() instanceof MultipleSelectOptionsList) {
148: Object[] values = value == null ? new Object[0]
149: : (Object[]) value;
150: isMultiple = true;
151: for (int i = 0; i < opts.length; i++) {
152: options.add(opts[i]);
153: selected.add(Boolean.FALSE);
154: for (int j = 0; j < values.length; j++) {
155: if (opts[i].getValue().equals(values[j]))
156: selected.set(i, Boolean.TRUE);
157: }
158: }
159: } else {
160: isMultiple = false;
161: for (int i = 0; i < opts.length; i++) {
162: options.add(opts[i]);
163: if (opts[i].getValue().equals(value))
164: selected.add(Boolean.TRUE);
165: else
166: selected.add(Boolean.FALSE);
167: }
168: }
169: }
170: }
171:
172: public Object getValue() {
173: if (selected == null)
174: return null;
175: if (isMultiple) {
176: ArrayList values = new ArrayList();
177: for (int i = 0; i < selected.size(); i++) {
178: if (Boolean.TRUE.equals(selected.get(i)))
179: values
180: .add(((Option) options.get(i))
181: .getValue());
182: }
183: return values.toArray(new Object[values.size()]);
184: } else {
185: for (int i = 0; i < selected.size(); i++) {
186: if (Boolean.TRUE.equals(selected.get(i)))
187: return ((Option) options.get(i)).getValue();
188: }
189: }
190: return null;
191: }
192:
193: ArrayList tableModelListenerList = new ArrayList();
194:
195: public void addTableModelListener(TableModelListener listener) {
196: tableModelListenerList.add(listener);
197: }
198:
199: public void removeTableModelListener(TableModelListener listener) {
200: tableModelListenerList.remove(listener);
201: }
202:
203: public void setDesignProperty(DesignProperty designProperty) {
204: this .designProperty = designProperty;
205: }
206:
207: public boolean isCellEditable(int rowIndex, int columnIndex) {
208: if (columnIndex != selectedIndex)
209: return false;
210: if (rowIndex < 0 || rowIndex >= options.size())
211: return false;
212: return true;
213: }
214:
215: public void setValueAt(Object newValue, int rowIndex,
216: int columnIndex) {
217: if (!isCellEditable(rowIndex, columnIndex))
218: return;
219: // If underlying property takes a list of values, update the list, and
220: // set property to new state of list.
221: if (isMultiple) {
222: selected.set(rowIndex, newValue);
223: ArrayList selectedValues = new ArrayList();
224: for (int i = 0; i < options.size(); i++) {
225: if (Boolean.TRUE.equals(selected.get(i)))
226: selectedValues.add(((Option) options.get(i))
227: .getValue());
228: }
229: //this.setValue(selectedValues.toArray());
230: }
231: // If underlying property takes a single value, and user has just deselected
232: // the previously selected value, then set property value to null.
233: else if (Boolean.FALSE.equals(newValue)) {
234: selected.set(rowIndex, Boolean.FALSE);
235: //this.setValue(null);
236: }
237: // If underlying property takes a single value, and user has just selected
238: // a new option, deselect previously selected option, and set property value
239: // to new option's value.
240: else {
241: for (int i = 0; i < selected.size(); i++) {
242: if (Boolean.TRUE.equals(selected.get(i))) {
243: selected.set(i, Boolean.FALSE);
244: //((TabularPropertyPanel) super.getCustomEditor()).updateTableData(i, selectedIndex);
245: for (int j = 0; j < tableModelListenerList
246: .size(); j++) {
247: TableModelListener l = (TableModelListener) tableModelListenerList
248: .get(j);
249: l.tableChanged(new TableModelEvent(this , i,
250: i, selectedIndex));
251: }
252: }
253: }
254: selected.set(rowIndex, Boolean.TRUE);
255: //this.setValue(((Option) options.get(rowIndex)).getValue());
256: }
257: }
258:
259: public Object getValueAt(int rowIndex, int columnIndex) {
260: if (rowIndex >= 0 && rowIndex < options.size()) {
261: if (columnIndex == labelIndex)
262: return ((Option) options.get(rowIndex)).getLabel();
263: else if (columnIndex == valueIndex)
264: return ((Option) options.get(rowIndex)).getValue();
265: else
266: return selected.get(rowIndex);
267: }
268: return null;
269: }
270:
271: public Class getColumnClass(int columnIndex) {
272: if (columnIndex == labelIndex)
273: return String.class;
274: else if (columnIndex == selectedIndex)
275: return Boolean.class;
276: else if (options.size() > 0 && options.get(0) != null)
277: return ((Option) options.get(0)).getValue().getClass();
278: else
279: return Object.class;
280: }
281:
282: public int getRowCount() {
283: return options.size();
284: }
285:
286: public String getColumnName(int columnIndex) {
287: return columnNames[columnIndex];
288: }
289:
290: public int getColumnCount() {
291: return columnNames.length;
292: }
293:
294: public boolean canAddRow() {
295: return false;
296: }
297:
298: public boolean addRow() {
299: return false;
300: }
301:
302: public boolean canMoveRow(int indexFrom, int indexTo) {
303: return false;
304: }
305:
306: public boolean moveRow(int indexFrom, int indexTo) {
307: return false;
308: }
309:
310: public boolean canRemoveRow(int index) {
311: return false;
312: }
313:
314: public boolean removeRow(int index) {
315: return false;
316: }
317:
318: public boolean removeAllRows() {
319: return false;
320: }
321:
322: }
323:
324: }
|