1 package net.sf.openrocket.gui.main;
3 import net.sf.openrocket.startup.Application;
4 import net.sf.openrocket.startup.Preferences;
6 import javax.swing.event.EventListenerList;
7 import java.beans.PropertyChangeEvent;
8 import java.beans.PropertyChangeListener;
9 import java.util.ArrayList;
10 import java.util.List;
13 * Implements a most-recently-used list backed by preferences. This is modified/adapted from an example on the
16 public class MRUDesignFile {
18 public static final String MRU_FILE_LIST_PROPERTY = "MRUFileList";
20 public static final int MAX_SIZE = 9;
22 private List<String> mruFileList;
24 private static MRUDesignFile instance; // The single instance
26 private EventListenerList listenerList;
29 instance = new MRUDesignFile();
33 * Returns the single instance, creating one if it's the first time this method is called.
35 * @return The single instance.
37 public static MRUDesignFile getInstance() {
44 protected MRUDesignFile() {
45 mruFileList = new ArrayList<String>(MAX_SIZE);
46 listenerList = new EventListenerList();
51 * Get the current most-recently-used list of design files.
53 * @return a list that contains absolute file names
55 public List<String> getMRUFileList() {
60 * Set the most-recently-used list to the given parameter and fire change events.
64 public void setMRUFileList(List<String> list) {
65 this.mruFileList.clear();
66 this.mruFileList.addAll(list.subList(0, Math.min(list.size(), MAX_SIZE)));
67 firePropertyChange(MRU_FILE_LIST_PROPERTY, null, mruFileList);
72 * Remove a file from the MRU list and fire change events.
74 * @param absolutePath the filename to be removed
76 public void removeFile(String absolutePath) {
77 mruFileList.remove(absolutePath);
78 firePropertyChange(MRU_FILE_LIST_PROPERTY, null, mruFileList);
83 * Add a file to the MRU list and fire change events.
85 * @param absolutePath the filename to be added
87 public void addFile(String absolutePath) {
89 mruFileList.remove(absolutePath);
92 mruFileList.add(0, absolutePath);
93 while (mruFileList.size() > MAX_SIZE) {
94 mruFileList.remove(mruFileList.size() - 1);
96 firePropertyChange(MRU_FILE_LIST_PROPERTY, null, mruFileList);
101 * Clear all existing MRU references in the preferences backing store.
103 private void clear() {
104 Preferences prefs = getPreferences();
106 for (int i = 0; i < MAX_SIZE; i++) {
107 prefs.putString(MRU_FILE_LIST_PROPERTY + i, null);
112 * Store the MRU list into the preferences backing store.
114 protected void store() {
115 Preferences prefs = getPreferences();
117 // clear the backing store
120 for (int i = 0; i < mruFileList.size(); i++) {
121 String str = mruFileList.get(i);
122 prefs.putString(MRU_FILE_LIST_PROPERTY + i, str);
127 * Load the MRU list from the backing store.
129 protected void retrieve() {
131 Preferences prefs = getPreferences();
133 for (int i = 0; i < MAX_SIZE; i++) {
134 String str = prefs.getString(MRU_FILE_LIST_PROPERTY + i, null);
136 mruFileList.add(str);
147 public void addPropertyChangeListener(PropertyChangeListener listener) {
148 listenerList.add(PropertyChangeListener.class, listener);
154 public void removePropertyChangeListener(PropertyChangeListener listener) {
155 listenerList.remove(PropertyChangeListener.class, listener);
158 protected void firePropertyChange(String propertyName, Object oldValue, Object newValue) {
159 // Guaranteed to return a non-null array
160 Object[] listeners = listenerList.getListenerList();
161 // Process the listeners last to first, notifying
162 // those that are interested in this event
163 PropertyChangeEvent event = new PropertyChangeEvent(this, propertyName, oldValue, newValue);
164 for (int i = listeners.length - 2; i >= 0; i -= 2) {
165 if (listeners[i] == PropertyChangeListener.class) {
166 ((PropertyChangeListener) listeners[i + 1]).propertyChange(event);
172 * Return the Preferences
174 * @return Preferences
176 protected final Preferences getPreferences() {
177 return Application.getPreferences();