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