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