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

The following examples show how to use java.util.prefs.Preferences#put() . 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: ServerDetailsView.java    From syncope with Apache License 2.0 6 votes vote down vote up
protected void okButtonActionPerformed(final java.awt.event.ActionEvent evt) {
    // validation
    List<String> validation = validate(schemeTxt, hostTxt, portTxt, userNameTxt);
    if (validation.isEmpty()) {
        Preferences prefs = NbPreferences.forModule(ResourceExplorerTopComponent.class);
        prefs.put("scheme", schemeTxt.getText());
        prefs.put("host", hostTxt.getText());
        prefs.put("port", portTxt.getText());
        prefs.put("username", userNameTxt.getText());
        prefs.put("password", new String(passwordTxt.getPassword()));
        // dismiss panel
        this.dispose();
    } else {
        JOptionPane.showMessageDialog(
                null, "Please insert valid " + validation, "Validation  Error", JOptionPane.ERROR_MESSAGE);
    }
}
 
Example 2
Source File: HistoryComboBoxModelTest.java    From snap-desktop with GNU General Public License v3.0 6 votes vote down vote up
@Test
public void testValidation() {
    final Preferences preferences = new DummyPreferences();
    preferences.put("historyItem.0", "one");
    preferences.put("historyItem.1", "two");
    preferences.put("historyItem.2", "three");

    final UserInputHistory history = new UserInputHistory(3, "historyItem") {
        @Override
        protected boolean isValidItem(String item) {
            return "two".equals(item);

        }
    };
    history.initBy(preferences);
    final HistoryComboBoxModel model = new HistoryComboBoxModel(history);
    assertEquals(1, model.getSize());
    assertEquals("two", model.getElementAt(0));
}
 
Example 3
Source File: PreferencesUtils.java    From CrossMobile with GNU Lesser General Public License v3.0 6 votes vote down vote up
public static boolean addPref(Preferences base, String node, String data) {
    if (base == null || node == null || node.isEmpty() || data == null || data.isEmpty())
        return false;
    try {
        removePref(base, node);
        String[] chunks = TextUtils.split(data, Preferences.MAX_VALUE_LENGTH - 1);
        if (chunks.length == 1)
            base.put(node, chunks[0]);
        else
            for (int i = 0; i < chunks.length; i++)
                base.node(node).put(String.valueOf(i), chunks[i]);
        return true;
    } catch (Exception ex) {
        return false;
    }
}
 
Example 4
Source File: SaveGame.java    From MikuMikuStudio with BSD 2-Clause "Simplified" License 6 votes vote down vote up
/**
     * Saves a savable in a system-dependent way. Note that only small amounts of data can be saved.
     * @param gamePath A unique path for this game, e.g. com/mycompany/mygame
     * @param dataName A unique name for this savegame, e.g. "save_001"
     * @param data The Savable to save
     */
    public static void saveGame(String gamePath, String dataName, Savable data) {
        Preferences prefs = Preferences.userRoot().node(gamePath);
        BinaryExporter ex = BinaryExporter.getInstance();
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        try {
            GZIPOutputStream zos = new GZIPOutputStream(out);
            ex.save(data, zos);
            zos.close();
        } catch (IOException ex1) {
            Logger.getLogger(SaveGame.class.getName()).log(Level.SEVERE, "Error saving data: {0}", ex1);
            ex1.printStackTrace();
        }
        UUEncoder enc = new UUEncoder();
        String dataString = enc.encodeBuffer(out.toByteArray());
//        System.out.println(dataString);
        if (dataString.length() > Preferences.MAX_VALUE_LENGTH) {
            throw new IllegalStateException("SaveGame dataset too large");
        }
        prefs.put(dataName, dataString);
    }
 
Example 5
Source File: EclipseProjectReference.java    From netbeans with Apache License 2.0 6 votes vote down vote up
public static void write(Project project, EclipseProjectReference ref) {
    Preferences prefs = ProjectUtils.getPreferences(project, EclipseProjectReference.class, true);
    File baseDir = FileUtil.toFile(project.getProjectDirectory());
    if (CollocationQuery.areCollocated(baseDir, ref.eclipseProjectLocation)) {
        prefs.put("project", PropertyUtils.relativizeFile(baseDir, ref.eclipseProjectLocation)); //NOI18N
    } else {
        prefs.put("project", ref.eclipseProjectLocation.getPath()); //NOI18N
    }
    if (ref.eclipseWorkspaceLocation != null) {
        if (CollocationQuery.areCollocated(baseDir, ref.eclipseWorkspaceLocation)) {
            prefs.put("workspace", PropertyUtils.relativizeFile(baseDir, ref.eclipseWorkspaceLocation)); //NOI18N
        } else {
            prefs.put("workspace", ref.eclipseWorkspaceLocation.getPath()); //NOI18N
        }
    }
    prefs.put("timestamp", Long.toString(ref.getCurrentTimestamp())); //NOI18N
    prefs.put("key", ref.key); //NOI18N
}
 
Example 6
Source File: ProxyPreferencesTest.java    From netbeans with Apache License 2.0 5 votes vote down vote up
public void testSimpleFlush() throws BackingStoreException {
    Preferences orig = Preferences.userRoot().node(getName());
    assertNull("Original contains value", orig.get("key-1", null));

    Preferences test = ProxyPreferences.getProxyPreferences(this, orig);
    assertNull("Test should not contains pair", orig.get("key-1", null));

    test.put("key-1", "xyz");
    assertEquals("Test doesn't contain new pair", "xyz", test.get("key-1", null));

    test.flush();
    assertEquals("Test should still contain the pair", "xyz", test.get("key-1", null));
    assertEquals("Test didn't flush the pair", "xyz", orig.get("key-1", null));
}
 
Example 7
Source File: ConverterPreferences.java    From phoebus with Eclipse Public License 1.0 5 votes vote down vote up
public static void setAutoConverterDir(final String path)
{
    auto_converter_dir = new File(path);

    final Preferences prefs = Preferences.userNodeForPackage(ConverterPreferences.class);
    prefs.put("auto_converter_dir",  path);
    try
    {
        prefs.flush();
    }
    catch (Exception ex)
    {
        logger.log(Level.WARNING, "Cannot update auto_converter_dir", ex);
    }
}
 
Example 8
Source File: ReindenterTest.java    From netbeans with Apache License 2.0 5 votes vote down vote up
public void testLineIndentationBeforeHalfIndentedDoWhileBlock() throws Exception {
    Preferences preferences = MimeLookup.getLookup(JavaTokenId.language().mimeType()).lookup(Preferences.class);
    preferences.put("otherBracePlacement", CodeStyle.BracePlacement.NEW_LINE_HALF_INDENTED.name());
    try {
        performLineIndentationTest("package t;\npublic class T {\n    public void op() {\n        do\n|            {\n    }\n}\n",
                "package t;\npublic class T {\n    public void op() {\n        do\n          {\n    }\n}\n");
    } finally {
        preferences.remove("otherBracePlacement");
    }
}
 
Example 9
Source File: ReindenterTest.java    From netbeans with Apache License 2.0 5 votes vote down vote up
public void testNewLineIndentationBeforeHalfIndentedStaticBlock() throws Exception {
    Preferences preferences = MimeLookup.getLookup(JavaTokenId.language().mimeType()).lookup(Preferences.class);
    preferences.put("otherBracePlacement", CodeStyle.BracePlacement.NEW_LINE_HALF_INDENTED.name());
    try {
        performNewLineIndentationTest("package t;\npublic class T {\n    static| {\n    }\n}\n",
                "package t;\npublic class T {\n    static\n      {\n    }\n}\n");
    } finally {
        preferences.remove("otherBracePlacement");
    }
}
 
Example 10
Source File: AppSettings.java    From jmonkeyengine with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
/**
 * Saves settings into the Java preferences.
 * <p>
 * On the Windows operating system, the preferences are saved in the registry
 * at the following key:<br>
 * <code>HKEY_CURRENT_USER\Software\JavaSoft\Prefs\[preferencesKey]</code>
 *
 * @param preferencesKey The preferences key to save at. Generally the
 * application's unique name.
 *
 * @throws BackingStoreException If an exception occurs with the preferences
 */
public void save(String preferencesKey) throws BackingStoreException {
    Preferences prefs = Preferences.userRoot().node(preferencesKey);

    // Clear any previous settings set before saving, this will
    // purge any other parameters set in older versions of the app, so
    // that they don't leak onto the AppSettings of newer versions.
    prefs.clear();

    for (String key : keySet()) {
        Object val = get(key);
        if (val instanceof Integer) {
            prefs.putInt("I_" + key, (Integer) val);
        } else if (val instanceof Float) {
            prefs.putFloat("F_" + key, (Float) val);
        } else if (val instanceof String) {
            prefs.put("S_" + key, (String) val);
        } else if (val instanceof Boolean) {
            prefs.putBoolean("B_" + key, (Boolean) val);
        }
        // NOTE: Ignore any parameters of unsupported types instead
        // of throwing exception. This is specifically for handling
        // BufferedImage which is used in setIcons(), as you do not
        // want to export such data in the preferences.
    }

    // Ensure the data is properly written into preferences before
    // continuing.
    prefs.sync();
}
 
Example 11
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 12
Source File: JSFTargetPanelProvider.java    From netbeans with Apache License 2.0 5 votes vote down vote up
public void storeSettings( TargetChooserPanel<FileType> panel ) {
    if (isFacelets()) {
        Preferences preferences = ProjectUtils.getPreferences(
                panel.getProject(), ProjectUtils.class, true);
        String key = "jsf.language";            //NOI18N
        String value = "Facelets";              //NOI18N
        if (!preferences.get(key, "").equals(value)){
            preferences.put(key, value);
        }
    }
    
    panel.getTemplateWizard().putProperty(FileType.IS_XML, false);
    panel.getTemplateWizard().putProperty(FileType.IS_SEGMENT, getUIManager().isSegment());
    panel.getTemplateWizard().putProperty(FileType.IS_FACELETS, getUIManager().isFacelets());
}
 
Example 13
Source File: ReindenterTest.java    From netbeans with Apache License 2.0 5 votes vote down vote up
public void testLineIndentationBeforeHalfIndentedForBlock() throws Exception {
    Preferences preferences = MimeLookup.getLookup(JavaTokenId.language().mimeType()).lookup(Preferences.class);
    preferences.put("otherBracePlacement", CodeStyle.BracePlacement.NEW_LINE_HALF_INDENTED.name());
    try {
        performLineIndentationTest("package t;\npublic class T {\n    public void op() {\n        for (;;)\n|            {\n    }\n}\n",
                "package t;\npublic class T {\n    public void op() {\n        for (;;)\n          {\n    }\n}\n");
    } finally {
        preferences.remove("otherBracePlacement");
    }
}
 
Example 14
Source File: ReindenterTest.java    From netbeans with Apache License 2.0 5 votes vote down vote up
public void testNewLineIndentationBeforeHalfIndentedArrayInit() throws Exception {
    Preferences preferences = MimeLookup.getLookup(JavaTokenId.language().mimeType()).lookup(Preferences.class);
    preferences.put("otherBracePlacement", CodeStyle.BracePlacement.NEW_LINE_HALF_INDENTED.name());
    try {
        performNewLineIndentationTest("package t;\npublic class T {\n    public void op() {\n        int[] arr =| {\n    }\n}\n",
                "package t;\npublic class T {\n    public void op() {\n        int[] arr =\n          {\n    }\n}\n");
    } finally {
        preferences.remove("otherBracePlacement");
    }
}
 
Example 15
Source File: ReindenterTest.java    From netbeans with Apache License 2.0 5 votes vote down vote up
public void testNewLineIndentationBeforeEmptyHalfIndentedClassEnd() throws Exception {
    Preferences preferences = MimeLookup.getLookup(JavaTokenId.language().mimeType()).lookup(Preferences.class);
    preferences.put("classDeclBracePlacement", CodeStyle.BracePlacement.NEW_LINE_HALF_INDENTED.name());
    try {
        performNewLineIndentationTest("package t;\npublic class T\n  {|}\n",
                "package t;\npublic class T\n  {\n  }\n");
    } finally {
        preferences.remove("classDeclBracePlacement");
    }
}
 
Example 16
Source File: DefaultStageService.java    From pdfsam with GNU Affero General Public License v3.0 5 votes vote down vote up
@Override
public void save(StageStatus status) {
    Preferences node = Preferences.userRoot().node(STAGE_PATH);
    try {
        node.put(STAGE_STATUS_KEY, JSON.std.asString(status));
        LOG.trace("Stage status saved {}", status);
    } catch (IOException e) {
        LOG.error("Unable to increment modules usage statistics", e);
    }
}
 
Example 17
Source File: CreateJREPanel.java    From netbeans with Apache License 2.0 5 votes vote down vote up
private static void setEJDKHome(@NullAllowed final String path) {
    final Preferences prefs = NbPreferences.forModule(CreateJREPanel.class);
    prefs.put(
        KEY_EJDK,
        path == null || path.isEmpty() ?
            null :
            path);
}
 
Example 18
Source File: FallbackProvider.java    From netbeans with Apache License 2.0 5 votes vote down vote up
private boolean _save(String key, char[] password, String description) {
    Preferences prefs = prefs();
    try {
        prefs.putByteArray(key, encryption.encrypt(password));
    } catch (Exception x) {
        LOG.log(Level.FINE, "failed to encrypt password for " + key, x);
        return false;
    }
    if (description != null) {
        // Preferences interface gives no access to *.properties comments, so:
        prefs.put(key + DESCRIPTION, description);
    }
    return true;
}
 
Example 19
Source File: UserPreferences.java    From orbit-image-analysis with GNU General Public License v3.0 4 votes vote down vote up
void store() {
  Preferences prefs = node().node("JTextComponent");
  prefs.put(text.getName(), text.getText());
}
 
Example 20
Source File: PreferencesManager.java    From netbeans-mmd-plugin with Apache License 2.0 4 votes vote down vote up
public void setFlag(@Nonnull final Preferences pref, @Nonnull final String key, final boolean flag) {
  synchronized (this.localCache) {
    this.localCache.put(key, Boolean.valueOf(flag));
    pref.put(key, Boolean.toString(flag));
  }
}