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:
042: package org.netbeans.modules.vmd.midp.propertyeditors;
043:
044: import java.awt.BorderLayout;
045: import java.awt.event.ActionEvent;
046: import java.awt.event.ActionListener;
047: import java.util.Collections;
048: import java.util.Map;
049: import javax.swing.DefaultComboBoxModel;
050: import javax.swing.JComboBox;
051: import javax.swing.JComponent;
052: import javax.swing.JPanel;
053: import javax.swing.JRadioButton;
054: import org.netbeans.modules.vmd.api.model.DesignComponent;
055: import org.netbeans.modules.vmd.api.model.PropertyValue;
056: import org.netbeans.modules.vmd.api.model.TypeID;
057: import org.netbeans.modules.vmd.midp.components.MidpTypes;
058: import org.netbeans.modules.vmd.midp.components.resources.FontCD;
059: import org.netbeans.modules.vmd.midp.propertyeditors.api.usercode.PropertyEditorElement;
060: import org.netbeans.modules.vmd.midp.propertyeditors.api.usercode.PropertyEditorUserCode;
061: import org.openide.awt.Mnemonics;
062: import org.openide.util.NbBundle;
063:
064: /**
065: *
066: * @author Anton Chechel
067: * @author Karol Harezlak
068: */
069: public final class PropertyEditorComboBox extends
070: PropertyEditorUserCode implements PropertyEditorElement {
071:
072: private final Map<String, PropertyValue> values;
073: private String[] tags;
074: private String valueLabel;
075:
076: private TypeID typeID;
077: private TypeID enableTypeID;
078:
079: private CustomEditor customEditor;
080: private JRadioButton radioButton;
081: private static String[] USERCODE_TAGS = new String[] { (PropertyEditorUserCode.USER_CODE_TEXT) };
082:
083: private PropertyEditorComboBox(Map<String, PropertyValue> values,
084: TypeID typeID, TypeID enableTypeID, String valueLabel,
085: String userCodeLabel) {
086: super (userCodeLabel);
087:
088: this .values = values;
089: this .typeID = typeID;
090: this .enableTypeID = enableTypeID;
091: this .valueLabel = valueLabel;
092: createTags();
093: initComponents();
094:
095: initElements(Collections
096: .<PropertyEditorElement> singleton(this ));
097: }
098:
099: public static PropertyEditorComboBox createInstance(
100: Map<String, PropertyValue> values, TypeID typeID,
101: String valueLabel, String userCodeLabel) {
102: return createInstance(values, typeID, null, valueLabel,
103: userCodeLabel);
104: }
105:
106: public static PropertyEditorComboBox createInstance(
107: Map<String, PropertyValue> values, TypeID typeID,
108: TypeID enableTypeID, String valueLabel, String userCodeLabel) {
109: if (values == null) {
110: throw new IllegalArgumentException(
111: "Argument values can't be null"); // NOI18N
112: }
113: for (String key : values.keySet()) {
114: PropertyValue value = values.get(key);
115: if (value == null) {
116: throw new IllegalArgumentException("PropertyValue for "
117: + key + " key can't be null"); // NOI18N
118: }
119: }
120:
121: PropertyEditorComboBox instance = new PropertyEditorComboBox(
122: values, typeID, enableTypeID, valueLabel, userCodeLabel);
123: return instance;
124: }
125:
126: private void initComponents() {
127: radioButton = new JRadioButton();
128: Mnemonics.setLocalizedText(radioButton, valueLabel);
129: customEditor = new CustomEditor();
130: customEditor.updateModel();
131: }
132:
133: public JComponent getCustomEditorComponent() {
134: return customEditor;
135: }
136:
137: public JRadioButton getRadioButton() {
138: return radioButton;
139: }
140:
141: public boolean isInitiallySelected() {
142: return true;
143: }
144:
145: public boolean isVerticallyResizable() {
146: return false;
147: }
148:
149: @Override
150: public String getAsText() {
151: String super Text = super .getAsText();
152: if (super Text != null) {
153: return super Text;
154: }
155:
156: PropertyValue value = (PropertyValue) super .getValue();
157: for (String key : values.keySet()) {
158: PropertyValue tmpValue = values.get(key);
159: if (value.getPrimitiveValue().equals(
160: tmpValue.getPrimitiveValue())) {
161: return key;
162: }
163: }
164: return NbBundle.getMessage(PropertyEditorComboBox.class,
165: "LBL_MULTIPLE"); // NOI18N
166: }
167:
168: public void setTextForPropertyValue(String text) {
169: saveValue(text);
170: }
171:
172: public String getTextForPropertyValue() {
173: return null;
174: }
175:
176: public void updateState(PropertyValue value) {
177: if (isCurrentValueANull() || value == null) {
178: // clear customEditor if needed
179: } else {
180: customEditor.setValue(value);
181: }
182: radioButton.setSelected(!isCurrentValueAUserCodeType());
183: }
184:
185: private void saveValue(String text) {
186: if (text.length() > 0) {
187: PropertyValue value = values.get(text);
188: if (value != null) {
189: super .setValue(value);
190: }
191: }
192: }
193:
194: @Override
195: public void customEditorOKButtonPressed() {
196: super .customEditorOKButtonPressed();
197: if (radioButton.isSelected()) {
198: saveValue(customEditor.getText());
199: }
200: }
201:
202: @Override
203: public String[] getTags() {
204: if (isCurrentValueAUserCodeType()) {
205: return USERCODE_TAGS;
206: }
207: return tags;
208: }
209:
210: private void createTags() {
211: int i = 0;
212: tags = new String[values.size()];
213: for (String valueAsText : values.keySet()) {
214: tags[i++] = valueAsText;
215: }
216: }
217:
218: @Override
219: public Boolean canEditAsText() {
220: return null;
221: }
222:
223: @Override
224: public boolean canWrite() {
225: final boolean[] canWrite = new boolean[] { true };
226: if (!MidpPropertyEditorSupport.singleSelectionEditAsTextOnly()) {
227: canWrite[0] = false;
228: } else if (enableTypeID != null) {
229: if (enableTypeID == FontCD.TYPEID) {
230: if (component != null && component.get() != null) {
231: final DesignComponent _component = component.get();
232: _component.getDocument().getTransactionManager()
233: .readAccess(new Runnable() {
234:
235: public void run() {
236: int kind = MidpTypes
237: .getInteger(_component
238: .readProperty(FontCD.PROP_FONT_KIND));
239: canWrite[0] = kind == FontCD.VALUE_KIND_CUSTOM;
240: }
241: });
242: }
243: }
244: }
245: return canWrite[0];
246: }
247:
248: private class CustomEditor extends JPanel implements ActionListener {
249:
250: private JComboBox combobox;
251:
252: public CustomEditor() {
253: initComponents();
254: }
255:
256: private void initComponents() {
257: setLayout(new BorderLayout());
258: combobox = new JComboBox();
259: combobox.setModel(new DefaultComboBoxModel());
260: combobox.addActionListener(this );
261: add(combobox, BorderLayout.CENTER);
262: }
263:
264: public void setValue(PropertyValue value) {
265: for (String key : values.keySet()) {
266: if (values.get(key).getPrimitiveValue().equals(
267: value.getPrimitiveValue())) {
268: combobox.setSelectedItem(key);
269: break;
270: }
271: }
272: }
273:
274: public String getText() {
275: return (String) combobox.getSelectedItem();
276: }
277:
278: public void updateModel() {
279: DefaultComboBoxModel model = (DefaultComboBoxModel) combobox
280: .getModel();
281: model.removeAllElements();
282: for (String tag : tags) {
283: model.addElement(tag);
284: }
285: }
286:
287: public void actionPerformed(ActionEvent evt) {
288: radioButton.setSelected(true);
289: }
290: }
291: }
|