1*cd519653SAndrew Rist /**************************************************************
2cdf0e10cSrcweir  *
3*cd519653SAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
4*cd519653SAndrew Rist  * or more contributor license agreements.  See the NOTICE file
5*cd519653SAndrew Rist  * distributed with this work for additional information
6*cd519653SAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
7*cd519653SAndrew Rist  * to you under the Apache License, Version 2.0 (the
8*cd519653SAndrew Rist  * "License"); you may not use this file except in compliance
9*cd519653SAndrew Rist  * with the License.  You may obtain a copy of the License at
10*cd519653SAndrew Rist  *
11*cd519653SAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
12*cd519653SAndrew Rist  *
13*cd519653SAndrew Rist  * Unless required by applicable law or agreed to in writing,
14*cd519653SAndrew Rist  * software distributed under the License is distributed on an
15*cd519653SAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16*cd519653SAndrew Rist  * KIND, either express or implied.  See the License for the
17*cd519653SAndrew Rist  * specific language governing permissions and limitations
18*cd519653SAndrew Rist  * under the License.
19*cd519653SAndrew Rist  *
20*cd519653SAndrew Rist  *************************************************************/
21*cd519653SAndrew Rist 
22*cd519653SAndrew Rist 
23cdf0e10cSrcweir 
24cdf0e10cSrcweir package org.openoffice.netbeans.modules.office.nodes;
25cdf0e10cSrcweir 
26cdf0e10cSrcweir import org.w3c.dom.*;
27cdf0e10cSrcweir 
28cdf0e10cSrcweir import org.openide.actions.*;
29cdf0e10cSrcweir import org.openide.nodes.*;
30cdf0e10cSrcweir import org.openide.util.HelpCtx;
31cdf0e10cSrcweir import org.openide.util.NbBundle;
32cdf0e10cSrcweir import org.openide.util.actions.SystemAction;
33cdf0e10cSrcweir 
34cdf0e10cSrcweir /** A simple node with no children.
35cdf0e10cSrcweir  *
36cdf0e10cSrcweir  * @author tomaso
37cdf0e10cSrcweir  */
38cdf0e10cSrcweir public class ScriptNode extends AbstractNode {
39cdf0e10cSrcweir     private Element element;
40cdf0e10cSrcweir     private static final String LOGICAL_NAME = "logicalname";
41cdf0e10cSrcweir     private static final String LANGUAGE_NAME = "languagename";
42cdf0e10cSrcweir 
ScriptNode(Element element)43cdf0e10cSrcweir     public ScriptNode(Element element) {
44cdf0e10cSrcweir         super(Children.LEAF);
45cdf0e10cSrcweir         this.element = element;
46cdf0e10cSrcweir         init();
47cdf0e10cSrcweir     }
48cdf0e10cSrcweir 
init()49cdf0e10cSrcweir     private void init() {
50cdf0e10cSrcweir         setIconBase("/org/openoffice/netbeans/modules/office/resources/OfficeIcon");
51cdf0e10cSrcweir         setDefaultAction(SystemAction.get(PropertiesAction.class));
52cdf0e10cSrcweir 
53cdf0e10cSrcweir         NodeList nl = element.getElementsByTagName(LOGICAL_NAME);
54cdf0e10cSrcweir         Element nameElement = (Element)nl.item(0);
55cdf0e10cSrcweir 
56cdf0e10cSrcweir         String name = nameElement.getAttribute("value");
57cdf0e10cSrcweir         setName(name);
58cdf0e10cSrcweir         setDisplayName(name.substring(name.lastIndexOf(".") + 1));
59cdf0e10cSrcweir         setShortDescription(name);
60cdf0e10cSrcweir     }
61cdf0e10cSrcweir 
createActions()62cdf0e10cSrcweir     protected SystemAction[] createActions() {
63cdf0e10cSrcweir         return new SystemAction[] {
64cdf0e10cSrcweir             SystemAction.get(ToolsAction.class),
65cdf0e10cSrcweir             null,
66cdf0e10cSrcweir             SystemAction.get(PropertiesAction.class),
67cdf0e10cSrcweir         };
68cdf0e10cSrcweir     }
69cdf0e10cSrcweir 
getHelpCtx()70cdf0e10cSrcweir     public HelpCtx getHelpCtx() {
71cdf0e10cSrcweir         return HelpCtx.DEFAULT_HELP;
72cdf0e10cSrcweir     }
73cdf0e10cSrcweir 
74cdf0e10cSrcweir     // RECOMMENDED - handle cloning specially (so as not to invoke the overhead of FilterNode):
75cdf0e10cSrcweir     /*
76cdf0e10cSrcweir     public Node cloneNode() {
77cdf0e10cSrcweir         // Try to pass in similar constructor params to what you originally got:
78cdf0e10cSrcweir         return new ScriptNode();
79cdf0e10cSrcweir     }
80cdf0e10cSrcweir      */
81cdf0e10cSrcweir 
createSheet()82cdf0e10cSrcweir     protected Sheet createSheet() {
83cdf0e10cSrcweir         Sheet sheet = super.createSheet();
84cdf0e10cSrcweir         Sheet.Set props = sheet.get(Sheet.PROPERTIES);
85cdf0e10cSrcweir         if (props == null) {
86cdf0e10cSrcweir             props = Sheet.createPropertiesSet();
87cdf0e10cSrcweir             sheet.put(props);
88cdf0e10cSrcweir         }
89cdf0e10cSrcweir 
90cdf0e10cSrcweir         org.openide.nodes.Node.Property prop = null;
91cdf0e10cSrcweir         if ((prop = getStringProperty(LOGICAL_NAME)) != null)
92cdf0e10cSrcweir             props.put(prop);
93cdf0e10cSrcweir 
94cdf0e10cSrcweir         if ((prop = getStringProperty(LANGUAGE_NAME)) != null)
95cdf0e10cSrcweir             props.put(prop);
96cdf0e10cSrcweir 
97cdf0e10cSrcweir         return sheet;
98cdf0e10cSrcweir     }
99cdf0e10cSrcweir 
getStringProperty(String name)100cdf0e10cSrcweir     private org.openide.nodes.Node.Property getStringProperty(String name) {
101cdf0e10cSrcweir         NodeList nl = element.getElementsByTagName(name);
102cdf0e10cSrcweir         if(nl.getLength() != 1)
103cdf0e10cSrcweir             return null;
104cdf0e10cSrcweir 
105cdf0e10cSrcweir         Element nameElement = (Element)nl.item(0);
106cdf0e10cSrcweir         String value = nameElement.getAttribute("value");
107cdf0e10cSrcweir 
108cdf0e10cSrcweir         return new StringProperty(this, name, value);
109cdf0e10cSrcweir     }
110cdf0e10cSrcweir 
111cdf0e10cSrcweir     private class StringProperty extends PropertySupport.ReadOnly {
112cdf0e10cSrcweir         private String name, value;
113cdf0e10cSrcweir         private ScriptNode sn;
114cdf0e10cSrcweir 
StringProperty(ScriptNode sn, String name, String value)115cdf0e10cSrcweir         public StringProperty(ScriptNode sn, String name, String value) {
116cdf0e10cSrcweir             super(value, String.class, name,
117cdf0e10cSrcweir                 "The name of the java language method for this script");
118cdf0e10cSrcweir             this.sn = sn;
119cdf0e10cSrcweir             this.name = name;
120cdf0e10cSrcweir             this.value = value;
121cdf0e10cSrcweir         }
122cdf0e10cSrcweir 
getValue()123cdf0e10cSrcweir         public Object getValue() {
124cdf0e10cSrcweir             return value;
125cdf0e10cSrcweir         }
126cdf0e10cSrcweir 
127cdf0e10cSrcweir         /* public void setValue(Object obj) {
128cdf0e10cSrcweir             System.out.println("Setting value to: " + obj.toString());
129cdf0e10cSrcweir 
130cdf0e10cSrcweir             if ((value instanceof String) != true)
131cdf0e10cSrcweir                 throw new IllegalArgumentException(name +
132cdf0e10cSrcweir                     " property must be of type String");
133cdf0e10cSrcweir 
134cdf0e10cSrcweir             value = obj.toString();
135cdf0e10cSrcweir             if (name.equals(LOGICAL_NAME)) {
136cdf0e10cSrcweir                 sn.setName(value);
137cdf0e10cSrcweir                 sn.setDisplayName(value.substring(value.lastIndexOf(".") + 1));
138cdf0e10cSrcweir                 sn.setShortDescription(value);
139cdf0e10cSrcweir             }
140cdf0e10cSrcweir         } */
141cdf0e10cSrcweir     }
142cdf0e10cSrcweir 
143cdf0e10cSrcweir     /* public boolean canRename() {
144cdf0e10cSrcweir         return true;
145cdf0e10cSrcweir     }
146cdf0e10cSrcweir 
147cdf0e10cSrcweir     public void setName(String nue) {
148cdf0e10cSrcweir         // Update visible name, fire property changes:
149cdf0e10cSrcweir         super.setName(nue);
150cdf0e10cSrcweir     } */
151cdf0e10cSrcweir 
152cdf0e10cSrcweir     /*
153cdf0e10cSrcweir      public boolean canDestroy() {
154cdf0e10cSrcweir         return true;
155cdf0e10cSrcweir     }
156cdf0e10cSrcweir     public void destroy() throws IOException {
157cdf0e10cSrcweir         // Actually remove the node itself and fire property changes:
158cdf0e10cSrcweir         super.destroy();
159cdf0e10cSrcweir         // perform additional actions, i.e. delete underlying object
160cdf0e10cSrcweir     } */
161cdf0e10cSrcweir 
162cdf0e10cSrcweir     // Handle copying and cutting specially:
163cdf0e10cSrcweir     /*
164cdf0e10cSrcweir     public boolean canCopy() {
165cdf0e10cSrcweir         return true;
166cdf0e10cSrcweir     }
167cdf0e10cSrcweir     public boolean canCut() {
168cdf0e10cSrcweir         return true;
169cdf0e10cSrcweir     }
170cdf0e10cSrcweir     public Transferable clipboardCopy() {
171cdf0e10cSrcweir         // Add to, do not replace, the default node copy flavor:
172cdf0e10cSrcweir         ExTransferable et = ExTransferable.create(super.clipboardCopy());
173cdf0e10cSrcweir         et.put(new ExTransferable.Single(DataFlavor.stringFlavor) {
174cdf0e10cSrcweir                 protected Object getData() {
175cdf0e10cSrcweir                     return ScriptNode.this.getDisplayName();
176cdf0e10cSrcweir                 }
177cdf0e10cSrcweir             });
178cdf0e10cSrcweir         return et;
179cdf0e10cSrcweir     }
180cdf0e10cSrcweir     public Transferable clipboardCut() {
181cdf0e10cSrcweir         // Add to, do not replace, the default node cut flavor:
182cdf0e10cSrcweir         ExTransferable et = ExTransferable.create(super.clipboardCut());
183cdf0e10cSrcweir         // This is not so useful because this node will not be destroyed afterwards
184cdf0e10cSrcweir         // (it is up to the paste type to decide whether to remove the "original",
185cdf0e10cSrcweir         // and it is not safe to assume that getData will only be called once):
186cdf0e10cSrcweir         et.put(new ExTransferable.Single(DataFlavor.stringFlavor) {
187cdf0e10cSrcweir                 protected Object getData() {
188cdf0e10cSrcweir                     return ScriptNode.this.getDisplayName();
189cdf0e10cSrcweir                 }
190cdf0e10cSrcweir             });
191cdf0e10cSrcweir         return et;
192cdf0e10cSrcweir     }
193cdf0e10cSrcweir      */
194cdf0e10cSrcweir 
195cdf0e10cSrcweir     // Permit user to customize whole node at once (instead of per-property):
196cdf0e10cSrcweir     /*
197cdf0e10cSrcweir     public boolean hasCustomizer() {
198cdf0e10cSrcweir         return true;
199cdf0e10cSrcweir     }
200cdf0e10cSrcweir     public Component getCustomizer() {
201cdf0e10cSrcweir         return new MyCustomizingPanel(this);
202cdf0e10cSrcweir     }
203cdf0e10cSrcweir      */
204cdf0e10cSrcweir 
205cdf0e10cSrcweir }
206