KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > tasklist > suggestions > settings > ManagerSettings


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.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 JavaDoc;
28 import org.xml.sax.Attributes JavaDoc;
29 import org.xml.sax.SAXException JavaDoc;
30 import org.xml.sax.InputSource JavaDoc;
31 import org.xml.sax.XMLReader JavaDoc;
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 JavaDoc;
37 import java.util.HashSet JavaDoc;
38 import java.util.Iterator JavaDoc;
39 import java.beans.IntrospectionException JavaDoc;
40
41 /**
42  * User's settings. Properties persistence depenends on bean info
43  * and its scope on mf-layer attrributes.
44  *
45  * @author Petr Kuzel
46  */

47 public final class ManagerSettings implements Node.Handle {
48
49     private static final long serialVersionUID = 1;
50
51     public static final String JavaDoc AFTER_OPEN_SCAN_DELAY = "showScanDelay";
52     public static final String JavaDoc AFTER_EDIT_SCAN_DELAY = "editScanDelay";
53
54     // for external providers
55
public static final String JavaDoc AFTER_SAVE_SCAN_DELAY = "saveScanDelay";
56
57
58
59     /** Delay to wait after a file has been shown before we rescan */
60     private int showScanDelay = DEFAULT_SHOW_SCAN_DELAY;
61
62     /** Delay to wait after a file has been edited before we rescan */
63     private int editScanDelay = DEFAULT_EDIT_SCAN_DELAY;
64
65     /** Delay to wait after a file has been saved before we rescan */
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         // note the diference in lifecycle, this singleton may
81
// become dead and get reloaded again
82
ManagerSettings cfg = (ManagerSettings) Lookup.getDefault().lookup(ManagerSettings.class);
83         assert cfg != null : "#45809 default lookup has failed to locate ...suggestions.settings.ManagerSettings!"; // NOI18N
84
return cfg;
85     }
86
87     public static ManagerSettings layerEntryPoint() {
88         return new ManagerSettings();
89     }
90
91     public void store() {
92         writeTypeRegistry();
93     }
94
95     /** Getter for property showScanDelay.
96      * @return Value of property showScanDelay.
97      *
98      */

99     public int getShowScanDelay() {
100         return showScanDelay;
101     }
102
103     /** Setter for property showScanDelay.
104      * @param showScanDelay New value of property showScanDelay.
105      *
106      */

107     public void setShowScanDelay(int showScanDelay) {
108         if (showScanDelay < 0) {
109             showScanDelay = 500;
110         }
111         this.showScanDelay = showScanDelay;
112     }
113
114     /** Getter for property editScanDelay.
115      * @return Value of property editScanDelay.
116      *
117      */

118     public int getEditScanDelay() {
119         return editScanDelay;
120     }
121
122     /** Setter for property editScanDelay.
123      * @param editScanDelay New value of property editScanDelay.
124      *
125      */

126     public void setEditScanDelay(int editScanDelay) {
127         if (editScanDelay < 0) {
128             editScanDelay = 1000;
129         }
130         this.editScanDelay = editScanDelay;
131     }
132
133     /** Getter for property saveScanDelay.
134      * @return Value of property saveScanDelay.
135      *
136      */

137     public int getSaveScanDelay() {
138         return saveScanDelay;
139     }
140
141     /** Setter for property saveScanDelay.
142      * @param saveScanDelay New value of property saveScanDelay.
143      *
144      */

145     public void setSaveScanDelay(int saveScanDelay) {
146         if (saveScanDelay < 0) {
147             saveScanDelay = 500;
148         }
149         this.saveScanDelay = saveScanDelay;
150     }
151
152     /** Getter for property scanOnShow.
153      * @return Value of property scanOnShow.
154      *
155      */

156     public boolean isScanOnShow() {
157         return getShowScanDelay() != 0;
158     }
159
160     /** Getter for property scanOnEdit.
161      * @return Value of property scanOnEdit.
162      *
163      */

164     public boolean isScanOnEdit() {
165         return getEditScanDelay() != 0;
166     }
167
168
169     /** Getter for property scanOnSave.
170      * @return Value of property scanOnSave.
171      *
172      */

173     public boolean isScanOnSave() {
174         return getSaveScanDelay() != 0;
175     }
176
177     private File getRegistryFile(boolean create) {
178         String JavaDoc loc = System.getProperty("netbeans.user") + // NOI18N
179
File.separatorChar + "system" + File.separatorChar + "TaskList" + //NOI18N
180
File.separatorChar + "suggestiontype-registry.xml"; // NOI18N
181
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 JavaDoc {
192         private boolean parsingDisabled = false;
193         private boolean parsingNoConfirm = false;
194         private boolean parsingExpanded = false;
195         private Set JavaDoc disabled = null;
196         private Set JavaDoc noconfirm = null;
197         private Set JavaDoc 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 JavaDoc getDisabled() {
212             return disabled;
213         }
214
215         public Set JavaDoc getNoConfirm() {
216             return noconfirm;
217         }
218
219         public Set JavaDoc getExpanded() {
220             return expanded;
221         }
222
223         public void startDocument() {
224         }
225
226         public void endDocument() {
227         }
228
229         public void startElement(String JavaDoc uri, String JavaDoc localName,
230                                  String JavaDoc name, Attributes JavaDoc attrs)
231             throws SAXException JavaDoc {
232             if (name.equals("type")) { // NOI18N
233
if (parsingDisabled) {
234                     String JavaDoc type = attrs.getValue("id"); // NOI18N
235
if (disabled == null) {
236                         disabled = new HashSet JavaDoc(50);
237                     }
238                     disabled.add(type);
239                 } else if (parsingNoConfirm) {
240                     String JavaDoc id = attrs.getValue("id"); // NOI18N
241
if (noconfirm == null) {
242                         noconfirm = new HashSet JavaDoc(50);
243                     }
244                     SuggestionType type = SuggestionTypes.getDefault().getType(id);
245                     noconfirm.add(type);
246                 } else if (parsingExpanded) {
247                     String JavaDoc id = attrs.getValue("id"); // NOI18N
248
if (expanded == null) {
249                         expanded = new HashSet JavaDoc(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); // NOI18N
255
}
256             } else if (name.equals("disabled")) { // NOI18N
257
parsingDisabled = true;
258             } else if (name.equals("noconfirm")) { // NOI18N
259
parsingNoConfirm = true;
260             } else if (name.equals("expanded")) { // NOI18N
261
parsingExpanded = true;
262             } else if (name.equals("scan-preference")) { // NOI18N
263
String JavaDoc event = attrs.getValue("event"); // NOI18N
264
String JavaDoc enabled = attrs.getValue("enabled"); // NOI18N
265
String JavaDoc delay = attrs.getValue("delay"); // NOI18N
266
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); // NOI18N
271
int interval = -1;
272                 try {
273                     interval = Integer.parseInt(delay);
274                 } catch (NumberFormatException JavaDoc e) {
275                 }
276                 if ("show".equals(event)) { // NOI18N
277
scanOnShow = on;
278                     showScanDelay = interval;
279                 } else if ("save".equals(event)) { // NOI18N
280
scanOnSave = on;
281                     saveScanDelay = interval;
282                 } else if ("edit".equals(event)) { // NOI18N
283
scanOnEdit = on;
284                     editScanDelay = interval;
285                 }
286             }
287         }
288
289         public void endElement(String JavaDoc uri, String JavaDoc localName, String JavaDoc name) throws SAXException JavaDoc {
290             if (name.equals("disabled")) { // NOI18N
291
parsingDisabled = false;
292             } else if (name.equals("noconfirm")) { // NOI18N
293
parsingNoConfirm = false;
294             } else if (name.equals("expanded")) { // NOI18N
295
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         /** No validation - don't read the DTD. Assume importers won't
323             require external entities. */

324         public InputSource JavaDoc resolveEntity(String JavaDoc pubid, String JavaDoc sysid) {
325             return new InputSource JavaDoc(new ByteArrayInputStream(new byte[0]));
326         }
327     }
328
329     /** Have we read the type registry yet? */
330     private boolean registryRead = false;
331
332     /** Read in the SuggestionType registry preferences.
333      * @return True iff the registry was completely initialized without error
334      */

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 JavaDoc fileReader = new BufferedReader(new FileReader(file));
344                 try {
345                     XMLReader JavaDoc 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 JavaDoc(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 JavaDoc e) {
360                     ErrorManager.getDefault().notify(
361                                                ErrorManager.INFORMATIONAL, e);
362                 }
363                 fileReader.close();
364             } catch (Exception JavaDoc e) {
365                 ErrorManager.getDefault().notify(
366                                                ErrorManager.INFORMATIONAL, e);
367             }
368         }
369         return false;
370     }
371
372     public synchronized boolean isEnabled(String JavaDoc id) {
373         if (disabled == null) {
374             readTypeRegistry();
375             if (disabled == null) {
376                 disabled = new HashSet JavaDoc(40);
377             }
378         }
379         return !disabled.contains(id);
380     }
381
382     /** Map containing names of Suggestion Types that have been disabled
383      * by the user. */

384     private Set JavaDoc disabled = null;
385
386     /** Write out the SuggestionType registry preferences.
387      * @param view The current view that we're focused on (used to
388      * persist type expansion state)
389      * @return True iff the registry was completely written out without error
390      */

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"); // NOI18N
396
writer.write("<!DOCTYPE suggestionregistry PUBLIC '-//NetBeans//DTD suggestion registry 1.0//EN' 'http://www.netbeans.org/dtds/suggestion-registry-1_0.dtd'>\n"); // NOI18N
397
writer.write("<typeregistry>\n"); // NOI18N
398
Iterator JavaDoc it;
399             if (disabled != null) {
400                 it = disabled.iterator();
401                 if (it.hasNext()) {
402                     writer.write(" <disabled>\n"); // NOI18N
403
while (it.hasNext()) {
404                         String JavaDoc typeName = (String JavaDoc)it.next();
405                         writer.write(" <type id=\""); // NOI18N
406
writer.write(typeName);
407                         writer.write("\"/>\n"); // NOI18N
408
}
409                     writer.write(" </disabled>\n"); // NOI18N
410
}
411             }
412
413             if (noconfirm != null) {
414                 it = noconfirm.iterator();
415                 if (it.hasNext()) {
416                     writer.write(" <noconfirm>\n"); // NOI18N
417
while (it.hasNext()) {
418                         SuggestionType type = (SuggestionType)it.next();
419                         writer.write(" <type id=\""); // NOI18N
420
writer.write(type.getName());
421                         writer.write("\"/>\n"); // NOI18N
422
}
423                     writer.write(" </noconfirm>\n"); // NOI18N
424
}
425             }
426
427             // Write node-expansion settings
428
if (expandedTypes != null) {
429                 it = expandedTypes.iterator();
430                 if (it.hasNext()) {
431                     writer.write(" <expanded>\n"); // NOI18N
432
while (it.hasNext()) {
433                         SuggestionType type = (SuggestionType)it.next();
434                         writer.write(" <type id=\""); // NOI18N
435
writer.write(type.getName());
436                         writer.write("\"/>\n"); // NOI18N
437
}
438                     writer.write(" </expanded>\n"); // NOI18N
439
}
440             }
441
442             // Write out the scanning preferences (if different
443
// from the default)
444
if ((isScanOnShow() != DEFAULT_SCAN_ON_SHOW) ||
445                 (showScanDelay != DEFAULT_SHOW_SCAN_DELAY)) {
446                 writer.write(" <scan-preference event=\"show\" enabled=\""); // NOI18N
447
writer.write(isScanOnShow() ? "on" : "off"); // NOI18N
448
writer.write("\" delay=\""); // NOI18N
449
writer.write(Integer.toString(showScanDelay));
450                 writer.write("\"/>\n"); // NOI18N
451
}
452             if ((isScanOnEdit() != DEFAULT_SCAN_ON_EDIT) ||
453                 (editScanDelay != DEFAULT_EDIT_SCAN_DELAY)) {
454                 writer.write(" <scan-preference event=\"edit\" enabled=\""); // NOI18N
455
writer.write(isScanOnEdit() ? "on" : "off"); // NOI18N
456
writer.write("\" delay=\""); // NOI18N
457
writer.write(Integer.toString(editScanDelay));
458                 writer.write("\"/>\n"); // NOI18N
459
}
460             if ((isScanOnSave() != DEFAULT_SCAN_ON_SAVE) ||
461                 (saveScanDelay != DEFAULT_SAVE_SCAN_DELAY)) {
462                 writer.write(" <scan-preference event=\"save\" enabled=\""); // NOI18N
463
writer.write(isScanOnSave() ? "on" : "off"); // NOI18N
464
writer.write("\" delay=\""); // NOI18N
465
writer.write(Integer.toString(saveScanDelay));
466                 writer.write("\"/>\n"); // NOI18N
467
}
468
469             writer.write("</typeregistry>\n"); // NOI18N
470
writer.close();
471             return true;
472         } catch (Exception JavaDoc e) {
473             ErrorManager.getDefault().notify(
474                                            ErrorManager.INFORMATIONAL, e);
475         }
476         return false;
477     }
478
479     public synchronized void setEnabled(String JavaDoc id, boolean enabled) {
480         if (disabled == null) {
481             disabled = new HashSet JavaDoc(40);
482         }
483
484         if (enabled) {
485             disabled.remove(id);
486             // Have EditTypes... gui now : setConfirm(type, true, false);
487
} 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 JavaDoc(40);
497             }
498         }
499         return !noconfirm.contains(type);
500     }
501
502
503     /** Map containing names of Suggestion Types that the user wants to
504      * fix without a confirmation dialog */

505     private Set JavaDoc noconfirm = null;
506
507     public synchronized void setConfirm(SuggestionType type, boolean confirm) {
508         if (noconfirm == null) {
509             noconfirm = new HashSet JavaDoc(40);
510         }
511
512         if (confirm) {
513             noconfirm.remove(type);
514         } else {
515             noconfirm.add(type);
516         }
517     }
518
519     /** List of SuggestionTypes that should be expanded */
520     private Set JavaDoc expandedTypes = null;
521
522     public boolean isExpandedType(SuggestionType type) {
523         readTypeRegistry();
524         if (expandedTypes == null) {
525             // Special case: default parse errors to expanded
526
return (type.getName() == "nb-java-errors"); // NOI18N
527
}
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 JavaDoc(2*SuggestionTypes.getDefault().getCount());
536             // Ensure that we default to showing java compilation errors
537
// expanded
538
SuggestionType jc =
539                 SuggestionTypes.getDefault().getType("nb-java-errors"); // NOI18N
540
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     // XXX Node.Handle gets special support from InstanceDataObject
552
// it takes effect once customized using tools: options
553
public Node getNode() throws IOException {
554         try {
555             Node node = new BeanNode(getDefault());
556             return node;
557         } catch (IntrospectionException JavaDoc e) {
558             IOException io = new IOException();
559             io.initCause(e);
560             throw io;
561         }
562     }
563
564 }
565
Popular Tags