1 19 20 package org.netbeans.modules.editor.options; 21 22 import java.io.IOException ; 23 import java.io.OutputStream ; 24 import java.util.HashMap ; 25 import java.util.Map ; 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 ; 35 import org.xml.sax.SAXException ; 36 import org.w3c.dom.Document ; 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 ; 41 import java.util.Iterator ; 42 import org.openide.modules.ModuleInfo; 43 import java.beans.PropertyChangeListener ; 44 import java.beans.PropertyChangeEvent ; 45 import java.beans.PropertyVetoException ; 46 47 52 public abstract class MIMEOptionFile{ 53 54 protected BaseOptions base; 55 protected MIMEProcessor processor; 56 protected Document dom; 57 protected Map properties; 58 private boolean loaded = false; 59 private boolean wasSaved = false; 60 private ModuleInfo editorMI; 61 62 64 private final FileChangeListener fileListener = new FileChangeAdapter() { 65 public void fileChanged(FileEvent fe){ 66 reloadSettings(); 67 } 68 }; 69 70 71 private final PropertyChangeListener moduleListener = new PropertyChangeListener () { 72 public void propertyChange(PropertyChangeEvent 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 83 private final ErrorCatcher ERROR_CATCHER = new ErrorCatcher(); 84 85 86 public MIMEOptionFile(BaseOptions base, Object proc) { 87 this.base = base; 88 processor = (MIMEProcessor)proc; 89 try { 90 properties = new HashMap (); 91 dom = processor.getXMLDataObject().getDocument(); 92 processor.getXMLDataObject().getPrimaryFile().addFileChangeListener(fileListener); 93 } catch (org.xml.sax.SAXException e) { 94 e.printStackTrace(); 95 } catch (IOException 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 Collection modules = result.allInstances(); 107 ModuleInfo curInfo; 108 for (Iterator iter = modules.iterator(); iter.hasNext(); ) { 109 curInfo = (ModuleInfo)iter.next(); 110 if ("org.netbeans.modules.editor".equals(curInfo.getCodeNameBase())) { curInfo.addPropertyChangeListener(moduleListener); 112 editorMI = curInfo; 113 break; 114 } 115 } 116 } 117 118 119 private void editorUninstalled(){ 120 processor.getXMLDataObject().getPrimaryFile().removeFileChangeListener(fileListener); 122 try{ 123 processor.getXMLDataObject().setValid(false); 124 }catch(PropertyVetoException pve){ 125 org.openide.ErrorManager.getDefault().notify(org.openide.ErrorManager.INFORMATIONAL, pve); 126 } 127 editorMI.removePropertyChangeListener(moduleListener); 129 } 130 131 133 public boolean isLoaded(){ 134 return loaded; 135 } 136 137 140 protected void setLoaded(boolean load){ 141 loaded = load; 142 } 143 144 145 public void reloadSettings(){ 146 synchronized (Settings.class) { 147 if (wasSaved){ 148 wasSaved = false; 150 return; 151 } 152 153 Document oldDoc = dom; 154 setLoaded(false); 155 try { 156 String loc = processor.getXMLDataObject().getPrimaryFile().getURL().toExternalForm(); 157 dom = XMLUtil.parse(new InputSource (loc), true, false, ERROR_CATCHER, org.openide.xml.EntityCatalog.getDefault()); 159 loadSettings(); 160 }catch(SAXException saxe){ 161 dom = oldDoc; 162 saxe.printStackTrace(); 163 }catch(IOException ioe){ 164 dom = oldDoc; 165 ioe.printStackTrace(); 166 } 167 } 168 } 169 170 protected void saveSettings(Document doc){ 171 synchronized (Settings.class) { 172 try{ 173 FileLock lock = processor.getXMLDataObject().getPrimaryFile().lock(); 174 try{ 175 OutputStream os = processor.getXMLDataObject().getPrimaryFile().getOutputStream(lock); 176 try { 177 wasSaved = true; 178 XMLUtil.write(doc, os, "UTF-8"); os.flush(); 180 } catch (Exception e){ 181 wasSaved = false; 182 e.printStackTrace(); 183 } finally { 184 os.close(); 185 } 186 }catch (IOException ioe){ 187 ioe.printStackTrace(); 188 }finally{ 189 lock.releaseLock(); 190 } 191 }catch (IOException ioexc){ 192 ioexc.printStackTrace(); 193 } 194 } 195 } 196 197 199 protected abstract void loadSettings(boolean propagate); 200 201 202 protected void loadSettings(){ 203 loadSettings(true); 204 } 205 206 207 protected abstract void updateSettings(Map map); 208 209 211 public Map getAllProperties(){ 212 if (!isLoaded()) loadSettings(false); 213 return properties; 214 } 215 216 217 218 class ErrorCatcher implements org.xml.sax.ErrorHandler { 219 private void message(String level, org.xml.sax.SAXParseException e) { 220 System.err.println("Error level:"+level); System.err.println("Line number:"+e.getLineNumber()); System.err.println("Column number:"+e.getColumnNumber()); System.err.println("Public ID:"+e.getPublicId()); System.err.println("System ID:"+e.getSystemId()); System.err.println("Error message:"+e.getMessage()); } 227 228 public void error(org.xml.sax.SAXParseException e) { 229 message("ERROR",e); } 231 232 public void warning(org.xml.sax.SAXParseException e) { 233 message("WARNING",e); } 235 236 public void fatalError(org.xml.sax.SAXParseException e) { 237 message("FATAL",e); } 239 } 241 } 242 | Popular Tags |