From 64ce98d5dae5ababb8eac2ecd70b4656b34fca68 Mon Sep 17 00:00:00 2001 From: Ben Fry Date: Thu, 13 Aug 2015 19:59:09 -0400 Subject: [PATCH] making Recent static removes half a dozen functions from Base --- app/src/processing/app/Base.java | 19 ++++++------ app/src/processing/app/Mode.java | 5 ++-- app/src/processing/app/Sketch.java | 7 +++-- app/src/processing/app/ui/Editor.java | 20 ++++++------- app/src/processing/app/ui/Recent.java | 43 +++++++++++++-------------- 5 files changed, 47 insertions(+), 47 deletions(-) diff --git a/app/src/processing/app/Base.java b/app/src/processing/app/Base.java index 8b26082db..9897313aa 100644 --- a/app/src/processing/app/Base.java +++ b/app/src/processing/app/Base.java @@ -97,7 +97,7 @@ public class Base { private JMenu sketchbookMenu; - private Recent recent; +// private Recent recent; // Used by handleOpen(), this saves the chooser to remember the directory. // Doesn't appear to be necessary with the AWT native file dialog. @@ -277,7 +277,8 @@ public class Base { // Needs to happen after the sketchbook folder has been located. // Also relies on the modes to be loaded so it knows what can be // marked as an example. - recent = new Recent(this); +// recent = new Recent(this); + Recent.init(this); String lastModeIdentifier = Preferences.get("mode.last"); //$NON-NLS-1$ if (lastModeIdentifier == null) { @@ -870,7 +871,7 @@ public class Base { if (tab.getFile().equals(file)) { editor.toFront(); // move back to the top of the recent list - handleRecent(editor); + Recent.handle(editor); return editor; } } @@ -898,7 +899,7 @@ public class Base { // opened successfully, let's go to work editor.getSketch().setUntitled(untitled); editors.add(editor); - handleRecent(editor); + Recent.handle(editor); // now that we're ready, show the window // (don't do earlier, cuz we might move it based on a window being closed) @@ -1034,7 +1035,7 @@ public class Base { } else { // on OS X, update the default file menu editor.setVisible(false); editor.dispose(); - defaultFileMenu.insert(getRecentMenu(), 2); + defaultFileMenu.insert(Recent.getMenu(), 2); activeEditor = null; editors.remove(editor); } @@ -1159,6 +1160,7 @@ public class Base { } + /* public JMenu getRecentMenu() { return recent.getMenu(); } @@ -1179,13 +1181,12 @@ public class Base { } - /** - * Called before a sketch is renamed so that its old name is - * no longer in the menu. - */ + // Called before a sketch is renamed so that its old name is + // no longer in the menu. public void removeRecent(Editor editor) { recent.remove(editor); } + */ /** diff --git a/app/src/processing/app/Mode.java b/app/src/processing/app/Mode.java index 5b66b5ea8..4ec5433a1 100644 --- a/app/src/processing/app/Mode.java +++ b/app/src/processing/app/Mode.java @@ -40,6 +40,7 @@ import processing.app.ui.Editor; import processing.app.ui.EditorException; import processing.app.ui.EditorState; import processing.app.ui.ExamplesFrame; +import processing.app.ui.Recent; import processing.app.ui.SketchbookFrame; import processing.app.ui.Toolkit; import processing.core.PApplet; @@ -374,13 +375,13 @@ public abstract class Mode { if (toolbarMenu == null) { rebuildToolbarMenu(); } else { - toolbarMenu.insert(base.getToolbarRecentMenu(), 1); + toolbarMenu.insert(Recent.getToolbarMenu(), 1); } } public void removeToolbarRecentMenu() { - toolbarMenu.remove(base.getToolbarRecentMenu()); + toolbarMenu.remove(Recent.getToolbarMenu()); } diff --git a/app/src/processing/app/Sketch.java b/app/src/processing/app/Sketch.java index ce11b61b2..7fdaa5161 100644 --- a/app/src/processing/app/Sketch.java +++ b/app/src/processing/app/Sketch.java @@ -25,6 +25,7 @@ package processing.app; import processing.app.ui.Editor; import processing.app.ui.ProgressFrame; +import processing.app.ui.Recent; import processing.app.ui.Toolkit; import processing.core.*; @@ -952,7 +953,7 @@ public class Sketch { // the Recent menu so that it's not sticking around after the rename. // If untitled, it won't be in the menu, so there's no point. if (!isUntitled()) { - editor.removeRecent(); + Recent.remove(editor); } // save the main tab with its new name @@ -965,7 +966,7 @@ public class Sketch { setUntitled(false); // Add this sketch back using the new name - editor.addRecent(); + Recent.handle(editor); // let Editor know that the save was successful return true; @@ -997,7 +998,7 @@ public class Sketch { // System.out.println("modified is now " + modified); editor.updateTitle(); editor.getBase().rebuildSketchbookMenus(); - editor.getBase().handleRecentRename(editor,oldPath); + Recent.handleRename(editor, oldPath); // editor.header.rebuild(); } diff --git a/app/src/processing/app/ui/Editor.java b/app/src/processing/app/ui/Editor.java index ba9e79154..0a520c5b7 100644 --- a/app/src/processing/app/ui/Editor.java +++ b/app/src/processing/app/ui/Editor.java @@ -182,7 +182,7 @@ public abstract class Editor extends JFrame implements RunnerListener { public void windowActivated(WindowEvent e) { base.handleActivated(Editor.this); - fileMenu.insert(base.getRecentMenu(), 2); + fileMenu.insert(Recent.getMenu(), 2); Toolkit.setMenuMnemsInside(fileMenu); //sketchMenu.insert(mode.getImportMenu(), 5); @@ -193,7 +193,7 @@ public abstract class Editor extends JFrame implements RunnerListener { } public void windowDeactivated(WindowEvent e) { - fileMenu.remove(base.getRecentMenu()); + fileMenu.remove(Recent.getMenu()); // JMenu importMenu = mode.getImportMenu(); // importIndex = sketchMenu.getComponentZOrder(mode.getImportMenu()); // sketchMenu.remove(mode.getImportMenu()); @@ -462,14 +462,14 @@ public abstract class Editor extends JFrame implements RunnerListener { } - public void removeRecent() { - base.removeRecent(this); - } - - - public void addRecent() { - base.handleRecent(this); - } +// public void removeRecent() { +// Recent.remove(this); +// } +// +// +// public void addRecent() { +// Recent.handle(this); +// } /** diff --git a/app/src/processing/app/ui/Recent.java b/app/src/processing/app/ui/Recent.java index 9c57731de..862ed702f 100644 --- a/app/src/processing/app/ui/Recent.java +++ b/app/src/processing/app/ui/Recent.java @@ -45,20 +45,17 @@ public class Recent { static final String FILENAME = "recent.txt"; static final String VERSION = "2"; - Base base; - File file; - /** How many recent sketches to remember. */ - int remember; - ArrayList records; + static Base base; + static File file; + static List records; /** actual menu used in the primary menu bar */ - JMenu mainMenu; + static JMenu mainMenu; /** copy of the menu to use in the toolbar */ - JMenu toolbarMenu; + static JMenu toolbarMenu; - public Recent(Base base) { - this.base = base; - remember = Preferences.getInteger("recent.count"); + static public void init(Base b) { + base = b; file = Base.getSettingsFile(FILENAME); mainMenu = new JMenu(Language.text("menu.file.recent")); toolbarMenu = new JMenu(Language.text("menu.file.open")); @@ -71,7 +68,7 @@ public class Recent { } - protected void load() throws IOException { + static protected void load() throws IOException { records = new ArrayList(); if (file.exists()) { BufferedReader reader = PApplet.createReader(file); @@ -86,7 +83,7 @@ public class Recent { if (new File(line).exists()) { // don't add ghost entries records.add(new Record(line)); } else { - Base.log(this, "ghost file: " + line); + Base.log("ghost file: " + line); } } } @@ -97,7 +94,7 @@ public class Recent { } - protected void save() { + static protected void save() { PrintWriter writer = PApplet.createWriter(file); writer.println(VERSION); for (Record record : records) { @@ -112,17 +109,17 @@ public class Recent { } - public JMenu getMenu() { + static public JMenu getMenu() { return mainMenu; } - public JMenu getToolbarMenu() { + static public JMenu getToolbarMenu() { return toolbarMenu; } - private void updateMenu(JMenu menu) { + static private void updateMenu(JMenu menu) { menu.removeAll(); String sketchbookPath = Base.getSketchbookFolder().getAbsolutePath(); for (Record rec : records) { @@ -131,7 +128,7 @@ public class Recent { } - private void updateMenuRecord(JMenu menu, final Record rec, String sketchbookPath) { + static private void updateMenuRecord(JMenu menu, final Record rec, String sketchbookPath) { try { String recPath = new File(rec.getPath()).getParent(); String purtyPath = null; @@ -231,7 +228,7 @@ public class Recent { } - synchronized public void remove(Editor editor) { + synchronized static public void remove(Editor editor) { int index = findRecord(editor.getSketch().getMainFilePath()); if (index != -1) { records.remove(index); @@ -278,12 +275,12 @@ public class Recent { * entry on the Recent queue. If the sketch is already in the list, it is * first removed so it doesn't show up multiple times. */ - synchronized public void handle(Editor editor) { + synchronized static public void handle(Editor editor) { if (!editor.getSketch().isUntitled()) { // If this sketch is already in the menu, remove it remove(editor); - if (records.size() == remember) { + if (records.size() == Preferences.getInteger("recent.count")) { records.remove(0); // remove the first entry } @@ -299,8 +296,8 @@ public class Recent { } //handles renaming done within processing - synchronized public void handleRename(Editor editor,String oldPath){ - if (records.size() == remember) { + synchronized static public void handleRename(Editor editor,String oldPath){ + if (records.size() == Preferences.getInteger("recent.count")) { records.remove(0); // remove the first entry } int index = findRecord(oldPath); @@ -313,7 +310,7 @@ public class Recent { } - int findRecord(String path) { + static int findRecord(String path) { for (int i = 0; i < records.size(); i++) { if (path.equals(records.get(i).path)) { return i;