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-2006 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.form.editors2;
042:
043: import java.io.IOException;
044: import javax.swing.DefaultListSelectionModel;
045: import javax.swing.ListSelectionModel;
046: import org.netbeans.modules.form.FormCodeAwareEditor;
047: import org.netbeans.modules.form.FormModel;
048: import org.netbeans.modules.form.FormProperty;
049: import org.netbeans.modules.form.RADComponent;
050: import org.netbeans.modules.form.RADProperty;
051: import org.netbeans.modules.form.codestructure.CodeVariable;
052: import org.netbeans.modules.form.editors.EnumEditor;
053: import org.openide.explorer.propertysheet.editors.XMLPropertyEditor;
054: import org.openide.util.NbBundle;
055: import org.w3c.dom.Document;
056: import org.w3c.dom.Node;
057:
058: /**
059: * Property editor for <code>JTable.selectionModel</code> property. It provides tags
060: * that correspond to valid values of <code>JTable.setSelectionMode()</code>.
061: *
062: * @author Jan Stola
063: */
064: public class JTableSelectionModelEditor extends EnumEditor implements
065: FormCodeAwareEditor, XMLPropertyEditor {
066: /** Property being edited. */
067: private FormProperty property;
068:
069: /**
070: * Creates new <code>JTableSelectionModelEditor</code>.
071: */
072: public JTableSelectionModelEditor() {
073: super (
074: new Object[] {
075: NbBundle.getMessage(
076: JTableSelectionModelEditor.class,
077: "JTableSelectionModelEditor_DEFAULT"), // NOI18N
078: new Object(),
079: "", // NOI18N
080: NbBundle
081: .getMessage(
082: JTableSelectionModelEditor.class,
083: "JTableSelectionModelEditor_SINGLE_SELECTION"), // NOI18N
084: createListSelectionModel(ListSelectionModel.SINGLE_SELECTION),
085: "ListSelectionModel.SINGLE_SELECTION", // NOI18N
086: NbBundle
087: .getMessage(
088: JTableSelectionModelEditor.class,
089: "JTableSelectionModelEditor_SINGLE_INTERVAL_SELECTION"), // NOI18N
090: createListSelectionModel(ListSelectionModel.SINGLE_INTERVAL_SELECTION),
091: "ListSelectionModel.SINGLE_INTERVAL_SELECTION", // NOI18N
092: NbBundle
093: .getMessage(
094: JTableSelectionModelEditor.class,
095: "JTableSelectionModelEditor_MULTIPLE_INTERVAL_SELECTION"), // NOI18N
096: createListSelectionModel(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION),
097: "ListSelectionModel.MULTIPLE_INTERVAL_SELECTION", // NOI18N
098: }, false);
099: }
100:
101: private static ListSelectionModel createListSelectionModel(
102: int selectionMode) {
103: DefaultListSelectionModel model = new DefaultListSelectionModel();
104: model.setSelectionMode(selectionMode);
105: return model;
106: }
107:
108: @Override
109: public void setValue(Object value) {
110: if (!(value instanceof ListSelectionModel)) {
111: value = property.getDefaultValue();
112: }
113: super .setValue(value);
114: }
115:
116: public String getSourceCode() {
117: String code = super .getJavaInitializationString();
118: if (code != null) {
119: RADProperty prop = (RADProperty) this .property;
120: RADComponent comp = prop.getRADComponent();
121: CodeVariable var = comp.getCodeExpression().getVariable();
122: String varName = (var == null) ? null : var.getName(); // NOI18N
123: varName = (varName == null) ? "" : varName + "."; // NOI18N
124: code = varName + "setSelectionMode(" + code + ");\n"; // NOI18N
125: }
126: return code;
127: }
128:
129: /**
130: * Sets context of the property editor.
131: *
132: * @param formModel form model.
133: * @param property property being edited.
134: */
135: public void setContext(FormModel formModel, FormProperty property) {
136: this .property = property;
137: }
138:
139: /**
140: * Raise form version to 6.1 - this editor is available since NB 6.1.
141: */
142: public void updateFormVersionLevel() {
143: property.getPropertyContext().getFormModel().raiseVersionLevel(
144: FormModel.FormVersion.NB61, FormModel.FormVersion.NB61);
145: }
146:
147: private static final String XML_TABLE_SELECTION_MODEL = "JTableSelectionModel"; // NOI18N
148: private static final String ATTR_SELECTION_MODE = "selectionMode"; // NOI18N
149:
150: public void readFromXML(Node element) throws IOException {
151: org.w3c.dom.NamedNodeMap attributes = element.getAttributes();
152: Object[] values = getEnumerationValues();
153: Object value;
154: Node node = attributes.getNamedItem(ATTR_SELECTION_MODE);
155: int selectionMode = Integer.valueOf(node.getNodeValue())
156: .intValue();
157: switch (selectionMode) {
158: case ListSelectionModel.SINGLE_SELECTION:
159: value = values[4];
160: break;
161: case ListSelectionModel.SINGLE_INTERVAL_SELECTION:
162: value = values[7];
163: break;
164: case ListSelectionModel.MULTIPLE_INTERVAL_SELECTION:
165: value = values[10];
166: break;
167: default:
168: value = values[1];
169: break;
170: }
171: setValue(value);
172: }
173:
174: public Node storeToXML(Document doc) {
175: Object value = getValue();
176: int selectionMode = -1;
177: Object[] values = getEnumerationValues();
178: if (values[4].equals(value)) {
179: selectionMode = ListSelectionModel.SINGLE_SELECTION;
180: } else if (values[7].equals(value)) {
181: selectionMode = ListSelectionModel.SINGLE_INTERVAL_SELECTION;
182: } else if (values[10].equals(value)) {
183: selectionMode = ListSelectionModel.MULTIPLE_INTERVAL_SELECTION;
184: }
185: org.w3c.dom.Element el = null;
186: el = doc.createElement(XML_TABLE_SELECTION_MODEL);
187: el.setAttribute(ATTR_SELECTION_MODE, Integer
188: .toString(selectionMode));
189: return el;
190: }
191:
192: }
|