Skip to content

Commit 4fa8dbf

Browse files
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 2fcd595 commit 4fa8dbf

File tree

3 files changed

+36
-13
lines changed

3 files changed

+36
-13
lines changed

Diff for: 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
}

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

+20-3
Original file line numberDiff line numberDiff line change
@@ -1583,6 +1583,21 @@ public Sketch getSketch() {
15831583
public EditorTab getCurrentTab() {
15841584
return tabs.get(currentTabIndex);
15851585
}
1586+
1587+
/**
1588+
* Gets the index of the currently displaying tab.
1589+
*/
1590+
public int getCurrentTabIndex() {
1591+
return currentTabIndex;
1592+
}
1593+
1594+
/**
1595+
* Returns an (unmodifiable) list of currently opened tabs.
1596+
*/
1597+
public List<EditorTab> getTabs() {
1598+
return Collections.unmodifiableList(tabs);
1599+
}
1600+
15861601
// . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
15871602
/**
15881603
* Change the currently displayed tab.
@@ -1970,10 +1985,12 @@ private void updateTitle() {
19701985
if (sketch == null) {
19711986
return;
19721987
}
1973-
if (sketch.getName().equals(sketch.getCurrentCode().getPrettyName())) {
1988+
SketchCode current = getCurrentTab().getSketchCode();
1989+
if (sketch.getName().equals(current.getPrettyName())) {
19741990
setTitle(I18n.format(tr("{0} | Arduino {1}"), sketch.getName(), BaseNoGui.VERSION_NAME_LONG));
19751991
} else {
1976-
setTitle(I18n.format(tr("{0} - {1} | Arduino {2}"), sketch.getName(), sketch.getCurrentCode().getFileName(), BaseNoGui.VERSION_NAME_LONG));
1992+
setTitle(I18n.format(tr("{0} - {1} | Arduino {2}"), sketch.getName(),
1993+
current.getFileName(), BaseNoGui.VERSION_NAME_LONG));
19771994
}
19781995
}
19791996

@@ -2526,7 +2543,7 @@ private void handlePrint() {
25262543
printerJob.setPrintable(getCurrentTab().getTextArea());
25272544
}
25282545
// set the name of the job to the code name
2529-
printerJob.setJobName(sketch.getCurrentCode().getPrettyName());
2546+
printerJob.setJobName(getCurrentTab().getSketchCode().getPrettyName());
25302547

25312548
if (printerJob.printDialog()) {
25322549
try {

Diff for: 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

@@ -229,15 +229,18 @@ public void paintComponent(Graphics screen) {
229229
g.setColor(backgroundColor);
230230
g.fillRect(0, 0, imageW, imageH);
231231

232-
int codeCount = sketch.getCodeCount();
232+
List<EditorTab> tabs = editor.getTabs();
233+
234+
int codeCount = tabs.size();
233235
if ((tabLeft == null) || (tabLeft.length < codeCount)) {
234236
tabLeft = new int[codeCount];
235237
tabRight = new int[codeCount];
236238
}
237239

238240
int x = 6; // offset from left edge of the component
239-
for (int i = 0; i < sketch.getCodeCount(); i++) {
240-
SketchCode code = sketch.getCode(i);
241+
int i = 0;
242+
for (EditorTab tab : tabs) {
243+
SketchCode code = tab.getSketchCode();
241244

242245
String codeName = code.isExtension(sketch.getHiddenExtensions()) ?
243246
code.getPrettyName() : code.getFileName();
@@ -252,7 +255,7 @@ public void paintComponent(Graphics screen) {
252255
int pieceCount = 2 + (textWidth / PIECE_WIDTH);
253256
int pieceWidth = pieceCount * PIECE_WIDTH;
254257

255-
int state = (code == sketch.getCurrentCode()) ? SELECTED : UNSELECTED;
258+
int state = (i == editor.getCurrentTabIndex()) ? SELECTED : UNSELECTED;
256259
g.drawImage(pieces[state][LEFT], x, 0, null);
257260
x += PIECE_WIDTH;
258261

@@ -272,6 +275,7 @@ public void paintComponent(Graphics screen) {
272275

273276
g.drawImage(pieces[state][RIGHT], x, 0, null);
274277
x += PIECE_WIDTH - 1; // overlap by 1 pixel
278+
i++;
275279
}
276280

277281
menuLeft = sizeW - (16 + pieces[0][MENU].getWidth(this));
@@ -318,7 +322,8 @@ public void rebuildMenu() {
318322
if (sketch != null) {
319323
menu.addSeparator();
320324
int i = 0;
321-
for (SketchCode code : sketch.getCodes()) {
325+
for (EditorTab tab : editor.getTabs()) {
326+
SketchCode code = tab.getSketchCode();
322327
final int index = i++;
323328
item = new JMenuItem(code.isExtension(sketch.getDefaultExtension()) ?
324329
code.getPrettyName() : code.getFileName());

0 commit comments

Comments
 (0)