KickJava   Java API By Example, From Geeks To Geeks.

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


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.io.OutputStream JavaDoc;
24 import java.util.HashMap JavaDoc;
25 import java.util.Map JavaDoc;
26 import org.netbeans.editor.Settings;
27
28 import org.openide.filesystems.FileChangeAdapter;
29 import org.openide.filesystems.FileChangeListener;
30 import org.openide.filesystems.FileEvent;
31 import org.openide.filesystems.FileLock;
32 import org.openide.loaders.XMLDataObject;
33 import org.openide.xml.XMLUtil;
34 import org.xml.sax.InputSource JavaDoc;
35 import org.xml.sax.SAXException JavaDoc;
36 import org.w3c.dom.Document JavaDoc;
37 import org.openide.util.Lookup;
38 import org.openide.util.Lookup.Template;
39 import org.openide.util.Lookup.Result;
40 import java.util.Collection JavaDoc;
41 import java.util.Iterator JavaDoc;
42 import org.openide.modules.ModuleInfo;
43 import java.beans.PropertyChangeListener JavaDoc;
44 import java.beans.PropertyChangeEvent JavaDoc;
45 import java.beans.PropertyVetoException JavaDoc;
46
47 /** MIME Option XML file.
48  *
49  * @author Martin Roskanin
50  * @since 08/2001
51  */

52 public abstract class MIMEOptionFile{
53     
54     protected BaseOptions base;
55     protected MIMEProcessor processor;
56     protected Document JavaDoc dom;
57     protected Map JavaDoc properties;
58     private boolean loaded = false;
59     private boolean wasSaved = false;
60     private ModuleInfo editorMI;
61     
62     /** File change listener.
63      * If file was externally modified, we have to reload settings.*/

64     private final FileChangeListener fileListener = new FileChangeAdapter() {
65         public void fileChanged(FileEvent fe){
66             reloadSettings();
67         }
68     };
69
70     /** Editor module uninstallation listener */
71     private final PropertyChangeListener JavaDoc moduleListener = new PropertyChangeListener JavaDoc() {
72         public void propertyChange(PropertyChangeEvent JavaDoc evt){
73             if (!ModuleInfo.PROP_ENABLED.endsWith(evt.getPropertyName())) return;
74             if (!(evt.getSource() instanceof ModuleInfo)) return;
75             
76             if (((ModuleInfo)evt.getSource() != null) && (!((ModuleInfo)evt.getSource()).isEnabled())){
77                 editorUninstalled();
78             }
79         }
80     };
81     
82     /* Singleton of error catcher */
83     private final ErrorCatcher ERROR_CATCHER = new ErrorCatcher();
84     
85     /** Creates new MIMEOptionFile */
86     public MIMEOptionFile(BaseOptions base, Object JavaDoc proc) {
87         this.base = base;
88         processor = (MIMEProcessor)proc;
89         try {
90             properties = new HashMap JavaDoc();
91             dom = processor.getXMLDataObject().getDocument();
92             processor.getXMLDataObject().getPrimaryFile().addFileChangeListener(fileListener);
93         } catch (org.xml.sax.SAXException JavaDoc e) {
94             e.printStackTrace();
95         } catch (IOException JavaDoc ioe){
96             ioe.printStackTrace();
97         }
98         addEditorModuleListener();
99     }
100
101
102     private void addEditorModuleListener(){
103         Lookup.Template templ = new Lookup.Template(ModuleInfo.class);
104         Lookup.Result result = Lookup.getDefault().lookup(templ);
105         // get all modules
106
Collection JavaDoc modules = result.allInstances();
107         ModuleInfo curInfo;
108         for (Iterator JavaDoc iter = modules.iterator(); iter.hasNext(); ) {
109             curInfo = (ModuleInfo)iter.next();
110             if ("org.netbeans.modules.editor".equals(curInfo.getCodeNameBase())) { //NOI18N
111
curInfo.addPropertyChangeListener(moduleListener);
112                 editorMI = curInfo;
113                 break;
114             }
115         }
116     }
117
118     /** Editor is uninstalled. We have to detach processor from XMLDO. */
119     private void editorUninstalled(){
120         // remove file change listener from xml file object, that listened to external modification
121
processor.getXMLDataObject().getPrimaryFile().removeFileChangeListener(fileListener);
122         try{
123             processor.getXMLDataObject().setValid(false);
124         }catch(PropertyVetoException JavaDoc pve){
125             org.openide.ErrorManager.getDefault().notify(org.openide.ErrorManager.INFORMATIONAL, pve);
126         }
127         // remove editor uninstallation listener
128
editorMI.removePropertyChangeListener(moduleListener);
129     }
130     
131     /** Getter for loaded flag.
132      * @return true if XML file has been already loaded */

133     public boolean isLoaded(){
134         return loaded;
135     }
136     
137     /** Sets loaded flag.
138      * @param load if true file has been already loaded
139      */

140     protected void setLoaded(boolean load){
141         loaded = load;
142     }
143     
144     /** Updates all properties from (external modified) XML file */
145     public void reloadSettings(){
146         synchronized (Settings.class) {
147             if (wasSaved){
148                 // the settings has been saved programatically. We don't need to reload them
149
wasSaved = false;
150                 return;
151             }
152
153             Document JavaDoc oldDoc = dom;
154             setLoaded(false);
155             try {
156                 String JavaDoc loc = processor.getXMLDataObject().getPrimaryFile().getURL().toExternalForm();
157                 // we need to reparse modified xml file, XMLDataObject caches old data ...
158
dom = XMLUtil.parse(new InputSource JavaDoc(loc), true, false, ERROR_CATCHER, org.openide.xml.EntityCatalog.getDefault());
159                 loadSettings();
160             }catch(SAXException JavaDoc saxe){
161                 dom = oldDoc;
162                 saxe.printStackTrace();
163             }catch(IOException JavaDoc ioe){
164                 dom = oldDoc;
165                 ioe.printStackTrace();
166             }
167         }
168     }
169     
170     protected void saveSettings(Document JavaDoc doc){
171         synchronized (Settings.class) {
172             try{
173                 FileLock lock = processor.getXMLDataObject().getPrimaryFile().lock();
174                 try{
175                     OutputStream JavaDoc os = processor.getXMLDataObject().getPrimaryFile().getOutputStream(lock);
176                     try {
177                         wasSaved = true;
178                         XMLUtil.write(doc, os, "UTF-8"); // NOI18N
179
os.flush();
180                     } catch (Exception JavaDoc e){
181                         wasSaved = false;
182                         e.printStackTrace();
183                     } finally {
184                         os.close();
185                     }
186                 }catch (IOException JavaDoc ioe){
187                     ioe.printStackTrace();
188                 }finally{
189                     lock.releaseLock();
190                 }
191             }catch (IOException JavaDoc ioexc){
192                 ioexc.printStackTrace();
193             }
194         }
195     }
196     
197     /** Loads settings stored in XML file
198      * @param propagate if true loaded settings take effect in editor */

199     protected abstract void loadSettings(boolean propagate);
200     
201     /** Loads settings stored in XML file and propagate them */
202     protected void loadSettings(){
203         loadSettings(true);
204     }
205     
206     /** Updates the settings in property map and save them in XML file */
207     protected abstract void updateSettings(Map JavaDoc map);
208     
209     /** Gets all properties stored in user XML setting file
210      * @return Map of properties */

211     public Map JavaDoc getAllProperties(){
212         if (!isLoaded()) loadSettings(false);
213         return properties;
214     }
215     
216     
217     /** Inner class error catcher for handling SAXParseExceptions */
218     class ErrorCatcher implements org.xml.sax.ErrorHandler JavaDoc {
219         private void message(String JavaDoc level, org.xml.sax.SAXParseException JavaDoc e) {
220             System.err.println("Error level:"+level); //NOI18N
221
System.err.println("Line number:"+e.getLineNumber()); //NOI18N
222
System.err.println("Column number:"+e.getColumnNumber()); //NOI18N
223
System.err.println("Public ID:"+e.getPublicId()); //NOI18N
224
System.err.println("System ID:"+e.getSystemId()); //NOI18N
225
System.err.println("Error message:"+e.getMessage()); //NOI18N
226
}
227         
228         public void error(org.xml.sax.SAXParseException JavaDoc e) {
229             message("ERROR",e); //NOI18N
230
}
231         
232         public void warning(org.xml.sax.SAXParseException JavaDoc e) {
233             message("WARNING",e); //NOI18N
234
}
235         
236         public void fatalError(org.xml.sax.SAXParseException JavaDoc e) {
237             message("FATAL",e); //NOI18N
238
}
239     } //end of inner class ErrorCatcher
240

241 }
242
Popular Tags