KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > openide > explorer > propertysheet > editors > MethodParameterArrayEditor


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.openide.explorer.propertysheet.editors;
21
22 import java.beans.*;
23 import java.util.*;
24
25 import javax.swing.*;
26
27 import org.openide.*;
28 import org.openide.src.MethodParameter;
29 import org.openide.src.Type;
30 import org.openide.util.NbBundle;
31 import org.openide.util.Utilities;
32 import org.openide.explorer.propertysheet.ExPropertyEditor;
33 import org.openide.explorer.propertysheet.PropertyEnv;
34
35 /** Property editor for array of org.openide.src.MethodParameter classes
36 *
37 * @author Petr Hamernik
38 */

39 public class MethodParameterArrayEditor extends PropertyEditorSupport implements ExPropertyEditor {
40
41     /** Custom property editor Component. */
42     MethodParameterArrayPanel panel;
43
44     /** Flag for prevention of cycle in firing
45     * of the properties changes.
46     */

47     boolean ignoreEditor = false;
48
49     /** Flag for prevention of cycle in firing
50     * of the properties changes.
51     */

52     boolean ignorePanel = false;
53     
54     private PropertyEnv env;
55
56     /**
57     * @return The property value as a human editable string.
58     * <p> Returns null if the value can't be expressed as an editable string.
59     * <p> If a non-null value is returned, then the PropertyEditor should
60     * be prepared to parse that string back in setAsText().
61     */

62     public String JavaDoc getAsText() {
63         MethodParameter[] params = (MethodParameter[]) getValue();
64         StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
65         if (params != null) {
66             for (int i = 0; i < params.length; i++) {
67                 if (i > 0)
68                     buf.append(", "); // NOI18N
69
buf.append(params[i].getSourceString());
70             }
71         }
72         return buf.toString();
73     }
74
75     /** Set the property value by parsing a given String. May raise
76     * java.lang.IllegalArgumentException if either the String is
77     * badly formatted or if this kind of property can't be expressed
78     * as text.
79     * @param text The string to be parsed.
80     */

81     public void setAsText(String JavaDoc text) throws IllegalArgumentException JavaDoc {
82         StringTokenizer tok = new StringTokenizer(text, ",", false); // NOI18N
83
ArrayList list = new ArrayList();
84         while (tok.hasMoreTokens()) {
85             String JavaDoc token = tok.nextToken();
86             MethodParameter par;
87             
88             try {
89                 par = MethodParameter.parse(token);
90             } catch (IllegalArgumentException JavaDoc ex) {
91                 String JavaDoc msg = java.text.MessageFormat.format(
92                         getString("MSG_IllegalMethodParameter"), // NOI18N
93
new Object JavaDoc[] { token });
94                 ErrorManager.getDefault().annotate(ex,
95                 ErrorManager.USER, null, msg, null, null);
96                 throw ex;
97             }
98             for (Iterator it = list.iterator(); it.hasNext(); ) {
99                 if (par.getName().equals(((MethodParameter)it.next()).getName())) {
100                     String JavaDoc msg = java.text.MessageFormat.format(
101                             getString("MSG_DuplicateName2"), // NOI18N
102
new Object JavaDoc[] { par.getName() });
103                     IllegalArgumentException JavaDoc ex= new IllegalArgumentException JavaDoc("Ambiguous name"); // NOI18N
104
ErrorManager.getDefault().annotate(ex,
105                 ErrorManager.USER, null, msg, null, null);
106                     throw ex;
107                 }
108             }
109             list.add(par);
110         }
111         MethodParameter[] params = new MethodParameter[list.size()];
112         list.toArray(params);
113         setValue(params);
114     }
115
116     /** Sets the value */
117     public void setValue(Object JavaDoc o) {
118         ignoreEditor = true;
119         boolean saveIgnorePanel = ignorePanel;
120         
121         ignorePanel = false;
122         super.setValue(o);
123         if ((panel != null) & !saveIgnorePanel) {
124             panel.setMethodParameters((MethodParameter[])o);
125         }
126         ignoreEditor = false;
127     }
128
129     /** @return <CODE>true</CODE> */
130     public boolean supportsCustomEditor () {
131         return true;
132     }
133
134     /** Create new panel for this property editor.
135     * @return the visual component for editing the property
136     */

137     public java.awt.Component JavaDoc getCustomEditor () {
138         if (panel == null) {
139             panel = new MethodParameterArrayPanel();
140             panel.setMnemonics(env);
141             panel.setMethodParameters((MethodParameter[]) getValue());
142
143             panel.addPropertyChangeListener(new PropertyChangeListener() {
144                                                 public void propertyChange(PropertyChangeEvent evt) {
145                                                     if (!ignoreEditor && MethodParameterArrayPanel.PROP_METHOD_PARAMETERS.equals(evt.getPropertyName())) {
146                                                         ignorePanel = true;
147                                                         setValue(evt.getNewValue());
148                                                         ignorePanel = false;
149                                                     }
150                                                 }
151                                             });
152
153         }
154         return panel;
155     }
156
157     /**
158      * This method is called by the IDE to pass
159      * the environment to the property editor.
160      */

161     public void attachEnv(PropertyEnv env) {
162         this.env = env;
163     }
164     
165     /** Implementation of the abstract ObjectArrayPanel2 class.
166     * It is used for editing of arrays of Identifier objects.
167     */

168     static class MethodParameterArrayPanel extends ObjectArrayPanel2
169     implements EnhancedCustomPropertyEditor {
170         /** Name of the 'methodParameters' property */
171         public static final String JavaDoc PROP_METHOD_PARAMETERS = "methodParameters"; // NOI18N
172

173         /** Previous value */
174         MethodParameter[] prevValue;
175
176         /** Constructor */
177         public MethodParameterArrayPanel() {
178             prevValue = new MethodParameter[0];
179
180             this.getListComponent().setCellRenderer(new DefaultListCellRenderer() {
181                                                         public java.awt.Component JavaDoc getListCellRendererComponent(JList list,
182                                                                 Object JavaDoc value, int index, boolean isSelected, boolean cellHasFocus) {
183                                                             java.awt.Component JavaDoc comp = super.getListCellRendererComponent(list,
184                                                                                       value, index, isSelected, cellHasFocus);
185                                                             if (comp == this) {
186                                                                 setText(((MethodParameter)value).toString());
187                                                             }
188                                                             return comp;
189                                                         }
190                                                     });
191         }
192
193         /** @return the current value
194         */

195         public MethodParameter[] getMethodParameters() {
196             MethodParameter[] ret = new MethodParameter[model.size()];
197             model.copyInto(ret);
198             return ret;
199         }
200
201         /** Set new value
202         */

203         public void setMethodParameters(MethodParameter[] data) {
204             model = new DefaultListModel();
205             if (data != null) {
206                 for (int i = 0; i < data.length; i++)
207                     model.addElement(data[i]);
208             }
209             this.getListComponent().setModel(model);
210             modelChanged();
211         }
212
213         /** Fire the 'methodParameters' property change. */
214         protected void modelChanged() {
215             super.modelChanged();
216             MethodParameter[] newValue = getMethodParameters();
217             firePropertyChange(PROP_METHOD_PARAMETERS, prevValue, newValue);
218             prevValue = newValue;
219         }
220
221         /** Ask user for new value.
222         * @return new value or <CODE>null</CODE> when
223         * operation was canceled.
224         */

225         protected Object JavaDoc insertNewValue() {
226             return openInputDialog(null);
227         }
228
229         /** Ask user for edit value.
230         * @param oldValue The previous value to be edited
231         * @return new value or <CODE>null</CODE> when
232         * operation was canceled.
233         */

234         protected Object JavaDoc editValue(Object JavaDoc oldValue) {
235             return openInputDialog((MethodParameter) oldValue);
236         }
237
238         /** Show dialog and allow user to enter new method parameter.
239         * @param defName Default value which is predefined.
240         * @param titleKey the key to resource bundle for the title of input dialog
241         * @return New valid name or <CODE>null</CODE> if user cancel the operation.
242         */

243         protected MethodParameter openInputDialog(MethodParameter origValue) {
244             MethodParameterPanel panel = new MethodParameterPanel();
245
246             NotifyDescriptor desriptor = new NotifyDescriptor(
247                                              panel,
248                                              getString("LAB_EnterParameter"),
249                                              NotifyDescriptor.OK_CANCEL_OPTION,
250                                              NotifyDescriptor.PLAIN_MESSAGE, null, null);
251
252             if (origValue != null) {
253                 panel.nameTextField.setText(origValue.getName().toString());
254                 panel.typeCombo.setSelectedItem(origValue.getType().toString());
255                 panel.finalCheckBox.setSelected(origValue.isFinal());
256             }
257
258             for (;;) {
259                 Object JavaDoc ret = DialogDisplayer.getDefault().notify(desriptor);
260                 if (ret == NotifyDescriptor.OK_OPTION) {
261                     String JavaDoc errMsg = null;
262                     String JavaDoc name = panel.nameTextField.getText();
263                     if (!Utilities.isJavaIdentifier(name))
264                         errMsg = "MSG_NotValidID"; // NOI18N
265
else {
266                         try {
267                             Type type = Type.parse(panel.typeCombo.getSelectedItem().toString());
268                             boolean isFinal = panel.finalCheckBox.isSelected();
269                           Enumeration methodEnum = model.elements();
270                           
271                           while(methodEnum.hasMoreElements())
272                           { MethodParameter oldPar = (MethodParameter)methodEnum.nextElement();
273                           
274                               if (origValue!=null && oldPar.equals(origValue))
275                                   continue;
276                               if (name.equals(oldPar.getName()))
277                               { errMsg = "MSG_DuplicateName"; // NOI18N
278
break;
279                               }
280                           }
281                           if (errMsg == null)
282                               return new MethodParameter(name, type, isFinal);
283                         }
284                         catch (IllegalArgumentException JavaDoc e) {
285                             errMsg = "MSG_NotValidType"; // NOI18N
286
}
287                     }
288                     DialogDisplayer.getDefault().notify(new NotifyDescriptor.Message(getString(errMsg)));
289                 }
290                 else {
291                     return null;
292                 }
293             }
294         }
295         
296         /** Get the customized property value.
297          * @return the property value
298          * @exception InvalidStateException when the custom property editor does not contain a valid property value
299          * (and thus it should not be set)
300  */

301         public Object JavaDoc getPropertyValue() throws IllegalStateException JavaDoc {
302             return getMethodParameters();
303         }
304         
305     }
306     
307     private static String JavaDoc getString(String JavaDoc key) {
308         return NbBundle.getBundle("org.openide.explorer.propertysheet.editors.Bundle2", Locale.getDefault(), MethodParameterArrayEditor.class.getClassLoader()).getString(key);
309     }
310
311 }
312
Popular Tags