KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > editor > hints > options > ModelImpl


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 package org.netbeans.modules.editor.hints.options;
20
21 import java.util.HashMap JavaDoc;
22 import java.util.Iterator JavaDoc;
23 import java.util.List JavaDoc;
24 import java.util.Map JavaDoc;
25 import java.util.Map.Entry;
26 import org.netbeans.spi.editor.hints.ProvidersList;
27 import org.netbeans.spi.editor.hints.Severity;
28
29 /**
30  *
31  * @author Jan Lahoda
32  */

33 public class ModelImpl {
34
35     private AdvancedHintsPanel panel;
36
37     private Map JavaDoc<String JavaDoc, Boolean JavaDoc> key2Enabled;
38     private Map JavaDoc<String JavaDoc, Map JavaDoc<String JavaDoc, Severity>> key2Error2Severity;
39
40     private int eagerness;
41
42     /** Creates a new instance of ModelImpl */
43     public ModelImpl(AdvancedHintsPanel panel) {
44     this.panel = panel;
45     key2Enabled = new HashMap JavaDoc<String JavaDoc, Boolean JavaDoc>();
46     key2Error2Severity = new HashMap JavaDoc<String JavaDoc, Map JavaDoc<String JavaDoc, Severity>>();
47         eagerness = (-1);
48     }
49     
50     public synchronized boolean isProviderEnabled(String JavaDoc providerKey) {
51     Boolean JavaDoc cache = (Boolean JavaDoc) key2Enabled.get(providerKey);
52     
53     if (cache == null)
54         return ProvidersList.isProviderEnabled(providerKey);
55     
56     return cache.booleanValue();
57     }
58     
59     public synchronized Severity getErrorSeverity(String JavaDoc providerKey, String JavaDoc errorKey) {
60     Map JavaDoc<String JavaDoc, Severity> error2Severity = key2Error2Severity.get(providerKey);
61     
62     if (error2Severity == null)
63         return ProvidersList.getErrorSeverity(providerKey, errorKey);
64     
65     Severity cache = error2Severity.get(errorKey);
66     
67     if (cache == null)
68         return ProvidersList.getErrorSeverity(providerKey, errorKey);
69         
70     return cache;
71     }
72     
73     public synchronized void setProviderEnabled(String JavaDoc providerKey, boolean enabled) {
74         key2Enabled.put(providerKey, Boolean.valueOf(enabled));
75     
76     panel.firePropertyChange(AdvancedHintsPanel.PROP_CHANGED, null, null);
77     }
78
79     public synchronized void setSeverity(String JavaDoc providerKey, String JavaDoc errorKey, Severity severity) {
80     Map JavaDoc<String JavaDoc, Severity> error2Severity = key2Error2Severity.get(providerKey);
81     
82     if (error2Severity == null) {
83         key2Error2Severity.put(providerKey, error2Severity = new HashMap JavaDoc<String JavaDoc, Severity>());
84     }
85     
86     error2Severity.put(errorKey, severity);
87     
88     panel.firePropertyChange(AdvancedHintsPanel.PROP_CHANGED, null, null);
89     }
90     
91     public synchronized List JavaDoc/*<ProviderDescription>*/ getDescriptions() {
92         return ProvidersListAccessor.INSTANCE.getDescriptions();
93     }
94     
95     public void rollBack() {
96     key2Enabled = new HashMap JavaDoc<String JavaDoc, Boolean JavaDoc>();
97     key2Error2Severity = new HashMap JavaDoc<String JavaDoc, Map JavaDoc<String JavaDoc, Severity>>();
98         eagerness = (-1);
99     }
100     
101     public void commit() {
102     for (Entry<String JavaDoc, Boolean JavaDoc> e : key2Enabled.entrySet()) {
103         String JavaDoc key = e.getKey();
104         Boolean JavaDoc value = e.getValue();
105         
106         ProvidersListAccessor.INSTANCE.setProviderEnabled(key, value.booleanValue());
107     }
108
109     for (Entry<String JavaDoc, Map JavaDoc<String JavaDoc, Severity>> eP : key2Error2Severity.entrySet()) {
110         String JavaDoc key = eP.getKey();
111         Map JavaDoc<String JavaDoc, Severity> error2Severity = eP.getValue();
112         
113         for (Entry<String JavaDoc, Severity> e : error2Severity.entrySet()) {
114         String JavaDoc errorKey = e.getKey();
115         Severity severity = e.getValue();
116         
117         ProvidersListAccessor.INSTANCE.setSeverity(key, errorKey, severity);
118         }
119     }
120     
121         
122     key2Enabled = new HashMap JavaDoc<String JavaDoc, Boolean JavaDoc>();
123     key2Error2Severity = new HashMap JavaDoc<String JavaDoc, Map JavaDoc<String JavaDoc, Severity>>();
124         eagerness = (-1);
125     }
126     
127     public boolean isModified() {
128     return !key2Enabled.isEmpty() || !key2Error2Severity.isEmpty() || (eagerness != (-1));
129     }
130 }
131
Popular Tags