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.event.ActionEvent;
045: import java.awt.event.ActionListener;
046: import java.util.ArrayList;
047: import java.util.List;
048: import javax.swing.DefaultComboBoxModel;
049: import javax.swing.JComboBox;
050: import javax.swing.JComponent;
051: import javax.swing.JRadioButton;
052: import javax.swing.JTextField;
053: import javax.swing.event.DocumentEvent;
054: import javax.swing.event.DocumentListener;
055: import org.netbeans.modules.vmd.api.model.PropertyValue;
056: import org.netbeans.modules.vmd.midp.components.MidpTypes;
057: import org.netbeans.modules.vmd.midp.propertyeditors.api.usercode.PropertyEditorElement;
058: import org.netbeans.modules.vmd.midp.propertyeditors.api.usercode.PropertyEditorUserCode;
059: import org.openide.awt.Mnemonics;
060: import org.openide.util.NbBundle;
061:
062: /**
063: *
064: * @author Anton Chechel
065: * @version 1.0
066: */
067: public class PropertyEditorInputMode extends PropertyEditorUserCode {
068:
069: private static final String[] PREDEFINED_INPUT_MODES = {
070: "UCB_BASIC_LATIN", "UCB_GREEK", "UCB_CYRILLIC",
071: "UCB_ARMENIAN", "UCB_HEBREW", "UCB_ARABIC",
072: "UCB_DEVANAGARI", "UCB_BENGALI", "UCB_THAI",
073: "UCB_HIRAGANA", "UCB_KATAKANA", "UCB_HANGUL_SYLLABLES" }; // NOI18N
074: private List<PropertyEditorElement> elements;
075:
076: private PropertyEditorInputMode() {
077: super (NbBundle.getMessage(PropertyEditorInputMode.class,
078: "LBL_INPUT_MODE_UCLABEL")); // NOI18N
079: elements = new ArrayList<PropertyEditorElement>(2);
080: elements.add(new PredefinedEditor());
081: elements.add(new CustomEditor());
082: initElements(elements);
083: }
084:
085: public static final PropertyEditorInputMode createInstance() {
086: return new PropertyEditorInputMode();
087: }
088:
089: @Override
090: public String getAsText() {
091: String super Text = super .getAsText();
092: if (super Text != null) {
093: return super Text;
094: }
095:
096: PropertyValue value = (PropertyValue) super .getValue();
097: return (String) value.getPrimitiveValue();
098: }
099:
100: private void saveValue(String text) {
101: if (text.length() > 0) {
102: super .setValue(MidpTypes.createStringValue(text));
103: }
104: }
105:
106: @Override
107: public void customEditorOKButtonPressed() {
108: super .customEditorOKButtonPressed();
109: for (PropertyEditorElement element : elements) {
110: if (element.getRadioButton().isSelected()) {
111: saveValue(element.getTextForPropertyValue());
112: break;
113: }
114: }
115: }
116:
117: private boolean isPredefined(String str) {
118: for (String inputMode : PREDEFINED_INPUT_MODES) {
119: if (inputMode.equals(str)) {
120: return true;
121: }
122: }
123: return false;
124: }
125:
126: private final class PredefinedEditor implements
127: PropertyEditorElement, ActionListener {
128:
129: private JRadioButton radioButton;
130: private DefaultComboBoxModel model;
131: private JComboBox combobox;
132:
133: public PredefinedEditor() {
134: radioButton = new JRadioButton();
135: Mnemonics.setLocalizedText(radioButton, NbBundle
136: .getMessage(PropertyEditorInputMode.class,
137: "LBL_PREDEFINED")); // NOI18N
138: model = new DefaultComboBoxModel(PREDEFINED_INPUT_MODES);
139: combobox = new JComboBox(model);
140: combobox.addActionListener(this );
141: }
142:
143: public void updateState(PropertyValue value) {
144: if (!isCurrentValueANull() && value != null) {
145: String inputMode;
146: for (int i = 0; i < model.getSize(); i++) {
147: inputMode = (String) model.getElementAt(i);
148: if (inputMode.equals((String) value
149: .getPrimitiveValue())) {
150: model.setSelectedItem(inputMode);
151: break;
152: }
153: }
154: }
155: }
156:
157: public void setTextForPropertyValue(String text) {
158: saveValue(text);
159: }
160:
161: public String getTextForPropertyValue() {
162: return (String) combobox.getSelectedItem();
163: }
164:
165: public JComponent getCustomEditorComponent() {
166: return combobox;
167: }
168:
169: public JRadioButton getRadioButton() {
170: return radioButton;
171: }
172:
173: public boolean isInitiallySelected() {
174: return true;
175: }
176:
177: public boolean isVerticallyResizable() {
178: return false;
179: }
180:
181: public void actionPerformed(ActionEvent evt) {
182: radioButton.setSelected(true);
183: }
184: }
185:
186: private final class CustomEditor implements PropertyEditorElement,
187: DocumentListener {
188:
189: private JRadioButton radioButton;
190: private JTextField textField;
191:
192: public CustomEditor() {
193: radioButton = new JRadioButton();
194: Mnemonics.setLocalizedText(radioButton, NbBundle
195: .getMessage(PropertyEditorInputMode.class,
196: "LBL_CUSTOM")); // NOI18N
197: textField = new JTextField();
198: textField.getDocument().addDocumentListener(this );
199: }
200:
201: public void updateState(PropertyValue value) {
202: if (!isCurrentValueANull() && value != null) {
203: String str = (String) value.getPrimitiveValue();
204: if (!isPredefined(str)) {
205: // if that value is not predefined
206: textField.setText(str);
207: }
208: } else {
209: textField.setText(null);
210: }
211: }
212:
213: public void setTextForPropertyValue(String text) {
214: saveValue(text);
215: }
216:
217: public String getTextForPropertyValue() {
218: return textField.getText();
219: }
220:
221: public JComponent getCustomEditorComponent() {
222: return textField;
223: }
224:
225: public JRadioButton getRadioButton() {
226: return radioButton;
227: }
228:
229: public boolean isInitiallySelected() {
230: return false;
231: }
232:
233: public boolean isVerticallyResizable() {
234: return false;
235: }
236:
237: public void insertUpdate(DocumentEvent evt) {
238: if (textField.hasFocus()) {
239: radioButton.setSelected(true);
240: }
241: }
242:
243: public void removeUpdate(DocumentEvent evt) {
244: if (textField.hasFocus()) {
245: radioButton.setSelected(true);
246: }
247: }
248:
249: public void changedUpdate(DocumentEvent evt) {
250: }
251: }
252: }
|