KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > openide > src > nodes > MemberElementNode


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.src.nodes;
21
22 import java.io.IOException JavaDoc;
23 import java.beans.*;
24 import java.lang.reflect.InvocationTargetException JavaDoc;
25 import java.text.MessageFormat JavaDoc;
26
27 import org.openide.ErrorManager;
28 import org.openide.explorer.propertysheet.editors.ModifierEditor;
29 import org.openide.src.*;
30 import org.openide.nodes.*;
31 import org.openide.util.Utilities;
32
33 /** Node representing some type of member element.
34 *
35 * @author Petr Hamernik
36 */

37 public abstract class MemberElementNode extends ElementNode {
38     /** Create a new node.
39     *
40     * @param element member element to represent
41     * @param children list of children
42     * @param writeable <code>true</code> to be writable
43     */

44     public MemberElementNode(MemberElement element, Children children, boolean writeable) {
45         super(element, children, writeable);
46         superSetName(element.getName().getName());
47     }
48
49     /** Set the node's (system) name.
50     * Attempts to change the element's name as well using {@link MemberElement#setName}.
51     * Read-only elements cannot have their name set.
52     * The display name will also be updated according to the proper format,
53     * if necessary (typically it will be).
54     *
55     * @param str the new element and node name
56     */

57     public void setName(final String JavaDoc str) {
58         try {
59             if (testJavaId(str)) {
60                 SourceEditSupport.invokeAtomicAsUser(element, new SourceEditSupport.ExceptionalRunnable() {
61                                                          public void run() throws SourceException {
62                                                              ((MemberElement)element).setName(Identifier.create(str));
63                                                              superSetName(str);
64                                                          }
65                                                      });
66             }
67         }
68         catch (IOException JavaDoc e) {
69             MessageFormat JavaDoc fmt = new MessageFormat JavaDoc(bundle.getString("MSG_ElementCantRename"));
70             String JavaDoc[] params = new String JavaDoc[] { ((MemberElement)element).getName().toString(), e.getMessage() };
71             if (params[1] == null)
72                 params[1] = ""; // NOI18N
73

74         IllegalArgumentException JavaDoc ex = new IllegalArgumentException JavaDoc("Invalid name"); // NOI18N
75
ErrorManager.getDefault().annotate(ex, ErrorManager.USER,
76             null, fmt.format(params), e, null);
77         throw ex;
78         }
79     }
80
81     /** Tests if the given string is java identifier and if not, notifies
82     * the user.
83     * @return true if it is ok.
84     */

85     boolean testJavaId(String JavaDoc str) throws IllegalArgumentException JavaDoc {
86         boolean ok = Utilities.isJavaIdentifier(str);
87         if (!ok) {
88         IllegalArgumentException JavaDoc ex = new IllegalArgumentException JavaDoc("Invalid identifier"); // NOI18N
89
ErrorManager.getDefault().annotate(ex, ErrorManager.USER,
90         null, bundle.getString("MSG_Not_Valid_Identifier"), null, null);
91             throw ex;
92         }
93         return ok;
94     }
95
96     /** Create a node property for the modifiers of the element.
97     * This property will typically display with a custom editor
98     * allowing individual modifiers to be examined.
99     * @param canW if <code>false</code>, the property will be read-only irrespective of
100     * the underlying element's ability to change the modifiers
101     * @return the property
102     */

103     protected Node.Property createModifiersProperty(boolean canW) {
104         Node.Property p = new ElementProp(PROP_MODIFIERS, Integer JavaDoc.class, canW) {
105                    /** Gets the value */
106                    public Object JavaDoc getValue () {
107                        return new Integer JavaDoc(((MemberElement) element).getModifiers());
108                    }
109
110                    /** Sets the value */
111                    public void setValue(final Object JavaDoc val) throws IllegalArgumentException JavaDoc,
112                        IllegalAccessException JavaDoc, InvocationTargetException JavaDoc {
113                        super.setValue(val);
114
115                        if (!(val instanceof Integer JavaDoc))
116                            throw new IllegalArgumentException JavaDoc();
117
118                        runAtomic(element, new SourceEditSupport.ExceptionalRunnable() {
119                                      public void run() throws SourceException {
120                                          ((MemberElement)element).setModifiers(((Integer JavaDoc)val).intValue());
121                                      }
122                                  });
123                    }
124
125                    /** Define property editor for this property. */
126                    public PropertyEditor getPropertyEditor () {
127                        return new ModifierEditor(((MemberElement)element).getModifiersMask());
128                    }
129                };
130         p.setValue("changeImmediate" /* PropertyEnv.PROP_CHANGE_IMMEDIATE */,Boolean.FALSE); // NOI18N
131
return p;
132     }
133
134     /** Create a node property representing the element's name.
135     * @param canW if <code>false</code>, property will be read-only
136     * @return the property.
137     */

138     protected Node.Property createNameProperty(boolean canW) {
139         return new ElementProp(ElementProperties.PROP_NAME, String JavaDoc.class, canW) {
140                    /** Gets the value */
141                    public Object JavaDoc getValue () {
142                        return ((MemberElement)element).getName().getName();
143                    }
144
145                    /** Sets the value */
146                    public void setValue(final Object JavaDoc val) throws IllegalArgumentException JavaDoc,
147                        IllegalAccessException JavaDoc, InvocationTargetException JavaDoc {
148                        super.setValue(val);
149                        if (!(val instanceof String JavaDoc))
150                            throw new IllegalArgumentException JavaDoc();
151
152                        runAtomic(element, new SourceEditSupport.ExceptionalRunnable() {
153                                      public void run() throws SourceException {
154                                          String JavaDoc sourceName = ((String JavaDoc) val).trim();
155                                          String JavaDoc fullName = sourceName;
156
157                                          String JavaDoc prevName = ((MemberElement)element).getName().getFullName();
158                                          int dot = prevName.lastIndexOf("."); // NOI18N
159
if (dot != -1) {
160                                              fullName = prevName.substring(0, dot + 1) + sourceName;
161                                          }
162
163                                          if (testJavaId(sourceName)) {
164                                              ((MemberElement)element).setName(Identifier.create(fullName, sourceName));
165                                          }
166                                      }
167                                  });
168                    }
169                };
170     }
171 }
172
Popular Tags