KickJava   Java API By Example, From Geeks To Geeks.

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


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.Identifier;
29 import org.openide.src.Type;
30 import org.openide.util.Utilities;
31 import org.openide.util.NbBundle;
32 import org.openide.explorer.propertysheet.ExPropertyEditor;
33 import org.openide.explorer.propertysheet.PropertyEnv;
34
35 /** Property editors for array of org.openide.src.Identifier
36 *
37 * @author Petr Hamernik
38 */

39 public class IdentifierArrayEditor extends PropertyEditorSupport implements ExPropertyEditor {
40
41     /** Custom property editor Component. */
42     IdentifierArrayPanel 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     PropertyEnv env;
55
56     /** @return text representation of the value */
57     public String JavaDoc getAsText() {
58         Identifier[] id = (Identifier []) getValue();
59         if ( id == null )
60             return ""; // NOI18N
61

62         StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
63
64         for (int i = 0; i < id.length; i++) {
65             if (i > 0)
66                 buf.append(", "); // NOI18N
67
buf.append(id[i].getSourceName());
68         }
69
70         return buf.toString();
71     }
72
73     /** Sets the value as the text */
74     public void setAsText(String JavaDoc text) throws IllegalArgumentException JavaDoc {
75         StringTokenizer tukac = new StringTokenizer(text, ", ", false); // NOI18N
76
ArrayList list = new ArrayList();
77
78         while (tukac.hasMoreTokens()) {
79             String JavaDoc id = tukac.nextToken();
80             try {
81                 // Assume any Java identifier can serve as a reference type name:
82
Type t = Type.parse(id);
83                 if (!t.isClass())
84                     throw new IllegalArgumentException JavaDoc();
85             } catch (IllegalArgumentException JavaDoc ex) {
86                 String JavaDoc msg = java.text.MessageFormat.format(
87                         getString("MSG_InvalidIdentifier"),
88                         new Object JavaDoc[] { id });
89                 ErrorManager.getDefault().annotate(ex,
90                 ErrorManager.USER, null, msg, null, null);
91                 throw ex;
92             }
93             list.add(Identifier.create(id));
94         }
95
96         Identifier[] ret = new Identifier[list.size()];
97         list.toArray(ret);
98         setValue(ret);
99     }
100
101     /** Set new value */
102     public void setValue(Object JavaDoc o) {
103         ignoreEditor = true;
104         boolean saveIgnorePanel = ignorePanel;
105         
106         ignorePanel = false;
107         super.setValue(o);
108         if ((panel != null) & !saveIgnorePanel) {
109             panel.setIdentifiers((Identifier[])o);
110         }
111         ignoreEditor = false;
112     }
113
114     /** @return <CODE>true</CODE> */
115     public boolean supportsCustomEditor () {
116         return true;
117     }
118
119     /** Create new panel for this property editor.
120     * @return the visual component for editing the property
121     */

122     public java.awt.Component JavaDoc getCustomEditor () {
123         if (panel == null) {
124             panel = new IdentifierArrayPanel();
125             panel.setIdentifiers((Identifier[])getValue());
126             panel.setMnemonics(env);
127             panel.addPropertyChangeListener(new PropertyChangeListener() {
128                                                 public void propertyChange(PropertyChangeEvent evt) {
129                                                     if (!ignoreEditor && IdentifierArrayPanel.PROP_IDENTIFIERS.equals(evt.getPropertyName())) {
130                                                         ignorePanel = true;
131                                                         setValue(evt.getNewValue());
132                                                         ignorePanel = false;
133                                                     }
134                                                 }
135                                             });
136         }
137         return panel;
138     }
139
140     /**
141      * This method is called by the IDE to pass
142      * the environment to the property editor.
143      */

144     public void attachEnv(PropertyEnv env) {
145         this.env = env;
146     }
147     
148     /** Implementation of the abstract ObjectArrayPanel2 class.
149     * It is used for editing of arrays of Identifier objects.
150     */

151     static class IdentifierArrayPanel extends ObjectArrayPanel2 {
152
153         /** Name of the 'identifiers' property. */
154         public static final String JavaDoc PROP_IDENTIFIERS = "identifiers"; // NOI18N
155

156         /** Previous value */
157         Identifier[] prevValue;
158
159         static final long serialVersionUID =-8655189809250688928L;
160         /** Constructor */
161         public IdentifierArrayPanel() {
162             prevValue = new Identifier[0];
163
164             this.getListComponent().setCellRenderer(new DefaultListCellRenderer() {
165                                                         public java.awt.Component JavaDoc getListCellRendererComponent(JList list,
166                                                                 Object JavaDoc value, int index, boolean isSelected, boolean cellHasFocus) {
167                                                             java.awt.Component JavaDoc comp = super.getListCellRendererComponent(list,
168                                                                                       value, index, isSelected, cellHasFocus);
169                                                             if (comp == this) {
170                                                                 setText(((Identifier)value).getFullName());
171                                                             }
172                                                             return comp;
173                                                         }
174                                                     });
175         }
176
177         /** @return the current value */
178         public Identifier[] getIdentifiers() {
179             Identifier[] ret = new Identifier[model.size()];
180             model.copyInto(ret);
181             return ret;
182         }
183
184         /** Set new value.
185         */

186         public void setIdentifiers(Identifier[] data) {
187             model = new DefaultListModel();
188             if (data != null) {
189                 for (int i = 0; i < data.length; i++)
190                     model.addElement(data[i]);
191             }
192             this.getListComponent().setModel(model);
193             modelChanged();
194         }
195
196         /** Fire the 'identifiers' property change. */
197         protected void modelChanged() {
198             Identifier[] newValue = getIdentifiers();
199             firePropertyChange(PROP_IDENTIFIERS, prevValue, newValue);
200             prevValue = newValue;
201         }
202
203         /** Ask user for new value.
204         * @return new value or <CODE>null</CODE> when
205         * operation was canceled.
206         */

207         protected Object JavaDoc insertNewValue() {
208             return openInputDialog(null);
209         }
210
211         /** Ask user for edit value.
212         * @param oldValue The previous value to be edited
213         * @return new value or <CODE>null</CODE> when
214         * operation was canceled.
215         */

216         protected Object JavaDoc editValue(Object JavaDoc oldValue) {
217             return openInputDialog((Identifier) oldValue);
218         }
219
220         /** Show dialog and allow user to enter new name.
221         * @param defName Default value which is predefined.
222         * @param titleKey the key to resource bundle for the title of input dialog
223         * @return New valid name or <CODE>null</CODE> if user cancel the operation.
224         */

225         protected Identifier openInputDialog(Identifier origValue) {
226             NotifyDescriptor.InputLine input = new NotifyDescriptor.InputLine(
227                                                    getString("LAB_NewName"),
228                                                    getString("LAB_NewIdentifier")
229                                                );
230             if (origValue != null)
231                 input.setInputText(origValue.getSourceName());
232
233             for (;;) {
234                 Object JavaDoc ret = DialogDisplayer.getDefault().notify(input);
235                 if (ret == NotifyDescriptor.OK_OPTION) {
236                     String JavaDoc retValue = input.getInputText();
237                     if (retValue != null && !"".equals(retValue)) { // NOI18N
238
if (!retValue.startsWith(".") && !retValue.endsWith(".") && // NOI18N
239
(retValue.indexOf("..") == -1)) { // NOI18N
240
boolean ok = true;
241                             StringTokenizer tokenizer = new StringTokenizer(retValue, ".", false); // NOI18N
242
while (tokenizer.hasMoreTokens()) {
243                                 String JavaDoc token = tokenizer.nextToken();
244                                 if (!Utilities.isJavaIdentifier(token)) {
245                                     ok = false;
246                                     break;
247                                 }
248                             }
249                             if (ok)
250                                 return Identifier.create(retValue);
251                         }
252                     }
253                     DialogDisplayer.getDefault().notify(new NotifyDescriptor.Message(getString("MSG_NotValidID")));
254                 }
255                 else {
256                     return null;
257                 }
258             }
259         }
260     }
261          
262     private static String JavaDoc getString(String JavaDoc key) {
263         return NbBundle.getBundle("org.openide.explorer.propertysheet.editors.Bundle2", Locale.getDefault(), IdentifierArrayEditor.class.getClassLoader()).getString(key);
264     }
265          
266 }
267
Popular Tags