KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > editor > options > MacrosMIMEOptionFile


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
20 package org.netbeans.modules.editor.options;
21
22 import java.io.IOException JavaDoc;
23 import java.util.ArrayList JavaDoc;
24 import java.util.HashMap JavaDoc;
25 import java.util.Iterator JavaDoc;
26 import java.util.Map JavaDoc;
27
28 import org.netbeans.editor.Settings;
29 import org.netbeans.editor.SettingsNames;
30 import org.openide.xml.XMLUtil;
31
32 import org.w3c.dom.Document JavaDoc;
33 import org.w3c.dom.Element JavaDoc;
34 import org.w3c.dom.Node JavaDoc;
35 import org.w3c.dom.NodeList JavaDoc;
36 import org.w3c.dom.Text JavaDoc;
37
38
39 /** MIME Option XML file for Macros settings.
40  * Macros settings are loaded and saved in XML format
41  * according to EditorMacros-1_0.dtd
42  *
43  * @author Martin Roskanin
44  * @since 08/2001
45  */

46 public class MacrosMIMEOptionFile extends MIMEOptionFile{
47     
48     /** Elements */
49     public static final String JavaDoc TAG_ROOT = "macros"; //NOI18N
50
public static final String JavaDoc TAG_MACRO = "macro"; //NOI18N
51

52     /** Attributes */
53     public static final String JavaDoc ATTR_NAME = "name"; //NOI18N
54
public static final String JavaDoc ATTR_REMOVE = "remove"; //NOI18N
55
public static final String JavaDoc ATTR_XML_SPACE = "xml:space"; //NOI18N
56
public static final String JavaDoc VALUE_XML_SPACE = "preserve"; //NOI18N
57

58     /** File name of this MIMEOptionFile */
59     static final String JavaDoc FILENAME = "macros"; //NOI18N
60

61     public MacrosMIMEOptionFile(BaseOptions base, Object JavaDoc proc) {
62         super(base, proc);
63     }
64     
65     /** Loads settings from XML file.
66      * @param propagate if true - propagates the loaded settings to Editor UI */

67     protected void loadSettings(boolean propagate){
68         synchronized (Settings.class) {
69             Document JavaDoc doc = dom;
70             Element JavaDoc rootElement = doc.getDocumentElement();
71
72             if (!TAG_ROOT.equals(rootElement.getTagName())) {
73                 // Wrong root element
74
return;
75             }
76
77             // gets current macro map
78
Map JavaDoc mapa = new HashMap JavaDoc((Map JavaDoc) Settings.getValue(base.getKitClass(), SettingsNames.MACRO_MAP));
79
80             properties.clear();
81
82             NodeList JavaDoc mcr = rootElement.getElementsByTagName(TAG_MACRO);
83             int len = mcr.getLength();
84             for (int i=0;i<len;i++){
85                 org.w3c.dom.Node JavaDoc node = mcr.item(i);
86                 Element JavaDoc FCElement = (Element JavaDoc)node;
87
88                 if (FCElement == null){
89                     continue;
90                 }
91
92                 String JavaDoc key = FCElement.getAttribute(ATTR_NAME);
93                 String JavaDoc delete = FCElement.getAttribute(ATTR_REMOVE);
94                 String JavaDoc action = "";
95                 if (! Boolean.valueOf(delete).booleanValue()){
96                     NodeList JavaDoc textList = FCElement.getChildNodes();
97                     if (textList.getLength() > 0) {
98                         Node JavaDoc subNode = textList.item(0);
99                         if (subNode instanceof Text JavaDoc) {
100                             Text JavaDoc textNode = (Text JavaDoc) subNode;
101                             action = textNode.getData();
102                         }
103                     }
104                 }
105
106                 properties.put(key, action);
107             }
108
109             if (properties.size()>0){
110                 // create updated map
111
mapa.putAll(properties);
112
113                 // remove all deleted values
114
for( Iterator JavaDoc i = properties.keySet().iterator(); i.hasNext(); ) {
115                     String JavaDoc key = (String JavaDoc)i.next();
116                     if(((String JavaDoc)properties.get(key)).length() == 0){
117                         mapa.remove(key);
118                     }
119                 }
120
121                 // setMacroMap without saving to XML
122
if (propagate){
123                     base.setMacroMap(mapa, false);
124                 }
125             }
126             if (propagate) setLoaded(true);
127         }
128     }
129     
130     /** Save settings to XML file
131      * @param changedProp the Map of settings to save */

132     protected void updateSettings(Map JavaDoc changedProp){
133         synchronized (Settings.class) {
134             // put changed properties to local map
135
properties.putAll(changedProp);
136
137             // now we can save local map to XML file
138
Document JavaDoc doc = XMLUtil.createDocument(TAG_ROOT, null, processor.getPublicID(), processor.getSystemID());
139             org.w3c.dom.Element JavaDoc rootElem = doc.getDocumentElement();
140
141             ArrayList JavaDoc removed = new ArrayList JavaDoc();
142
143             Map JavaDoc defaultMacros = base.getDefaultMacrosMap();
144             // if default macros don't exist for appropriate kit, set them empty
145
if (defaultMacros == null) defaultMacros = new HashMap JavaDoc();
146
147
148             // save XML
149
for( Iterator JavaDoc i = properties.keySet().iterator(); i.hasNext(); ) {
150                 String JavaDoc key = (String JavaDoc)i.next();
151                 if (properties.get(key) instanceof String JavaDoc){
152
153                     String JavaDoc action = (String JavaDoc) properties.get(key);
154                     if (action.length()==0){
155                         // null value => DETETE: if property is in default set, mark it as deleted else delete it completely
156
if (!defaultMacros.containsKey(key)) {
157                             removed.add(key);
158                             continue;
159                         }
160                     } else{
161                         // if key and value is already in settings default, no need to store
162
// this in diff XML file
163
if (defaultMacros.containsKey(key)){
164                             String JavaDoc defValue = (String JavaDoc) defaultMacros.get(key);
165                             if (defValue.equals(action)){
166                                 removed.add(key);
167                                 continue;
168                             }
169                         }
170                     }
171
172                     org.w3c.dom.Element JavaDoc macroElem = doc.createElement(TAG_MACRO);
173                     macroElem.setAttribute(ATTR_NAME, key);
174                     if (action.length()==0){
175                         macroElem.setAttribute(ATTR_REMOVE, Boolean.TRUE.toString());
176                     }else{
177                         macroElem.setAttribute(ATTR_XML_SPACE, VALUE_XML_SPACE);
178                         macroElem.appendChild(doc.createTextNode(action));
179                     }
180                     rootElem.appendChild(macroElem);
181                 }
182             }
183
184             for (int i=0; i<removed.size(); i++){
185                 properties.remove(removed.get(i));
186             }
187
188             doc.getDocumentElement().normalize();
189
190             saveSettings(doc);
191         }
192     }
193     
194 }
195
Popular Tags