Skip to content

Commit d2bac86

Browse files
matthijskooijmanfacchinm
authored andcommitted
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 0764eb7 commit d2bac86

File tree

4 files changed

+42
-84
lines changed

4 files changed

+42
-84
lines changed

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

app/src/processing/app/Editor.java

+27-10
Original file line numberDiff line numberDiff line change
@@ -1601,6 +1601,7 @@ public void selectTab(final int index) {
16011601
undoAction.updateUndoState();
16021602
redoAction.updateRedoState();
16031603
updateTitle();
1604+
header.rebuild();
16041605

16051606
// This must be run in the GUI thread
16061607
SwingUtilities.invokeLater(() -> {
@@ -1616,6 +1617,14 @@ public void selectTab(final int index) {
16161617
});
16171618
}
16181619

1620+
public void selectNextTab() {
1621+
selectTab((currentTabIndex + 1) % tabs.size());
1622+
}
1623+
1624+
public void selectPrevTab() {
1625+
selectTab((currentTabIndex - 1 + tabs.size()) % tabs.size());
1626+
}
1627+
16191628
public EditorTab findTab(final SketchCode doc) {
16201629
return tabs.get(findTabIndex(doc));
16211630
}
@@ -1628,6 +1637,22 @@ public int findTabIndex(final SketchCode doc) {
16281637
return -1;
16291638
}
16301639

1640+
/**
1641+
* Finds the tab that shows the given file (as returned by
1642+
* SketchCode.getFileName() or SketchCode.getPrettyName()).
1643+
*/
1644+
public int findTabIndex(final String name) {
1645+
int i = 0;
1646+
for (EditorTab tab : tabs) {
1647+
SketchCode code = tab.getSketchCode();
1648+
if (name.equals(code.getFileName()) ||
1649+
name.equals(code.getPrettyName())) {
1650+
return i;
1651+
}
1652+
}
1653+
return -1;
1654+
}
1655+
16311656
public void sketchLoaded(Sketch sketch) {
16321657
tabs.clear();
16331658
currentTabIndex = -1;
@@ -1642,14 +1667,6 @@ public void sketchLoaded(Sketch sketch) {
16421667
}
16431668
}
16441669

1645-
/**
1646-
* Switch between tabs, this swaps out the Document object
1647-
* that's currently being manipulated.
1648-
*/
1649-
protected void setCode(final SketchCodeDocument codeDoc) {
1650-
selectTab(findTabIndex(codeDoc.getCode()));
1651-
}
1652-
16531670
/**
16541671
* Add a new tab.
16551672
*
@@ -1888,7 +1905,7 @@ protected void handleOpenUnchecked(File file, int codeIndex,
18881905
// untitled document, then editor.untitled will be set by Base.
18891906
untitled = false;
18901907

1891-
sketch.setCurrentCode(codeIndex);
1908+
selectTab(codeIndex);
18921909
getCurrentTab().setSelection(selStart, selStop);
18931910
getCurrentTab().setScrollPosition(scrollPos);
18941911
}
@@ -2653,7 +2670,7 @@ public void statusError(Exception e) {
26532670
if (e instanceof RunnerException) {
26542671
RunnerException re = (RunnerException) e;
26552672
if (re.hasCodeIndex()) {
2656-
sketch.setCurrentCode(re.getCodeIndex());
2673+
selectTab(re.getCodeIndex());
26572674
}
26582675
if (re.hasCodeLine()) {
26592676
int line = re.getCodeLine();

app/src/processing/app/EditorHeader.java

+7-8
Original file line numberDiff line numberDiff line change
@@ -102,12 +102,10 @@ public class Actions {
102102
});
103103

104104
public final Action prevTab = new SimpleAction(tr("Previous Tab"),
105-
Keys.ctrlAlt(KeyEvent.VK_LEFT),
106-
() -> editor.sketch.handlePrevCode());
105+
Keys.ctrlAlt(KeyEvent.VK_LEFT), () -> editor.selectPrevTab());
107106

108107
public final Action nextTab = new SimpleAction(tr("Next Tab"),
109-
Keys.ctrlAlt(KeyEvent.VK_RIGHT),
110-
() -> editor.sketch.handleNextCode());
108+
Keys.ctrlAlt(KeyEvent.VK_RIGHT), () -> editor.selectNextTab());
111109

112110
Actions() {
113111
// Explicitly bind keybindings for the actions with accelerators above
@@ -181,10 +179,10 @@ public void mousePressed(MouseEvent e) {
181179
popup.show(EditorHeader.this, x, y);
182180

183181
} else {
184-
Sketch sketch = editor.getSketch();
185-
for (int i = 0; i < sketch.getCodeCount(); i++) {
182+
int numTabs = editor.getTabs().size();
183+
for (int i = 0; i < numTabs; i++) {
186184
if ((x > tabLeft[i]) && (x < tabRight[i])) {
187-
sketch.setCurrentCode(i);
185+
editor.selectTab(i);
188186
repaint();
189187
}
190188
}
@@ -326,14 +324,15 @@ public void rebuildMenu() {
326324
Sketch sketch = editor.getSketch();
327325
if (sketch != null) {
328326
menu.addSeparator();
327+
329328
int i = 0;
330329
for (EditorTab tab : editor.getTabs()) {
331330
SketchCode code = tab.getSketchCode();
332331
final int index = i++;
333332
item = new JMenuItem(code.isExtension(sketch.getDefaultExtension()) ?
334333
code.getPrettyName() : code.getFileName());
335334
item.addActionListener((ActionEvent e) -> {
336-
editor.getSketch().setCurrentCode(index);
335+
editor.selectTab(index);
337336
});
338337
menu.add(item);
339338
}

app/src/processing/app/Sketch.java

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

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

420420
// set the new guy as current
421-
setCurrentCode(newName);
421+
editor.selectTab(editor.findTabIndex(newName));
422422

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

489489
// just set current tab to the main tab
490-
setCurrentCode(0);
490+
editor.selectTab(0);
491491

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

498-
499-
/**
500-
* Move to the previous tab.
501-
*/
502-
public void handlePrevCode() {
503-
int prev = editor.getCurrentTabIndex() - 1;
504-
if (prev < 0) prev = data.getCodeCount()-1;
505-
setCurrentCode(prev);
506-
}
507-
508-
509-
/**
510-
* Move to the next tab.
511-
*/
512-
public void handleNextCode() {
513-
setCurrentCode((editor.getCurrentTabIndex() + 1) % data.getCodeCount());
514-
}
515-
516498
/**
517499
* Called whenever the modification status of one of the tabs changes. TODO:
518500
* Move this code into Editor and improve decoupling from EditorTab
@@ -889,7 +871,7 @@ public boolean addFile(File sourceFile) {
889871
data.addCode(newCode);
890872
data.sortCode();
891873
}
892-
setCurrentCode(filename);
874+
editor.selectTab(editor.findTabIndex(filename));
893875
}
894876
return true;
895877
}
@@ -916,7 +898,7 @@ public void importLibrary(UserLibrary lib) throws IOException {
916898
// if the current code is a .java file, insert into current
917899
//if (current.flavor == PDE) {
918900
if (hasDefaultExtension(editor.getCurrentTab().getSketchCode())) {
919-
setCurrentCode(0);
901+
editor.selectTab(0);
920902
}
921903
// could also scan the text in the file to see if each import
922904
// statement is already in there, but if the user has the import
@@ -933,44 +915,6 @@ public void importLibrary(UserLibrary lib) throws IOException {
933915
editor.getCurrentTab().setSelection(0, 0); // scroll to start
934916
}
935917

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

0 commit comments

Comments
 (0)