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.ArrayList;
048: import java.util.Collection;
049: import java.util.Collections;
050: import java.util.List;
051: import java.util.Map;
052: import java.util.TreeMap;
053: import javax.swing.DefaultComboBoxModel;
054: import javax.swing.JComboBox;
055: import javax.swing.JComponent;
056: import javax.swing.JPanel;
057: import javax.swing.JRadioButton;
058: import org.netbeans.modules.vmd.api.model.DesignComponent;
059: import org.netbeans.modules.vmd.api.model.DesignDocument;
060: import org.netbeans.modules.vmd.api.model.PropertyValue;
061: import org.netbeans.modules.vmd.api.model.TypeID;
062: import org.netbeans.modules.vmd.midp.components.MidpDocumentSupport;
063: import org.netbeans.modules.vmd.midp.components.MidpTypes;
064: import org.netbeans.modules.vmd.midp.components.MidpValueSupport;
065: import org.netbeans.modules.vmd.midp.components.categories.CommandsCategoryCD;
066: import org.netbeans.modules.vmd.midp.components.commands.CommandCD;
067: import org.netbeans.modules.vmd.midp.components.commands.ListSelectCommandCD;
068: import org.netbeans.modules.vmd.midp.components.displayables.DisplayableCD;
069: import org.netbeans.modules.vmd.midp.components.sources.CommandEventSourceCD;
070: import org.netbeans.modules.vmd.midp.propertyeditors.api.usercode.PropertyEditorElement;
071: import org.netbeans.modules.vmd.midp.propertyeditors.api.usercode.PropertyEditorUserCode;
072: import org.openide.awt.Mnemonics;
073: import org.openide.util.NbBundle;
074:
075: /**
076: *
077: * @author Anton Chechel
078: * @author Karol Harezlak
079: */
080: public final class PropertyEditorListSelectCommand extends
081: PropertyEditorUserCode implements PropertyEditorElement {
082:
083: private final List<String> tags = new ArrayList<String>();
084: private final Map<String, DesignComponent> values = new TreeMap<String, DesignComponent>();
085: private CustomEditor customEditor;
086: private JRadioButton radioButton;
087: private TypeID typeID;
088: private String noneItem;
089: private String defaultItem;
090:
091: public static PropertyEditorListSelectCommand createInstance() {
092: String mnemonic = NbBundle.getMessage(
093: PropertyEditorListSelectCommand.class,
094: "LBL_SEL_COMMAND_STR"); // NOI18N
095: String noneItem = NbBundle.getMessage(
096: PropertyEditorListSelectCommand.class,
097: "LBL_SELECTCOMMAND_NONE"); // NOI18N
098: String defaultItem = NbBundle.getMessage(
099: PropertyEditorListSelectCommand.class,
100: "LBL_SELECTCOMMAND_DEFAULT"); // NOI18N
101: String userCodeLabel = NbBundle.getMessage(
102: PropertyEditorListSelectCommand.class,
103: "LBL_SELECTCOMMAND_UCLABEL"); // NOI18N
104: return new PropertyEditorListSelectCommand(
105: ListSelectCommandCD.TYPEID, mnemonic, noneItem,
106: defaultItem, userCodeLabel);
107: }
108:
109: private PropertyEditorListSelectCommand(TypeID typeID,
110: String mnemonic, String noneItem, String defaultItem,
111: String userCodeLabel) {
112: super (userCodeLabel);
113: initComponents();
114: this .typeID = typeID;
115: Mnemonics.setLocalizedText(radioButton, mnemonic);
116: this .noneItem = noneItem;
117: this .defaultItem = defaultItem;
118:
119: initElements(Collections
120: .<PropertyEditorElement> singleton(this ));
121: }
122:
123: private void initComponents() {
124: radioButton = new JRadioButton();
125:
126: customEditor = new CustomEditor();
127: radioButton.addActionListener(customEditor);
128: }
129:
130: public JComponent getCustomEditorComponent() {
131: return customEditor;
132: }
133:
134: public JRadioButton getRadioButton() {
135: return radioButton;
136: }
137:
138: public boolean isInitiallySelected() {
139: return true;
140: }
141:
142: public boolean isVerticallyResizable() {
143: return false;
144: }
145:
146: @Override
147: public String getAsText() {
148: if (isCurrentValueAUserCodeType()) {
149: return USER_CODE_TEXT;
150: } else if (isCurrentValueANull()) {
151: return noneItem;
152: }
153:
154: PropertyValue value = (PropertyValue) super .getValue();
155: return getDecodeValue(value);
156: }
157:
158: public String getTextForPropertyValue() {
159: return null;
160: }
161:
162: public void setTextForPropertyValue(String text) {
163: saveValue(text);
164: }
165:
166: public void updateState(PropertyValue value) {
167: customEditor.updateModel();
168: if (isCurrentValueANull() || value == null) {
169: customEditor.setValue(null);
170: } else {
171: customEditor.setValue(value);
172: }
173: radioButton.setSelected(!isCurrentValueAUserCodeType());
174: }
175:
176: private void saveValue(String text) {
177: if (text.length() > 0) {
178: if (noneItem.equals(text)) {
179: super .setValue(NULL_VALUE);
180: } else {
181: super
182: .setValue(PropertyValue
183: .createComponentReference(getCommandEvenSource(text)));
184: }
185: }
186: }
187:
188: @Override
189: public void customEditorOKButtonPressed() {
190: super .customEditorOKButtonPressed();
191: if (radioButton.isSelected()) {
192: saveValue(customEditor.getText());
193: }
194: }
195:
196: @Override
197: public Boolean canEditAsText() {
198: return null;
199: }
200:
201: @Override
202: public String[] getTags() {
203: tags.clear();
204: if (isCurrentValueAUserCodeType()) {
205: tags.add(PropertyEditorUserCode.USER_CODE_TEXT);
206: } else {
207: tags.add(noneItem);
208: values.clear();
209: values.put(noneItem, null);
210:
211: if (component != null && component.get() != null) {
212: final DesignDocument document = component.get()
213: .getDocument();
214: document.getTransactionManager().writeAccess(
215: new Runnable() {
216:
217: public void run() {
218: Collection<DesignComponent> components = MidpDocumentSupport
219: .getCategoryComponent(
220: document,
221: CommandsCategoryCD.TYPEID)
222: .getComponents();
223: Collection<DesignComponent> commands = new ArrayList<DesignComponent>(
224: components.size());
225: for (DesignComponent command : components) {
226: PropertyValue ordinaryValue = command
227: .readProperty(CommandCD.PROP_ORDINARY);
228: if (MidpTypes
229: .getBoolean(ordinaryValue)) {
230: commands.add(command);
231: }
232: }
233:
234: tags.add(defaultItem);
235: values.put(defaultItem,
236: getListSelectCommand(document));
237:
238: for (DesignComponent command : commands) {
239: String displayName = getComponentDisplayName(command);
240: tags.add(displayName);
241: values.put(displayName, command);
242: }
243: }
244: });
245: }
246: }
247: return tags.toArray(new String[tags.size()]);
248: }
249:
250: private DesignComponent getListSelectCommand(DesignDocument document) {
251: return MidpDocumentSupport
252: .getSingletonCommand(document, typeID);
253: }
254:
255: private String getComponentDisplayName(DesignComponent component) {
256: return MidpValueSupport.getHumanReadableString(component);
257: }
258:
259: private String getDecodeValue(final PropertyValue value) {
260: final String[] decodeValue = new String[1];
261: final DesignComponent valueComponent = value.getComponent();
262: if (valueComponent != null) {
263: final DesignDocument document = valueComponent
264: .getDocument();
265: if (document != null) {
266: document.getTransactionManager().readAccess(
267: new Runnable() {
268:
269: public void run() {
270: DesignComponent valueComponent = value
271: .getComponent();
272: if (valueComponent != null) {
273: PropertyValue pv = valueComponent
274: .readProperty(CommandEventSourceCD.PROP_COMMAND);
275: if (pv != null) {
276: DesignComponent refComponent = pv
277: .getComponent();
278: if (refComponent != null
279: && refComponent
280: .equals(getListSelectCommand(document))) {
281: decodeValue[0] = defaultItem;
282: } else {
283: decodeValue[0] = getComponentDisplayName(valueComponent);
284: }
285: } else {
286: decodeValue[0] = noneItem;
287: }
288: } else {
289: decodeValue[0] = noneItem;
290: }
291: }
292: });
293: }
294: }
295:
296: return decodeValue[0];
297: }
298:
299: private DesignComponent getCommandEvenSource(final String name) {
300: final DesignComponent[] itemCommandEvenSource = new DesignComponent[1];
301: if (component != null && component.get() != null) {
302: final DesignComponent listComponent = component.get();
303: listComponent.getDocument().getTransactionManager()
304: .writeAccess(new Runnable() {
305:
306: public void run() {
307: DesignComponent command = values.get(name);
308: List<PropertyValue> listESValues = listComponent
309: .readProperty(
310: DisplayableCD.PROP_COMMANDS)
311: .getArray();
312: for (PropertyValue esValue : listESValues) {
313: DesignComponent existingES = esValue
314: .getComponent();
315: if (existingES
316: .readProperty(
317: CommandEventSourceCD.PROP_COMMAND)
318: .getComponent().equals(command)) {
319: itemCommandEvenSource[0] = existingES;
320: break;
321: }
322: }
323:
324: if (itemCommandEvenSource[0] == null) {
325: // create new ItemCommandEvenSource
326: itemCommandEvenSource[0] = MidpDocumentSupport
327: .attachCommandToDisplayable(
328: listComponent, command);
329: }
330: }
331: });
332: }
333: return itemCommandEvenSource[0];
334: }
335:
336: private class CustomEditor extends JPanel implements ActionListener {
337:
338: private JComboBox combobox;
339:
340: public CustomEditor() {
341: initComponents();
342: }
343:
344: private void initComponents() {
345: setLayout(new BorderLayout());
346: combobox = new JComboBox();
347: combobox.setModel(new DefaultComboBoxModel());
348: combobox.addActionListener(this );
349: add(combobox, BorderLayout.CENTER);
350: }
351:
352: public void setValue(final PropertyValue value) {
353: if (value == null) {
354: combobox.setSelectedItem(noneItem);
355: return;
356: }
357:
358: final PropertyValue[] cmdValue = new PropertyValue[1];
359: if (component != null && component.get() != null) {
360: final DesignDocument document = component.get()
361: .getDocument();
362: document.getTransactionManager().readAccess(
363: new Runnable() {
364:
365: public void run() {
366: cmdValue[0] = value
367: .getComponent()
368: .readProperty(
369: CommandEventSourceCD.PROP_COMMAND);
370: }
371: });
372: }
373: if (cmdValue[0] == null) {
374: return;
375: }
376: DesignComponent command = cmdValue[0].getComponent();
377: for (String key : values.keySet()) {
378: DesignComponent tmpCommand = values.get(key);
379: if (tmpCommand != null && tmpCommand.equals(command)) {
380: combobox.setSelectedItem(key);
381: break;
382: }
383: }
384: }
385:
386: public String getText() {
387: return (String) combobox.getSelectedItem();
388: }
389:
390: public void updateModel() {
391: DefaultComboBoxModel model = (DefaultComboBoxModel) combobox
392: .getModel();
393: model.removeAllElements();
394: for (String tag : tags) {
395: model.addElement(tag);
396: }
397: }
398:
399: public void actionPerformed(ActionEvent evt) {
400: radioButton.setSelected(true);
401: }
402: }
403: }
|