1 19 20 package org.netbeans.modules.tasklist.usertasks.options; 21 22 import java.beans.PropertyChangeListener ; 23 import java.beans.PropertyChangeSupport ; 24 import java.io.File ; 25 import java.util.HashMap ; 26 import java.util.Map ; 27 import java.util.prefs.Preferences ; 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 40 public class Settings { 41 public static final String PROP_APPEND = "append"; public static final String PROP_FILENAME = "filename"; public static final String PROP_COLLECT_WORK_PERIODS = 44 "collectWorkPeriods"; public static final String PROP_DETECT_INACTIVITY = 46 "detectInactivity"; public static final String PROP_AUTO_SWITCH_TO_COMPUTED = 48 "autoSwitchToComputed"; public static final String PROP_LAST_USED_EXPORT_FOLDER = 50 "lastUsedExportFolder"; public static final String PROP_WORKING_DAY_START = 52 "workingDayStart"; public static final String PROP_WORKING_DAY_END = 54 "workingDayEnd"; public static final String PROP_PAUSE_START = "pauseStart"; public static final String PROP_PAUSE_END = "pauseEnd"; 58 62 public static final String PROP_WORKING_DAYS = "workingDays"; 64 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 72 private int minutesPerDay = -1; 73 74 75 private int workingDays = -1; 76 77 80 public static Settings getDefault() { 81 return INSTANCE; 82 } 83 84 89 public static Preferences getPreferences() { 90 return NbPreferences.forModule(Settings.class); 91 } 92 93 private PropertyChangeSupport pcs = new PropertyChangeSupport (this); 94 95 98 public Settings() { 99 } 100 101 106 public File getLastUsedExportFolder() { 107 String path = getPreferences().get(PROP_LAST_USED_EXPORT_FOLDER, 108 System.getProperty("user.home")); 109 return new File (path); 110 } 111 112 117 public void setLastUsedExportFolder(File 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 130 public boolean getAutoSwitchToComputed() { 131 return getPreferences().getBoolean(PROP_AUTO_SWITCH_TO_COMPUTED, false); 132 } 133 134 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 151 public boolean getCollectWorkPeriods() { 152 return getPreferences().getBoolean(PROP_COLLECT_WORK_PERIODS, true); 153 } 154 155 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 171 public boolean getDetectInactivity() { 172 return getPreferences().getBoolean(PROP_DETECT_INACTIVITY, false); 173 } 174 175 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 190 public boolean getAppend() { 191 return getPreferences().getBoolean(PROP_APPEND, false); 192 } 193 194 200 public void setAppend(boolean append) { 201 getPreferences().putBoolean(PROP_APPEND, append); 202 pcs.firePropertyChange(PROP_APPEND, 203 null, null); 204 } 205 206 209 public void setFilename(String fname) { 210 String t = getFilename(); 211 if (t.equals(fname)) 212 return; 213 214 if (fname.trim().length() == 0) { 215 fname = NbBundle.getMessage(Settings.class, 217 "DefaultFilename"); } 219 220 File f = new File (expand(fname)); 224 File p = f.getParentFile(); 225 if (!p.exists()) { 226 StatusDisplayer.getDefault().setStatusText( 228 NbBundle.getMessage(Settings.class, 229 "NoFolder", p.getPath())); 231 throw new IllegalArgumentException (); 232 } 233 getPreferences().put(PROP_FILENAME, fname); 234 pcs.firePropertyChange(PROP_FILENAME, 235 null, null); 236 } 237 238 241 public String getFilename() { 242 return getPreferences().get(PROP_FILENAME, 243 NbBundle.getMessage(Settings.class, 244 "DefaultFilename")); } 246 247 254 public String getExpandedFilename() { 255 String fname = getFilename(); 256 return expand(fname); 257 } 258 259 264 public int getMinutesPerDay() { 265 if (minutesPerDay < 0) { 266 minutesPerDay = (getPauseStart() - getWorkingDayStart()) + 267 (getWorkingDayEnd() - getPauseEnd()); 268 } 269 return minutesPerDay; 270 } 271 272 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 292 private static String expand(String fname) { 293 Map <String , String > m = new HashMap <String , String >(2); 294 m.put("userdir", System.getProperty("netbeans.user")); m.put("/", File.separator); MapFormat f = new MapFormat(m); 297 String result = f.format(fname); 298 return result; 299 } 300 301 306 public void addPropertyChangeListener(PropertyChangeListener l) { 307 pcs.addPropertyChangeListener(l); 308 } 309 310 315 public int getWorkingDayStart() { 316 return getPreferences().getInt("workingDayStart", 8 * 60); 317 } 318 319 324 public int getPauseStart() { 325 return getPreferences().getInt("pauseStart", 12 * 60); 326 } 327 328 333 public int getPauseEnd() { 334 return getPreferences().getInt("pauseEnd", 13 * 60); 335 } 336 337 338 343 public int getWorkingDayEnd() { 344 return getPreferences().getInt("workingDayEnd", 17 * 60); 345 } 346 347 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 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 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 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 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 408 public boolean[] getWorkingDays() { 409 Preferences 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 |