Example usage for java.util.prefs Preferences get

List of usage examples for java.util.prefs Preferences get

Introduction

In this page you can find the example usage for java.util.prefs Preferences get.

Prototype

public abstract String get(String key, String def);

Source Link

Document

Returns the value associated with the specified key in this preference node.

Usage

From source file:PreferencesTest.java

public PreferencesFrame() {
    // get position, size, title from preferences

    Preferences root = Preferences.userRoot();
    final Preferences node = root.node("/com/horstmann/corejava");
    int left = node.getInt("left", 0);
    int top = node.getInt("top", 0);
    int width = node.getInt("width", DEFAULT_WIDTH);
    int height = node.getInt("height", DEFAULT_HEIGHT);
    setBounds(left, top, width, height);

    // if no title given, ask user

    String title = node.get("title", "");
    if (title.equals(""))
        title = JOptionPane.showInputDialog("Please supply a frame title:");
    if (title == null)
        title = "";
    setTitle(title);/*from w  ww.j  av a 2 s.co  m*/

    // set up file chooser that shows XML files

    final JFileChooser chooser = new JFileChooser();
    chooser.setCurrentDirectory(new File("."));

    // accept all files ending with .xml
    chooser.setFileFilter(new javax.swing.filechooser.FileFilter() {
        public boolean accept(File f) {
            return f.getName().toLowerCase().endsWith(".xml") || f.isDirectory();
        }

        public String getDescription() {
            return "XML files";
        }
    });

    // set up menus
    JMenuBar menuBar = new JMenuBar();
    setJMenuBar(menuBar);
    JMenu menu = new JMenu("File");
    menuBar.add(menu);

    JMenuItem exportItem = new JMenuItem("Export preferences");
    menu.add(exportItem);
    exportItem.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent event) {
            if (chooser.showSaveDialog(PreferencesFrame.this) == JFileChooser.APPROVE_OPTION) {
                try {
                    OutputStream out = new FileOutputStream(chooser.getSelectedFile());
                    node.exportSubtree(out);
                    out.close();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }
    });

    JMenuItem importItem = new JMenuItem("Import preferences");
    menu.add(importItem);
    importItem.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent event) {
            if (chooser.showOpenDialog(PreferencesFrame.this) == JFileChooser.APPROVE_OPTION) {
                try {
                    InputStream in = new FileInputStream(chooser.getSelectedFile());
                    Preferences.importPreferences(in);
                    in.close();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }
    });

    JMenuItem exitItem = new JMenuItem("Exit");
    menu.add(exitItem);
    exitItem.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent event) {
            node.putInt("left", getX());
            node.putInt("top", getY());
            node.putInt("width", getWidth());
            node.putInt("height", getHeight());
            node.put("title", getTitle());
            System.exit(0);
        }
    });
}

From source file:net.sourceforge.metware.binche.loader.OfficialChEBIOboLoader.java

/**
 * The constructor loads the OBO file from the ChEBI ftp and executes the reasoning steps.
 *
 * @throws IOException//from   w  ww .ja  v  a  2s.com
 * @throws BackingStoreException
 */
public OfficialChEBIOboLoader() throws IOException, BackingStoreException {
    Preferences binchePrefs = Preferences.userNodeForPackage(BiNChe.class);

    if (binchePrefs.keys().length == 0) {
        binchePrefs = (new DefaultPreferenceSetter()).getDefaultSetPrefs();
    }

    PreProcessOboFile ppof = new PreProcessOboFile();
    File tmpFileObo = File.createTempFile("BiNChE", ".obo");
    FileUtils.copyURLToFile(new URL(oboURL), tmpFileObo);
    ppof.getTransitiveClosure(tmpFileObo.getAbsolutePath(),
            binchePrefs.get(BiNChEOntologyPrefs.RoleOntology.name(), null), false, true,
            BiNChEOntologyPrefs.RoleOntology.getRootChEBIEntries(), Arrays.asList("rdfs:label"),
            new ArrayList<String>());
    File tmpRoleOnt = new File(binchePrefs.get(BiNChEOntologyPrefs.RoleOntology.name(), null) + ".temp");
    tmpRoleOnt.delete();

    ppof.getTransitiveClosure(tmpFileObo.getAbsolutePath(),
            binchePrefs.get(BiNChEOntologyPrefs.StructureOntology.name(), null), false, false,
            BiNChEOntologyPrefs.StructureOntology.getRootChEBIEntries(), Arrays.asList("rdfs:label", "InChI"),
            new ArrayList<String>());
    File tmpStructOnt = new File(binchePrefs.get(BiNChEOntologyPrefs.StructureOntology.name(), null) + ".temp");
    tmpStructOnt.delete();

    ppof.getTransitiveClosure(tmpFileObo.getAbsolutePath(),
            binchePrefs.get(BiNChEOntologyPrefs.RoleAndStructOntology.name(), null), true, true,
            BiNChEOntologyPrefs.RoleAndStructOntology.getRootChEBIEntries(),
            Arrays.asList("rdfs:label", "InChI"), Arrays.asList("http://purl.obolibrary.org/obo/RO_0000087"));
    File tmpStructRoleOnt = new File(
            binchePrefs.get(BiNChEOntologyPrefs.RoleAndStructOntology.name(), null) + ".temp");
    tmpStructRoleOnt.delete();
    File structRoleAnnot = new File(
            binchePrefs.get(BiNChEOntologyPrefs.RoleAndStructOntology.name(), null).replace(".obo", ".txt"));
    String fullPath = binchePrefs.get(BiNChEOntologyPrefs.RoleAndStructOntology.name(), null);
    structRoleAnnot.renameTo(new File(
            fullPath.substring(0, fullPath.lastIndexOf(File.separator)) + File.separator + "chebi_roles.anno"));

    tmpFileObo.delete();
}

From source file:com.vaadin.testbench.tools.CvalChecker.java

private CvalInfo getCachedLicenseInfo(String productName) {
    Preferences p = Preferences.userNodeForPackage(CvalInfo.class);
    String json = p.get(productName, "");
    if (!json.isEmpty()) {
        CvalInfo info = parseJson(json);
        if (info != null) {
            return info;
        }//from   w  w  w .  j  a  va 2 s. c o  m
    }
    return null;
}

From source file:verdandi.ui.ProjectViewerPanel.java

private void importProjects() {
    Preferences prefs = Preferences.userNodeForPackage(getClass());
    File importDir = new File(prefs.get("import.dir", System.getProperty("user.home")));

    JFileChooser chooser = new JFileChooser(importDir);
    chooser.setDialogTitle(RB.getString("projectviewer.import.filechooser.title"));
    chooser.setMultiSelectionEnabled(false);

    if (chooser.showOpenDialog(this) != JFileChooser.APPROVE_OPTION) {
        LOG.debug("User cancelled");
        return;/*www. j a  v  a 2  s.c  o  m*/
    }

    File importFile = chooser.getSelectedFile();

    prefs.put("import.dir", importFile.getParent());
    try {
        prefs.flush();
    } catch (BackingStoreException e) {
        LOG.error("Cannot write export file preference", e);
    }

    ObjectInputStream projectsIn = null;

    try {
        setCursor(CURSOR_WAIT);
        projectsIn = new ObjectInputStream(new FileInputStream(importFile));

        Object o = projectsIn.readObject();

        if (!(o instanceof Collection)) {
            LOG.error("The file does not contain a valid verdandi project list");
            return;
        }

        Collection<?> importcoll = (Collection<?>) o;
        @SuppressWarnings("unchecked")
        List<CostUnit> projects = (List<CostUnit>) o;
        new ProjectImporter(projects).start();
    } catch (FileNotFoundException e) {
        LOG.error("", e);
    } catch (IOException e) {
        LOG.error("No verdandi project list?", e);
    } catch (ClassNotFoundException e) {
        LOG.error("", e);
    } finally {
        setCursor(CURSOR_DEFAULT);
        if (projectsIn != null) {
            try {
                projectsIn.close();
            } catch (Throwable t) {
                LOG.error("Cannot close stream: ", t);
            }
        }
    }

}

From source file:verdandi.ui.ProjectViewerPanel.java

private void exportProjects() {
    Preferences prefs = Preferences.userNodeForPackage(getClass());

    File exportDir = new File(prefs.get("export.dir", System.getProperty("user.home")));

    JFileChooser chooser = new JFileChooser(exportDir);
    chooser.setDialogTitle(RB.getString("projectviewer.export.filechooser.title"));
    chooser.setMultiSelectionEnabled(false);

    if (chooser.showSaveDialog(this) != JFileChooser.APPROVE_OPTION) {
        LOG.debug("User cancelled");
        return;/*from ww  w  .  j  a v a 2s  .  c  o  m*/
    }

    int[] selectedProjects = projectTable.getSelectedRows();

    if (selectedProjects.length == 0) {
        LOG.debug("NO row selected");
        return;
    }

    List<CostUnit> projectsToExport = new ArrayList<CostUnit>();

    for (int i = 0; i < selectedProjects.length; i++) {
        // int selModel = projectTable.getFilters().convertRowIndexToModel(
        // selectedProjects[i]);
        int selModel = selectedProjects[i];
        CostUnit p = tableModel.getProject(selModel);
        LOG.debug("Adding project to export list: " + p);
        projectsToExport.add(p);
    }

    File exportFile = chooser.getSelectedFile();
    LOG.debug("Exporting projects to " + exportFile.getAbsolutePath());

    ObjectOutputStream listOut = null;
    try {
        listOut = new ObjectOutputStream(new FileOutputStream(exportFile));
        listOut.writeObject(projectsToExport);
    } catch (FileNotFoundException e) {
        LOG.error("", e);
    } catch (IOException e) {
        LOG.error("", e);
    } finally {
        if (listOut != null) {
            try {
                listOut.close();
            } catch (Throwable t) {
                LOG.error("Cannot close stream: ", t);
            }
        }
    }

    prefs.put("export.dir", exportFile.getParent());
    try {
        prefs.flush();
    } catch (BackingStoreException e) {
        LOG.error("Cannot write export file preference", e);
    }

}

From source file:com.igormaznitsa.nbmindmap.nb.swing.PlainTextEditor.java

public PlainTextEditor(final String text) {
    initComponents();//from   w  w w .jav  a 2 s .  co m

    final JEditorPane editor = UI_COMPO_FACTORY.makeEditorPane();
    editor.setEditorKit(getEditorKit());
    this.document = Utilities.getDocument(editor);

    setText(text);

    final Preferences docPreferences = CodeStylePreferences.get(this.document).getPreferences();
    this.oldWrapping = Wrapping.findFor(docPreferences.get(SimpleValueNames.TEXT_LINE_WRAP, "none"));
    this.wrapping = oldWrapping;

    this.lastComponent = makeEditorForText(this.document);
    this.lastComponent.setPreferredSize(new Dimension(620, 440));
    this.add(this.lastComponent, BorderLayout.CENTER);

    this.labelWrapMode.setMinimumSize(new Dimension(55, this.labelWrapMode.getMinimumSize().height));

    updateBottomPanel();
}

From source file:com.trifork.riak.RiakClient.java

/**
 * helper method to use a reasonable default client id
 * /*  w w w. ja v  a2  s. c  o m*/
 * @throws IOException
 */
public void prepareClientID() throws IOException {
    Preferences prefs = Preferences.userNodeForPackage(RiakClient.class);

    String clid = prefs.get("client_id", null);
    if (clid == null) {
        SecureRandom sr;
        try {
            sr = SecureRandom.getInstance("SHA1PRNG");
        } catch (NoSuchAlgorithmException e) {
            throw new RuntimeException(e);
        }
        byte[] data = new byte[6];
        sr.nextBytes(data);
        clid = Base64Coder.encodeLines(data);
        prefs.put("client_id", clid);
        try {
            prefs.flush();
        } catch (BackingStoreException e) {
            throw new IOException(e);
        }
    }

    setClientID(clid);
}

From source file:main.UIController.java

public Time calculateSunsetForActualLocation(Calendar calendar, boolean useCacheSunset) {
    Preferences data = this.getCurrentPreferences();
    String city = data.get(FIELD_CITY, DEF_CITY);
    String country = data.get(FIELD_COUNTRY, DEF_COUNTRY);
    Time time = this.calculateSunset(calendar, city, country, useCacheSunset);
    return time;/*w  w w .ja v  a  2  s .c o  m*/
}

From source file:main.UIController.java

private UIController() {
    LangManager langManager = LangManager.getInstance();
    Preferences data = this.getCurrentPreferences();
    String shortName = data.get(FIELD_LANG, DEF_LANG);
    if (!langManager.defineLang(shortName)) {
        langManager.defineLang(DEF_LANG);
    }/*  w  w w .jav  a2  s . c o m*/
    this.setLangManager(langManager);
}

From source file:ome.formats.importer.util.IniFileLoader.java

/**
 * Parse Flex reader server maps/* w  w w  .j  a  v a2  s  . co  m*/
 *
 * @param maps
 * @return
 */
public Map<String, List<String>> parseFlexMaps(Preferences maps) {
    Map<String, List<String>> rv = new HashMap<String, List<String>>();
    try {
        for (String key : maps.keys()) {
            String mapValues = maps.get(key, null);
            log.info("Raw Flex reader map values: " + mapValues);
            if (mapValues == null) {
                continue;
            }
            List<String> list = new ArrayList<String>();
            rv.put(key, list);
            for (String value : mapValues.split(";")) {
                value = value.trim();
                list.add(value);
            }
        }
    } catch (BackingStoreException e) {
        log.warn("Error updating Flex reader server maps.", e);
    }
    return rv;
}