001: /*
002: * The contents of this file are subject to the terms of the Common Development
003: * and Distribution License (the License). You may not use this file except in
004: * compliance with the License.
005: *
006: * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
007: * or http://www.netbeans.org/cddl.txt.
008: *
009: * When distributing Covered Code, include this CDDL Header Notice in each file
010: * and include the License file at http://www.netbeans.org/cddl.txt.
011: * If applicable, add the following below the CDDL Header, with the fields
012: * enclosed by brackets [] replaced by your own identifying information:
013: * "Portions Copyrighted [year] [name of copyright owner]"
014: *
015: * The Original Software is NetBeans. The Initial Developer of the Original
016: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2007 Sun
017: * Microsystems, Inc. All Rights Reserved.
018: */
019:
020: package org.netbeans.modules.bpel.project.ui.customizer;
021:
022: import java.awt.event.ActionEvent;
023: import java.awt.event.ActionListener;
024: import java.util.Collections;
025: import java.util.HashMap;
026: import java.util.List;
027:
028: import javax.swing.JCheckBox;
029: import javax.swing.JComboBox;
030: import javax.swing.JTextField;
031: import javax.swing.event.DocumentEvent;
032: import javax.swing.event.DocumentListener;
033: import javax.swing.text.BadLocationException;
034: import javax.swing.text.Document;
035: import org.netbeans.modules.compapp.projects.base.ui.customizer.IcanproProjectProperties;
036:
037: /** Class which makes creation of the GUI easier. Registers JComponent
038: * property names and handles reading/storing the values from the components
039: * automaticaly.
040: *
041: * @author Petr Hrebejk
042: */
043: public final class VisualPropertySupport {
044:
045: private static final String WRONG_TYPE = "WrongType";
046:
047: private IcanproProjectProperties webProperties;
048: private HashMap component2property;
049: private ComponentListener componentListener;
050:
051: private int comboType; //0 ... display text == value
052: //1 ... display text != value
053: private String[] comboValues;
054:
055: public VisualPropertySupport(IcanproProjectProperties webProperties) {
056: this .webProperties = webProperties;
057: this .component2property = new HashMap(10);
058: this .componentListener = new ComponentListener();
059: }
060:
061: /** Registers the component with given property, Fills the component
062: * with given object.
063: */
064: public void register(JCheckBox component, String propertyName) {
065:
066: Boolean value = (Boolean) getAsType(propertyName, Boolean.class);
067: component2property.put(component, propertyName);
068: component.setSelected(value != null && value.booleanValue());
069: component.removeActionListener(componentListener);
070: component.addActionListener(componentListener);
071: }
072:
073: /** Registers the component with given property, Fills the component
074: * with given object.
075: */
076: public void register(JTextField component, String propertyName) {
077: String value = (String) getAsType(propertyName, String.class);
078: component2property.put(component.getDocument(), propertyName);
079: component.setText(value != null ? value : "");
080: component.getDocument().addDocumentListener(componentListener);
081: }
082:
083: /** Registers JTable containing VisualClassPath items and acompaniing
084: * buttons for handling the class path
085: */
086: public void register(VisualClasspathSupport component,
087: String propertyName) {
088: List value = (List) getAsType(propertyName, List.class);
089: component2property.put(component, propertyName);
090: component.setVisualClassPathItems(value != null ? value
091: : Collections.EMPTY_LIST);
092: component.removeActionListener(componentListener);
093: component.addActionListener(componentListener);
094: }
095:
096: /** Registers combo box.
097: */
098: public void register(JComboBox component, String items[],
099: String propertyName) {
100: comboType = 0;
101: String value = (String) getAsType(propertyName, String.class);
102: component2property.put(component, propertyName);
103: // Add all items and find the selected one
104: component.removeAllItems();
105: int selectedIndex = 0;
106: for (int i = 0; i < items.length; i++) {
107: component.addItem(items[i]);
108: if (items[i].equals(value)) {
109: selectedIndex = i;
110: }
111: }
112: component.setSelectedIndex(selectedIndex);
113: component.removeActionListener(componentListener);
114: component.addActionListener(componentListener);
115: }
116:
117: /** Registers combo box.
118: */
119: public void register(JComboBox component, String displayNames[],
120: String[] values, String propertyName) {
121: comboType = 1;
122: comboValues = values;
123: String value = (String) getAsType(propertyName, String.class);
124: component2property.put(component, propertyName);
125: // Add all items and find the selected one
126: component.removeAllItems();
127: int selectedIndex = 0;
128: for (int i = 0; i < displayNames.length; i++) {
129: component.addItem(displayNames[i]);
130: if (values[i].equals(value))
131: selectedIndex = i;
132: }
133: component.setSelectedIndex(selectedIndex);
134: component.removeActionListener(componentListener);
135: component.addActionListener(componentListener);
136: }
137:
138: /**
139: * Registers JList containing VisualClassPath items and acompaniing buttons for handling the
140: * class path
141: *
142: * @param component DOCUMENT ME!
143: * @param propertyName DOCUMENT ME!
144: */
145: public void register(VisualArchiveIncludesSupport component,
146: String propertyName) {
147: List value = (List) getAsType(propertyName, List.class);
148: component2property.put(component, propertyName);
149: component.setVisualWarItems((value != null) ? value
150: : Collections.EMPTY_LIST);
151: component.removeActionListener(componentListener);
152: component.addActionListener(componentListener);
153: }
154:
155: // Static methods for reading components and models ------------------------
156:
157: private static Boolean readValue(JCheckBox checkBox) {
158: return checkBox.isSelected() ? Boolean.TRUE : Boolean.FALSE;
159: }
160:
161: private static String readValue(Document document) {
162: try {
163: return document.getText(0, document.getLength());
164: } catch (BadLocationException e) {
165: assert false : "Invalid document "; //NOI18N
166: return ""; // NOI18N
167: }
168: }
169:
170: private static String readValue(JComboBox comboBox) {
171: return (String) comboBox.getSelectedItem();
172: }
173:
174: // Private methods ---------------------------------------------------------
175:
176: private Object getAsType(String propertyName, Class expectedType) {
177: return getAsType(propertyName, expectedType, true);
178: }
179:
180: private Object getAsType(String propertyName, Class expectedType,
181: boolean throwException) {
182:
183: Object value = webProperties.get(propertyName);
184:
185: if (value == null || expectedType.isInstance(value)) {
186: return value;
187: } else if (throwException) {
188: throw new IllegalArgumentException("Value of property: "
189: + propertyName + // NOI18N
190: " exbected to be: " + expectedType.getName() + // NOI18N
191: " but was: " + value.getClass().getName() + "!"); // NOI18N
192: } else {
193: return WRONG_TYPE;
194: }
195:
196: }
197:
198: private class ComponentListener implements ActionListener,
199: DocumentListener {
200:
201: // Implementation of action listener -----------------------------------
202:
203: public void actionPerformed(ActionEvent e) {
204: Object source = e.getSource();
205: String propertyName = (String) component2property
206: .get(source);
207: if (propertyName != null) {
208: if (source instanceof JCheckBox) {
209: webProperties.put(propertyName,
210: readValue((JCheckBox) source));
211: } else if (source instanceof VisualClasspathSupport) {
212: webProperties.put(propertyName,
213: ((VisualClasspathSupport) source)
214: .getVisualClassPathItems());
215: } else if (source instanceof JComboBox) {
216: if (((JComboBox) source).getItemCount() == 0) {
217: return;
218: }
219:
220: switch (comboType) {
221: case 0:
222: webProperties.put(propertyName,
223: readValue((JComboBox) source));
224: break;
225: case 1:
226: webProperties.put(propertyName,
227: comboValues[((JComboBox) source)
228: .getSelectedIndex()]);
229: break;
230: }
231: } else if (source instanceof VisualArchiveIncludesSupport) {
232: webProperties.put(propertyName,
233: ((VisualArchiveIncludesSupport) source)
234: .getVisualWarItems());
235: }
236: }
237: }
238:
239: // Implementation of document listener ---------------------------------
240:
241: public void changedUpdate(DocumentEvent e) {
242: Document document = e.getDocument();
243: String propertyName = (String) component2property
244: .get(document);
245: if (propertyName != null) {
246: webProperties.put(propertyName, readValue(document));
247: }
248: }
249:
250: public void insertUpdate(DocumentEvent e) {
251: changedUpdate(e);
252: }
253:
254: public void removeUpdate(DocumentEvent e) {
255: changedUpdate(e);
256: }
257: }
258: }
|