Skip to content

Commit

Permalink
Remove tab switching logic from Sketch
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
matthijskooijman committed Dec 30, 2015
1 parent f9824f4 commit 39ffb7f
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 84 deletions.
8 changes: 3 additions & 5 deletions app/src/cc/arduino/view/findreplace/FindReplace.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@

import processing.app.Base;
import processing.app.Editor;
import processing.app.Sketch;
import processing.app.helpers.OSUtils;

import java.awt.*;
Expand Down Expand Up @@ -328,7 +327,6 @@ private boolean find(boolean wrap, boolean backwards, boolean searchTabs, int or
// 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();

Expand All @@ -345,12 +343,12 @@ private boolean find(boolean wrap, boolean backwards, boolean searchTabs, int or
}

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);
}
Expand Down Expand Up @@ -420,7 +418,7 @@ private void replaceAll() {
}

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
Expand Down
37 changes: 27 additions & 10 deletions app/src/processing/app/Editor.java
Original file line number Diff line number Diff line change
Expand Up @@ -1610,6 +1610,7 @@ public void selectTab(final int index) {
undoAction.updateUndoState();
redoAction.updateRedoState();
updateTitle();
header.rebuild();

// This must be run in the GUI thread
SwingUtilities.invokeLater(() -> {
Expand All @@ -1625,6 +1626,14 @@ public void selectTab(final int index) {
});
}

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));
}
Expand All @@ -1637,6 +1646,22 @@ public int findTabIndex(final SketchCode doc) {
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;
Expand All @@ -1651,14 +1676,6 @@ public void sketchLoaded(Sketch sketch) {
}
}

/**
* 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.
*
Expand Down Expand Up @@ -1896,7 +1913,7 @@ protected void handleOpenUnchecked(File file, int codeIndex,
// untitled document, then editor.untitled will be set by Base.
untitled = false;

sketch.setCurrentCode(codeIndex);
selectTab(codeIndex);
getCurrentTab().setSelection(selStart, selStop);
getCurrentTab().setScrollPosition(scrollPos);
}
Expand Down Expand Up @@ -2588,7 +2605,7 @@ public void statusError(Exception e) {
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();
Expand Down
16 changes: 8 additions & 8 deletions app/src/processing/app/EditorHeader.java
Original file line number Diff line number Diff line change
Expand Up @@ -96,12 +96,10 @@ public class Actions {
});

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
Expand Down Expand Up @@ -170,10 +168,10 @@ public void mousePressed(MouseEvent e) {
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();
}
}
Expand Down Expand Up @@ -321,16 +319,18 @@ public void rebuildMenu() {
Sketch sketch = editor.getSketch();
if (sketch != null) {
menu.addSeparator();

int i = 0;
for (EditorTab tab : editor.getTabs()) {
SketchCode code = tab.getSketchCode();
final int index = i++;
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);
i++;
}
}
}
Expand Down
66 changes: 5 additions & 61 deletions app/src/processing/app/Sketch.java
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ protected void load(boolean forceUpdate) throws IOException {
if (current < 0)
current = 0;
editor.sketchLoaded(this);
setCurrentCode(current, forceUpdate);
editor.selectTab(current);
}
}

Expand Down Expand Up @@ -419,7 +419,7 @@ protected void nameCode(String newName) {
data.sortCode();

// set the new guy as current
setCurrentCode(newName);
editor.selectTab(editor.findTabIndex(newName));

// update the tabs
editor.header.rebuild();
Expand Down Expand Up @@ -488,32 +488,14 @@ public void handleDeleteCode() throws IOException {
data.removeCode(current);

// just set current tab to the main tab
setCurrentCode(0);
editor.selectTab(0);

// update the tabs
editor.header.repaint();
}
}
}


/**
* 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
Expand Down Expand Up @@ -890,7 +872,7 @@ public boolean addFile(File sourceFile) {
data.addCode(newCode);
data.sortCode();
}
setCurrentCode(filename);
editor.selectTab(editor.findTabIndex(filename));
}
return true;
}
Expand All @@ -917,7 +899,7 @@ private void importLibrary(File jarPath) throws IOException {
// 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
Expand All @@ -934,44 +916,6 @@ private void importLibrary(File jarPath) throws IOException {
editor.getCurrentTab().setSelection(0, 0); // scroll to start
}


/**
* Change what file is currently being edited. Changes the current tab index.
* <OL>
* <LI> store the String for the text of the current file.
* <LI> retrieve the String for the text of the new file.
* <LI> change the text that's visible in the text area
* </OL>
*/
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.
* <P>
Expand Down

0 comments on commit 39ffb7f

Please sign in to comment.