KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > tasklist > usertasks > options > 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.tasklist.usertasks.options;
21
22 import java.beans.PropertyChangeListener JavaDoc;
23 import java.beans.PropertyChangeSupport JavaDoc;
24 import java.io.File JavaDoc;
25 import java.util.HashMap JavaDoc;
26 import java.util.Map JavaDoc;
27 import java.util.prefs.Preferences JavaDoc;
28
29 import org.openide.awt.StatusDisplayer;
30 import org.openide.util.MapFormat;
31 import org.openide.util.NbBundle;
32 import org.openide.util.NbPreferences;
33
34 /**
35  * Settings for the user tasks module.
36  *
37  * @author Tor Norbye
38  * @author tl
39  */

40 public class Settings {
41     public static final String JavaDoc PROP_APPEND = "append"; // NOI18N
42
public static final String JavaDoc PROP_FILENAME = "filename"; // NOI18N
43
public static final String JavaDoc PROP_COLLECT_WORK_PERIODS =
44             "collectWorkPeriods"; // NOI18N
45
public static final String JavaDoc PROP_DETECT_INACTIVITY =
46             "detectInactivity"; // NOI18N
47
public static final String JavaDoc PROP_AUTO_SWITCH_TO_COMPUTED =
48             "autoSwitchToComputed"; // NOI18N
49
public static final String JavaDoc PROP_LAST_USED_EXPORT_FOLDER =
50             "lastUsedExportFolder"; // NOI18N
51
public static final String JavaDoc PROP_WORKING_DAY_START =
52             "workingDayStart"; // NOI18N
53
public static final String JavaDoc PROP_WORKING_DAY_END =
54             "workingDayEnd"; // NOI18N
55
public static final String JavaDoc PROP_PAUSE_START = "pauseStart"; // NOI18N
56
public static final String JavaDoc PROP_PAUSE_END = "pauseEnd"; // NOI18N
57

58     /**
59      * Will be fired if the "working day" flag for one of the week days
60      * changes
61      */

62     public static final String JavaDoc PROP_WORKING_DAYS = "workingDays"; // NOI18N
63

64     /** default working days. */
65     private static boolean[] DEF_WORKING_DAYS = {
66         true, true, true, true, true, false, false};
67
68     
69     private static final Settings INSTANCE = new Settings();
70     
71     /** -1 means "not yet computed" */
72     private int minutesPerDay = -1;
73
74     /** -1 means "not yet computed" */
75     private int workingDays = -1;
76     
77     /**
78      * Return the signleton cppSettings
79      */

80     public static Settings getDefault() {
81     return INSTANCE;
82     }
83
84     /**
85      * Returns preferences node.
86      *
87      * @return preferences node
88      */

89     public static Preferences JavaDoc getPreferences() {
90         return NbPreferences.forModule(Settings.class);
91     }
92
93     private PropertyChangeSupport JavaDoc pcs = new PropertyChangeSupport JavaDoc(this);
94     
95     /**
96      * Default constructor.
97      */

98     public Settings() {
99     }
100     
101     /**
102      * Returns the last folder used for export.
103      *
104      * @return folder
105      */

106     public File JavaDoc getLastUsedExportFolder() {
107         String JavaDoc path = getPreferences().get(PROP_LAST_USED_EXPORT_FOLDER,
108                 System.getProperty("user.home"));
109         return new File JavaDoc(path);
110     }
111     
112     /**
113      * Sets the last folder used for export.
114      *
115      * @param f folder
116      */

117     public void setLastUsedExportFolder(File JavaDoc f) {
118         getPreferences().put(PROP_LAST_USED_EXPORT_FOLDER,
119                 f.getAbsolutePath());
120         pcs.firePropertyChange(PROP_LAST_USED_EXPORT_FOLDER,
121                 null, null);
122     }
123
124     /**
125      * Getter for the autoSwitchToComputed property.
126      *
127      * @return true if each time a subtask (b) is added to a task (a)
128      * (a) will automatically compute spent time, progress and effort.
129      */

130     public boolean getAutoSwitchToComputed() {
131         return getPreferences().getBoolean(PROP_AUTO_SWITCH_TO_COMPUTED, false);
132     }
133
134     /**
135      * Sets the autoSwitchToComputed property
136      *
137      * @param b true if each time a subtask (b) is added to a task (a)
138      * (a) will automatically compute spent time, progress and effort.
139      */

140     public void setAutoSwitchToComputed(boolean b) {
141         getPreferences().putBoolean(PROP_AUTO_SWITCH_TO_COMPUTED, b);
142         pcs.firePropertyChange(PROP_AUTO_SWITCH_TO_COMPUTED,
143                 null, null);
144     }
145
146     /**
147      * Returns the collectWorkPeriods property.
148      *
149      * @return true if the work periods should be collected.
150      */

151     public boolean getCollectWorkPeriods() {
152         return getPreferences().getBoolean(PROP_COLLECT_WORK_PERIODS, true);
153     }
154
155     /**
156      * Sets the makeBackups property
157      *
158      * @param b true if the work periods should be collected
159      */

160     public void setCollectWorkPeriods(boolean b) {
161         getPreferences().putBoolean(PROP_COLLECT_WORK_PERIODS, b);
162         pcs.firePropertyChange(PROP_COLLECT_WORK_PERIODS,
163                 null, null);
164     }
165
166     /**
167      * Returns the detectInactivity property.
168      *
169      * @return true if the user inactivity should be detected
170      */

171     public boolean getDetectInactivity() {
172         return getPreferences().getBoolean(PROP_DETECT_INACTIVITY, false);
173     }
174
175     /**
176      * Sets the detectInactivity property
177      *
178      * @param b true if the user inactivity should be detected.
179      */

180     public void setDetectInactivity(boolean b) {
181         getPreferences().putBoolean(PROP_DETECT_INACTIVITY, b);
182         pcs.firePropertyChange(PROP_DETECT_INACTIVITY,
183                 null, null);
184     }
185
186     /**
187      * @return true iff the user wants to append items to the
188      * tasklist instead of prepending.
189      */

190     public boolean getAppend() {
191         return getPreferences().getBoolean(PROP_APPEND, false);
192     }
193
194     /**
195      * Indicate if the user wants to append items to the
196      * tasklist instead of prepending.
197      *
198      * @param append True iff you want to append instead of prepend
199      */

200     public void setAppend(boolean append) {
201         getPreferences().putBoolean(PROP_APPEND, append);
202         pcs.firePropertyChange(PROP_APPEND,
203                 null, null);
204     }
205
206     /**
207      * Sets the name of the file to read/write the tasklist in
208      */

209     public void setFilename(String JavaDoc fname) {
210         String JavaDoc t = getFilename();
211         if (t.equals(fname))
212             return;
213
214     if (fname.trim().length() == 0) {
215         // Use default
216
fname = NbBundle.getMessage(Settings.class,
217                     "DefaultFilename"); // NOI18N
218
}
219
220         // Check that the file is valid? Should at least make sure
221
// the parent dir exists
222
// Try compiling the regular expression to make sure it's valid
223
File JavaDoc f = new File JavaDoc(expand(fname));
224         File JavaDoc p = f.getParentFile();
225         if (!p.exists()) {
226             // Print message in the message window?
227
StatusDisplayer.getDefault().setStatusText(
228                     NbBundle.getMessage(Settings.class,
229                     "NoFolder", // NOI18N
230
p.getPath()));
231             throw new IllegalArgumentException JavaDoc();
232         }
233         getPreferences().put(PROP_FILENAME, fname);
234         pcs.firePropertyChange(PROP_FILENAME,
235                 null, null);
236     }
237
238     /**
239      * Gets the name of the file to read/write the tasklist in
240      */

241     public String JavaDoc getFilename() {
242         return getPreferences().get(PROP_FILENAME,
243                 NbBundle.getMessage(Settings.class,
244                 "DefaultFilename")); // NOI18N
245
}
246
247     /**
248      * Gets the name of the file to read/write the tasklist in,
249      * expanded such that {userdir} etc. is expanded to the real
250      * filename.
251      *
252      * @return file name of the default user task list
253      */

254     public String JavaDoc getExpandedFilename() {
255         String JavaDoc fname = getFilename();
256         return expand(fname);
257     }
258
259     /**
260      * Hours per day property
261      *
262      * @return value of the property
263      */

264     public int getMinutesPerDay() {
265         if (minutesPerDay < 0) {
266             minutesPerDay = (getPauseStart() - getWorkingDayStart()) +
267                     (getWorkingDayEnd() - getPauseEnd());
268         }
269         return minutesPerDay;
270     }
271     
272     /**
273      * Working days per week
274      *
275      * @return value of the property
276      */

277     public int getDaysPerWeek() {
278         if (workingDays < 0) {
279             boolean[] wd = getWorkingDays();
280             workingDays = 0;
281             for (int i = 0; i < wd.length; i++) {
282                 if (wd[i])
283                     workingDays++;
284             }
285         }
286         return workingDays;
287     }
288     
289     /**
290      * Expand a given filename using our map format
291      */

292     private static String JavaDoc expand(String JavaDoc fname) {
293         Map JavaDoc<String JavaDoc, String JavaDoc> m = new HashMap JavaDoc<String JavaDoc, String JavaDoc>(2);
294         m.put("userdir", System.getProperty("netbeans.user")); // NOI18N
295
m.put("/", File.separator); // NOI18N
296
MapFormat f = new MapFormat(m);
297         String JavaDoc result = f.format(fname);
298         return result;
299     }
300     
301     /**
302      * Adds a property change listener.
303      *
304      * @param l new listener
305      */

306     public void addPropertyChangeListener(PropertyChangeListener JavaDoc l) {
307         pcs.addPropertyChangeListener(l);
308     }
309
310     /**
311      * Returns offset in minutes for the start of working day.
312      *
313      * @return offset in minutes since 00:00
314      */

315     public int getWorkingDayStart() {
316         return getPreferences().getInt("workingDayStart", 8 * 60);
317     }
318
319     /**
320      * Returns offset in minutes for the start of pause.
321      *
322      * @return offset in minutes since 00:00
323      */

324     public int getPauseStart() {
325         return getPreferences().getInt("pauseStart", 12 * 60);
326     }
327
328     /**
329      * Returns offset in minutes for the end of pause.
330      *
331      * @return offset in minutes since 00:00
332      */

333     public int getPauseEnd() {
334         return getPreferences().getInt("pauseEnd", 13 * 60);
335     }
336
337
338     /**
339      * Returns offset in minutes for the end of working day.
340      *
341      * @return offset in minutes since 00:00
342      */

343     public int getWorkingDayEnd() {
344         return getPreferences().getInt("workingDayEnd", 17 * 60);
345     }
346     
347     /**
348      * Returns offset in minutes for the start of working day.
349      *
350      * @param minutes offset in minutes since 00:00
351      */

352     public void setWorkingDayStart(int minutes) {
353         getPreferences().putInt("workingDayStart", minutes);
354         minutesPerDay = -1;
355         pcs.firePropertyChange(PROP_WORKING_DAY_START, null, null);
356     }
357
358     /**
359      * Returns offset in minutes for the start of pause.
360      *
361      * @return offset in minutes since 00:00
362      */

363     public void setPauseStart(int minutes) {
364         getPreferences().putInt("pauseStart", minutes);
365         minutesPerDay = -1;
366         pcs.firePropertyChange(PROP_PAUSE_START, null, null);
367     }
368
369     /**
370      * Returns offset in minutes for the end of pause.
371      *
372      * @return offset in minutes since 00:00
373      */

374     public void setPauseEnd(int minutes) {
375         getPreferences().putInt("pauseEnd", minutes);
376         minutesPerDay = -1;
377         pcs.firePropertyChange(PROP_PAUSE_START, null, null);
378     }
379
380     /**
381      * Returns offset in minutes for the end of working day.
382      *
383      * @return offset in minutes since 00:00
384      */

385     public void setWorkingDayEnd(int minutes) {
386         getPreferences().putInt("workingDayEnd", minutes);
387         minutesPerDay = -1;
388         pcs.firePropertyChange(PROP_WORKING_DAY_START, null, null);
389     }
390     
391     /**
392      * Sets the flag for working days.
393      *
394      * @param index day of the week 0 - monday
395      * @param work true = working day
396      */

397     public void setWorkingDay(int index, boolean work) {
398         getPreferences().putBoolean("workingDay" + index, work);
399         workingDays = -1;
400         pcs.firePropertyChange(PROP_WORKING_DAYS, null, null);
401     }
402     
403     /**
404      * Returns working days.
405      *
406      * @return boolean[7] true = working day. [0] - monday
407      */

408     public boolean[] getWorkingDays() {
409         Preferences JavaDoc p = getPreferences();
410         boolean[] r = new boolean[7];
411         for (int i = 0; i < 7; i++) {
412             r[i] = p.getBoolean("workingDay" + i, DEF_WORKING_DAYS[i]);
413         }
414         return r;
415     }
416 }
417
Popular Tags