1 19 20 package org.netbeans.modules.tasklist.suggestions.settings; 21 22 import org.openide.util.Lookup; 23 import org.openide.ErrorManager; 24 import org.openide.nodes.Node; 25 import org.openide.nodes.BeanNode; 26 import org.openide.xml.XMLUtil; 27 import org.xml.sax.helpers.DefaultHandler ; 28 import org.xml.sax.Attributes ; 29 import org.xml.sax.SAXException ; 30 import org.xml.sax.InputSource ; 31 import org.xml.sax.XMLReader ; 32 import org.netbeans.modules.tasklist.suggestions.SuggestionType; 33 import org.netbeans.modules.tasklist.suggestions.SuggestionTypes; 34 35 import java.io.*; 36 import java.util.Set ; 37 import java.util.HashSet ; 38 import java.util.Iterator ; 39 import java.beans.IntrospectionException ; 40 41 47 public final class ManagerSettings implements Node.Handle { 48 49 private static final long serialVersionUID = 1; 50 51 public static final String AFTER_OPEN_SCAN_DELAY = "showScanDelay"; 52 public static final String AFTER_EDIT_SCAN_DELAY = "editScanDelay"; 53 54 public static final String AFTER_SAVE_SCAN_DELAY = "saveScanDelay"; 56 57 58 59 60 private int showScanDelay = DEFAULT_SHOW_SCAN_DELAY; 61 62 63 private int editScanDelay = DEFAULT_EDIT_SCAN_DELAY; 64 65 66 private int saveScanDelay = DEFAULT_SAVE_SCAN_DELAY; 67 68 private final static int DEFAULT_SHOW_SCAN_DELAY = 500; 69 private final static int DEFAULT_EDIT_SCAN_DELAY = 1000; 70 private final static int DEFAULT_SAVE_SCAN_DELAY = 1000; 71 72 private final static boolean DEFAULT_SCAN_ON_SHOW = true; 73 private final static boolean DEFAULT_SCAN_ON_EDIT = true; 74 private final static boolean DEFAULT_SCAN_ON_SAVE = false; 75 76 private ManagerSettings() { 77 } 78 79 public static ManagerSettings getDefault() { 80 ManagerSettings cfg = (ManagerSettings) Lookup.getDefault().lookup(ManagerSettings.class); 83 assert cfg != null : "#45809 default lookup has failed to locate ...suggestions.settings.ManagerSettings!"; return cfg; 85 } 86 87 public static ManagerSettings layerEntryPoint() { 88 return new ManagerSettings(); 89 } 90 91 public void store() { 92 writeTypeRegistry(); 93 } 94 95 99 public int getShowScanDelay() { 100 return showScanDelay; 101 } 102 103 107 public void setShowScanDelay(int showScanDelay) { 108 if (showScanDelay < 0) { 109 showScanDelay = 500; 110 } 111 this.showScanDelay = showScanDelay; 112 } 113 114 118 public int getEditScanDelay() { 119 return editScanDelay; 120 } 121 122 126 public void setEditScanDelay(int editScanDelay) { 127 if (editScanDelay < 0) { 128 editScanDelay = 1000; 129 } 130 this.editScanDelay = editScanDelay; 131 } 132 133 137 public int getSaveScanDelay() { 138 return saveScanDelay; 139 } 140 141 145 public void setSaveScanDelay(int saveScanDelay) { 146 if (saveScanDelay < 0) { 147 saveScanDelay = 500; 148 } 149 this.saveScanDelay = saveScanDelay; 150 } 151 152 156 public boolean isScanOnShow() { 157 return getShowScanDelay() != 0; 158 } 159 160 164 public boolean isScanOnEdit() { 165 return getEditScanDelay() != 0; 166 } 167 168 169 173 public boolean isScanOnSave() { 174 return getSaveScanDelay() != 0; 175 } 176 177 private File getRegistryFile(boolean create) { 178 String loc = System.getProperty("netbeans.user") + File.separatorChar + "system" + File.separatorChar + "TaskList" + File.separatorChar + "suggestiontype-registry.xml"; File file = new File(loc); 182 if (create) { 183 if (!file.exists()) { 184 File parent = file.getParentFile(); 185 parent.mkdirs(); 186 } 187 } 188 return file; 189 } 190 191 private static class TypeXMLHandler extends DefaultHandler { 192 private boolean parsingDisabled = false; 193 private boolean parsingNoConfirm = false; 194 private boolean parsingExpanded = false; 195 private Set disabled = null; 196 private Set noconfirm = null; 197 private Set expanded = null; 198 199 private int showScanDelay = DEFAULT_SHOW_SCAN_DELAY; 200 private int editScanDelay = DEFAULT_EDIT_SCAN_DELAY; 201 private int saveScanDelay = DEFAULT_SAVE_SCAN_DELAY; 202 203 private boolean scanOnShow = DEFAULT_SCAN_ON_SHOW; 204 private boolean scanOnEdit = DEFAULT_SCAN_ON_EDIT; 205 private boolean scanOnSave = DEFAULT_SCAN_ON_SAVE; 206 207 208 TypeXMLHandler() { 209 } 210 211 public Set getDisabled() { 212 return disabled; 213 } 214 215 public Set getNoConfirm() { 216 return noconfirm; 217 } 218 219 public Set getExpanded() { 220 return expanded; 221 } 222 223 public void startDocument() { 224 } 225 226 public void endDocument() { 227 } 228 229 public void startElement(String uri, String localName, 230 String name, Attributes attrs) 231 throws SAXException { 232 if (name.equals("type")) { if (parsingDisabled) { 234 String type = attrs.getValue("id"); if (disabled == null) { 236 disabled = new HashSet (50); 237 } 238 disabled.add(type); 239 } else if (parsingNoConfirm) { 240 String id = attrs.getValue("id"); if (noconfirm == null) { 242 noconfirm = new HashSet (50); 243 } 244 SuggestionType type = SuggestionTypes.getDefault().getType(id); 245 noconfirm.add(type); 246 } else if (parsingExpanded) { 247 String id = attrs.getValue("id"); if (expanded == null) { 249 expanded = new HashSet (50); 250 } 251 SuggestionType type = SuggestionTypes.getDefault().getType(id); 252 expanded.add(type); 253 } else { 254 ErrorManager.getDefault().log(ErrorManager.WARNING, "SuggestionType Registry Parsing Error: " + name + ", " + attrs); } 256 } else if (name.equals("disabled")) { parsingDisabled = true; 258 } else if (name.equals("noconfirm")) { parsingNoConfirm = true; 260 } else if (name.equals("expanded")) { parsingExpanded = true; 262 } else if (name.equals("scan-preference")) { String event = attrs.getValue("event"); String enabled = attrs.getValue("enabled"); String delay = attrs.getValue("delay"); if ((event == null) || (enabled == null) || (delay == null)) { 267 ErrorManager.getDefault().log(ErrorManager.WARNING, "Got scan-preference event="+event+", enabled="+enabled+", "+delay); 268 return; 269 } 270 boolean on = "on".equals(enabled); int interval = -1; 272 try { 273 interval = Integer.parseInt(delay); 274 } catch (NumberFormatException e) { 275 } 276 if ("show".equals(event)) { scanOnShow = on; 278 showScanDelay = interval; 279 } else if ("save".equals(event)) { scanOnSave = on; 281 saveScanDelay = interval; 282 } else if ("edit".equals(event)) { scanOnEdit = on; 284 editScanDelay = interval; 285 } 286 } 287 } 288 289 public void endElement(String uri, String localName, String name) throws SAXException { 290 if (name.equals("disabled")) { parsingDisabled = false; 292 } else if (name.equals("noconfirm")) { parsingNoConfirm = false; 294 } else if (name.equals("expanded")) { parsingExpanded = false; 296 } 297 298 } 299 300 301 302 public int getShowScanDelay() { 303 return showScanDelay; 304 } 305 public int getEditScanDelay() { 306 return editScanDelay; 307 } 308 public int getSaveScanDelay() { 309 return saveScanDelay; 310 } 311 public boolean isScanOnShow() { 312 return scanOnShow; 313 } 314 public boolean isScanOnEdit() { 315 return scanOnEdit; 316 } 317 public boolean isScanOnSave() { 318 return scanOnSave; 319 } 320 321 322 324 public InputSource resolveEntity(String pubid, String sysid) { 325 return new InputSource (new ByteArrayInputStream(new byte[0])); 326 } 327 } 328 329 330 private boolean registryRead = false; 331 332 335 private boolean readTypeRegistry() { 336 if (registryRead) { 337 return true; 338 } 339 registryRead = true; 340 File file = getRegistryFile(false); 341 if (file.exists()) { 342 try { 343 Reader fileReader = new BufferedReader(new FileReader(file)); 344 try { 345 XMLReader reader = XMLUtil.createXMLReader(false); 346 347 TypeXMLHandler handler = new TypeXMLHandler(); 348 reader.setContentHandler(handler); 349 reader.setErrorHandler(handler); 350 reader.setEntityResolver(handler); 351 reader.parse(new InputSource (fileReader)); 352 disabled = handler.getDisabled(); 353 noconfirm = handler.getNoConfirm(); 354 expandedTypes = handler.getExpanded(); 355 showScanDelay = handler.getShowScanDelay(); 356 editScanDelay = handler.getEditScanDelay(); 357 saveScanDelay = handler.getSaveScanDelay(); 358 return true; 359 } catch (SAXException e) { 360 ErrorManager.getDefault().notify( 361 ErrorManager.INFORMATIONAL, e); 362 } 363 fileReader.close(); 364 } catch (Exception e) { 365 ErrorManager.getDefault().notify( 366 ErrorManager.INFORMATIONAL, e); 367 } 368 } 369 return false; 370 } 371 372 public synchronized boolean isEnabled(String id) { 373 if (disabled == null) { 374 readTypeRegistry(); 375 if (disabled == null) { 376 disabled = new HashSet (40); 377 } 378 } 379 return !disabled.contains(id); 380 } 381 382 384 private Set disabled = null; 385 386 391 boolean writeTypeRegistry() { 392 File file = getRegistryFile(true); 393 try { 394 Writer writer = new BufferedWriter(new FileWriter(file)); 395 writer.write("<?xml version=\"1.0\"?>\n"); writer.write("<!DOCTYPE suggestionregistry PUBLIC '-//NetBeans//DTD suggestion registry 1.0//EN' 'http://www.netbeans.org/dtds/suggestion-registry-1_0.dtd'>\n"); writer.write("<typeregistry>\n"); Iterator it; 399 if (disabled != null) { 400 it = disabled.iterator(); 401 if (it.hasNext()) { 402 writer.write(" <disabled>\n"); while (it.hasNext()) { 404 String typeName = (String )it.next(); 405 writer.write(" <type id=\""); writer.write(typeName); 407 writer.write("\"/>\n"); } 409 writer.write(" </disabled>\n"); } 411 } 412 413 if (noconfirm != null) { 414 it = noconfirm.iterator(); 415 if (it.hasNext()) { 416 writer.write(" <noconfirm>\n"); while (it.hasNext()) { 418 SuggestionType type = (SuggestionType)it.next(); 419 writer.write(" <type id=\""); writer.write(type.getName()); 421 writer.write("\"/>\n"); } 423 writer.write(" </noconfirm>\n"); } 425 } 426 427 if (expandedTypes != null) { 429 it = expandedTypes.iterator(); 430 if (it.hasNext()) { 431 writer.write(" <expanded>\n"); while (it.hasNext()) { 433 SuggestionType type = (SuggestionType)it.next(); 434 writer.write(" <type id=\""); writer.write(type.getName()); 436 writer.write("\"/>\n"); } 438 writer.write(" </expanded>\n"); } 440 } 441 442 if ((isScanOnShow() != DEFAULT_SCAN_ON_SHOW) || 445 (showScanDelay != DEFAULT_SHOW_SCAN_DELAY)) { 446 writer.write(" <scan-preference event=\"show\" enabled=\""); writer.write(isScanOnShow() ? "on" : "off"); writer.write("\" delay=\""); writer.write(Integer.toString(showScanDelay)); 450 writer.write("\"/>\n"); } 452 if ((isScanOnEdit() != DEFAULT_SCAN_ON_EDIT) || 453 (editScanDelay != DEFAULT_EDIT_SCAN_DELAY)) { 454 writer.write(" <scan-preference event=\"edit\" enabled=\""); writer.write(isScanOnEdit() ? "on" : "off"); writer.write("\" delay=\""); writer.write(Integer.toString(editScanDelay)); 458 writer.write("\"/>\n"); } 460 if ((isScanOnSave() != DEFAULT_SCAN_ON_SAVE) || 461 (saveScanDelay != DEFAULT_SAVE_SCAN_DELAY)) { 462 writer.write(" <scan-preference event=\"save\" enabled=\""); writer.write(isScanOnSave() ? "on" : "off"); writer.write("\" delay=\""); writer.write(Integer.toString(saveScanDelay)); 466 writer.write("\"/>\n"); } 468 469 writer.write("</typeregistry>\n"); writer.close(); 471 return true; 472 } catch (Exception e) { 473 ErrorManager.getDefault().notify( 474 ErrorManager.INFORMATIONAL, e); 475 } 476 return false; 477 } 478 479 public synchronized void setEnabled(String id, boolean enabled) { 480 if (disabled == null) { 481 disabled = new HashSet (40); 482 } 483 484 if (enabled) { 485 disabled.remove(id); 486 } else { 488 disabled.add(id); 489 } 490 } 491 492 public synchronized boolean isConfirm(SuggestionType type) { 493 if (noconfirm == null) { 494 readTypeRegistry(); 495 if (noconfirm == null) { 496 noconfirm = new HashSet (40); 497 } 498 } 499 return !noconfirm.contains(type); 500 } 501 502 503 505 private Set noconfirm = null; 506 507 public synchronized void setConfirm(SuggestionType type, boolean confirm) { 508 if (noconfirm == null) { 509 noconfirm = new HashSet (40); 510 } 511 512 if (confirm) { 513 noconfirm.remove(type); 514 } else { 515 noconfirm.add(type); 516 } 517 } 518 519 520 private Set expandedTypes = null; 521 522 public boolean isExpandedType(SuggestionType type) { 523 readTypeRegistry(); 524 if (expandedTypes == null) { 525 return (type.getName() == "nb-java-errors"); } 528 return expandedTypes.contains(type); 529 } 530 531 532 public void setExpandedType(SuggestionType type, boolean expanded) { 533 readTypeRegistry(); 534 if (expandedTypes == null) { 535 expandedTypes = new HashSet (2*SuggestionTypes.getDefault().getCount()); 536 SuggestionType jc = 539 SuggestionTypes.getDefault().getType("nb-java-errors"); if (jc != null) { 541 expandedTypes.add(jc); 542 } 543 } 544 if (expanded) { 545 expandedTypes.add(type); 546 } else { 547 expandedTypes.remove(type); 548 } 549 } 550 551 public Node getNode() throws IOException { 554 try { 555 Node node = new BeanNode(getDefault()); 556 return node; 557 } catch (IntrospectionException e) { 558 IOException io = new IOException(); 559 io.initCause(e); 560 throw io; 561 } 562 } 563 564 } 565 | Popular Tags |