KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > autoupdate > Settings


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.autoupdate;
21
22 import java.beans.PropertyChangeListener JavaDoc;
23 import java.beans.PropertyChangeSupport JavaDoc;
24 import java.beans.PropertyEditorSupport JavaDoc;
25 import java.io.BufferedReader JavaDoc;
26 import java.io.IOException JavaDoc;
27 import java.io.InputStream JavaDoc;
28 import java.io.InputStreamReader JavaDoc;
29 import java.lang.Object JavaDoc;
30 import java.lang.Object JavaDoc;
31 import java.lang.String JavaDoc;
32 import java.util.Date JavaDoc;
33 import java.text.SimpleDateFormat JavaDoc;
34 import java.util.HashMap JavaDoc;
35 import java.util.Iterator JavaDoc;
36 import java.util.Map JavaDoc;
37 import java.util.Random JavaDoc;
38 import java.util.logging.Level JavaDoc;
39 import java.util.logging.Logger JavaDoc;
40 import java.util.prefs.Preferences JavaDoc;
41 import org.openide.filesystems.FileObject;
42 import org.openide.filesystems.Repository;
43
44 import org.openide.util.NbBundle;
45 import org.openide.util.NbPreferences;
46
47 /** Options autoupdate module
48 *
49 * @author Petr Hrebejk
50 */

51 public class Settings //implements ViewerConstants
52
{
53     private static final Settings INSTANCE = new Settings();
54     private static final String JavaDoc PROP_ASK_BEFORE = "askBefore"; // NOI18N
55
private static final String JavaDoc PROP_NEGATIVE_RESULTS = "negativeResults"; // NOI18N
56
private static final String JavaDoc PROP_PERIOD = "period"; // NOI18N
57
private static final String JavaDoc PROP_LAST_CHECK = "lastCheck"; // NOI18N
58
private static final String JavaDoc PROP_IDE_IDENTITY = "ideIdentity"; // NOI18N
59
private static final String JavaDoc PROP_ACCEPTED_NOTIFICATIONS = "acceptedNotifications"; // NOI18N
60

61     public static final int EVERY_STARTUP = 0;
62     public static final int EVERY_DAY = 1;
63     public static final int EVERY_WEEK = 2;
64     public static final int EVERY_2WEEKS = 3;
65     public static final int EVERY_MONTH = 4;
66     public static final int EVERY_NEVER = 5;
67     
68     /** NODE_DEFAULT_ACTION property */
69     public static final String JavaDoc NODE_DEFAULT_ACTION = "nodeDefaultAction"; // NOI18N
70
private static final Random JavaDoc RANDOM = new Random JavaDoc ();
71     private static final Logger JavaDoc err = Logger.getLogger("org.netbeans.modules.autoupdate"); // NOI18N
72
private String JavaDoc tempIdeIdentity = null;
73     private Map JavaDoc notifications;
74     private PropertyChangeSupport JavaDoc ps;
75         
76     private static Preferences JavaDoc getPreferences() {
77         return NbPreferences.forModule(Settings.class);
78     }
79     
80     public static Settings getShared() {
81         if (INSTANCE.ps == null) {
82             INSTANCE.ps = new PropertyChangeSupport JavaDoc(INSTANCE);
83             
84         }
85         return INSTANCE;
86     }
87     
88     public final void addPropertyChangeListener(PropertyChangeListener JavaDoc l) {
89         ps.addPropertyChangeListener(l);
90     }
91
92     public final void removePropertyChangeListener(PropertyChangeListener JavaDoc l) {
93         ps.removePropertyChangeListener(l);
94     }
95     
96
97     /** Getter for period
98     */

99     public int getPeriod() {
100         return getPreferences().getInt(PROP_PERIOD, EVERY_WEEK);
101     }
102
103     /** Setter for period
104     */

105     public void setPeriod( int period ) {
106         getPreferences().putInt(PROP_PERIOD, period);
107     }
108     
109     /** Allows subclasses to override the default autoupdate type.
110     */

111     protected AutoupdateType defaultAutoupdateType() {
112         return AutoupdateType.getDefault ();
113     }
114
115     public void setIdeIdentity (String JavaDoc identity) {
116         getPreferences().put(PROP_IDE_IDENTITY, identity);
117     }
118
119     public String JavaDoc getIdeIdentity () {
120         if (tempIdeIdentity instanceof String JavaDoc) {
121             return tempIdeIdentity;
122         }
123         Object JavaDoc oldIdeIdentity = getPreferences().get(PROP_IDE_IDENTITY,null);
124         String JavaDoc newIdeIdentity = null;
125         if (oldIdeIdentity == null) {
126             newIdeIdentity = modifyIdeIdentityIfNeeded (Integer.toString (generateNewId ()));
127         } else {
128             newIdeIdentity = modifyIdeIdentityIfNeeded ((String JavaDoc) oldIdeIdentity);
129         }
130         tempIdeIdentity = newIdeIdentity;
131         if (! newIdeIdentity.equals (oldIdeIdentity)) {
132             err.log (Level.FINE, "Put new value of PROP_IDE_IDENTITY to " + newIdeIdentity);
133             getPreferences().put(PROP_IDE_IDENTITY, newIdeIdentity);
134         }
135         return tempIdeIdentity;
136     }
137     
138     public void setAcceptedNotifications (Map JavaDoc notifications) {
139         this.notifications = notifications;
140         storeAcceptedNotifications(notifications);
141     }
142
143     public Map JavaDoc getAcceptedNotifications () {
144         if (this.notifications == null) {
145             this.notifications = initAcceptedNotifications();
146         }
147         return this.notifications;
148     }
149     
150     private static Map JavaDoc initAcceptedNotifications() {
151         Map JavaDoc m = new HashMap JavaDoc();
152         String JavaDoc allPairs = getPreferences().get(PROP_ACCEPTED_NOTIFICATIONS, null);
153         if (allPairs != null) {
154             String JavaDoc[] pairs = allPairs.split("|");
155             for (int i = 0; i < pairs.length; i++) {
156                 String JavaDoc p = pairs[i];
157                 String JavaDoc[] elems = p.split(",");
158                 assert elems.length == 2;
159                 m.put(Integer.valueOf(elems[0]), Integer.valueOf(elems[1]));
160             }
161
162         }
163         return m;
164     }
165     
166     private static void storeAcceptedNotifications(Map JavaDoc m) {
167         StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
168         for (Iterator JavaDoc it = m.entrySet().iterator(); it.hasNext();) {
169             Map.Entry JavaDoc e = (Map.Entry JavaDoc)it.next();
170             sb.append(e.getKey()).append(",").append(e.getValue());//NOI18N
171
if (it.hasNext()) {
172                 sb.append("|");//NOI18N
173
}
174         }
175         String JavaDoc toStore = sb.toString();
176         if (toStore.length() > 0) {
177             getPreferences().put(PROP_ACCEPTED_NOTIFICATIONS, toStore);
178         }
179     }
180     
181     
182     /** Getter for askBefore
183     */

184     public boolean isAskBefore () {
185         return getPreferences().getBoolean(PROP_ASK_BEFORE, false);
186     }
187
188     /** Setter for askBefore
189     */

190     public void setAskBefore (boolean askBefore) {
191         getPreferences().putBoolean(PROP_ASK_BEFORE, askBefore);
192     }
193
194     /** Getter for negativeResults
195     */

196     public boolean isNegativeResults () {
197         return getPreferences().getBoolean(PROP_NEGATIVE_RESULTS, false);
198         //return negativeResults;
199
}
200
201     /** Setter for negativeResults
202     */

203     public void setNegativeResults (boolean negativeResults) {
204         getPreferences().putBoolean(PROP_NEGATIVE_RESULTS, negativeResults);
205     }
206
207     /** Getter for last check
208     */

209     public Date JavaDoc getLastCheck() {
210         long t = getPreferences().getLong(PROP_LAST_CHECK, -1);
211         return (t > 0) ? new Date JavaDoc(t) : null;
212
213     }
214
215     /** Setter for last check
216     */

217     public void setLastCheck( Date JavaDoc lastCheck ) {
218         if (lastCheck != null) {
219             getPreferences().putLong(PROP_LAST_CHECK, lastCheck.getTime());
220         } else {
221             getPreferences().remove(PROP_LAST_CHECK);
222         }
223     }
224     
225     public void fireNodeDefaultAction() {
226         ps.firePropertyChange(NODE_DEFAULT_ACTION, null, null);
227     }
228
229     private static String JavaDoc getBundle( String JavaDoc key ) {
230         return NbBundle.getMessage( Settings.class, key );
231     }
232     
233     /** property editor for period property
234     */

235     public static class PeriodPropertyEditor extends PropertyEditorSupport JavaDoc {
236
237         /** Array of tags
238         */

239         private static final String JavaDoc[] tags = {
240             getBundle( "CTL_PeriodEditor_Startup" ),
241             getBundle( "CTL_PeriodEditor_Day" ),
242             getBundle( "CTL_PeriodEditor_Week" ),
243             getBundle( "CTL_PeriodEditor_2Weeks" ),
244             getBundle( "CTL_PeriodEditor_Month" ),
245             getBundle( "CTL_PeriodEditor_Never" ) } ;
246
247         private static final int [] values = {
248             Settings.EVERY_STARTUP,
249             Settings.EVERY_DAY,
250             Settings.EVERY_WEEK,
251             Settings.EVERY_2WEEKS,
252             Settings.EVERY_MONTH,
253             Settings.EVERY_NEVER };
254
255         /** @return names of the supported member Acces types */
256         public String JavaDoc[] getTags() {
257             return tags;
258         }
259
260         /** @return text for the current value */
261         public String JavaDoc getAsText () {
262             long value = ((Integer JavaDoc)getValue()).intValue();
263
264             for (int i = 0; i < values.length ; i++)
265                 if (values[i] == value)
266                     return tags[i];
267
268             return getBundle( "CTL_PeriodEditor_Unsupported" );
269         }
270
271         /** @param text A text for the current value. */
272         public void setAsText (String JavaDoc text) {
273             for (int i = 0; i < tags.length ; i++)
274                 if (tags[i] == text) {
275                     setValue(new Integer JavaDoc(values[i]));
276                     return;
277                 }
278
279             setValue( new Integer JavaDoc(0) );
280         }
281     }
282
283     /** property editor for last check property
284     */

285     public static class LastCheckPropertyEditor extends PropertyEditorSupport JavaDoc {
286
287         private static final SimpleDateFormat JavaDoc sdf = new SimpleDateFormat JavaDoc();
288
289
290         /** @return text for the current value */
291         public String JavaDoc getAsText () {
292             Date JavaDoc dt = (Date JavaDoc)getValue();
293             return (dt == null) ? "" : sdf.format( dt ); // NOI18N
294
}
295
296         /** @param text A text for the current value. */
297         public void setAsText (String JavaDoc text) {
298
299             try {
300                 Date JavaDoc newValue = sdf.parse( text );
301                 setValue( newValue );
302             }
303             catch ( java.text.ParseException JavaDoc e ) {
304                 // leave the old value if the user types some nocense
305
}
306         }
307     }
308
309     // helper methods
310
private static String JavaDoc modifyIdeIdentityIfNeeded (String JavaDoc oldIdeIdentity) {
311         String JavaDoc [] ideIdentityArr = oldIdeIdentity.split ("\\d"); // NOI18N
312
String JavaDoc id = null;
313         String JavaDoc oldPrefix = null;
314         if (ideIdentityArr.length == 0) {
315             id = oldIdeIdentity;
316             oldPrefix = "";
317         } else {
318             assert ideIdentityArr.length == 1 : "ideIdentityArr cannot be greater then 1!";
319             oldPrefix = ideIdentityArr [0];
320             id = oldIdeIdentity.substring (oldPrefix.length ());
321         }
322         err.log (Level.FINER, "Old IDE Identity Prefix: " + oldPrefix); // NOI18N
323
err.log (Level.FINER, "Old IDE Identity ID: " + id); // NOI18N
324
String JavaDoc newPrefix = "";
325         try {
326             FileObject fo = Repository.getDefault().getDefaultFileSystem().findResource("/productid"); // NOI18N
327
if (fo != null) {
328                 InputStream JavaDoc is = fo.getInputStream();
329                 try {
330                     BufferedReader JavaDoc r = new BufferedReader JavaDoc(new InputStreamReader JavaDoc(is));
331                     newPrefix = r.readLine().trim();
332                 } finally {
333                     is.close();
334                 }
335             }
336         } catch (IOException JavaDoc ignore) {
337             // IGNORE
338
}
339         if (!newPrefix.equals (oldPrefix)) {
340             err.log (Level.FINER, "New IDE Identity Prefix: " + newPrefix); // NOI18N
341
} else {
342             err.log (Level.FINER, "No new prefix."); // NOI18N
343
}
344         return newPrefix + id;
345     }
346
347     private static int generateNewId () {
348         int id = RANDOM.nextInt ();
349         if (id < 0) {
350             id = - (id + 1);
351         }
352         return id;
353     }
354
355 }
356
Popular Tags