From d2bac8659ede252948be5c36698b9e8d543cb355 Mon Sep 17 00:00:00 2001 From: Matthijs Kooijman Date: Tue, 8 Dec 2015 15:14:27 +0100 Subject: [PATCH] Remove tab switching logic from Sketch This lets all code directly call `Editor.selectTab()`, or the newly introduced `Editor.selectNextTab()` or `Editor.selectPrevTab()`. This also adds a new `Editor.findTabIndex(String)` to look up a tab based on the filename (what `Sketch.setCurrentCode(String)` used to do). At some point, this method might need to be removed, but for now it allows other code to keep working with minimal changes. --- .../arduino/view/findreplace/FindReplace.java | 8 +-- app/src/processing/app/Editor.java | 37 ++++++++--- app/src/processing/app/EditorHeader.java | 15 ++--- app/src/processing/app/Sketch.java | 66 ++----------------- 4 files changed, 42 insertions(+), 84 deletions(-) diff --git a/app/src/cc/arduino/view/findreplace/FindReplace.java b/app/src/cc/arduino/view/findreplace/FindReplace.java index b585fe0dd..1b40e1b12 100644 --- a/app/src/cc/arduino/view/findreplace/FindReplace.java +++ b/app/src/cc/arduino/view/findreplace/FindReplace.java @@ -31,7 +31,6 @@ package cc.arduino.view.findreplace; import processing.app.Base; import processing.app.Editor; -import processing.app.Sketch; import processing.app.helpers.OSUtils; import java.awt.*; @@ -328,7 +327,6 @@ public class FindReplace extends javax.swing.JFrame { // Nothing found on this tab: Search other tabs if required if (searchTabs) { int numTabs = editor.getTabs().size(); - Sketch sketch = editor.getSketch(); if (numTabs > 1) { int realCurrentTab = editor.getCurrentTabIndex(); @@ -345,12 +343,12 @@ public class FindReplace extends javax.swing.JFrame { } if (backwards) { - sketch.handlePrevCode(); + editor.selectNextTab(); this.setVisible(true); int l = editor.getCurrentTab().getText().length() - 1; editor.getCurrentTab().setSelection(l, l); } else { - sketch.handleNextCode(); + editor.selectPrevTab(); this.setVisible(true); editor.getCurrentTab().setSelection(0, 0); } @@ -420,7 +418,7 @@ public class FindReplace extends javax.swing.JFrame { } if (searchAllFilesBox.isSelected()) { - editor.getSketch().setCurrentCode(0); // select the first tab + editor.selectTab(0); // select the first tab } editor.getCurrentTab().setSelection(0, 0); // move to the beginning diff --git a/app/src/processing/app/Editor.java b/app/src/processing/app/Editor.java index f183c11ef..98b45fdd8 100644 --- a/app/src/processing/app/Editor.java +++ b/app/src/processing/app/Editor.java @@ -1601,6 +1601,7 @@ public class Editor extends JFrame implements RunnerListener { undoAction.updateUndoState(); redoAction.updateRedoState(); updateTitle(); + header.rebuild(); // This must be run in the GUI thread SwingUtilities.invokeLater(() -> { @@ -1616,6 +1617,14 @@ public class Editor extends JFrame implements RunnerListener { }); } + public void selectNextTab() { + selectTab((currentTabIndex + 1) % tabs.size()); + } + + public void selectPrevTab() { + selectTab((currentTabIndex - 1 + tabs.size()) % tabs.size()); + } + public EditorTab findTab(final SketchCode doc) { return tabs.get(findTabIndex(doc)); } @@ -1628,6 +1637,22 @@ public class Editor extends JFrame implements RunnerListener { return -1; } + /** + * Finds the tab that shows the given file (as returned by + * SketchCode.getFileName() or SketchCode.getPrettyName()). + */ + public int findTabIndex(final String name) { + int i = 0; + for (EditorTab tab : tabs) { + SketchCode code = tab.getSketchCode(); + if (name.equals(code.getFileName()) || + name.equals(code.getPrettyName())) { + return i; + } + } + return -1; + } + public void sketchLoaded(Sketch sketch) { tabs.clear(); currentTabIndex = -1; @@ -1642,14 +1667,6 @@ public class Editor extends JFrame implements RunnerListener { } } - /** - * Switch between tabs, this swaps out the Document object - * that's currently being manipulated. - */ - protected void setCode(final SketchCodeDocument codeDoc) { - selectTab(findTabIndex(codeDoc.getCode())); - } - /** * Add a new tab. * @@ -1888,7 +1905,7 @@ public class Editor extends JFrame implements RunnerListener { // untitled document, then editor.untitled will be set by Base. untitled = false; - sketch.setCurrentCode(codeIndex); + selectTab(codeIndex); getCurrentTab().setSelection(selStart, selStop); getCurrentTab().setScrollPosition(scrollPos); } @@ -2653,7 +2670,7 @@ public class Editor extends JFrame implements RunnerListener { if (e instanceof RunnerException) { RunnerException re = (RunnerException) e; if (re.hasCodeIndex()) { - sketch.setCurrentCode(re.getCodeIndex()); + selectTab(re.getCodeIndex()); } if (re.hasCodeLine()) { int line = re.getCodeLine(); diff --git a/app/src/processing/app/EditorHeader.java b/app/src/processing/app/EditorHeader.java index b9d9e778e..1e2de174b 100644 --- a/app/src/processing/app/EditorHeader.java +++ b/app/src/processing/app/EditorHeader.java @@ -102,12 +102,10 @@ public class EditorHeader extends JComponent { }); public final Action prevTab = new SimpleAction(tr("Previous Tab"), - Keys.ctrlAlt(KeyEvent.VK_LEFT), - () -> editor.sketch.handlePrevCode()); + Keys.ctrlAlt(KeyEvent.VK_LEFT), () -> editor.selectPrevTab()); public final Action nextTab = new SimpleAction(tr("Next Tab"), - Keys.ctrlAlt(KeyEvent.VK_RIGHT), - () -> editor.sketch.handleNextCode()); + Keys.ctrlAlt(KeyEvent.VK_RIGHT), () -> editor.selectNextTab()); Actions() { // Explicitly bind keybindings for the actions with accelerators above @@ -181,10 +179,10 @@ public class EditorHeader extends JComponent { popup.show(EditorHeader.this, x, y); } else { - Sketch sketch = editor.getSketch(); - for (int i = 0; i < sketch.getCodeCount(); i++) { + int numTabs = editor.getTabs().size(); + for (int i = 0; i < numTabs; i++) { if ((x > tabLeft[i]) && (x < tabRight[i])) { - sketch.setCurrentCode(i); + editor.selectTab(i); repaint(); } } @@ -326,6 +324,7 @@ public class EditorHeader extends JComponent { Sketch sketch = editor.getSketch(); if (sketch != null) { menu.addSeparator(); + int i = 0; for (EditorTab tab : editor.getTabs()) { SketchCode code = tab.getSketchCode(); @@ -333,7 +332,7 @@ public class EditorHeader extends JComponent { item = new JMenuItem(code.isExtension(sketch.getDefaultExtension()) ? code.getPrettyName() : code.getFileName()); item.addActionListener((ActionEvent e) -> { - editor.getSketch().setCurrentCode(index); + editor.selectTab(index); }); menu.add(item); } diff --git a/app/src/processing/app/Sketch.java b/app/src/processing/app/Sketch.java index c43ef37a0..e9b65094e 100644 --- a/app/src/processing/app/Sketch.java +++ b/app/src/processing/app/Sketch.java @@ -101,7 +101,7 @@ public class Sketch { if (current < 0) current = 0; editor.sketchLoaded(this); - setCurrentCode(current, forceUpdate); + editor.selectTab(current); } } @@ -418,7 +418,7 @@ public class Sketch { data.sortCode(); // set the new guy as current - setCurrentCode(newName); + editor.selectTab(editor.findTabIndex(newName)); // update the tabs editor.header.rebuild(); @@ -487,7 +487,7 @@ public class Sketch { data.removeCode(current); // just set current tab to the main tab - setCurrentCode(0); + editor.selectTab(0); // update the tabs editor.header.repaint(); @@ -495,24 +495,6 @@ public class Sketch { } } - - /** - * Move to the previous tab. - */ - public void handlePrevCode() { - int prev = editor.getCurrentTabIndex() - 1; - if (prev < 0) prev = data.getCodeCount()-1; - setCurrentCode(prev); - } - - - /** - * Move to the next tab. - */ - public void handleNextCode() { - setCurrentCode((editor.getCurrentTabIndex() + 1) % data.getCodeCount()); - } - /** * Called whenever the modification status of one of the tabs changes. TODO: * Move this code into Editor and improve decoupling from EditorTab @@ -889,7 +871,7 @@ public class Sketch { data.addCode(newCode); data.sortCode(); } - setCurrentCode(filename); + editor.selectTab(editor.findTabIndex(filename)); } return true; } @@ -916,7 +898,7 @@ public class Sketch { // if the current code is a .java file, insert into current //if (current.flavor == PDE) { if (hasDefaultExtension(editor.getCurrentTab().getSketchCode())) { - setCurrentCode(0); + editor.selectTab(0); } // could also scan the text in the file to see if each import // statement is already in there, but if the user has the import @@ -933,44 +915,6 @@ public class Sketch { editor.getCurrentTab().setSelection(0, 0); // scroll to start } - - /** - * Change what file is currently being edited. Changes the current tab index. - *
    - *
  1. store the String for the text of the current file. - *
  2. retrieve the String for the text of the new file. - *
  3. change the text that's visible in the text area - *
- */ - public void setCurrentCode(int which) { - setCurrentCode(which, false); - } - - private void setCurrentCode(int which, boolean forceUpdate) { - if (!forceUpdate && (editor.getCurrentTabIndex() == which)) { - return; - } - - editor.setCode((SketchCodeDocument)editor.getTabs().get(which).getSketchCode().getMetadata()); - editor.header.rebuild(); - } - - - /** - * Internal helper function to set the current tab based on a name. - * @param findName the file name (not pretty name) to be shown - */ - protected void setCurrentCode(String findName) { - for (SketchCode code : data.getCodes()) { - if (findName.equals(code.getFileName()) || - findName.equals(code.getPrettyName())) { - setCurrentCode(data.indexOfCode(code)); - return; - } - } - } - - /** * Preprocess, Compile, and Run the current code. *