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.GridLayout;
045: import java.awt.event.ItemEvent;
046: import java.awt.event.ItemListener;
047: import java.util.ArrayList;
048: import java.util.Collections;
049: import java.util.HashMap;
050: import java.util.List;
051: import java.util.Map;
052: import javax.swing.BorderFactory;
053: import javax.swing.ButtonGroup;
054: import javax.swing.JCheckBox;
055: import javax.swing.JComponent;
056: import javax.swing.JPanel;
057: import javax.swing.JRadioButton;
058: import javax.swing.JToggleButton;
059: import org.netbeans.modules.vmd.api.model.PropertyValue;
060: import org.netbeans.modules.vmd.midp.components.MidpTypes;
061: import org.netbeans.modules.vmd.midp.components.general.Bitmask.BitmaskItem;
062: import org.netbeans.modules.vmd.midp.components.items.Constraints;
063: import org.netbeans.modules.vmd.midp.components.items.TextFieldCD;
064: import org.netbeans.modules.vmd.midp.propertyeditors.api.usercode.PropertyEditorElement;
065: import org.netbeans.modules.vmd.midp.propertyeditors.api.usercode.PropertyEditorUserCode;
066: import org.openide.awt.Mnemonics;
067: import org.openide.util.NbBundle;
068:
069: /**
070: *
071: * @author Anton Chechel
072: * @author Karol Harezlak
073: */
074: public class PropertyEditorConstraints extends PropertyEditorUserCode
075: implements PropertyEditorElement {
076:
077: private CustomEditorConstraints customEditor;
078: private JRadioButton radioButton;
079:
080: private PropertyEditorConstraints() {
081: super (NbBundle.getMessage(PropertyEditorConstraints.class,
082: "LBL_CONSTR_UCLABEL")); // NOI18N
083: initComponents();
084:
085: initElements(Collections
086: .<PropertyEditorElement> singleton(this ));
087: }
088:
089: public static final PropertyEditorConstraints createInstance() {
090: return new PropertyEditorConstraints();
091: }
092:
093: private void initComponents() {
094: radioButton = new JRadioButton();
095: Mnemonics.setLocalizedText(radioButton, NbBundle.getMessage(
096: PropertyEditorConstraints.class, "LBL_CONSTR_STR")); // NOI18N
097: customEditor = new CustomEditorConstraints(0);
098: }
099:
100: public JComponent getCustomEditorComponent() {
101: return customEditor;
102: }
103:
104: public JRadioButton getRadioButton() {
105: return radioButton;
106: }
107:
108: public boolean isInitiallySelected() {
109: return true;
110: }
111:
112: public boolean isVerticallyResizable() {
113: return true;
114: }
115:
116: @Override
117: public String getAsText() {
118: String super Text = super .getAsText();
119: if (super Text != null) {
120: return super Text;
121: }
122: customEditor.setBitmask(MidpTypes
123: .getInteger((PropertyValue) super .getValue()));
124: return customEditor.getBitmaskAsText();
125: }
126:
127: public void setTextForPropertyValue(String text) {
128: }
129:
130: public String getTextForPropertyValue() {
131: return null;
132: }
133:
134: public void updateState(PropertyValue value) {
135: if (isCurrentValueANull() || value == null) {
136: customEditor.setBitmask(0);
137: } else {
138: customEditor.setBitmask(MidpTypes.getInteger(value));
139: }
140: radioButton.setSelected(!isCurrentValueAUserCodeType());
141: }
142:
143: private void saveValue() {
144: super .setValue(MidpTypes.createIntegerValue(customEditor
145: .getBitMask()));
146: }
147:
148: @Override
149: public void customEditorOKButtonPressed() {
150: super .customEditorOKButtonPressed();
151: if (radioButton.isSelected()) {
152: saveValue();
153: }
154: }
155:
156: @Override
157: public Boolean canEditAsText() {
158: return false;
159: }
160:
161: private class CustomEditorConstraints extends JPanel implements
162: ItemListener {
163:
164: private Constraints constraints;
165: private Map<JToggleButton, BitmaskItem> radioButtonsMap;
166: private Map<JToggleButton, BitmaskItem> checkBoxesMap;
167: private List<JToggleButton> guiItems;
168: private JRadioButton anyRadioButton;
169: private JCheckBox passwordCheckBox;
170:
171: private int bitMask;
172:
173: public CustomEditorConstraints(int bitmask) {
174: this .constraints = new Constraints(bitmask);
175: this .radioButtonsMap = new HashMap<JToggleButton, BitmaskItem>();
176: this .checkBoxesMap = new HashMap<JToggleButton, BitmaskItem>();
177: this .guiItems = new ArrayList<JToggleButton>();
178: this .bitMask = bitmask;
179: initComponents();
180: }
181:
182: private void initComponents() {
183: JToggleButton guiItem;
184: ButtonGroup buttonGroup = new ButtonGroup();
185:
186: setLayout(new GridLayout(6, 2));
187: this .setBorder(BorderFactory.createTitledBorder(Bundle
188: .getMessage("PNL_TEXTFIELDPE_NAME"))); // NOI18N
189:
190: // ANY
191: anyRadioButton = new JRadioButton(constraints
192: .getBitmaskItem(TextFieldCD.VALUE_ANY)
193: .getDisplayName());
194: anyRadioButton.setMnemonic(Bundle
195: .getChar("MNM_TEXTFIELDPE_ANY")); // NOI18N
196: buttonGroup.add(anyRadioButton);
197: radioButtonsMap.put(anyRadioButton, constraints
198: .getBitmaskItem(TextFieldCD.VALUE_ANY));
199: //anyRadioButton.setSelected( bitmask == TextFieldCD.VALUE_ANY);
200: guiItems.add(anyRadioButton);
201: this .add(anyRadioButton);
202:
203: // PASSWORD
204: passwordCheckBox = new JCheckBox(constraints
205: .getBitmaskItem(TextFieldCD.VALUE_PASSWORD)
206: .getDisplayName());
207: passwordCheckBox.setMnemonic(Bundle
208: .getChar("MNM_TEXTFIELDPE_PASSWORD")); // NOI18N
209: checkBoxesMap.put(passwordCheckBox, constraints
210: .getBitmaskItem(TextFieldCD.VALUE_PASSWORD));
211: guiItems.add(passwordCheckBox);
212: this .add(passwordCheckBox);
213:
214: // NUMERIC
215: guiItem = new JRadioButton(constraints.getBitmaskItem(
216: TextFieldCD.VALUE_NUMERIC).getDisplayName());
217: guiItem.setMnemonic(Bundle
218: .getChar("MNM_TEXTFIELDPE_NUMERIC")); // NOI18N
219: buttonGroup.add(guiItem);
220: radioButtonsMap.put(guiItem, constraints
221: .getBitmaskItem(TextFieldCD.VALUE_NUMERIC));
222: guiItems.add(guiItem);
223: this .add(guiItem);
224:
225: // UNEDITABLE
226: guiItem = new JCheckBox(constraints.getBitmaskItem(
227: TextFieldCD.VALUE_UNEDITABLE).getDisplayName());
228: guiItem.setMnemonic(Bundle
229: .getChar("MNM_TEXTFIELDPE_UNEDITABLE")); // NOI18N
230: checkBoxesMap.put(guiItem, constraints
231: .getBitmaskItem(TextFieldCD.VALUE_UNEDITABLE));
232: guiItems.add(guiItem);
233: this .add(guiItem);
234:
235: // EMAIL
236: guiItem = new JRadioButton(constraints.getBitmaskItem(
237: TextFieldCD.VALUE_EMAILADDR).getDisplayName());
238: guiItem
239: .setMnemonic(Bundle
240: .getChar("MNM_TEXTFIELDPE_EMAIL")); // NOI18N
241: buttonGroup.add(guiItem);
242: radioButtonsMap.put(guiItem, constraints
243: .getBitmaskItem(TextFieldCD.VALUE_EMAILADDR));
244: guiItems.add(guiItem);
245: this .add(guiItem);
246:
247: // SENSITIVE
248: guiItem = new JCheckBox(constraints.getBitmaskItem(
249: TextFieldCD.VALUE_SENSITIVE).getDisplayName());
250: guiItem.setMnemonic(Bundle
251: .getChar("MNM_TEXTFIELDPE_SENSITIVE")); // NOI18N
252: checkBoxesMap.put(guiItem, constraints
253: .getBitmaskItem(TextFieldCD.VALUE_SENSITIVE));
254: guiItems.add(guiItem);
255: this .add(guiItem);
256:
257: // PHONE NUMBER
258: guiItem = new JRadioButton(constraints.getBitmaskItem(
259: TextFieldCD.VALUE_PHONENUMBER).getDisplayName());
260: guiItem
261: .setMnemonic(Bundle
262: .getChar("MNM_TEXTFIELDPE_PHONE")); // NOI18N
263: buttonGroup.add(guiItem);
264: radioButtonsMap.put(guiItem, constraints
265: .getBitmaskItem(TextFieldCD.VALUE_PHONENUMBER));
266: guiItems.add(guiItem);
267: this .add(guiItem);
268:
269: // NON_PREDICTIVE
270: guiItem = new JCheckBox(constraints.getBitmaskItem(
271: TextFieldCD.VALUE_NON_PREDICTIVE).getDisplayName());
272: guiItem.setMnemonic(Bundle
273: .getChar("MNM_TEXTFIELDPE_NONPREDICTIVE")); // NOI18N
274: checkBoxesMap.put(guiItem, constraints
275: .getBitmaskItem(TextFieldCD.VALUE_NON_PREDICTIVE));
276: guiItems.add(guiItem);
277: this .add(guiItem);
278:
279: // URL
280: guiItem = new JRadioButton(constraints.getBitmaskItem(
281: TextFieldCD.VALUE_URL).getDisplayName());
282: guiItem.setMnemonic(Bundle.getChar("MNM_TEXTFIELDPE_URL")); // NOI18N
283: buttonGroup.add(guiItem);
284: radioButtonsMap.put(guiItem, constraints
285: .getBitmaskItem(TextFieldCD.VALUE_URL));
286: guiItems.add(guiItem);
287: this .add(guiItem);
288:
289: // INITIAL_CAPS_WORD
290: guiItem = new JCheckBox(constraints.getBitmaskItem(
291: TextFieldCD.VALUE_INITIAL_CAPS_WORD)
292: .getDisplayName());
293: guiItem.setMnemonic(Bundle
294: .getChar("MNM_TEXTFIELDPE_CAPS_WORD")); // NOI18N
295: checkBoxesMap
296: .put(
297: guiItem,
298: constraints
299: .getBitmaskItem(TextFieldCD.VALUE_INITIAL_CAPS_WORD));
300: guiItems.add(guiItem);
301: this .add(guiItem);
302:
303: // DECIMAL
304: guiItem = new JRadioButton(constraints.getBitmaskItem(
305: TextFieldCD.VALUE_DECIMAL).getDisplayName());
306: guiItem.setMnemonic(Bundle
307: .getChar("MNM_TEXTFIELDPE_DECIMAL")); // NOI18N
308: buttonGroup.add(guiItem);
309: radioButtonsMap.put(guiItem, constraints
310: .getBitmaskItem(TextFieldCD.VALUE_DECIMAL));
311: guiItems.add(guiItem);
312: this .add(guiItem);
313:
314: // INITIAL_CAPS_SENTENCE
315: guiItem = new JCheckBox(constraints.getBitmaskItem(
316: TextFieldCD.VALUE_INITIAL_CAPS_SENTENCE)
317: .getDisplayName());
318: guiItem.setMnemonic(Bundle
319: .getChar("MNM_TEXTFIELDPE_CAPS_SENTENCE")); // NOI18N
320: checkBoxesMap
321: .put(
322: guiItem,
323: constraints
324: .getBitmaskItem(TextFieldCD.VALUE_INITIAL_CAPS_SENTENCE));
325: guiItems.add(guiItem);
326: this .add(guiItem);
327:
328: setGui();
329:
330: for (JToggleButton button : guiItems) {
331: button.addItemListener(this );
332: }
333: }
334:
335: public void setBitmask(int bitmask) {
336: for (JToggleButton button : guiItems) {
337: button.removeItemListener(this );
338: }
339: constraints.setBitmask(bitmask);
340: setGui();
341: for (JToggleButton button : guiItems) {
342: button.addItemListener(this );
343: }
344: this .bitMask = bitmask;
345: }
346:
347: public void itemStateChanged(ItemEvent e) {
348: constraints.setBitmask(0);
349: for (JToggleButton button : radioButtonsMap.keySet()) {
350: if (button.isSelected()) {
351: constraints.addToBitmask(radioButtonsMap
352: .get(button), true);
353: }
354: }
355: for (JToggleButton button : checkBoxesMap.keySet()) {
356: if (button.isSelected()) {
357: constraints.addToBitmask(checkBoxesMap.get(button),
358: true);
359: }
360: }
361: bitMask = constraints.getBitmask();
362: }
363:
364: private void setGui() {
365: int radioButtonBitmask = 0;
366:
367: for (JToggleButton button : radioButtonsMap.keySet()) {
368: if (constraints.isSet(radioButtonsMap.get(button))
369: && (radioButtonBitmask <= radioButtonsMap.get(
370: button).getAffectedBits())) {
371: radioButtonBitmask = radioButtonsMap.get(button)
372: .getAffectedBits();
373: button.setSelected(true);
374: }
375: }
376: for (JToggleButton button : checkBoxesMap.keySet()) {
377: if (constraints.isSet(checkBoxesMap.get(button))) {
378: button.setSelected(true);
379: }
380: }
381: }
382:
383: public int getBitMask() {
384: return bitMask;
385: }
386:
387: public String getBitmaskAsText() {
388: int radioButtonBitmask = 0;
389: StringBuffer bitmaskAsText = null;
390:
391: for (JToggleButton button : radioButtonsMap.keySet()) {
392: if (constraints.isSet(radioButtonsMap.get(button))
393: && (radioButtonBitmask <= radioButtonsMap.get(
394: button).getAffectedBits())) {
395: radioButtonBitmask = radioButtonsMap.get(button)
396: .getAffectedBits();
397: bitmaskAsText = new StringBuffer(radioButtonsMap
398: .get(button).getName());
399: }
400: }
401: for (JToggleButton button : checkBoxesMap.keySet()) {
402: if (constraints.isSet(checkBoxesMap.get(button))) {
403: bitmaskAsText.append(" | "); // NOI18N
404: bitmaskAsText.append(checkBoxesMap.get(button)
405: .getName());
406: }
407: }
408:
409: return bitmaskAsText.toString();
410: }
411: }
412: }
|