All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.jmisb.viewer.MruFileList Maven / Gradle / Ivy

package org.jmisb.viewer;

import java.util.ArrayList;
import java.util.List;
import java.util.prefs.Preferences;
import javax.swing.*;

/** Stores most-recently-used files */
class MruFileList {
    private static int MAX_ENTRIES = 4;

    private MruFileList() {}

    static List getList() {
        Preferences prefs = Preferences.userNodeForPackage(MruFileList.class);
        List filenames = getFilenames(prefs);

        List list = new ArrayList<>();
        for (String filename : filenames) {
            JMenuItem menuItem = new JMenuItem(filename);
            menuItem.setName("File|Mru|" + filename);
            list.add(menuItem);
        }
        return list;
    }

    static void add(String filename) {
        Preferences prefs = Preferences.userNodeForPackage(MruFileList.class);
        List currentList = getFilenames(prefs);

        prefs.put("mruFile0", filename);

        int i = 1;
        for (String mruFile : currentList) {
            if (!mruFile.equals(filename)) {
                prefs.put("mruFile" + i, mruFile);
                i++;
            }
            if (i > MAX_ENTRIES) {
                break;
            }
        }
    }

    private static List getFilenames(Preferences prefs) {
        List list = new ArrayList<>();

        for (int i = 0; i < MAX_ENTRIES; i++) {
            String mruFile = prefs.get("mruFile" + i, "");
            if (!mruFile.isEmpty()) {
                list.add(mruFile);
            }
        }
        return list;
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy