KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > xml > text > syntax > XMLSettingsInitializer


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 package org.netbeans.modules.xml.text.syntax;
20
21 import java.awt.Color JavaDoc;
22 import java.util.*;
23
24 import org.netbeans.editor.Settings;
25 import org.netbeans.editor.SettingsUtil;
26 import org.netbeans.editor.SettingsNames;
27 import org.netbeans.editor.Coloring;
28 import org.netbeans.editor.BaseKit;
29 import org.netbeans.editor.Acceptor;
30 import org.netbeans.editor.AcceptorFactory;
31 import org.netbeans.editor.TokenCategory;
32 import org.netbeans.editor.TokenContext;
33 import org.netbeans.editor.TokenContextPath;
34 import org.netbeans.modules.xml.text.api.XMLDefaultTokenContext;
35
36 /**
37  * Editor settings defaults.
38  * It shoudl be replaced by layer based "Defaults" to simplify
39  * {@link TextEditorModuleInstall}.
40  */

41 public class XMLSettingsInitializer extends Settings.AbstractInitializer {
42
43     /** Name assigned to initializer */
44     public static final String JavaDoc NAME = "xml-settings-initializer"; // NOI18N
45

46     public XMLSettingsInitializer() {
47         super(NAME);
48     }
49
50     public void updateSettingsMap (Class JavaDoc kitClass, Map settingsMap) {
51         // editor breaks the contact, handle it somehow
52
if (kitClass == null) return;
53
54         if (kitClass == BaseKit.class) {
55
56             new XMLTokenColoringInitializer().updateSettingsMap(kitClass, settingsMap);
57
58             new DTDTokenColoringInitializer().updateSettingsMap(kitClass, settingsMap);
59         }
60
61
62
63         /** Add editor actions to DTD Kit. */
64         if (kitClass == DTDKit.class) {
65
66             // layer based default does not work!
67
settingsMap.put (SettingsNames.ABBREV_MAP, getDTDAbbrevMap());
68
69             SettingsUtil.updateListSetting (settingsMap, SettingsNames.TOKEN_CONTEXT_LIST,
70                     new TokenContext[] { DTDTokenContext.context }
71             );
72
73         }
74
75
76         /** Add editor actions to XML Kit. */
77         if (kitClass == XMLKit.class) {
78
79             settingsMap.put(SettingsNames.CODE_FOLDING_ENABLE, Boolean.TRUE);
80             
81             // layer based default does not work!
82
settingsMap.put (SettingsNames.ABBREV_MAP, getXMLAbbrevMap());
83
84             SettingsUtil.updateListSetting (settingsMap, SettingsNames.TOKEN_CONTEXT_LIST,
85                     new TokenContext[] { XMLDefaultTokenContext.context }
86             );
87             
88             settingsMap.put(SettingsNames.IDENTIFIER_ACCEPTOR, getXMLIdentifierAcceptor());
89         }
90
91
92         /* Allow '?' and '!' in abbrevirations. */
93         if (kitClass == XMLKit.class || kitClass == DTDKit.class) {
94             settingsMap.put(SettingsNames.ABBREV_RESET_ACCEPTOR,
95                             new Acceptor() {
96                                 public boolean accept(char ch) {
97                                     return AcceptorFactory.NON_JAVA_IDENTIFIER.accept(ch) && ch != '!' && ch != '?';
98                                 }
99                             }
100                            );
101         }
102
103     }
104     
105     // This must be synchronized with org/netbeans/modules/xml/text/resources/XMLEditor-abbreviations.xml!!!
106
Map getXMLAbbrevMap() {
107         Map xmlAbbrevMap = new TreeMap();
108
109         xmlAbbrevMap.put ("?xm", "<?xml version=\"1.0\" encoding=\"UTF-8\"?>"); // NOI18N
110
xmlAbbrevMap.put ("!do", "<!DOCTYPE "); // NOI18N
111
xmlAbbrevMap.put ("!cd", "<![CDATA[|]]>"); // NOI18N
112
xmlAbbrevMap.put ("!at", "<!ATTLIST |>"); // NOI18N
113
xmlAbbrevMap.put ("!el", "<!ELEMENT |>"); // NOI18N
114
xmlAbbrevMap.put ("!en", "<!ENTITY |>"); // NOI18N
115
xmlAbbrevMap.put ("pu", "PUBLIC \"|\""); // NOI18N
116
xmlAbbrevMap.put ("sy", "SYSTEM \"|\""); // NOI18N
117

118         return xmlAbbrevMap;
119     }
120
121     // This must be synchronized with org/netbeans/modules/xml/text/resources/DTDEditor-abbreviations.xml!!!
122
Map getDTDAbbrevMap() {
123         Map dtdAbbrevMap = new TreeMap();
124
125         dtdAbbrevMap.put ("!at", "<!ATTLIST |>"); // NOI18N
126
dtdAbbrevMap.put ("!el", "<!ELEMENT |>"); // NOI18N
127
dtdAbbrevMap.put ("!en", "<!ENTITY |>"); // NOI18N
128
dtdAbbrevMap.put ("!no", "<!NOTATION |>"); // NOI18N
129
dtdAbbrevMap.put ("cd", "CDATA"); // NOI18N
130
dtdAbbrevMap.put ("em", "EMPTY"); // NOI18N
131
dtdAbbrevMap.put ("en", "ENTITY"); // NOI18N
132
dtdAbbrevMap.put ("ens", "ENTITIES"); // NOI18N
133
dtdAbbrevMap.put ("fi", "#FIXED"); // NOI18N
134
dtdAbbrevMap.put ("im", "#IMPLIED"); // NOI18N
135
dtdAbbrevMap.put ("nm", "NMTOKEN"); // NOI18N
136
dtdAbbrevMap.put ("nms", "NMTOKENS"); // NOI18N
137
dtdAbbrevMap.put ("nn", "NOTATION"); // NOI18N
138
dtdAbbrevMap.put ("pc", "#PCDATA"); // NOI18N
139
dtdAbbrevMap.put ("pu", "PUBLIC \"|\""); // NOI18N
140
dtdAbbrevMap.put ("re", "#REQUIRED"); // NOI18N
141
dtdAbbrevMap.put ("rf", "IDREF"); // NOI18N
142
dtdAbbrevMap.put ("rfs", "IDREFS"); // NOI18N
143
dtdAbbrevMap.put ("sy", "SYSTEM \"|\""); // NOI18N
144

145         return dtdAbbrevMap;
146     }
147     
148     
149     /*
150      * Identifiers accept all NameChar [4].
151      */

152     Acceptor getXMLIdentifierAcceptor() {
153         
154         return new Acceptor() {
155             public boolean accept(char ch) {
156                 switch (ch) {
157                     case ' ': case '\t': case '\n': case '\r': // WS
158
case '>': case '<': case '&': case '\'': case '"': case '/':
159                     case '\\': // markup
160
return false;
161                 }
162
163                 return true;
164             }
165         };
166     }
167
168     
169     /** XML colorings */
170     static class XMLTokenColoringInitializer
171     extends SettingsUtil.TokenColoringInitializer {
172
173         public XMLTokenColoringInitializer() {
174             super(XMLDefaultTokenContext.context);
175         }
176
177         public Object JavaDoc getTokenColoring(TokenContextPath tokenContextPath, TokenCategory tokenIDOrCategory, boolean printingSet) {
178             // see XML_fontsColors.xml for actual values
179
return new Coloring (null, Color.BLACK, null);
180         }
181     }
182
183     /** DTD colorings */
184     static class DTDTokenColoringInitializer
185         extends SettingsUtil.TokenColoringInitializer {
186
187         public DTDTokenColoringInitializer() {
188             super(DTDTokenContext.context);
189         }
190
191         public Object JavaDoc getTokenColoring(TokenContextPath tokenContextPath, TokenCategory tokenIDOrCategory, boolean printingSet) {
192             // see DTD_fontsColors.xml for actual values
193
return new Coloring (null, Color.BLACK, null);
194         }
195     }
196
197 }
198
Popular Tags