Skip to content

Commit 39ffb7f

Browse files
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.
1 parent f9824f4 commit 39ffb7f

File tree

4 files changed

+43
-84
lines changed

4 files changed

+43
-84
lines changed

Diff for: app/src/cc/arduino/view/findreplace/FindReplace.java

+3-5
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,6 @@
3131

3232
import processing.app.Base;
3333
import processing.app.Editor;
34-
import processing.app.Sketch;
3534
import processing.app.helpers.OSUtils;
3635

3736
import java.awt.*;
@@ -328,7 +327,6 @@ private boolean find(boolean wrap, boolean backwards, boolean searchTabs, int or
328327
// Nothing found on this tab: Search other tabs if required
329328
if (searchTabs) {
330329
int numTabs = editor.getTabs().size();
331-
Sketch sketch = editor.getSketch();
332330
if (numTabs > 1) {
333331
int realCurrentTab = editor.getCurrentTabIndex();
334332

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

347345
if (backwards) {
348-
sketch.handlePrevCode();
346+
editor.selectNextTab();
349347
this.setVisible(true);
350348
int l = editor.getCurrentTab().getText().length() - 1;
351349
editor.getCurrentTab().setSelection(l, l);
352350
} else {
353-
sketch.handleNextCode();
351+
editor.selectPrevTab();
354352
this.setVisible(true);
355353
editor.getCurrentTab().setSelection(0, 0);
356354
}
@@ -420,7 +418,7 @@ private void replaceAll() {
420418
}
421419

422420
if (searchAllFilesBox.isSelected()) {
423-
editor.getSketch().setCurrentCode(0); // select the first tab
421+
editor.selectTab(0); // select the first tab
424422
}
425423

426424
editor.getCurrentTab().setSelection(0, 0); // move to the beginning

Diff for: app/src/processing/app/Editor.java

+27-10
Original file line numberDiff line numberDiff line change
@@ -1610,6 +1610,7 @@ public void selectTab(final int index) {
16101610
undoAction.updateUndoState();
16111611
redoAction.updateRedoState();
16121612
updateTitle();
1613+
header.rebuild();
16131614

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

1629+
public void selectNextTab() {
1630+
selectTab((currentTabIndex + 1) % tabs.size());
1631+
}
1632+
1633+
public void selectPrevTab() {
1634+
selectTab((currentTabIndex - 1 + tabs.size()) % tabs.size());
1635+
}
1636+
16281637
public EditorTab findTab(final SketchCode doc) {
16291638
return tabs.get(findTabIndex(doc));
16301639
}
@@ -1637,6 +1646,22 @@ public int findTabIndex(final SketchCode doc) {
16371646
return -1;
16381647
}
16391648

1649+
/**
1650+
* Finds the tab that shows the given file (as returned by
1651+
* SketchCode.getFileName() or SketchCode.getPrettyName()).
1652+
*/
1653+
public int findTabIndex(final String name) {
1654+
int i = 0;
1655+
for (EditorTab tab : tabs) {
1656+
SketchCode code = tab.getSketchCode();
1657+
if (name.equals(code.getFileName()) ||
1658+
name.equals(code.getPrettyName())) {
1659+
return i;
1660+
}
1661+
}
1662+
return -1;
1663+
}
1664+
16401665
public void sketchLoaded(Sketch sketch) {
16411666
tabs.clear();
16421667
currentTabIndex = -1;
@@ -1651,14 +1676,6 @@ public void sketchLoaded(Sketch sketch) {
16511676
}
16521677
}
16531678

1654-
/**
1655-
* Switch between tabs, this swaps out the Document object
1656-
* that's currently being manipulated.
1657-
*/
1658-
protected void setCode(final SketchCodeDocument codeDoc) {
1659-
selectTab(findTabIndex(codeDoc.getCode()));
1660-
}
1661-
16621679
/**
16631680
* Add a new tab.
16641681
*
@@ -1896,7 +1913,7 @@ protected void handleOpenUnchecked(File file, int codeIndex,
18961913
// untitled document, then editor.untitled will be set by Base.
18971914
untitled = false;
18981915

1899-
sketch.setCurrentCode(codeIndex);
1916+
selectTab(codeIndex);
19001917
getCurrentTab().setSelection(selStart, selStop);
19011918
getCurrentTab().setScrollPosition(scrollPos);
19021919
}
@@ -2588,7 +2605,7 @@ public void statusError(Exception e) {
25882605
if (e instanceof RunnerException) {
25892606
RunnerException re = (RunnerException) e;
25902607
if (re.hasCodeIndex()) {
2591-
sketch.setCurrentCode(re.getCodeIndex());
2608+
selectTab(re.getCodeIndex());
25922609
}
25932610
if (re.hasCodeLine()) {
25942611
int line = re.getCodeLine();

Diff for: app/src/processing/app/EditorHeader.java

+8-8
Original file line numberDiff line numberDiff line change
@@ -96,12 +96,10 @@ public class Actions {
9696
});
9797

9898
public final Action prevTab = new SimpleAction(tr("Previous Tab"),
99-
Keys.ctrlAlt(KeyEvent.VK_LEFT),
100-
() -> editor.sketch.handlePrevCode());
99+
Keys.ctrlAlt(KeyEvent.VK_LEFT), () -> editor.selectPrevTab());
101100

102101
public final Action nextTab = new SimpleAction(tr("Next Tab"),
103-
Keys.ctrlAlt(KeyEvent.VK_RIGHT),
104-
() -> editor.sketch.handleNextCode());
102+
Keys.ctrlAlt(KeyEvent.VK_RIGHT), () -> editor.selectNextTab());
105103

106104
Actions() {
107105
// Explicitly bind keybindings for the actions with accelerators above
@@ -170,10 +168,10 @@ public void mousePressed(MouseEvent e) {
170168
popup.show(EditorHeader.this, x, y);
171169

172170
} else {
173-
Sketch sketch = editor.getSketch();
174-
for (int i = 0; i < sketch.getCodeCount(); i++) {
171+
int numTabs = editor.getTabs().size();
172+
for (int i = 0; i < numTabs; i++) {
175173
if ((x > tabLeft[i]) && (x < tabRight[i])) {
176-
sketch.setCurrentCode(i);
174+
editor.selectTab(i);
177175
repaint();
178176
}
179177
}
@@ -321,16 +319,18 @@ public void rebuildMenu() {
321319
Sketch sketch = editor.getSketch();
322320
if (sketch != null) {
323321
menu.addSeparator();
322+
324323
int i = 0;
325324
for (EditorTab tab : editor.getTabs()) {
326325
SketchCode code = tab.getSketchCode();
327326
final int index = i++;
328327
item = new JMenuItem(code.isExtension(sketch.getDefaultExtension()) ?
329328
code.getPrettyName() : code.getFileName());
330329
item.addActionListener((ActionEvent e) -> {
331-
editor.getSketch().setCurrentCode(index);
330+
editor.selectTab(index);
332331
});
333332
menu.add(item);
333+
i++;
334334
}
335335
}
336336
}

Diff for: app/src/processing/app/Sketch.java

+5-61
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ protected void load(boolean forceUpdate) throws IOException {
102102
if (current < 0)
103103
current = 0;
104104
editor.sketchLoaded(this);
105-
setCurrentCode(current, forceUpdate);
105+
editor.selectTab(current);
106106
}
107107
}
108108

@@ -419,7 +419,7 @@ protected void nameCode(String newName) {
419419
data.sortCode();
420420

421421
// set the new guy as current
422-
setCurrentCode(newName);
422+
editor.selectTab(editor.findTabIndex(newName));
423423

424424
// update the tabs
425425
editor.header.rebuild();
@@ -488,32 +488,14 @@ public void handleDeleteCode() throws IOException {
488488
data.removeCode(current);
489489

490490
// just set current tab to the main tab
491-
setCurrentCode(0);
491+
editor.selectTab(0);
492492

493493
// update the tabs
494494
editor.header.repaint();
495495
}
496496
}
497497
}
498498

499-
500-
/**
501-
* Move to the previous tab.
502-
*/
503-
public void handlePrevCode() {
504-
int prev = editor.getCurrentTabIndex() - 1;
505-
if (prev < 0) prev = data.getCodeCount()-1;
506-
setCurrentCode(prev);
507-
}
508-
509-
510-
/**
511-
* Move to the next tab.
512-
*/
513-
public void handleNextCode() {
514-
setCurrentCode((editor.getCurrentTabIndex() + 1) % data.getCodeCount());
515-
}
516-
517499
/**
518500
* Called whenever the modification status of one of the tabs changes. TODO:
519501
* Move this code into Editor and improve decoupling from EditorTab
@@ -890,7 +872,7 @@ public boolean addFile(File sourceFile) {
890872
data.addCode(newCode);
891873
data.sortCode();
892874
}
893-
setCurrentCode(filename);
875+
editor.selectTab(editor.findTabIndex(filename));
894876
}
895877
return true;
896878
}
@@ -917,7 +899,7 @@ private void importLibrary(File jarPath) throws IOException {
917899
// if the current code is a .java file, insert into current
918900
//if (current.flavor == PDE) {
919901
if (hasDefaultExtension(editor.getCurrentTab().getSketchCode())) {
920-
setCurrentCode(0);
902+
editor.selectTab(0);
921903
}
922904
// could also scan the text in the file to see if each import
923905
// statement is already in there, but if the user has the import
@@ -934,44 +916,6 @@ private void importLibrary(File jarPath) throws IOException {
934916
editor.getCurrentTab().setSelection(0, 0); // scroll to start
935917
}
936918

937-
938-
/**
939-
* Change what file is currently being edited. Changes the current tab index.
940-
* <OL>
941-
* <LI> store the String for the text of the current file.
942-
* <LI> retrieve the String for the text of the new file.
943-
* <LI> change the text that's visible in the text area
944-
* </OL>
945-
*/
946-
public void setCurrentCode(int which) {
947-
setCurrentCode(which, false);
948-
}
949-
950-
private void setCurrentCode(int which, boolean forceUpdate) {
951-
if (!forceUpdate && (editor.getCurrentTabIndex() == which)) {
952-
return;
953-
}
954-
955-
editor.setCode((SketchCodeDocument)editor.getTabs().get(which).getSketchCode().getMetadata());
956-
editor.header.rebuild();
957-
}
958-
959-
960-
/**
961-
* Internal helper function to set the current tab based on a name.
962-
* @param findName the file name (not pretty name) to be shown
963-
*/
964-
protected void setCurrentCode(String findName) {
965-
for (SketchCode code : data.getCodes()) {
966-
if (findName.equals(code.getFileName()) ||
967-
findName.equals(code.getPrettyName())) {
968-
setCurrentCode(data.indexOfCode(code));
969-
return;
970-
}
971-
}
972-
}
973-
974-
975919
/**
976920
* Preprocess, Compile, and Run the current code.
977921
* <P>

0 commit comments

Comments
 (0)