Skip to content

Commit 629953e

Browse files
matthijskooijmanfacchinm
authored andcommitted
Rename Sketch and SketchData classes
Sketch is now called SketchController, since it didn't really represent a sketch, but just handled the GUI-related stuff for a given sketch (note that it is not strictly a controller in the MVC-sense, but it does have a similar function). SketchData more accurately represented the actual sketch, so it is now called Sketch. Below, the new names are used. Editor now keeps both a current Sketch and SketchController object, and the Sketch object is created by Editor and passed to SketchController, instead passing a File and letting SketchController create the Sketch. Wherever possible, code now uses the Sketch directly (or indirectly, through the new `SketchController.getSketch()`) and the accessors in SketchController that merely forwarded to Sketch have been removed. There are few things that now live in SketchController but should be moved to Sketch (`isModified()`, `isUntitled()`), so some of the code still has a dependency on SketchController that should be removed later. This commit mostly renames classes, methods and variables, it should not change the behaviour in any way.
1 parent eba1098 commit 629953e

File tree

13 files changed

+137
-187
lines changed

13 files changed

+137
-187
lines changed

app/src/processing/app/Base.java

+8-8
Original file line numberDiff line numberDiff line change
@@ -542,8 +542,8 @@ protected void storeSketches() {
542542
Collections.reverse(reversedEditors);
543543
int index = 0;
544544
for (Editor editor : reversedEditors) {
545-
Sketch sketch = editor.getSketch();
546-
String path = sketch.getMainFilePath();
545+
SketchController sketch = editor.getSketchController();
546+
String path = sketch.getSketch().getMainFilePath();
547547
// Skip untitled sketches if they do not contains changes.
548548
if (path.startsWith(untitledPath) && !sketch.isModified()) {
549549
continue;
@@ -581,13 +581,13 @@ private int[] retrieveSketchLocation(String index) {
581581
return PApplet.parseInt(PApplet.split(locationStr, ','));
582582
}
583583

584-
protected void storeRecentSketches(Sketch sketch) {
584+
protected void storeRecentSketches(SketchController sketch) {
585585
if (sketch.isUntitled()) {
586586
return;
587587
}
588588

589589
Set<String> sketches = new LinkedHashSet<String>();
590-
sketches.add(sketch.getMainFilePath());
590+
sketches.add(sketch.getSketch().getMainFilePath());
591591
sketches.addAll(PreferencesData.getCollection("recent.sketches"));
592592

593593
PreferencesData.setCollection("recent.sketches", sketches);
@@ -871,7 +871,7 @@ protected Editor handleOpen(File file, int[] storedLocation, int[] defaultLocati
871871
Editor editor = new Editor(this, file, storedLocation, defaultLocation, BaseNoGui.getPlatform());
872872

873873
// Make sure that the sketch actually loaded
874-
if (editor.getSketch() == null) {
874+
if (editor.getSketchController() == null) {
875875
return null; // Just walk away quietly
876876
}
877877

@@ -883,7 +883,7 @@ protected Editor handleOpen(File file, int[] storedLocation, int[] defaultLocati
883883
// Store information on who's open and running
884884
// (in case there's a crash or something that can't be recovered)
885885
storeSketches();
886-
storeRecentSketches(editor.getSketch());
886+
storeRecentSketches(editor.getSketchController());
887887
rebuildRecentSketchesMenuItems();
888888
PreferencesData.save();
889889
}
@@ -1176,7 +1176,7 @@ public void actionPerformed(ActionEvent e) {
11761176
public void actionPerformed(ActionEvent event) {
11771177
UserLibrary l = (UserLibrary) getValue("library");
11781178
try {
1179-
activeEditor.getSketch().importLibrary(l);
1179+
activeEditor.getSketchController().importLibrary(l);
11801180
} catch (IOException e) {
11811181
showWarning(tr("Error"), I18n.format("Unable to list header files in {0}", l.getSrcFolder()), e);
11821182
}
@@ -1714,7 +1714,7 @@ protected void addLibraries(JMenu menu, LibraryList libs) throws IOException {
17141714
public void actionPerformed(ActionEvent event) {
17151715
UserLibrary l = (UserLibrary) getValue("library");
17161716
try {
1717-
activeEditor.getSketch().importLibrary(l);
1717+
activeEditor.getSketchController().importLibrary(l);
17181718
} catch (IOException e) {
17191719
showWarning(tr("Error"), I18n.format("Unable to list header files in {0}", l.getSrcFolder()), e);
17201720
}

app/src/processing/app/Editor.java

+38-27
Original file line numberDiff line numberDiff line change
@@ -85,18 +85,18 @@ public class Editor extends JFrame implements RunnerListener {
8585
private ArrayList<EditorTab> tabs = new ArrayList<>();
8686
private int currentTabIndex = -1;
8787

88-
private static class ShouldSaveIfModified implements Predicate<Sketch> {
88+
private static class ShouldSaveIfModified implements Predicate<SketchController> {
8989

9090
@Override
91-
public boolean test(Sketch sketch) {
91+
public boolean test(SketchController sketch) {
9292
return PreferencesData.getBoolean("editor.save_on_verify") && sketch.isModified() && !sketch.isReadOnly(BaseNoGui.librariesIndexer.getInstalledLibraries(), BaseNoGui.getExamplesPath());
9393
}
9494
}
9595

96-
private static class ShouldSaveReadOnly implements Predicate<Sketch> {
96+
private static class ShouldSaveReadOnly implements Predicate<SketchController> {
9797

9898
@Override
99-
public boolean test(Sketch sketch) {
99+
public boolean test(SketchController sketch) {
100100
return sketch.isReadOnly(BaseNoGui.librariesIndexer.getInstalledLibraries(), BaseNoGui.getExamplesPath());
101101
}
102102
}
@@ -158,6 +158,7 @@ public boolean test(Sketch sketch) {
158158
private JSplitPane splitPane;
159159

160160
// currently opened program
161+
SketchController sketchController;
161162
Sketch sketch;
162163

163164
EditorLineStatus lineStatus;
@@ -332,7 +333,7 @@ public void windowDeactivated(WindowEvent e) {
332333

333334
// Open the document that was passed in
334335
boolean loaded = handleOpenInternal(file);
335-
if (!loaded) sketch = null;
336+
if (!loaded) sketchController = null;
336337
}
337338

338339

@@ -358,7 +359,7 @@ public boolean importData(JComponent src, Transferable transferable) {
358359
List<File> list = (List<File>)
359360
transferable.getTransferData(DataFlavor.javaFileListFlavor);
360361
for (File file : list) {
361-
if (sketch.addFile(file)) {
362+
if (sketchController.addFile(file)) {
362363
successful++;
363364
}
364365
}
@@ -376,7 +377,7 @@ public boolean importData(JComponent src, Transferable transferable) {
376377
} else if (piece.startsWith("file:/")) {
377378
path = piece.substring(5);
378379
}
379-
if (sketch.addFile(new File(path))) {
380+
if (sketchController.addFile(new File(path))) {
380381
successful++;
381382
}
382383
}
@@ -675,7 +676,7 @@ public void actionPerformed(ActionEvent e) {
675676
item = newJMenuItemAlt(tr("Export compiled Binary"), 'S');
676677
item.addActionListener(new ActionListener() {
677678
public void actionPerformed(ActionEvent e) {
678-
if (new ShouldSaveReadOnly().test(sketch) && !handleSave(true)) {
679+
if (new ShouldSaveReadOnly().test(sketchController) && !handleSave(true)) {
679680
System.out.println(tr("Export canceled, changes must first be saved."));
680681
return;
681682
}
@@ -713,7 +714,7 @@ public void actionPerformed(ActionEvent e) {
713714
item = new JMenuItem(tr("Add File..."));
714715
item.addActionListener(new ActionListener() {
715716
public void actionPerformed(ActionEvent e) {
716-
sketch.handleAddFile();
717+
sketchController.handleAddFile();
717718
}
718719
});
719720
sketchMenu.add(item);
@@ -1562,7 +1563,14 @@ private void resetHandlers() {
15621563

15631564

15641565
/**
1565-
* Gets the current sketch object.
1566+
* Gets the current sketch controller.
1567+
*/
1568+
public SketchController getSketchController() {
1569+
return sketchController;
1570+
}
1571+
1572+
/**
1573+
* Gets the current sketch.
15661574
*/
15671575
public Sketch getSketch() {
15681576
return sketch;
@@ -1719,9 +1727,9 @@ public void handleRun(final boolean verbose, Runnable verboseHandler, Runnable n
17191727
handleRun(verbose, new ShouldSaveIfModified(), verboseHandler, nonVerboseHandler);
17201728
}
17211729

1722-
private void handleRun(final boolean verbose, Predicate<Sketch> shouldSavePredicate, Runnable verboseHandler, Runnable nonVerboseHandler) {
1730+
private void handleRun(final boolean verbose, Predicate<SketchController> shouldSavePredicate, Runnable verboseHandler, Runnable nonVerboseHandler) {
17231731
internalCloseRunner();
1724-
if (shouldSavePredicate.test(sketch)) {
1732+
if (shouldSavePredicate.test(sketchController)) {
17251733
handleSave(true);
17261734
}
17271735
toolbar.activateRun();
@@ -1762,7 +1770,7 @@ public BuildHandler(boolean verbose, boolean saveHex) {
17621770
public void run() {
17631771
try {
17641772
removeAllLineHighlights();
1765-
sketch.build(verbose, saveHex);
1773+
sketchController.build(verbose, saveHex);
17661774
statusNotice(tr("Done compiling."));
17671775
} catch (PreferencesMapException e) {
17681776
statusError(I18n.format(
@@ -1830,14 +1838,15 @@ public void internalCloseRunner() {
18301838
* @return false if canceling the close/quit operation
18311839
*/
18321840
protected boolean checkModified() {
1833-
if (!sketch.isModified()) return true;
1841+
if (!sketchController.isModified()) return true;
18341842

18351843
// As of Processing 1.0.10, this always happens immediately.
18361844
// http://dev.processing.org/bugs/show_bug.cgi?id=1456
18371845

18381846
toFront();
18391847

1840-
String prompt = I18n.format(tr("Save changes to \"{0}\"? "), sketch.getName());
1848+
String prompt = I18n.format(tr("Save changes to \"{0}\"? "),
1849+
sketch.getName());
18411850

18421851
if (!OSUtils.isMacOS()) {
18431852
int result =
@@ -1924,7 +1933,7 @@ protected boolean handleOpenInternal(File sketchFile) {
19241933
// in a folder of the same name
19251934
String fileName = sketchFile.getName();
19261935

1927-
File file = SketchData.checkSketchFile(sketchFile);
1936+
File file = Sketch.checkSketchFile(sketchFile);
19281937

19291938
if (file == null) {
19301939
if (!fileName.endsWith(".ino") && !fileName.endsWith(".pde")) {
@@ -1980,11 +1989,12 @@ protected boolean handleOpenInternal(File sketchFile) {
19801989
}
19811990

19821991
try {
1983-
sketch = new Sketch(this, file);
1992+
sketch = new Sketch(file);
19841993
} catch (IOException e) {
19851994
Base.showWarning(tr("Error"), tr("Could not create the sketch."), e);
19861995
return false;
19871996
}
1997+
sketchController = new SketchController(this, sketch);
19881998
createTabs();
19891999

19902000
// Disable untitled setting from previous document, if any
@@ -1995,12 +2005,13 @@ protected boolean handleOpenInternal(File sketchFile) {
19952005
}
19962006

19972007
private void updateTitle() {
1998-
if (sketch == null) {
2008+
if (sketchController == null) {
19992009
return;
20002010
}
20012011
SketchCode current = getCurrentTab().getSketchCode();
20022012
if (sketch.getName().equals(current.getPrettyName())) {
2003-
setTitle(I18n.format(tr("{0} | Arduino {1}"), sketch.getName(), BaseNoGui.VERSION_NAME_LONG));
2013+
setTitle(I18n.format(tr("{0} | Arduino {1}"), sketch.getName(),
2014+
BaseNoGui.VERSION_NAME_LONG));
20042015
} else {
20052016
setTitle(I18n.format(tr("{0} - {1} | Arduino {2}"), sketch.getName(),
20062017
current.getFileName(), BaseNoGui.VERSION_NAME_LONG));
@@ -2045,15 +2056,15 @@ private boolean handleSave2() {
20452056
statusNotice(tr("Saving..."));
20462057
boolean saved = false;
20472058
try {
2048-
boolean wasReadOnly = sketch.isReadOnly(BaseNoGui.librariesIndexer.getInstalledLibraries(), BaseNoGui.getExamplesPath());
2059+
boolean wasReadOnly = sketchController.isReadOnly(BaseNoGui.librariesIndexer.getInstalledLibraries(), BaseNoGui.getExamplesPath());
20492060
String previousMainFilePath = sketch.getMainFilePath();
2050-
saved = sketch.save();
2061+
saved = sketchController.save();
20512062
if (saved) {
20522063
statusNotice(tr("Done Saving."));
20532064
if (wasReadOnly) {
20542065
base.removeRecentSketchPath(previousMainFilePath);
20552066
}
2056-
base.storeRecentSketches(sketch);
2067+
base.storeRecentSketches(sketchController);
20572068
base.rebuildRecentSketchesMenuItems();
20582069
} else {
20592070
statusEmpty();
@@ -2090,8 +2101,8 @@ public boolean handleSaveAs() {
20902101
//public void run() {
20912102
statusNotice(tr("Saving..."));
20922103
try {
2093-
if (sketch.saveAs()) {
2094-
base.storeRecentSketches(sketch);
2104+
if (sketchController.saveAs()) {
2105+
base.storeRecentSketches(sketchController);
20952106
base.rebuildRecentSketchesMenuItems();
20962107
statusNotice(tr("Done Saving."));
20972108
// Disabling this for 0125, instead rebuild the menu inside
@@ -2158,7 +2169,7 @@ private boolean serialPrompt() {
21582169
*/
21592170
synchronized public void handleExport(final boolean usingProgrammer) {
21602171
if (PreferencesData.getBoolean("editor.save_on_verify")) {
2161-
if (sketch.isModified() && !sketch.isReadOnly(BaseNoGui.librariesIndexer.getInstalledLibraries(), BaseNoGui.getExamplesPath())) {
2172+
if (sketchController.isModified() && !sketchController.isReadOnly(BaseNoGui.librariesIndexer.getInstalledLibraries(), BaseNoGui.getExamplesPath())) {
21622173
handleSave(true);
21632174
}
21642175
}
@@ -2185,7 +2196,7 @@ public void run() {
21852196

21862197
uploading = true;
21872198

2188-
boolean success = sketch.exportApplet(false);
2199+
boolean success = sketchController.exportApplet(false);
21892200
if (success) {
21902201
statusNotice(tr("Done uploading."));
21912202
}
@@ -2282,7 +2293,7 @@ public void run() {
22822293

22832294
uploading = true;
22842295

2285-
boolean success = sketch.exportApplet(true);
2296+
boolean success = sketchController.exportApplet(true);
22862297
if (success) {
22872298
statusNotice(tr("Done uploading."));
22882299
}

app/src/processing/app/EditorHeader.java

+4-4
Original file line numberDiff line numberDiff line change
@@ -88,14 +88,14 @@ public class EditorHeader extends JComponent {
8888
public class Actions {
8989
public final Action newTab = new SimpleAction(tr("New Tab"),
9090
Keys.ctrlShift(KeyEvent.VK_N),
91-
() -> editor.getSketch().handleNewCode());
91+
() -> editor.getSketchController().handleNewCode());
9292

9393
public final Action renameTab = new SimpleAction(tr("Rename"),
94-
() -> editor.getSketch().handleRenameCode());
94+
() -> editor.getSketchController().handleRenameCode());
9595

9696
public final Action deleteTab = new SimpleAction(tr("Delete"), () -> {
9797
try {
98-
editor.getSketch().handleDeleteCode();
98+
editor.getSketchController().handleDeleteCode();
9999
} catch (IOException e) {
100100
e.printStackTrace();
101101
}
@@ -195,7 +195,7 @@ public void mousePressed(MouseEvent e) {
195195
public void paintComponent(Graphics screen) {
196196
if (screen == null) return;
197197

198-
Sketch sketch = editor.getSketch();
198+
SketchController sketch = editor.getSketchController();
199199
if (sketch == null) return; // ??
200200

201201
Dimension size = getSize();

app/src/processing/app/EditorStatus.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -246,7 +246,7 @@ private void initialize() {
246246
// answering to rename/new code question
247247
if (mode == EDIT) { // this if() isn't (shouldn't be?) necessary
248248
String answer = editField.getText();
249-
editor.getSketch().nameCode(answer);
249+
editor.getSketchController().nameCode(answer);
250250
unedit();
251251
}
252252
});
@@ -285,7 +285,7 @@ public void keyTyped(KeyEvent event) {
285285

286286
if (c == KeyEvent.VK_ENTER) { // accept the input
287287
String answer = editField.getText();
288-
editor.getSketch().nameCode(answer);
288+
editor.getSketchController().nameCode(answer);
289289
unedit();
290290
event.consume();
291291

app/src/processing/app/EditorTab.java

+3-3
Original file line numberDiff line numberDiff line change
@@ -361,8 +361,8 @@ public SketchTextArea getTextArea() {
361361
/**
362362
* Get the sketch this tab is editing a file from.
363363
*/
364-
public Sketch getSketch() {
365-
return editor.getSketch();
364+
public SketchController getSketch() {
365+
return editor.getSketchController();
366366
}
367367

368368
/**
@@ -440,7 +440,7 @@ private void setModified(boolean value) {
440440
if (value != modified) {
441441
modified = value;
442442
// TODO: Improve decoupling
443-
editor.getSketch().calcModified();
443+
editor.getSketchController().calcModified();
444444
}
445445
}
446446

0 commit comments

Comments
 (0)