KickJava   Java API By Example, From Geeks To Geeks.

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


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 package org.netbeans.modules.xml.schema.ui.nodes.schema;
21
22 import java.beans.FeatureDescriptor JavaDoc;
23 import java.beans.PropertyChangeEvent JavaDoc;
24 import java.beans.PropertyEditor JavaDoc;
25 import java.beans.PropertyEditorSupport JavaDoc;
26 import java.lang.reflect.InvocationTargetException JavaDoc;
27 import java.util.regex.Pattern JavaDoc;
28 import javax.swing.Action JavaDoc;
29 import org.netbeans.modules.xml.schema.ui.basic.SchemaSettings;
30 import org.openide.actions.CustomizeAction;
31
32 import org.openide.nodes.Children;
33 import org.openide.nodes.Node;
34 import org.openide.nodes.Sheet;
35 import org.openide.util.NbBundle;
36 import org.openide.ErrorManager;
37 import org.openide.explorer.propertysheet.ExPropertyEditor;
38 import org.openide.explorer.propertysheet.PropertyEnv;
39
40 import org.netbeans.modules.xml.schema.model.Documentation;
41 import org.netbeans.modules.xml.schema.model.SchemaComponentReference;
42 import org.netbeans.modules.xml.xam.ui.customizer.Customizer;
43 import org.netbeans.modules.xml.xam.ui.customizer.CustomizerProvider;
44 import org.netbeans.modules.xml.schema.ui.basic.editors.StringEditor;
45 import org.netbeans.modules.xml.schema.ui.nodes.ReadOnlyCookie;
46 import org.netbeans.modules.xml.schema.ui.nodes.SchemaComponentNode;
47 import org.netbeans.modules.xml.schema.ui.nodes.SchemaModelFlushWrapper;
48 import org.netbeans.modules.xml.schema.ui.nodes.SchemaUIContext;
49 import org.netbeans.modules.xml.schema.ui.nodes.categorized.customizer.DocumentationCustomizer;
50 import org.netbeans.modules.xml.schema.ui.nodes.schema.properties.BaseSchemaProperty;
51 import org.netbeans.modules.xml.schema.ui.nodes.schema.properties.NamespaceProperty;
52 import org.openide.nodes.PropertySupport;
53 import org.openide.util.actions.SystemAction;
54
55 /**
56  *
57  * @author Todd Fast, todd.fast@sun.com
58  * @author Jeri Lockhart
59  * @author Ajit Bhate
60  */

61 public class DocumentationNode extends SchemaComponentNode<Documentation> {
62     /**
63      *
64      *
65      */

66     public DocumentationNode(SchemaUIContext context,
67             SchemaComponentReference<Documentation> reference,
68             Children children) {
69         super(context,reference,children);
70         
71         setIconBaseWithExtension(
72                 "org/netbeans/modules/xml/schema/ui/nodes/resources/documentation.png");
73     }
74     
75     
76     public String JavaDoc getHtmlDisplayName() {
77         String JavaDoc language = getReference().get().getLanguage();
78         String JavaDoc retValue = super.getDefaultDisplayName();
79         if(language!=null) {
80             retValue = retValue +" <font color='#999999'>"+"(" + language + ")"+"</font>";
81         }
82         return retValue;
83     }
84     
85     /**
86      *
87      *
88      */

89     @Override JavaDoc
90     public String JavaDoc getTypeDisplayName() {
91         return NbBundle.getMessage(DocumentationNode.class,
92                 "LBL_DocumentationNode_TypeDisplayName"); // NOI18N
93
}
94     
95     
96     public void propertyChange(PropertyChangeEvent JavaDoc event) {
97         if(!isValid()) return;
98         super.propertyChange(event);
99         String JavaDoc property = event.getPropertyName();
100         if(Documentation.LANGUAGE_PROPERTY.equals(event.getPropertyName()) &&
101                 event.getSource() == getReference().get() ) {
102             fireDisplayNameChange(null,getDisplayName());
103         }
104     }
105     
106     @Override JavaDoc
107     protected Sheet createSheet() {
108         Sheet sheet = null;
109         //sheet = super.createSheet();
110
sheet = Sheet.createDefault();
111         Sheet.Set props = sheet.get(Sheet.PROPERTIES);
112         Sheet.Set set=sheet.get(Sheet.PROPERTIES);
113         set.put(
114                 new PropertySupport("kind",String JavaDoc.class,
115                 NbBundle.getMessage(SchemaComponentNode.class,
116                 "PROP_SchemaComponentNode_Kind"),
117                 "",true,false) {
118             public Object JavaDoc getValue() {
119                 return getTypeDisplayName();
120             }
121             
122             public void setValue(Object JavaDoc value) {
123                 // Not modifiable
124
}
125         });
126         if (props == null) {
127             props = Sheet.createPropertiesSet();
128             sheet.put(props);
129         }
130         
131         try {
132             // Source (URI)
133
Node.Property uriProp = new NamespaceProperty(
134                     getReference().get(),
135                     Documentation.SOURCE_PROPERTY, // name
136
NbBundle.getMessage(DocumentationNode.class,"PROP_URI_DisplayName"), // display name
137
NbBundle.getMessage(DocumentationNode.class,"HINT_URI") // descr
138
,null) {
139                 @Override JavaDoc
140                 public java.beans.PropertyEditor JavaDoc getPropertyEditor() {
141                     return new StringEditor();
142                 }
143             };
144             props.put(new SchemaModelFlushWrapper(getReference().get(), uriProp));
145             
146             // Language
147
Node.Property langProp = new BaseSchemaProperty(
148                     getReference().get(),
149                     String JavaDoc.class, // type
150
Documentation.LANGUAGE_PROPERTY, // name
151
NbBundle.getMessage(DocumentationNode.class,"PROP_Language_DisplayName"), // display name
152
NbBundle.getMessage(DocumentationNode.class,"HINT_Language") // descr
153
, LanguageEditor.class) {
154                 public void setValue(Object JavaDoc o) throws
155                         IllegalAccessException JavaDoc, InvocationTargetException JavaDoc {
156                     if(o==null) {
157                         super.setValue(null);
158                         return;
159                     }
160                     if(o instanceof String JavaDoc) {
161                         String JavaDoc lang = (String JavaDoc)o;
162                         if(Pattern.matches(
163                                 "([a-zA-Z]{2}|[iI]-[a-zA-Z]+|[xX]-[a-zA-Z]{1,8})(-[a-zA-Z]{1,8})*"
164                                 ,lang)) {
165                             super.setValue(o);
166                             SchemaSettings.getDefault().setLanguage(lang);
167                             return;
168                         }
169                     }
170                     String JavaDoc msg = NbBundle.getMessage(DocumentationNode.class,
171                             "MSG_Invalid_Language",o); //NOI18N
172
IllegalArgumentException JavaDoc iae = new IllegalArgumentException JavaDoc(msg);
173                     ErrorManager.getDefault().annotate(iae, ErrorManager.USER,
174                             msg, msg, null, new java.util.Date JavaDoc());
175                     throw iae;
176                 }
177             };
178             
179             props.put(new SchemaModelFlushWrapper(getReference().get(),langProp));
180             
181             // Text
182
Node.Property textProp = new BaseSchemaProperty(
183                     getReference().get(),
184                     String JavaDoc.class, // type
185
Documentation.CONTENT_PROPERTY, // name
186
NbBundle.getMessage(DocumentationNode.class,"PROP_Text_DisplayName"), // display name
187
NbBundle.getMessage(DocumentationNode.class,"HINT_Text") // descr
188
, null) {
189                 public Object JavaDoc getValue() {
190                     return getReference().get().getContentFragment();
191                 }
192                 public PropertyEditor JavaDoc getPropertyEditor() {
193                     if (hasCustomizer()) {
194                         return new PropertyEditorSupport JavaDoc() {
195                             public boolean supportsCustomEditor() {
196                                 return true;
197                             }
198                             public java.awt.Component JavaDoc getCustomEditor() {
199                                 return getCustomizer();
200                             }
201                             
202                         };
203                     }
204                     return super.getPropertyEditor();
205                 }
206             };
207             
208             props.put(new SchemaModelFlushWrapper(getReference().get(),textProp));
209             
210             props.remove("structure");
211             
212         } catch (NoSuchMethodException JavaDoc nsme) {
213             assert false:"properties must be defined";
214         }
215         return sheet;
216     }
217     
218     @Override JavaDoc
219     public Action JavaDoc getPreferredAction() {
220         ReadOnlyCookie roc = (ReadOnlyCookie) getContext().getLookup().lookup(
221                 ReadOnlyCookie.class);
222         if (roc == null || !roc.isReadOnly()) {
223             return SystemAction.get(CustomizeAction.class);
224         }
225         return super.getPreferredAction();
226     }
227     
228     
229     protected CustomizerProvider getCustomizerProvider() {
230         return new CustomizerProvider() {
231             public Customizer getCustomizer() {
232                 return new DocumentationCustomizer(getReference());
233             }
234         };
235     }
236     
237     public boolean hasCustomizer() {
238         return isEditable();
239     }
240     
241     public static class LanguageEditor extends StringEditor
242             implements ExPropertyEditor {
243         /**
244          * Creates a new instance of LanguageEditor
245          */

246         public LanguageEditor() {
247         }
248         
249         private static String JavaDoc[] languages = {"en","en-US","en-GB"};
250         public String JavaDoc[] getTags() {
251             return languages;
252         }
253         
254         public boolean isPaintable() {
255             return false;
256         }
257         /**
258          *
259          * implement ExPropertyEditor
260          *
261          */

262         public void attachEnv(PropertyEnv env ) {
263             FeatureDescriptor JavaDoc desc = env.getFeatureDescriptor();
264             // make this an editable combo tagged editor
265
desc.setValue("canEditAsText", Boolean.TRUE); // NOI18N
266
}
267     }
268 }
269
Popular Tags