KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > xml > schema > ui > nodes > categorized > ReadOnlySchemaComponentNode


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-2007 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 /*
21  * ReadOnlySchemaComponentNode.java
22  *
23  * Created on April 12, 2006, 2:41 PM
24  *
25  * To change this template, choose Tools | Template Manager
26  * and open the template in the editor.
27  */

28
29 package org.netbeans.modules.xml.schema.ui.nodes.categorized;
30
31 import java.awt.datatransfer.Transferable JavaDoc;
32 import java.beans.PropertyEditor JavaDoc;
33 import java.lang.reflect.InvocationTargetException JavaDoc;
34 import java.text.MessageFormat JavaDoc;
35 import java.util.ArrayList JavaDoc;
36 import java.util.Collections JavaDoc;
37 import javax.swing.Action JavaDoc;
38 import org.netbeans.modules.xml.refactoring.actions.RefactorAction;
39 import org.netbeans.modules.xml.schema.ui.nodes.SchemaComponentNode;
40 import org.openide.nodes.FilterNode;
41 import org.openide.nodes.Node;
42 import org.openide.nodes.Node.Property;
43 import org.openide.nodes.Node.PropertySet;
44 import org.openide.nodes.PropertySupport;
45 import org.openide.util.Lookup;
46 import org.openide.util.datatransfer.NewType;
47 import org.openide.util.datatransfer.PasteType;
48 import org.openide.util.lookup.AbstractLookup;
49 import org.openide.util.lookup.InstanceContent;
50 import org.openide.util.lookup.ProxyLookup;
51
52 /**
53  *
54  * @author Ajit Bhate
55  */

56 public class ReadOnlySchemaComponentNode extends FilterNode {
57     private transient String JavaDoc displayTemplate;
58     
59     public ReadOnlySchemaComponentNode(Node original ,String JavaDoc displayTemplate) {
60         this(original);
61         this.displayTemplate = displayTemplate;
62     }
63     
64     public ReadOnlySchemaComponentNode(Node original) {
65         this(original,new Children(original), new InstanceContent());
66     }
67     
68     protected ReadOnlySchemaComponentNode(Node original,
69             org.openide.nodes.Children children, InstanceContent ic) {
70         super(original, children, new ProxyLookup(new Lookup[] {
71             new AbstractLookup(ic), original.getLookup()}));
72         ic.add(this);
73         SchemaComponentNode scn = (SchemaComponentNode) original.getLookup().
74                 lookup(SchemaComponentNode.class);
75         if(scn!=null) scn.setReferencingNode(this);
76     }
77     
78     public void setDisplayTemplate(String JavaDoc displayTemplate) {
79         this.displayTemplate = displayTemplate;
80     }
81     
82     public String JavaDoc getDefaultDisplayName() {
83     String JavaDoc displayName = null;
84     SchemaComponentNode scn = (SchemaComponentNode)
85         getLookup().lookup(SchemaComponentNode.class);
86     if (scn != null) {
87         displayName = scn.getDefaultDisplayName();
88     } else {
89         displayName = super.getDisplayName();
90     }
91     if(displayTemplate!=null)
92             displayName = MessageFormat.format(displayTemplate,displayName);
93     return displayName;
94     }
95     
96     
97     public String JavaDoc getHtmlDisplayName() {
98         String JavaDoc retValue = super.getHtmlDisplayName();
99         if(retValue == null) retValue = getDefaultDisplayName();
100         if(retValue != null)
101             retValue = "<font color='#999999'>"+retValue+"</font>";
102         return retValue;
103     }
104     
105     public boolean canRename() {
106         return false;
107     }
108     
109     public boolean canDestroy() {
110         return false;
111     }
112     
113     public boolean canCut() {
114         return false;
115     }
116     
117     public boolean canCopy() {
118         // Allow read-only components to be copied (issue 95341).
119
return true;
120     }
121     
122     public boolean hasCustomizer() {
123         return false;
124     }
125     
126     public Node.PropertySet[] getPropertySets() {
127         PropertySet[] retValue;
128         
129         retValue = super.getPropertySets();
130         for(int i=0;i<retValue.length;i++) {
131             Property[] props = retValue[i].getProperties();
132             for(int j=0;j<props.length;j++) {
133                 final Property prop = props[j];
134                 props[j] = new PropertySupport.ReadOnly(
135                         prop.getName(), prop.getValueType(),
136                         prop.getDisplayName(),prop.getShortDescription()) {
137                     public Object JavaDoc getValue() throws IllegalAccessException JavaDoc,InvocationTargetException JavaDoc {
138                         return prop.getValue();
139                     }
140                     public PropertyEditor JavaDoc getPropertyEditor() {
141                         //TODO editor should be r/o
142
return prop.getPropertyEditor();
143                     }
144                     
145                 };
146             }
147         }
148         return retValue;
149     }
150     
151     public NewType[] getNewTypes() {
152         return new NewType[]{};
153     }
154     
155     public PasteType[] getPasteTypes(Transferable JavaDoc transferable) {
156         // Disallow pasting anything to read-only nodes.
157
return new PasteType[0];
158     }
159     
160     public PasteType getDropType(Transferable JavaDoc transferable, int action, int index) {
161         // Disallow dropping anything to read-only nodes.
162
return null;
163     }
164     
165     public Action JavaDoc[] getActions(boolean context) {
166         ArrayList JavaDoc<Action JavaDoc> actionList = new ArrayList JavaDoc<Action JavaDoc>();
167         Collections.addAll(actionList,super.getActions(context));
168         for(int i=0;i<actionList.size();) {
169             Action JavaDoc a=actionList.get(i++);
170             if(a instanceof RefactorAction) {
171                 actionList.remove(a);
172                 break;
173             }
174         }
175         return actionList.toArray(new Action JavaDoc[0]);
176     }
177     
178     private static class Children extends FilterNode.Children {
179         public Children(Node original) {
180             super(original);
181         }
182         
183         protected Node copyNode(Node node) {
184             // set details node r/o
185
DetailsNode detailsNode =
186                     (DetailsNode) node.getLookup().lookup(DetailsNode.class);
187             if(detailsNode!=null) detailsNode.setReadOnly(true);
188             // if already wrapped in RO node return super.copyNode
189
ReadOnlySchemaComponentNode roNode = (ReadOnlySchemaComponentNode)node.
190                     getLookup().lookup(ReadOnlySchemaComponentNode.class);
191             if(roNode!=null) return super.copyNode(roNode);
192             // wrap into readonlyscnode
193
return new ReadOnlySchemaComponentNode(node);
194         }
195     }
196 }
197
Popular Tags