1 19 20 package org.netbeans.modules.autoupdate; 21 22 import java.beans.PropertyChangeListener ; 23 import java.beans.PropertyChangeSupport ; 24 import java.beans.PropertyEditorSupport ; 25 import java.io.BufferedReader ; 26 import java.io.IOException ; 27 import java.io.InputStream ; 28 import java.io.InputStreamReader ; 29 import java.lang.Object ; 30 import java.lang.Object ; 31 import java.lang.String ; 32 import java.util.Date ; 33 import java.text.SimpleDateFormat ; 34 import java.util.HashMap ; 35 import java.util.Iterator ; 36 import java.util.Map ; 37 import java.util.Random ; 38 import java.util.logging.Level ; 39 import java.util.logging.Logger ; 40 import java.util.prefs.Preferences ; 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 51 public class Settings { 53 private static final Settings INSTANCE = new Settings(); 54 private static final String PROP_ASK_BEFORE = "askBefore"; private static final String PROP_NEGATIVE_RESULTS = "negativeResults"; private static final String PROP_PERIOD = "period"; private static final String PROP_LAST_CHECK = "lastCheck"; private static final String PROP_IDE_IDENTITY = "ideIdentity"; private static final String PROP_ACCEPTED_NOTIFICATIONS = "acceptedNotifications"; 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 69 public static final String NODE_DEFAULT_ACTION = "nodeDefaultAction"; private static final Random RANDOM = new Random (); 71 private static final Logger err = Logger.getLogger("org.netbeans.modules.autoupdate"); private String tempIdeIdentity = null; 73 private Map notifications; 74 private PropertyChangeSupport ps; 75 76 private static Preferences getPreferences() { 77 return NbPreferences.forModule(Settings.class); 78 } 79 80 public static Settings getShared() { 81 if (INSTANCE.ps == null) { 82 INSTANCE.ps = new PropertyChangeSupport (INSTANCE); 83 84 } 85 return INSTANCE; 86 } 87 88 public final void addPropertyChangeListener(PropertyChangeListener l) { 89 ps.addPropertyChangeListener(l); 90 } 91 92 public final void removePropertyChangeListener(PropertyChangeListener l) { 93 ps.removePropertyChangeListener(l); 94 } 95 96 97 99 public int getPeriod() { 100 return getPreferences().getInt(PROP_PERIOD, EVERY_WEEK); 101 } 102 103 105 public void setPeriod( int period ) { 106 getPreferences().putInt(PROP_PERIOD, period); 107 } 108 109 111 protected AutoupdateType defaultAutoupdateType() { 112 return AutoupdateType.getDefault (); 113 } 114 115 public void setIdeIdentity (String identity) { 116 getPreferences().put(PROP_IDE_IDENTITY, identity); 117 } 118 119 public String getIdeIdentity () { 120 if (tempIdeIdentity instanceof String ) { 121 return tempIdeIdentity; 122 } 123 Object oldIdeIdentity = getPreferences().get(PROP_IDE_IDENTITY,null); 124 String newIdeIdentity = null; 125 if (oldIdeIdentity == null) { 126 newIdeIdentity = modifyIdeIdentityIfNeeded (Integer.toString (generateNewId ())); 127 } else { 128 newIdeIdentity = modifyIdeIdentityIfNeeded ((String ) 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 notifications) { 139 this.notifications = notifications; 140 storeAcceptedNotifications(notifications); 141 } 142 143 public Map getAcceptedNotifications () { 144 if (this.notifications == null) { 145 this.notifications = initAcceptedNotifications(); 146 } 147 return this.notifications; 148 } 149 150 private static Map initAcceptedNotifications() { 151 Map m = new HashMap (); 152 String allPairs = getPreferences().get(PROP_ACCEPTED_NOTIFICATIONS, null); 153 if (allPairs != null) { 154 String [] pairs = allPairs.split("|"); 155 for (int i = 0; i < pairs.length; i++) { 156 String p = pairs[i]; 157 String [] 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 m) { 167 StringBuffer sb = new StringBuffer (); 168 for (Iterator it = m.entrySet().iterator(); it.hasNext();) { 169 Map.Entry e = (Map.Entry )it.next(); 170 sb.append(e.getKey()).append(",").append(e.getValue()); if (it.hasNext()) { 172 sb.append("|"); } 174 } 175 String toStore = sb.toString(); 176 if (toStore.length() > 0) { 177 getPreferences().put(PROP_ACCEPTED_NOTIFICATIONS, toStore); 178 } 179 } 180 181 182 184 public boolean isAskBefore () { 185 return getPreferences().getBoolean(PROP_ASK_BEFORE, false); 186 } 187 188 190 public void setAskBefore (boolean askBefore) { 191 getPreferences().putBoolean(PROP_ASK_BEFORE, askBefore); 192 } 193 194 196 public boolean isNegativeResults () { 197 return getPreferences().getBoolean(PROP_NEGATIVE_RESULTS, false); 198 } 200 201 203 public void setNegativeResults (boolean negativeResults) { 204 getPreferences().putBoolean(PROP_NEGATIVE_RESULTS, negativeResults); 205 } 206 207 209 public Date getLastCheck() { 210 long t = getPreferences().getLong(PROP_LAST_CHECK, -1); 211 return (t > 0) ? new Date (t) : null; 212 213 } 214 215 217 public void setLastCheck( Date 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 getBundle( String key ) { 230 return NbBundle.getMessage( Settings.class, key ); 231 } 232 233 235 public static class PeriodPropertyEditor extends PropertyEditorSupport { 236 237 239 private static final String [] 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 256 public String [] getTags() { 257 return tags; 258 } 259 260 261 public String getAsText () { 262 long value = ((Integer )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 272 public void setAsText (String text) { 273 for (int i = 0; i < tags.length ; i++) 274 if (tags[i] == text) { 275 setValue(new Integer (values[i])); 276 return; 277 } 278 279 setValue( new Integer (0) ); 280 } 281 } 282 283 285 public static class LastCheckPropertyEditor extends PropertyEditorSupport { 286 287 private static final SimpleDateFormat sdf = new SimpleDateFormat (); 288 289 290 291 public String getAsText () { 292 Date dt = (Date )getValue(); 293 return (dt == null) ? "" : sdf.format( dt ); } 295 296 297 public void setAsText (String text) { 298 299 try { 300 Date newValue = sdf.parse( text ); 301 setValue( newValue ); 302 } 303 catch ( java.text.ParseException e ) { 304 } 306 } 307 } 308 309 private static String modifyIdeIdentityIfNeeded (String oldIdeIdentity) { 311 String [] ideIdentityArr = oldIdeIdentity.split ("\\d"); String id = null; 313 String 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); err.log (Level.FINER, "Old IDE Identity ID: " + id); String newPrefix = ""; 325 try { 326 FileObject fo = Repository.getDefault().getDefaultFileSystem().findResource("/productid"); if (fo != null) { 328 InputStream is = fo.getInputStream(); 329 try { 330 BufferedReader r = new BufferedReader (new InputStreamReader (is)); 331 newPrefix = r.readLine().trim(); 332 } finally { 333 is.close(); 334 } 335 } 336 } catch (IOException ignore) { 337 } 339 if (!newPrefix.equals (oldPrefix)) { 340 err.log (Level.FINER, "New IDE Identity Prefix: " + newPrefix); } else { 342 err.log (Level.FINER, "No new prefix."); } 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 |