Skip to content

Commit 6b31cff

Browse files
matthijskooijmanfacchinm
authored andcommitted
Add getTabs() and getCurrentTabIndex() to Editor and use them
Previously, some of the GUI code would use Editor.getSketch() to get the current sketch, and Sketch.getCurrentCode() to find out the currently selected tab. Since this code is really concerned with the currently open tab in the GUI, it makes more sense to query the Editor tabs list directly. This removes all references the current sketch code, as tracked by Sketch, external to Sketch itself. This prepares for removing the current tab tracking from Sketch later.
1 parent ca57335 commit 6b31cff

File tree

3 files changed

+36
-13
lines changed

3 files changed

+36
-13
lines changed

app/src/cc/arduino/view/findreplace/FindReplace.java

+5-4
Original file line numberDiff line numberDiff line change
@@ -327,18 +327,19 @@ private boolean find(boolean wrap, boolean backwards, boolean searchTabs, int or
327327
if (nextIndex == -1) {
328328
// Nothing found on this tab: Search other tabs if required
329329
if (searchTabs) {
330-
// editor.
330+
int numTabs = editor.getTabs().size();
331331
Sketch sketch = editor.getSketch();
332-
if (sketch.getCodeCount() > 1) {
333-
int realCurrentTab = sketch.getCodeIndex(sketch.getCurrentCode());
332+
if (numTabs > 1) {
333+
int realCurrentTab = editor.getCurrentTabIndex();
334334

335335
if (originTab != realCurrentTab) {
336336
if (originTab < 0) {
337337
originTab = realCurrentTab;
338338
}
339339

340340
if (!wrap) {
341-
if ((!backwards && realCurrentTab + 1 >= sketch.getCodeCount()) || (backwards && realCurrentTab - 1 < 0)) {
341+
if ((!backwards && realCurrentTab + 1 >= numTabs)
342+
|| (backwards && realCurrentTab - 1 < 0)) {
342343
return false; // Can't continue without wrap
343344
}
344345
}

app/src/processing/app/Editor.java

+20-3
Original file line numberDiff line numberDiff line change
@@ -1574,6 +1574,21 @@ public Sketch getSketch() {
15741574
public EditorTab getCurrentTab() {
15751575
return tabs.get(currentTabIndex);
15761576
}
1577+
1578+
/**
1579+
* Gets the index of the currently displaying tab.
1580+
*/
1581+
public int getCurrentTabIndex() {
1582+
return currentTabIndex;
1583+
}
1584+
1585+
/**
1586+
* Returns an (unmodifiable) list of currently opened tabs.
1587+
*/
1588+
public List<EditorTab> getTabs() {
1589+
return Collections.unmodifiableList(tabs);
1590+
}
1591+
15771592
// . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
15781593
/**
15791594
* Change the currently displayed tab.
@@ -1962,10 +1977,12 @@ private void updateTitle() {
19621977
if (sketch == null) {
19631978
return;
19641979
}
1965-
if (sketch.getName().equals(sketch.getCurrentCode().getPrettyName())) {
1980+
SketchCode current = getCurrentTab().getSketchCode();
1981+
if (sketch.getName().equals(current.getPrettyName())) {
19661982
setTitle(I18n.format(tr("{0} | Arduino {1}"), sketch.getName(), BaseNoGui.VERSION_NAME_LONG));
19671983
} else {
1968-
setTitle(I18n.format(tr("{0} - {1} | Arduino {2}"), sketch.getName(), sketch.getCurrentCode().getFileName(), BaseNoGui.VERSION_NAME_LONG));
1984+
setTitle(I18n.format(tr("{0} - {1} | Arduino {2}"), sketch.getName(),
1985+
current.getFileName(), BaseNoGui.VERSION_NAME_LONG));
19691986
}
19701987
}
19711988

@@ -2591,7 +2608,7 @@ private void handlePrint() {
25912608
printerJob.setPrintable(getCurrentTab().getTextArea());
25922609
}
25932610
// set the name of the job to the code name
2594-
printerJob.setJobName(sketch.getCurrentCode().getPrettyName());
2611+
printerJob.setJobName(getCurrentTab().getSketchCode().getPrettyName());
25952612

25962613
if (printerJob.printDialog()) {
25972614
try {

app/src/processing/app/EditorHeader.java

+11-6
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@
3232
import java.awt.*;
3333
import java.awt.event.*;
3434
import java.io.IOException;
35-
35+
import java.util.List;
3636
import javax.swing.*;
3737

3838
import static processing.app.Theme.scale;
@@ -235,15 +235,18 @@ public void paintComponent(Graphics screen) {
235235
g.setColor(backgroundColor);
236236
g.fillRect(0, 0, imageW, imageH);
237237

238-
int codeCount = sketch.getCodeCount();
238+
List<EditorTab> tabs = editor.getTabs();
239+
240+
int codeCount = tabs.size();
239241
if ((tabLeft == null) || (tabLeft.length < codeCount)) {
240242
tabLeft = new int[codeCount];
241243
tabRight = new int[codeCount];
242244
}
243245

244246
int x = scale(6); // offset from left edge of the component
245-
for (int i = 0; i < sketch.getCodeCount(); i++) {
246-
SketchCode code = sketch.getCode(i);
247+
int i = 0;
248+
for (EditorTab tab : tabs) {
249+
SketchCode code = tab.getSketchCode();
247250

248251
String codeName = code.isExtension(sketch.getHiddenExtensions()) ?
249252
code.getPrettyName() : code.getFileName();
@@ -257,7 +260,7 @@ public void paintComponent(Graphics screen) {
257260
int pieceCount = 2 + (textWidth / PIECE_WIDTH);
258261
int pieceWidth = pieceCount * PIECE_WIDTH;
259262

260-
int state = (code == sketch.getCurrentCode()) ? SELECTED : UNSELECTED;
263+
int state = (i == editor.getCurrentTabIndex()) ? SELECTED : UNSELECTED;
261264
g.drawImage(pieces[state][LEFT], x, 0, null);
262265
x += PIECE_WIDTH;
263266

@@ -277,6 +280,7 @@ public void paintComponent(Graphics screen) {
277280

278281
g.drawImage(pieces[state][RIGHT], x, 0, null);
279282
x += PIECE_WIDTH - 1; // overlap by 1 pixel
283+
i++;
280284
}
281285

282286
menuLeft = sizeW - (16 + menuButtons[0].getWidth(this));
@@ -323,7 +327,8 @@ public void rebuildMenu() {
323327
if (sketch != null) {
324328
menu.addSeparator();
325329
int i = 0;
326-
for (SketchCode code : sketch.getCodes()) {
330+
for (EditorTab tab : editor.getTabs()) {
331+
SketchCode code = tab.getSketchCode();
327332
final int index = i++;
328333
item = new JMenuItem(code.isExtension(sketch.getDefaultExtension()) ?
329334
code.getPrettyName() : code.getFileName());

0 commit comments

Comments
 (0)