Example usage for java.util.prefs Preferences sync

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

Introduction

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

Prototype

public abstract void sync() throws BackingStoreException;

Source Link

Document

Ensures that future reads from this preference node and its descendants reflect any changes that were committed to the persistent store (from any VM) prior to the sync invocation.

Usage

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

/**
 * Removes the specified file from the most recently used file list
 * @param filename The filename to remove
 *///ww  w.  j  a va 2 s  .c o m
public static void removeFileFromMRU(String filename) {

    String[] existingFiles = getPreviouslyUsedFiles();

    StringBuilder b = new StringBuilder();
    for (int i = 0; i < existingFiles.length; ++i) {

        if (!existingFiles[i].equalsIgnoreCase(filename)) {

            if (b.length() > 0) {
                b.append(MRU_SEPARATOR);
            }
            b.append(existingFiles[i]);
        }
    }

    Preferences prefs = Preferences.userNodeForPackage(DeltaEditor.class);
    prefs.put(MRU_PREF_KEY, b.toString());
    try {
        prefs.sync();
    } catch (BackingStoreException e) {
        throw new RuntimeException(e);
    }

}

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

/**
 * Adds the supplied filename to the top of the most recently used files.
 * //w w w  .j  a v  a  2s .  c om
 * @param filename
 */
public static void addFileToMRU(String filename) {

    Queue<String> q = new LinkedList<String>();

    q.add(filename);

    String[] existingFiles = getPreviouslyUsedFiles();
    if (existingFiles != null) {
        for (String existingFile : existingFiles) {
            if (!q.contains(existingFile)) {
                q.add(existingFile);
            }
        }
    }

    StringBuilder b = new StringBuilder();
    for (int i = 0; i < MAX_SIZE_MRU && q.size() > 0; ++i) {
        if (i > 0) {
            b.append(MRU_SEPARATOR);
        }
        b.append(q.poll());
    }

    Preferences prefs = Preferences.userNodeForPackage(DeltaEditor.class);
    prefs.put(MRU_PREF_KEY, b.toString());
    try {
        prefs.sync();
    } catch (BackingStoreException e) {
        throw new RuntimeException(e);
    }
}

From source file:au.org.ala.delta.intkey.ui.UIUtils.java

/**
 * Removes the specified file from the most recently used file list
 * //from   ww w  .  j  a  v  a2s.  c o  m
 * @param filename
 *            The filename to remove
 */
public static void removeFileFromMRU(String filename) {

    List<Pair<String, String>> existingFiles = getPreviouslyUsedFiles();

    StringBuilder b = new StringBuilder();
    for (int i = 0; i < existingFiles.size(); ++i) {

        Pair<String, String> fileNameTitlePair = existingFiles.get(i);
        String existingFileName = fileNameTitlePair.getFirst();

        if (!existingFileName.equalsIgnoreCase(filename)) {

            if (b.length() > 0) {
                b.append(MRU_FILES_SEPARATOR);
            }
            b.append(fileNameTitlePair.getFirst() + MRU_ITEM_SEPARATOR + fileNameTitlePair.getSecond());
        }
    }

    Preferences prefs = Preferences.userNodeForPackage(Intkey.class);
    prefs.put(MRU_FILES_PREF_KEY, b.toString());
    try {
        prefs.sync();
    } catch (BackingStoreException e) {
        throw new RuntimeException(e);
    }

}

From source file:au.org.ala.delta.intkey.ui.UIUtils.java

/**
 * Adds the supplied filename to the top of the most recently used files.
 * // w  w w  .j a  va  2 s  . co m
 * @param filename
 */
public static void addFileToMRU(String filename, String title, List<Pair<String, String>> existingFiles) {
    // Strip any RTF formatting, and characters used as separators in the MRU text from the title.
    title = RTFUtils.stripFormatting(title);
    title = title.replace(MRU_ITEM_SEPARATOR, " ");
    title = title.replace(MRU_FILES_SEPARATOR, " ");

    Queue<String> q = new LinkedList<String>();

    String newFilePathAndTitle;
    if (StringUtils.isEmpty(title)) {
        newFilePathAndTitle = filename + MRU_ITEM_SEPARATOR + filename;
    } else {
        newFilePathAndTitle = filename + MRU_ITEM_SEPARATOR + title;
    }
    q.add(newFilePathAndTitle);

    if (existingFiles != null) {
        for (Pair<String, String> existingFile : existingFiles) {
            String existingFilePathAndTitle = existingFile.getFirst() + MRU_ITEM_SEPARATOR
                    + existingFile.getSecond();
            if (!q.contains(existingFilePathAndTitle)) {
                q.add(existingFilePathAndTitle);
            }
        }
    }

    StringBuilder b = new StringBuilder();
    for (int i = 0; i < MAX_SIZE_MRU && q.size() > 0; ++i) {
        if (i > 0) {
            b.append(MRU_FILES_SEPARATOR);
        }
        b.append(q.poll());
    }

    Preferences prefs = Preferences.userNodeForPackage(Intkey.class);
    prefs.put(MRU_FILES_PREF_KEY, b.toString());
    try {
        prefs.sync();
    } catch (BackingStoreException e) {
        throw new RuntimeException(e);
    }
}

From source file:au.org.ala.delta.intkey.ui.UIUtils.java

public static void setPreferredLookAndFeel(String lookAndFeelName) {
    Preferences prefs = Preferences.userNodeForPackage(Intkey.class);
    if (prefs != null) {
        prefs.put(LOOK_AND_FEEL_KEY, lookAndFeelName);
        try {/*from www  .  ja v  a  2 s  .c  om*/
            prefs.sync();
        } catch (BackingStoreException e) {
            throw new RuntimeException(e);
        }
    }
}

From source file:au.org.ala.delta.intkey.ui.UIUtils.java

/**
 * Save the mode in which the application was last used before shutdown -
 * advanced or basic - to the preferences
 * //w  w  w .ja  va  2s  .  co  m
 * @param advancedMode
 *            true if application was last used in advanced mode
 */
public static void savePreviousApplicationMode(boolean advancedMode) {
    Preferences prefs = Preferences.userNodeForPackage(Intkey.class);
    prefs.put(MODE_PREF_KEY, advancedMode ? ADVANCED_MODE_PREF_VALUE : BASIC_MODE_PREF_VALUE);
    try {
        prefs.sync();
    } catch (BackingStoreException e) {
        throw new RuntimeException(e);
    }
}

From source file:au.org.ala.delta.intkey.ui.UIUtils.java

/**
 * Save the parent directory for the most recently opened dataset to
 * preferences/*from www  .  ja  v a  2 s.  com*/
 * 
 * @param lastOpenedDatasetDirectory
 */
public static void saveLastOpenedDatasetDirectory(File lastOpenedDatasetDirectory) {
    Preferences prefs = Preferences.userNodeForPackage(Intkey.class);
    prefs.put(LAST_OPENED_DATASET_LOCATION_PREF_KEY, lastOpenedDatasetDirectory.getAbsolutePath());
    try {
        prefs.sync();
    } catch (BackingStoreException e) {
        throw new RuntimeException(e);
    }
}

From source file:au.org.ala.delta.intkey.ui.UIUtils.java

/**
 * Save the dataset index to preferences.
 * //w w  w . j a  va2  s  .c o m
 * @param datasetIndexList
 *            The dataset index - a list of dataset description, dataset
 *            path value pairs
 */
public static void writeDatasetIndex(List<Pair<String, String>> datasetIndexList) {
    Preferences prefs = Preferences.userNodeForPackage(Intkey.class);

    List<List<String>> listToSerialize = new ArrayList<List<String>>();

    for (Pair<String, String> datasetInfoPair : datasetIndexList) {
        listToSerialize.add(Arrays.asList(datasetInfoPair.getFirst(), datasetInfoPair.getSecond()));
    }

    String jsonList = JSONSerializer.toJSON(listToSerialize).toString();
    prefs.put(DATASET_INDEX_PREF_KEY, jsonList);
    try {
        prefs.sync();
    } catch (BackingStoreException e) {
        throw new RuntimeException(e);
    }
}

From source file:au.org.ala.delta.intkey.ui.UIUtils.java

public static List<Pair<String, String>> getPreviouslyUsedFiles() {
    List<Pair<String, String>> retList = new ArrayList<Pair<String, String>>();

    Preferences prefs = Preferences.userNodeForPackage(Intkey.class);
    if (prefs != null) {
        try {//  w w w. j av a2 s . c  o m
            String mru = prefs.get(MRU_FILES_PREF_KEY, "");
            if (!StringUtils.isEmpty(mru)) {
                String[] mruFiles = mru.split(MRU_FILES_SEPARATOR);
                for (String mruFile : mruFiles) {
                    String[] mruFileItems = mruFile.split(MRU_ITEM_SEPARATOR);
                    retList.add(new Pair<String, String>(mruFileItems[0], mruFileItems[1]));
                }
            }
        } catch (Exception e) {
            // An error occurred. Clear the most recently used files list and return an empty list.

            prefs.remove(MRU_FILES_PREF_KEY);
            try {
                prefs.sync();
            } catch (BackingStoreException bse) {
                throw new RuntimeException(bse);
            }
            return Collections.EMPTY_LIST;
        }
    }

    return retList;
}

From source file:com.github.fritaly.dualcommander.DualCommander.java

@Override
public void windowClosing(WindowEvent e) {
    try {// ww  w .ja  v a 2s  .c  o  m
        if (logger.isDebugEnabled()) {
            logger.debug("Saving preferences ...");
        }

        // Save the program state
        final Preferences prefs = Preferences.userNodeForPackage(this.getClass());

        this.leftPane.saveState(prefs.node("left.panel"));
        this.rightPane.saveState(prefs.node("right.panel"));
        this.preferences.saveState(prefs.node("user.preferences"));

        prefs.sync();

        if (logger.isInfoEnabled()) {
            logger.info("Saved preferences");
        }
    } catch (BackingStoreException e1) {
        // Not a big deal
    }
}