Merge commit '42b2e5ca519766e37ce6941ba4faecc9691cc403' into upstream
[debian/openrocket] / core / src / net / sf / openrocket / gui / main / MRUDesignFileAction.java
diff --git a/core/src/net/sf/openrocket/gui/main/MRUDesignFileAction.java b/core/src/net/sf/openrocket/gui/main/MRUDesignFileAction.java
new file mode 100644 (file)
index 0000000..8757eaa
--- /dev/null
@@ -0,0 +1,86 @@
+package net.sf.openrocket.gui.main;
+
+import javax.swing.AbstractAction;
+import javax.swing.Action;
+import javax.swing.JMenu;
+import javax.swing.JMenuItem;
+import java.awt.Window;
+import java.awt.event.ActionEvent;
+import java.beans.PropertyChangeEvent;
+import java.beans.PropertyChangeListener;
+import java.io.File;
+import java.util.List;
+
+/**
+ * Implements a menu for the Most-Recently-Used Open Rocket design files.
+ */
+public final class MRUDesignFileAction extends JMenu {
+
+    /**
+     * The window to which an open design file action will be parented to (typically an instance of BasicFrame).
+     */
+    private Window parent;
+
+    /**
+     * Constructor.
+     *
+     * @param s         the I18N menu string
+     * @param theParent the window to which an open design file action will be parented to (typically an instance of
+     *                  BasicFrame).
+     */
+    public MRUDesignFileAction(String s, Window theParent) {
+        super(s);
+
+        parent = theParent;
+        MRUDesignFile opts = MRUDesignFile.getInstance();
+        opts.addPropertyChangeListener(new PropertyChangeListener() {
+            public void propertyChange(PropertyChangeEvent evt) {
+                if (!evt.getPropertyName().equals(MRUDesignFile.MRU_FILE_LIST_PROPERTY)) {
+                    return;
+                }
+                updateMenu();
+            }
+        });
+
+        updateMenu();
+    }
+
+    /**
+     * Create menu items.
+     */
+    private void updateMenu() {
+        removeAll();
+        List<String> list = MRUDesignFile.getInstance().getMRUFileList();
+        for (String name : list) {
+            Action action = createAction(name);
+            action.putValue(Action.NAME, name);
+            JMenuItem menuItem = new JMenuItem(action);
+            add(menuItem);
+        }
+    }
+
+    /**
+     * When a user clicks on one of the recently used design files, open it.
+     *
+     * @param file the design file name (absolute path)
+     *
+     * @return the action to open a design file
+     */
+    private Action createAction(String file) {
+        Action action = new AbstractAction() {
+            public void actionPerformed(ActionEvent e) {
+                String command = e.getActionCommand();
+                if (BasicFrame.open(new File(command), parent)) {
+                    MRUDesignFile.getInstance().addFile(command);
+                }
+                else {
+                    MRUDesignFile.getInstance().removeFile(command);
+                }
+            }
+        };
+
+        action.putValue(Action.ACTION_COMMAND_KEY, file);
+        return action;
+    }
+
+}
\ No newline at end of file