Java Code Examples for java.util.prefs.Preferences#addPreferenceChangeListener()

The following examples show how to use java.util.prefs.Preferences#addPreferenceChangeListener() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: AppPreferences.java    From Logisim with GNU General Public License v3.0 6 votes vote down vote up
private static Preferences getPrefs(boolean shouldClear) {
	if (prefs == null) {
		synchronized (AppPreferences.class) {
			if (prefs == null) {
				Preferences p = Preferences.userNodeForPackage(Main.class);
				if (shouldClear) {
					try {
						p.clear();
					} catch (BackingStoreException e) {
					}
				}
				myListener = new MyListener();
				p.addPreferenceChangeListener(myListener);
				prefs = p;

				setTemplateFile(convertFile(p.get(TEMPLATE_FILE, null)));
				setLibrariesFolder(convertFile(p.get(LIBRARIES_FOLDER_PATH, null)));
				setTemplateType(p.getInt(TEMPLATE_TYPE, TEMPLATE_PLAIN));
			}
		}
	}
	return prefs;
}
 
Example 2
Source File: OptionCodeCompletionSettings.java    From netbeans with Apache License 2.0 5 votes vote down vote up
private static void lazyInit() {
    if (!inited) {
        inited = true;

        // correctly we should use a proper mime type for the document where the completion runs,
        // but at the moment this is enough, because completion settings are mainted globaly for all mime types
        Preferences prefs = MimeLookup.getLookup(MimePath.EMPTY).lookup(Preferences.class);
        prefs.addPreferenceChangeListener(WeakListeners.create(PreferenceChangeListener.class, settingsListener, prefs));
        setCaseSensitive(prefs.getBoolean(SimpleValueNames.COMPLETION_CASE_SENSITIVE, false));
    }
}
 
Example 3
Source File: ProxyPreferencesImplTest.java    From netbeans with Apache License 2.0 5 votes vote down vote up
/**
 * If a key is locally removed from ProxyPrefs, then it is removed from the underlying storage as well,
 * ProxyPrefs attempt to recover the value through inheritance. If the value is not inherited at all,
 * no event should be produced, as the removal was already done (in-memory).
 */
public void testRemoveLocallyRemovedKeyNothingInherited() throws Exception {
    Preferences stored = new MapPreferences();
    Preferences inherited = new MapPreferences();
    
    stored.putBoolean("toBeRemoved", Boolean.TRUE);

    MemoryPreferences mem = MemoryPreferences.getWithInherited(this, inherited, stored);
    Preferences test = mem.getPreferences();
    
    PL pl = new PL();
    test.addPreferenceChangeListener(pl);

    pl.arm();
    test.remove("toBeRemoved");
    pl.waitEvent();
    
    // check the first removal was fired properly
    assertNull(test.get("toBeRemoved", null));
    assertEquals(1, pl.changeCount);
    assertNull(pl.value);
    assertEquals("toBeRemoved", pl.key);
    
    pl.arm();
    stored.remove("toBeRemoved");
    pl.waitEvent();
    // no additional event was seen
    assertEquals(1, pl.changeCount);
}
 
Example 4
Source File: InheritedPreferences.java    From netbeans with Apache License 2.0 5 votes vote down vote up
public InheritedPreferences(Preferences inherited, Preferences stored) {
    super(null, ""); // NOI18N
    this.inherited = inherited;
    if (!(stored instanceof OverridePreferences)) {
        throw new IllegalArgumentException();
    }
    this.stored = stored;
    stored.addPreferenceChangeListener(WeakListeners.create(PreferenceChangeListener.class, this, stored));
    inherited.addPreferenceChangeListener(WeakListeners.create(PreferenceChangeListener.class, this, inherited));
}
 
Example 5
Source File: BreadcrumbsController.java    From netbeans with Apache License 2.0 5 votes vote down vote up
public static void addBreadCrumbsEnabledListener(@NonNull final ChangeListener l) {
    Preferences prefs = MimeLookup.getLookup(MimePath.EMPTY).lookup(Preferences.class);
    
    prefs.addPreferenceChangeListener(new PreferenceChangeListener() {
        @Override public void preferenceChange(PreferenceChangeEvent evt) {
            if (evt == null || SideBarFactoryImpl.KEY_BREADCRUMBS.equals(evt.getKey())) {
                l.stateChanged(new ChangeEvent(evt));
            }
        }
    });
}
 
Example 6
Source File: GsfCompletionProvider.java    From netbeans with Apache License 2.0 5 votes vote down vote up
private static void lazyInit() {
    if (!inited) {
        inited = true;
        
        // correctly we should use a proper mime type for the document where the completion runs,
        // but at the moment this is enough, because completion settings are mainted globaly for all mime types
        // (ie. their the same for all mime types). Also, if using a specific mime type
        // this code should hold the prefs instance somewhere, but not in a static field!
        Preferences prefs = MimeLookup.getLookup(MimePath.EMPTY).lookup(Preferences.class);
        prefs.addPreferenceChangeListener(WeakListeners.create(PreferenceChangeListener.class, settingsListener, prefs));
        
        setCaseSensitive(prefs.getBoolean(SimpleValueNames.COMPLETION_CASE_SENSITIVE, false));
        setAutoPopup(prefs.getBoolean(SimpleValueNames.COMPLETION_AUTO_POPUP, false));
    }
}
 
Example 7
Source File: DefaultFoldingOptions.java    From netbeans with Apache License 2.0 5 votes vote down vote up
@Override
public void setDefaultPreferences(Preferences pref) {
    if (this.defaultPrefs != null) {
        defaultPrefs.removePreferenceChangeListener(weakL);
    }
    // anomaly: if the local Preference contains 'removedKey' for a preference
    // which has been turned to default, it blocks all change propagations.
    this.defaultPrefs = pref;
    if (pref != null) {
        weakL = WeakListeners.create(PreferenceChangeListener.class, this, pref);
        pref.addPreferenceChangeListener(weakL);
    }
}
 
Example 8
Source File: ProxyPreferencesImplTest.java    From netbeans with Apache License 2.0 5 votes vote down vote up
/**
 * Checks that events for values that are not overriden
 * are propagated even from the inherited store
 */
public void testSeeInheritedEvents() throws Exception {
    Preferences stored = new MapPreferences();
    Preferences inherited = new MapPreferences();
    
    inherited.putInt("intValue", 100);

    MemoryPreferences mem = MemoryPreferences.getWithInherited(this, inherited, stored);
    Preferences test = mem.getPreferences();
    
    PL pl = new PL();
    test.addPreferenceChangeListener(pl);
    
    // add
    pl.arm();
    inherited.put("newValue", "baa");
    pl.waitEvent();
    assertEquals(1, pl.changeCount);
    assertEquals("newValue", pl.key);
    assertEquals("baa", pl.value);
    
    // change
    pl.arm();
    inherited.putInt("intValue", 3);
    pl.waitEvent();
    assertEquals(2, pl.changeCount);
    assertEquals("intValue", pl.key);
    assertEquals("3", pl.value);
    
    // remove not inherited
    pl.arm();
    inherited.remove("newValue");
    pl.waitEvent();
    assertEquals(3, pl.changeCount);
    assertEquals("newValue", pl.key);
    assertEquals(null, pl.value);
}
 
Example 9
Source File: PrefMonitorInt.java    From Logisim with GNU General Public License v3.0 5 votes vote down vote up
PrefMonitorInt(String name, int dflt) {
	super(name);
	this.dflt = dflt;
	this.value = dflt;
	Preferences prefs = AppPreferences.getPrefs();
	set(Integer.valueOf(prefs.getInt(name, dflt)));
	prefs.addPreferenceChangeListener(this);
}
 
Example 10
Source File: PreferencesTest.java    From netbeans with Apache License 2.0 5 votes vote down vote up
@RandomlyFails
public void testEvents142723() throws Exception {
    Preferences prefsA = MimeLookup.getLookup(MimePath.EMPTY).lookup(Preferences.class);
    Preferences prefsB = MimeLookup.getLookup(MimePath.parse("text/x-testA")).lookup(Preferences.class);

    String key1 = "all-lang-key-" + getName();
    prefsA.put(key1, "xyz");
    assertEquals("'" + key1 + "' has wrong value", "xyz", prefsA.get(key1, null));

    // attach listeners
    L listenerA = new L();
    prefsA.addPreferenceChangeListener(listenerA);
    L listenerB = new L();
    prefsB.addPreferenceChangeListener(listenerB);
    
    // putting the same value again should not fire an event
    prefsA.put(key1, "xyz");
    assertEquals("'" + key1 + "' has wrong value", "xyz", prefsA.get(key1, null));
    assertEquals("There should be no events from prefsA", 0, listenerA.count);

    assertEquals("'" + key1 + "' should inherit the value", "xyz", prefsB.get(key1, null));
    assertEquals("There should be no events from prefsB", 0, listenerB.count);

    // putting the same value again should not fire an event
    prefsB.put(key1, "xyz");
    assertEquals("'" + key1 + "' has wrong value in prefsB", "xyz", prefsB.get(key1, null));
    assertEquals("There should still be no events from prefsB", 0, listenerB.count);
}
 
Example 11
Source File: JsonPreferences.java    From netbeans with Apache License 2.0 5 votes vote down vote up
/**
 * Adds {@link PropertyChangeListener}.
 * @param listener the listener to be added
 * @since 1.5
 */
public void addPropertyChangeListener(@NonNull final PropertyChangeListener listener) {
    Parameters.notNull("listener", listener);   //NOI18N
    if (!listens.get() && listens.compareAndSet(false, true)) {
        final Preferences p = getPreferences();
        p.addPreferenceChangeListener(WeakListeners.create(PreferenceChangeListener.class, pcl, p));
    }
    listeners.addPropertyChangeListener(listener);
}
 
Example 12
Source File: AlwaysEnabledActionTest.java    From netbeans with Apache License 2.0 5 votes vote down vote up
private void checkPreferencesAction(Action a, Preferences prefsNode) throws Exception {
    prefsNode.putBoolean("myKey", true);
    prefsNode.sync();
    class L implements PreferenceChangeListener {
        boolean notified;

        public synchronized void preferenceChange(PreferenceChangeEvent evt) {
            notified = true;
            notifyAll();
        }

        public synchronized void waitFor() throws Exception {
            while (!notified) {
                wait();
            }
            notified = false;
        }
    }
    L listener = new L();

    // Verify value
    assertTrue("Expected true as preference value", prefsNode.getBoolean("myKey", false));

    TestCase.assertTrue("Expected to be instance of Presenter.Menu", a instanceof Presenter.Menu);
    JMenuItem item = ((Presenter.Menu) a).getMenuPresenter();
    TestCase.assertTrue("Expected to be selected", item.isSelected());
    prefsNode.addPreferenceChangeListener(listener);
    prefsNode.putBoolean("myKey", false);
    prefsNode.sync();
    listener.waitFor();
    TestCase.assertFalse("Expected to not be selected", item.isSelected());
    a.actionPerformed(null); // new ActionEvent(null, 0, ""));
    listener.waitFor();
    TestCase.assertTrue("Expected to be selected", item.isSelected());
    prefsNode.putBoolean("myKey", false);
    prefsNode.sync();
    listener.waitFor();
}
 
Example 13
Source File: ImageCursorSynchronizer.java    From snap-desktop with GNU General Public License v3.0 5 votes vote down vote up
@Override
public void run() {
    psvOverlayMap = new WeakHashMap<>();
    viewPplMap = new WeakHashMap<>();
    psvOverlayMapUpdater = new PsvListUpdater();

    Preferences preferences = SnapApp.getDefault().getPreferences();
    preferences.addPreferenceChangeListener(new ImageCursorSynchronizerPreferenceChangeListener());
}
 
Example 14
Source File: TestPreferences.java    From netbeans with Apache License 2.0 5 votes vote down vote up
public void testAddPreferenceChangeListener() throws BackingStoreException, InterruptedException {
    final Preferences pref = getPreferencesNode();
    PreferenceChangeListener l = new PreferenceChangeListener() {
        public void preferenceChange(PreferenceChangeEvent evt) {
            synchronized (TestPreferences.class) {
                //assertionerrors cause deadlock here
                assertSame(pref, evt.getNode());
                assertEquals("key", evt.getKey());
                assertEquals(evt.getNewValue(),pref.get(evt.getKey(),null), evt.getNewValue());
                TestPreferences.class.notifyAll();
            }
        }
    };
    pref.addPreferenceChangeListener(l);
    try {
        synchronized (TestPreferences.class) {
            pref.put("key","AddPreferenceChangeListener");
            pref.flush();
            TestPreferences.class.wait();
        }
        
        synchronized (TestPreferences.class) {
            pref.remove("key");
            pref.flush();
            TestPreferences.class.wait();
        }
        
        synchronized (TestPreferences.class) {
            pref.put("key","AddPreferenceChangeListener2");
            pref.flush();
            TestPreferences.class.wait();
        }
    } finally {
        pref.removePreferenceChangeListener(l);
    }
}
 
Example 15
Source File: PreferencesTest.java    From netbeans with Apache License 2.0 5 votes vote down vote up
public void testEvents1() throws Exception {
    Preferences prefsA = MimeLookup.getLookup(MimePath.EMPTY).lookup(Preferences.class);
    L listenerA = new L();
    prefsA.addPreferenceChangeListener(listenerA);

    Preferences prefsB = MimeLookup.getLookup(MimePath.parse("text/x-testA")).lookup(Preferences.class);
    L listenerB = new L();
    prefsB.addPreferenceChangeListener(listenerB);
    
    assertNotNull("'simple-value-setting-A' should not be null", prefsA.get("simple-value-setting-A", null));
    assertNotNull("'simple-value-setting-A' should not be null", prefsB.get("simple-value-setting-A", null));
    assertEquals("Wrong value for 'testA-1-setting-1'", "value-of-testA-1-setting-1", prefsB.get("testA-1-setting-1", null));

    assertEquals("There should be no A events", 0, listenerA.count);
    assertEquals("There should be no B events", 0, listenerB.count);
    
    prefsA.put("simple-value-setting-A", "new-value");
    assertEquals("Wrong value for 'simple-value-setting-A'", "new-value", prefsA.get("simple-value-setting-A", null));
    assertEquals("The value for 'simple-value-setting-A' was not propagated", "new-value", prefsB.get("simple-value-setting-A", null));
    
    Thread.sleep(500);
    
    assertEquals("Wrong number of A events", 1, listenerA.count);
    assertEquals("Wrong setting name in the A event", "simple-value-setting-A", listenerA.lastEvent.getKey());
    assertEquals("Wrong setting value in the A event", "new-value", listenerA.lastEvent.getNewValue());
    assertSame("Wrong Preferences instance in the A event", prefsA, listenerA.lastEvent.getNode());
    assertEquals("Wrong number of B events", 1, listenerB.count);
    assertEquals("Wrong setting name in the B event", "simple-value-setting-A", listenerB.lastEvent.getKey());
    assertEquals("Wrong setting value in the B event", "new-value", listenerB.lastEvent.getNewValue());
    assertSame("Wrong Preferences instance in the B event", prefsB, listenerB.lastEvent.getNode());
}
 
Example 16
Source File: RecentProjects.java    From Logisim with GNU General Public License v3.0 5 votes vote down vote up
RecentProjects() {
	recentFiles = new File[NUM_RECENT];
	recentTimes = new long[NUM_RECENT];
	Arrays.fill(recentTimes, System.currentTimeMillis());

	Preferences prefs = AppPreferences.getPrefs();
	prefs.addPreferenceChangeListener(this);

	for (int index = 0; index < NUM_RECENT; index++) {
		getAndDecode(prefs, index);
	}
}
 
Example 17
Source File: MissingModuleProblemsProvider.java    From netbeans with Apache License 2.0 5 votes vote down vote up
private Preferences getPreferences() {
    Preferences prefs = prefsCache.get();
    if (prefs == null) {
        prefs = ProjectUtils.getPreferences(project, MissingModuleProblemsProvider.class, true);
        if (prefsCache.compareAndSet(null, prefs)) {
            prefs.addPreferenceChangeListener(WeakListeners.create(PreferenceChangeListener.class, this, prefs));
        }
    }
    return prefs;
}
 
Example 18
Source File: PNode.java    From snap-desktop with GNU General Public License v3.0 5 votes vote down vote up
private PNode(Product product, PContent group) {
    super(product, group);
    this.group = group;
    group.node = this;
    setDisplayName(product.getDisplayName());
    setShortDescription(product.getDescription());
    setIconBaseWithExtension("org/esa/snap/rcp/icons/RsProduct16.gif");
    Preferences preferences = SnapApp.getDefault().getPreferences();
    preferences.addPreferenceChangeListener(WeakListeners.create(PreferenceChangeListener.class, this, preferences));
}
 
Example 19
Source File: ProxyPreferencesImplTest.java    From netbeans with Apache License 2.0 4 votes vote down vote up
/**
 * Checks that puts to the direct store is refired
 */
public void testSeeStoredEvents() throws Exception {
    Preferences stored = new MapPreferences();
    Preferences inherited = new MapPreferences();
    
    inherited.putInt("intValue", 100);
    stored.putInt("intValue", 2);
    stored.putBoolean("toBeRemoved", Boolean.TRUE);

    MemoryPreferences mem = MemoryPreferences.getWithInherited(this, inherited, stored);
    Preferences test = mem.getPreferences();
    
    PL pl = new PL();
    test.addPreferenceChangeListener(pl);
    
    // add
    pl.arm();
    stored.put("newValue", "baa");
    pl.waitEvent();
    assertEquals(1, pl.changeCount);
    assertEquals("newValue", pl.key);
    assertEquals("baa", pl.value);
    
    // change
    pl.arm();
    stored.putInt("intValue", 3);
    pl.waitEvent();
    assertEquals(2, pl.changeCount);
    assertEquals("intValue", pl.key);
    assertEquals("3", pl.value);
    
    // remove not inherited
    pl.arm();
    stored.remove("newValue");
    pl.waitEvent();
    assertEquals(3, pl.changeCount);
    assertEquals("newValue", pl.key);
    assertEquals(null, pl.value);
    
    // remove inherited
    pl.arm();
    stored.remove("intValue");
    pl.waitEvent();
    assertEquals(4, pl.changeCount);
    assertEquals("intValue", pl.key);
    assertEquals("100", pl.value);
}
 
Example 20
Source File: PreferencesTest.java    From netbeans with Apache License 2.0 4 votes vote down vote up
public void testEvents2() throws Exception {
    Preferences prefsA = MimeLookup.getLookup(MimePath.EMPTY).lookup(Preferences.class);
    L listenerA = new L();
    prefsA.addPreferenceChangeListener(listenerA);

    Preferences prefsB = MimeLookup.getLookup(MimePath.parse("text/x-testA")).lookup(Preferences.class);
    L listenerB = new L();
    prefsB.addPreferenceChangeListener(listenerB);
    
    String origValue = prefsA.get("simple-value-setting-A", null);
    assertNotNull("'simple-value-setting-A' should not be null", origValue);
    assertNotNull("'simple-value-setting-A' should not be null", prefsB.get("simple-value-setting-A", null));
    assertEquals("Wrong value for 'testA-1-setting-1'", "value-of-testA-1-setting-1", prefsB.get("testA-1-setting-1", null));

    assertEquals("There should be no A events", 0, listenerA.count);
    assertEquals("There should be no B events", 0, listenerB.count);
    
    prefsB.put("simple-value-setting-A", "another-value-for-testA");
    assertEquals("Wrong value for 'simple-value-setting-A'", origValue, prefsA.get("simple-value-setting-A", null));
    assertEquals("Wrong value for 'simple-value-setting-A' in 'text/x-testA'", "another-value-for-testA", prefsB.get("simple-value-setting-A", null));
    
    Thread.sleep(500);
    
    assertEquals("Wrong number of A events", 0, listenerA.count);
    assertEquals("Wrong number of B events", 1, listenerB.count);
    assertEquals("Wrong setting name in the B event", "simple-value-setting-A", listenerB.lastEvent.getKey());
    assertEquals("Wrong setting value in the B event", "another-value-for-testA", listenerB.lastEvent.getNewValue());
    assertSame("Wrong Preferences instance in the B event", prefsB, listenerB.lastEvent.getNode());
    
    listenerA.count = 0; listenerA.lastEvent = null;
    listenerB.count = 0; listenerB.lastEvent = null;
    
    // now change the value in MimeType.EMPTY
    
    prefsA.put("simple-value-setting-A", "another-value-for-MimeType.EMPTY");
    assertEquals("Wrong value for 'simple-value-setting-A'", "another-value-for-MimeType.EMPTY", prefsA.get("simple-value-setting-A", null));
    assertEquals("Wrong value for 'simple-value-setting-A' in 'text/x-testA'", "another-value-for-testA", prefsB.get("simple-value-setting-A", null));
    
    Thread.sleep(500);
    
    assertEquals("Wrong number of A events", 1, listenerA.count);
    assertEquals("Wrong setting name in the A event", "simple-value-setting-A", listenerA.lastEvent.getKey());
    assertEquals("Wrong setting value in the A event", "another-value-for-MimeType.EMPTY", listenerA.lastEvent.getNewValue());
    assertSame("Wrong Preferences instance in the A event", prefsA, listenerA.lastEvent.getNode());
    assertEquals("Wrong number of B events", 0, listenerB.count);
}