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.
This commit is contained in:
Matthijs Kooijman 2015-12-08 12:12:00 +01:00 committed by Martino Facchin
parent ca573351bb
commit 6b31cffaec
3 changed files with 36 additions and 13 deletions

View File

@ -327,10 +327,10 @@ public class FindReplace extends javax.swing.JFrame {
if (nextIndex == -1) {
// Nothing found on this tab: Search other tabs if required
if (searchTabs) {
// editor.
int numTabs = editor.getTabs().size();
Sketch sketch = editor.getSketch();
if (sketch.getCodeCount() > 1) {
int realCurrentTab = sketch.getCodeIndex(sketch.getCurrentCode());
if (numTabs > 1) {
int realCurrentTab = editor.getCurrentTabIndex();
if (originTab != realCurrentTab) {
if (originTab < 0) {
@ -338,7 +338,8 @@ public class FindReplace extends javax.swing.JFrame {
}
if (!wrap) {
if ((!backwards && realCurrentTab + 1 >= sketch.getCodeCount()) || (backwards && realCurrentTab - 1 < 0)) {
if ((!backwards && realCurrentTab + 1 >= numTabs)
|| (backwards && realCurrentTab - 1 < 0)) {
return false; // Can't continue without wrap
}
}

View File

@ -1574,6 +1574,21 @@ public class Editor extends JFrame implements RunnerListener {
public EditorTab getCurrentTab() {
return tabs.get(currentTabIndex);
}
/**
* Gets the index of the currently displaying tab.
*/
public int getCurrentTabIndex() {
return currentTabIndex;
}
/**
* Returns an (unmodifiable) list of currently opened tabs.
*/
public List<EditorTab> getTabs() {
return Collections.unmodifiableList(tabs);
}
// . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
/**
* Change the currently displayed tab.
@ -1962,10 +1977,12 @@ public class Editor extends JFrame implements RunnerListener {
if (sketch == null) {
return;
}
if (sketch.getName().equals(sketch.getCurrentCode().getPrettyName())) {
SketchCode current = getCurrentTab().getSketchCode();
if (sketch.getName().equals(current.getPrettyName())) {
setTitle(I18n.format(tr("{0} | Arduino {1}"), sketch.getName(), BaseNoGui.VERSION_NAME_LONG));
} else {
setTitle(I18n.format(tr("{0} - {1} | Arduino {2}"), sketch.getName(), sketch.getCurrentCode().getFileName(), BaseNoGui.VERSION_NAME_LONG));
setTitle(I18n.format(tr("{0} - {1} | Arduino {2}"), sketch.getName(),
current.getFileName(), BaseNoGui.VERSION_NAME_LONG));
}
}
@ -2591,7 +2608,7 @@ public class Editor extends JFrame implements RunnerListener {
printerJob.setPrintable(getCurrentTab().getTextArea());
}
// set the name of the job to the code name
printerJob.setJobName(sketch.getCurrentCode().getPrettyName());
printerJob.setJobName(getCurrentTab().getSketchCode().getPrettyName());
if (printerJob.printDialog()) {
try {

View File

@ -32,7 +32,7 @@ import static processing.app.I18n.tr;
import java.awt.*;
import java.awt.event.*;
import java.io.IOException;
import java.util.List;
import javax.swing.*;
import static processing.app.Theme.scale;
@ -235,15 +235,18 @@ public class EditorHeader extends JComponent {
g.setColor(backgroundColor);
g.fillRect(0, 0, imageW, imageH);
int codeCount = sketch.getCodeCount();
List<EditorTab> tabs = editor.getTabs();
int codeCount = tabs.size();
if ((tabLeft == null) || (tabLeft.length < codeCount)) {
tabLeft = new int[codeCount];
tabRight = new int[codeCount];
}
int x = scale(6); // offset from left edge of the component
for (int i = 0; i < sketch.getCodeCount(); i++) {
SketchCode code = sketch.getCode(i);
int i = 0;
for (EditorTab tab : tabs) {
SketchCode code = tab.getSketchCode();
String codeName = code.isExtension(sketch.getHiddenExtensions()) ?
code.getPrettyName() : code.getFileName();
@ -257,7 +260,7 @@ public class EditorHeader extends JComponent {
int pieceCount = 2 + (textWidth / PIECE_WIDTH);
int pieceWidth = pieceCount * PIECE_WIDTH;
int state = (code == sketch.getCurrentCode()) ? SELECTED : UNSELECTED;
int state = (i == editor.getCurrentTabIndex()) ? SELECTED : UNSELECTED;
g.drawImage(pieces[state][LEFT], x, 0, null);
x += PIECE_WIDTH;
@ -277,6 +280,7 @@ public class EditorHeader extends JComponent {
g.drawImage(pieces[state][RIGHT], x, 0, null);
x += PIECE_WIDTH - 1; // overlap by 1 pixel
i++;
}
menuLeft = sizeW - (16 + menuButtons[0].getWidth(this));
@ -323,7 +327,8 @@ public class EditorHeader extends JComponent {
if (sketch != null) {
menu.addSeparator();
int i = 0;
for (SketchCode code : sketch.getCodes()) {
for (EditorTab tab : editor.getTabs()) {
SketchCode code = tab.getSketchCode();
final int index = i++;
item = new JMenuItem(code.isExtension(sketch.getDefaultExtension()) ?
code.getPrettyName() : code.getFileName());