Example usage for java.util.prefs Preferences getInt

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

Introduction

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

Prototype

public abstract int getInt(String key, int def);

Source Link

Document

Returns the int value represented by the string associated with the specified key in this preference node.

Usage

From source file:gridrover.GridRover.java

/**
* This method does the work of getting our application started.
*
* @param args Command line arguments/*from  w  ww  .j av  a 2 s. co m*/
*/
public static void main(String[] args) {
    System.out.println("GridRover Copyright (C) 2008-2009 Lucas Adam M. Paul");
    System.out.println("This program comes with ABSOLUTELY NO WARRANTY; for details see LICENSE.TXT.");
    System.out.println("This is free software, and you are welcome to redistribute it");
    System.out.println("under certain conditions; see LICENSE.TXT for details.\n");

    log.info("Loading preferences...");
    Preferences prefs = Preferences.userNodeForPackage(GridRover.class);
    int width = prefs.getInt(MAP_WIDTH, 10);
    int length = prefs.getInt(MAP_HEIGHT, 10);
    double maxElevation = prefs.getDouble(MAX_ELEVATION, 25.0);
    int precision = prefs.getInt(ELEVATION_PRECISION, 2);

    log.info("Loading data files...");

    XmlFileParser fileParser = new XmlFileParser(new ResourceLocater(null));
    List<Spectrum> spectra = fileParser.getSpectra("spectrum_types.xml");
    log.debug("Checking we got our spectra.");
    for (Spectrum spec : spectra) {
        log.debug("Type: " + spec.getName());
        List<String> list = spec.getColors();
        for (String color : list)
            log.debug("\tColor: " + color);
        list = spec.getShapes();
        for (String shape : list)
            log.debug("\tShape: " + shape);
    }
    //List<Thing> itemPrototypes = fileParser.getThings("physical_objects.xml");
    List<ThingBean> itemPrototypes = fileParser.getThings("physical_objects.xml", spectra);
    log.debug("Checking we got our Things.");
    for (ThingBean tb : itemPrototypes) {
        log.debug("Name: " + tb.getName());
        log.debug("Mass: " + tb.getMinMass() + " - " + tb.getMaxMass());
        log.debug("Density: " + tb.getMinDensity() + " - " + tb.getMaxDensity());
        List<AppearanceBean> appearance = tb.getAppearanceBeans();
        for (AppearanceBean ab : appearance) {
            log.debug("Under the " + ab.getStimulus() + " spectrum, " + tb.getName() + " reacts with:");
            List<ResponseBean> responses = ab.getResponseBeans();
            for (ResponseBean rb : responses) {
                log.debug(rb.getSpectrumAction().toString() + " " + rb.getColor() + " " + rb.getShape()
                        + " in spectrum " + rb.getSpectrum());
            }
        }
    }

    log.info("Initializing GridRover...");
    GameEngine engine = new GameEngine(width, length, maxElevation, precision);
    Rover rover = new Rover("Rover", 185.0, 5.52, 100.0, new CommandlineRoverControl()); // Mass 185.0 kg, 1.5 meters tall by 2.3 meters wide by 1.6 meters long
    engine.addRover(rover, width / 2, length / 2); // Add a single rover in the middle of the map
    engine.addAmbientLighting(spectra.get(0));
    engine.scatterItemsRandomly(itemPrototypes, 0.5, 5); // 50% chance of items in a given square, up to 5 items per square

    log.info("Running GridRover...");
    engine.eventLoop();
    System.out.println("All events completed.  GridRover now terminating.");
}

From source file:Main.java

public static void main(String[] args) throws Exception {
    Preferences prefs = Preferences.userNodeForPackage(Main.class);
    prefs.put("key1", "value1");
    prefs.put("key2", "value2");
    prefs.putInt("intValue", 4);
    prefs.putBoolean("booleanValue", true);
    int usageCount = prefs.getInt("intValue", 0);
    usageCount++;/*  ww  w .ja v a  2 s. c  om*/
    prefs.putInt("UsageCount", usageCount);
    Iterator it = Arrays.asList(prefs.keys()).iterator();
    while (it.hasNext()) {
        String key = it.next().toString();
        System.out.println(key + ": " + prefs.get(key, null));
    }
    System.out.println(prefs.getInt("booleanValue", 0));
}

From source file:MainClass.java

public static void main(String[] args) throws Exception {
    Preferences prefs = Preferences.userNodeForPackage(MainClass.class);
    prefs.put("key1", "value1");
    prefs.put("key2", "value2");
    prefs.putInt("intValue", 4);
    prefs.putBoolean("booleanValue", true);
    int usageCount = prefs.getInt("intValue", 0);
    usageCount++;/*from w  w  w .  ja v a  2s .co  m*/
    prefs.putInt("UsageCount", usageCount);
    Iterator it = Arrays.asList(prefs.keys()).iterator();
    while (it.hasNext()) {
        String key = it.next().toString();
        System.out.println(key + ": " + prefs.get(key, null));
    }
    System.out.println(prefs.getInt("booleanValue", 0));
}

From source file:PreferencesDemo.java

public static void main(String[] args) throws Exception {
    Preferences prefs = Preferences.userNodeForPackage(PreferencesDemo.class);
    prefs.put("Location", "Oz");
    prefs.put("Footwear", "Ruby Slippers");
    prefs.putInt("Companions", 4);
    prefs.putBoolean("Are there witches?", true);
    int usageCount = prefs.getInt("UsageCount", 0);
    usageCount++;//from w  w w .j  av a2 s . co  m
    prefs.putInt("UsageCount", usageCount);
    Iterator it = Arrays.asList(prefs.keys()).iterator();
    while (it.hasNext()) {
        String key = it.next().toString();
        System.out.println(key + ": " + prefs.get(key, null));
    }
    // You must always provide a default value:
    System.out.println("How many companions does Dorothy have? " + prefs.getInt("Companions", 0));
}

From source file:PlanetPrefs.java

public static void main(String args[]) {
    String names[] = { "Mercury", "Venus", "Earth", "Mars", "Jupiter", "Saturn", "Uranus", "Neptune", "Pluto" };
    int moons[] = { 0, 0, 1, 2, 16, 18, 21, 8, 1 };

    Preferences prefs = Preferences.userRoot().node("/MasteringJava/Chap17");

    for (int i = 0, n = names.length; i < n; i++) {
        prefs.putInt(names[i], moons[i]);
    }//from  w w w.  jav a  2  s  . c  o  m

    try {
        String keys[] = prefs.keys();
        for (int i = 0, n = keys.length; i < n; i++) {
            System.out.println(keys[i] + ": " + prefs.getInt(keys[i], 0));
        }
    } catch (BackingStoreException e) {
        System.err.println("Unable to read backing store: " + e);
    }
}

From source file:Main.java

public static void main(String[] argv) throws Exception {
    Preferences prefs = Preferences.userNodeForPackage(Main.class);

    // Preference key name
    final String PREF_NAME = "name_of_preference";

    // Save/*from  w w  w  .j  a va 2  s.c  om*/
    prefs.put(PREF_NAME, "a string"); // String
    prefs.putBoolean(PREF_NAME, true); // boolean
    prefs.putInt(PREF_NAME, 123); // int
    prefs.putLong(PREF_NAME, 123L); // long
    prefs.putFloat(PREF_NAME, 12.3F); // float
    prefs.putDouble(PREF_NAME, 12.3); // double
    byte[] bytes = new byte[1024];
    prefs.putByteArray(PREF_NAME, bytes); // byte[]

    // Retrieve
    String s = prefs.get(PREF_NAME, "a string"); // String
    boolean b = prefs.getBoolean(PREF_NAME, true); // boolean
    int i = prefs.getInt(PREF_NAME, 123); // int
    long l = prefs.getLong(PREF_NAME, 123L); // long
    float f = prefs.getFloat(PREF_NAME, 12.3F); // float
    double d = prefs.getDouble(PREF_NAME, 12.3); // double
    bytes = prefs.getByteArray(PREF_NAME, bytes); // byte[]
}

From source file:au.org.ala.delta.editor.EditorPreferences.java

public static int getViewerDividerOffset() {
    Preferences prefs = Preferences.userNodeForPackage(DeltaEditor.class);
    if (prefs != null) {
        return prefs.getInt(VIEWER_DIVIDER_OFFSET_KEY, DEFAULT_VIEWER_DIVIDER_OFFSET);

    }/*w w  w.j a  v a  2 s  .co  m*/
    return DEFAULT_VIEWER_DIVIDER_OFFSET;
}

From source file:Main.java

public static void centerFrame(JFrame frame, Preferences prefs, int defWidth, int defHeight) {
    Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
    int w = (int) ((screenSize.getWidth() / 100) * defWidth);
    int h = (int) ((screenSize.getHeight() / 100) * defHeight);
    int x = (int) ((screenSize.getWidth() - w) / 2);
    int y = (int) ((screenSize.getHeight() - h) / 2);
    if (prefs != null) {
        frame.setBounds(prefs.getInt("x", x), prefs.getInt("y", y), prefs.getInt("w", w), prefs.getInt("h", h));
    } else {/*ww  w.j av  a 2 s  . c  o  m*/
        frame.setBounds(x, y, w, h);
    }
}

From source file:edu.umd.cs.findbugs.gui2.GUISaveState.java

public static void loadInstance() {
    GUISaveState newInstance = new GUISaveState();
    newInstance.recentFiles = new ArrayList<File>();
    Preferences p = Preferences.userNodeForPackage(GUISaveState.class);

    newInstance.tabSize = p.getInt(TAB_SIZE, 4);

    newInstance.fontSize = p.getFloat(FONT_SIZE, 12.0f);

    newInstance.starterDirectoryForLoadBugs = new File(
            p.get(GUISaveState.STARTERDIRECTORY, SystemProperties.getProperty("user.dir")));

    int prevCommentsSize = p.getInt(GUISaveState.PREVCOMMENTSSIZE, 0);

    for (int x = 0; x < prevCommentsSize; x++) {
        String comment = p.get(GUISaveState.COMMENTKEYS[x], "");
        newInstance.previousComments.add(comment);
    }/*  w  w w  . j  a  v a 2  s . c o  m*/

    int size = Math.min(MAXNUMRECENTPROJECTS, p.getInt(GUISaveState.NUMPROJECTS, 0));
    for (int x = 0; x < size; x++) {
        newInstance.addRecentFile(new File(p.get(GUISaveState.RECENTPROJECTKEYS[x], "")));
    }

    int sorterSize = p.getInt(GUISaveState.SORTERTABLELENGTH, -1);
    if (sorterSize != -1) {
        ArrayList<Sortables> sortColumns = new ArrayList<Sortables>();
        String[] sortKeys = GUISaveState.generateSorterKeys(sorterSize);
        for (int x = 0; x < sorterSize; x++) {
            Sortables s = Sortables.getSortableByPrettyName(p.get(sortKeys[x], "*none*"));

            if (s == null) {
                if (MainFrame.GUI2_DEBUG) {
                    System.err.println("Sort order was corrupted, using default sort order");
                }
                newInstance.useDefault = true;
                break;
            }
            sortColumns.add(s);
        }
        if (!newInstance.useDefault) {
            // add in default columns
            Set<Sortables> missingSortColumns = new HashSet<Sortables>(Arrays.asList(DEFAULT_COLUMN_HEADERS));
            missingSortColumns.removeAll(sortColumns);
            sortColumns.addAll(missingSortColumns);
            newInstance.sortColumns = sortColumns.toArray(new Sortables[sortColumns.size()]);
        }
    } else {
        newInstance.useDefault = true;
    }

    newInstance.dockingLayout = p.getByteArray(DOCKINGLAYOUT, new byte[0]);

    String boundsString = p.get(FRAME_BOUNDS, null);
    Rectangle r = new Rectangle(0, 0, 800, 650);
    if (boundsString != null) {
        String[] a = boundsString.split(",", 4);
        if (a.length > 0) {
            try {
                r.x = Math.max(0, Integer.parseInt(a[0]));
            } catch (NumberFormatException nfe) {
                assert true;
            }
        }
        if (a.length > 1) {
            try {
                r.y = Math.max(0, Integer.parseInt(a[1]));
            } catch (NumberFormatException nfe) {
                assert true;
            }
        }
        if (a.length > 2) {
            try {
                r.width = Math.max(40, Integer.parseInt(a[2]));
            } catch (NumberFormatException nfe) {
                assert true;
            }
        }
        if (a.length > 3) {
            try {
                r.height = Math.max(40, Integer.parseInt(a[3]));
            } catch (NumberFormatException nfe) {
                assert true;
            }
        }
    }
    newInstance.frameBounds = r;
    newInstance.extendedWindowState = p.getInt(EXTENDED_WINDOW_STATE, Frame.NORMAL);

    newInstance.splitMain = p.getInt(SPLIT_MAIN, 400);
    newInstance.splitSummary = p.getInt(SPLIT_SUMMARY_NEW, 400);
    newInstance.splitTop = p.getInt(SPLIT_TOP, -1);
    newInstance.splitTreeComments = p.getInt(SPLIT_TREE_COMMENTS, 250);
    newInstance.packagePrefixSegments = p.getInt(PACKAGE_PREFIX_SEGEMENTS, 3);

    String plugins = p.get(CUSTOM_PLUGINS, "");
    if (plugins.length() > 0) {
        for (String s : plugins.split(" ")) {
            try {
                URI u = new URI(s);
                Plugin.addCustomPlugin(u);
                newInstance.customPlugins.add(u);
            } catch (PluginException e) {
                assert true;
            } catch (URISyntaxException e) {
                assert true;
            }
        }
    }

    String enabledPluginsString = p.get(ENABLED_PLUGINS, "");
    String disabledPluginsString = p.get(DISABLED_PLUGINS, "");
    newInstance.enabledPlugins = new ArrayList<String>(Arrays.asList(enabledPluginsString.split(",")));
    newInstance.disabledPlugins = new ArrayList<String>(Arrays.asList(disabledPluginsString.split(",")));

    instance = newInstance;
}

From source file:org.griphyn.vdl.karajan.monitor.monitors.swing.GraphPanel.java

public static GraphPanel load(Preferences p, SystemState state, GraphsPanel gps) {
    DataSampler sampler = (DataSampler) state.getItemByID(DataSampler.ID, StatefulItemClass.WORKFLOW);
    GraphPanel g = new GraphPanel(state, gps);
    int ec = p.getInt("enabledCount", 0);
    for (int i = 0; i < ec; i++) {
        Preferences gp = p.node("series" + i);
        String key = gp.get("key", null);
        if (key == null) {
            throw new RuntimeException("Null series key");
        }/*from   w ww .j  a  va  2s  . co  m*/
        int cr = gp.getInt("color.r", 255);
        int cg = gp.getInt("color.g", 0);
        int cb = gp.getInt("color.b", 0);
        g.enable(sampler.getSeries(key), false);
        g.setColor(key, new Color(cr, cg, cb));
    }
    return g;
}