Skip to content

Commit 32a97b2

Browse files
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 ad5c0a4 commit 32a97b2

File tree

13 files changed

+138
-187
lines changed

13 files changed

+138
-187
lines changed

app/src/processing/app/Base.java

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -570,7 +570,7 @@ protected void storeSketches() {
570570
String path = editor.getSketch().getMainFilePath();
571571
// In case of a crash, save untitled sketches if they contain changes.
572572
// (Added this for release 0158, may not be a good idea.)
573-
if (path.startsWith(untitledPath) && !editor.getSketch().isModified()) {
573+
if (path.startsWith(untitledPath) && !editor.getSketchController().isModified()) {
574574
continue;
575575
}
576576
PreferencesData.set("last.sketch" + index + ".path", path);
@@ -583,13 +583,13 @@ protected void storeSketches() {
583583
PreferencesData.setInteger("last.sketch.count", index);
584584
}
585585

586-
protected void storeRecentSketches(Sketch sketch) {
586+
protected void storeRecentSketches(SketchController sketch) {
587587
if (sketch.isUntitled()) {
588588
return;
589589
}
590590

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

595595
PreferencesData.setCollection("recent.sketches", sketches);
@@ -876,7 +876,7 @@ protected Editor handleOpen(File file, int[] storedLocation, int[] defaultLocati
876876
Editor editor = new Editor(this, file, storedLocation, defaultLocation, BaseNoGui.getPlatform());
877877

878878
// Make sure that the sketch actually loaded
879-
if (editor.getSketch() == null) {
879+
if (editor.getSketchController() == null) {
880880
return null; // Just walk away quietly
881881
}
882882

@@ -888,7 +888,7 @@ protected Editor handleOpen(File file, int[] storedLocation, int[] defaultLocati
888888
// Store information on who's open and running
889889
// (in case there's a crash or something that can't be recovered)
890890
storeSketches();
891-
storeRecentSketches(editor.getSketch());
891+
storeRecentSketches(editor.getSketchController());
892892
rebuildRecentSketchesMenuItems();
893893
PreferencesData.save();
894894
}
@@ -1184,7 +1184,7 @@ public void actionPerformed(ActionEvent e) {
11841184
public void actionPerformed(ActionEvent event) {
11851185
UserLibrary l = (UserLibrary) getValue("library");
11861186
try {
1187-
activeEditor.getSketch().importLibrary(l);
1187+
activeEditor.getSketchController().importLibrary(l);
11881188
} catch (IOException e) {
11891189
showWarning(tr("Error"), I18n.format("Unable to list header files in {0}", l.getSrcFolder()), e);
11901190
}
@@ -1719,7 +1719,7 @@ protected void addLibraries(JMenu menu, LibraryList libs) throws IOException {
17191719
public void actionPerformed(ActionEvent event) {
17201720
UserLibrary l = (UserLibrary) getValue("library");
17211721
try {
1722-
activeEditor.getSketch().importLibrary(l);
1722+
activeEditor.getSketchController().importLibrary(l);
17231723
} catch (IOException e) {
17241724
showWarning(tr("Error"), I18n.format("Unable to list header files in {0}", l.getSrcFolder()), e);
17251725
}

app/src/processing/app/Editor.java

Lines changed: 38 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -84,18 +84,18 @@ public class Editor extends JFrame implements RunnerListener {
8484
private ArrayList<EditorTab> tabs = new ArrayList<>();
8585
private int currentTabIndex = -1;
8686

87-
private static class ShouldSaveIfModified implements Predicate<Sketch> {
87+
private static class ShouldSaveIfModified implements Predicate<SketchController> {
8888

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

95-
private static class ShouldSaveReadOnly implements Predicate<Sketch> {
95+
private static class ShouldSaveReadOnly implements Predicate<SketchController> {
9696

9797
@Override
98-
public boolean test(Sketch sketch) {
98+
public boolean test(SketchController sketch) {
9999
return sketch.isReadOnly(BaseNoGui.librariesIndexer.getInstalledLibraries(), BaseNoGui.getExamplesPath());
100100
}
101101
}
@@ -155,6 +155,7 @@ public boolean test(Sketch sketch) {
155155
private JSplitPane splitPane;
156156

157157
// currently opened program
158+
SketchController sketchController;
158159
Sketch sketch;
159160

160161
EditorLineStatus lineStatus;
@@ -341,7 +342,7 @@ public void windowDeactivated(WindowEvent e) {
341342

342343
// Open the document that was passed in
343344
boolean loaded = handleOpenInternal(file);
344-
if (!loaded) sketch = null;
345+
if (!loaded) sketchController = null;
345346

346347
// System.out.println("t5");
347348

@@ -372,7 +373,7 @@ public boolean importData(JComponent src, Transferable transferable) {
372373
List<File> list = (List<File>)
373374
transferable.getTransferData(DataFlavor.javaFileListFlavor);
374375
for (File file : list) {
375-
if (sketch.addFile(file)) {
376+
if (sketchController.addFile(file)) {
376377
successful++;
377378
}
378379
}
@@ -390,7 +391,7 @@ public boolean importData(JComponent src, Transferable transferable) {
390391
} else if (piece.startsWith("file:/")) {
391392
path = piece.substring(5);
392393
}
393-
if (sketch.addFile(new File(path))) {
394+
if (sketchController.addFile(new File(path))) {
394395
successful++;
395396
}
396397
}
@@ -701,7 +702,7 @@ public void actionPerformed(ActionEvent e) {
701702
item = newJMenuItemAlt(tr("Export compiled Binary"), 'S');
702703
item.addActionListener(new ActionListener() {
703704
public void actionPerformed(ActionEvent e) {
704-
if (new ShouldSaveReadOnly().test(sketch) && !handleSave(true)) {
705+
if (new ShouldSaveReadOnly().test(sketchController) && !handleSave(true)) {
705706
System.out.println(tr("Export canceled, changes must first be saved."));
706707
return;
707708
}
@@ -739,7 +740,7 @@ public void actionPerformed(ActionEvent e) {
739740
item = new JMenuItem(tr("Add File..."));
740741
item.addActionListener(new ActionListener() {
741742
public void actionPerformed(ActionEvent e) {
742-
sketch.handleAddFile();
743+
sketchController.handleAddFile();
743744
}
744745
});
745746
sketchMenu.add(item);
@@ -1571,7 +1572,14 @@ private void resetHandlers() {
15711572

15721573

15731574
/**
1574-
* Gets the current sketch object.
1575+
* Gets the current sketch controller.
1576+
*/
1577+
public SketchController getSketchController() {
1578+
return sketchController;
1579+
}
1580+
1581+
/**
1582+
* Gets the current sketch.
15751583
*/
15761584
public Sketch getSketch() {
15771585
return sketch;
@@ -1728,9 +1736,9 @@ public void handleRun(final boolean verbose, Runnable verboseHandler, Runnable n
17281736
handleRun(verbose, new ShouldSaveIfModified(), verboseHandler, nonVerboseHandler);
17291737
}
17301738

1731-
private void handleRun(final boolean verbose, Predicate<Sketch> shouldSavePredicate, Runnable verboseHandler, Runnable nonVerboseHandler) {
1739+
private void handleRun(final boolean verbose, Predicate<SketchController> shouldSavePredicate, Runnable verboseHandler, Runnable nonVerboseHandler) {
17321740
internalCloseRunner();
1733-
if (shouldSavePredicate.test(sketch)) {
1741+
if (shouldSavePredicate.test(sketchController)) {
17341742
handleSave(true);
17351743
}
17361744
toolbar.activateRun();
@@ -1771,7 +1779,7 @@ public BuildHandler(boolean verbose, boolean saveHex) {
17711779
public void run() {
17721780
try {
17731781
removeAllLineHighlights();
1774-
sketch.build(verbose, saveHex);
1782+
sketchController.build(verbose, saveHex);
17751783
statusNotice(tr("Done compiling."));
17761784
} catch (PreferencesMapException e) {
17771785
statusError(I18n.format(
@@ -1838,14 +1846,15 @@ public void internalCloseRunner() {
18381846
* @return false if canceling the close/quit operation
18391847
*/
18401848
protected boolean checkModified() {
1841-
if (!sketch.isModified()) return true;
1849+
if (!sketchController.isModified()) return true;
18421850

18431851
// As of Processing 1.0.10, this always happens immediately.
18441852
// http://dev.processing.org/bugs/show_bug.cgi?id=1456
18451853

18461854
toFront();
18471855

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

18501859
if (!OSUtils.isMacOS()) {
18511860
int result =
@@ -1932,7 +1941,7 @@ protected boolean handleOpenInternal(File sketchFile) {
19321941
// in a folder of the same name
19331942
String fileName = sketchFile.getName();
19341943

1935-
File file = SketchData.checkSketchFile(sketchFile);
1944+
File file = Sketch.checkSketchFile(sketchFile);
19361945

19371946
if (file == null) {
19381947
if (!fileName.endsWith(".ino") && !fileName.endsWith(".pde")) {
@@ -1988,11 +1997,12 @@ protected boolean handleOpenInternal(File sketchFile) {
19881997
}
19891998

19901999
try {
1991-
sketch = new Sketch(this, file);
2000+
sketch = new Sketch(file);
19922001
} catch (IOException e) {
19932002
Base.showWarning(tr("Error"), tr("Could not create the sketch."), e);
19942003
return false;
19952004
}
2005+
sketchController = new SketchController(this, sketch);
19962006
createTabs();
19972007

19982008
// Disable untitled setting from previous document, if any
@@ -2003,12 +2013,13 @@ protected boolean handleOpenInternal(File sketchFile) {
20032013
}
20042014

20052015
private void updateTitle() {
2006-
if (sketch == null) {
2016+
if (sketchController == null) {
20072017
return;
20082018
}
20092019
SketchCode current = getCurrentTab().getSketchCode();
20102020
if (sketch.getName().equals(current.getPrettyName())) {
2011-
setTitle(I18n.format(tr("{0} | Arduino {1}"), sketch.getName(), BaseNoGui.VERSION_NAME_LONG));
2021+
setTitle(I18n.format(tr("{0} | Arduino {1}"), sketch.getName(),
2022+
BaseNoGui.VERSION_NAME_LONG));
20122023
} else {
20132024
setTitle(I18n.format(tr("{0} - {1} | Arduino {2}"), sketch.getName(),
20142025
current.getFileName(), BaseNoGui.VERSION_NAME_LONG));
@@ -2053,15 +2064,15 @@ private boolean handleSave2() {
20532064
statusNotice(tr("Saving..."));
20542065
boolean saved = false;
20552066
try {
2056-
boolean wasReadOnly = sketch.isReadOnly(BaseNoGui.librariesIndexer.getInstalledLibraries(), BaseNoGui.getExamplesPath());
2067+
boolean wasReadOnly = sketchController.isReadOnly(BaseNoGui.librariesIndexer.getInstalledLibraries(), BaseNoGui.getExamplesPath());
20572068
String previousMainFilePath = sketch.getMainFilePath();
2058-
saved = sketch.save();
2069+
saved = sketchController.save();
20592070
if (saved) {
20602071
statusNotice(tr("Done Saving."));
20612072
if (wasReadOnly) {
20622073
base.removeRecentSketchPath(previousMainFilePath);
20632074
}
2064-
base.storeRecentSketches(sketch);
2075+
base.storeRecentSketches(sketchController);
20652076
base.rebuildRecentSketchesMenuItems();
20662077
} else {
20672078
statusEmpty();
@@ -2098,8 +2109,8 @@ public boolean handleSaveAs() {
20982109
//public void run() {
20992110
statusNotice(tr("Saving..."));
21002111
try {
2101-
if (sketch.saveAs()) {
2102-
base.storeRecentSketches(sketch);
2112+
if (sketchController.saveAs()) {
2113+
base.storeRecentSketches(sketchController);
21032114
base.rebuildRecentSketchesMenuItems();
21042115
statusNotice(tr("Done Saving."));
21052116
// Disabling this for 0125, instead rebuild the menu inside
@@ -2166,7 +2177,7 @@ private boolean serialPrompt() {
21662177
*/
21672178
synchronized public void handleExport(final boolean usingProgrammer) {
21682179
if (PreferencesData.getBoolean("editor.save_on_verify")) {
2169-
if (sketch.isModified() && !sketch.isReadOnly(BaseNoGui.librariesIndexer.getInstalledLibraries(), BaseNoGui.getExamplesPath())) {
2180+
if (sketchController.isModified() && !sketchController.isReadOnly(BaseNoGui.librariesIndexer.getInstalledLibraries(), BaseNoGui.getExamplesPath())) {
21702181
handleSave(true);
21712182
}
21722183
}
@@ -2192,7 +2203,7 @@ public void run() {
21922203

21932204
uploading = true;
21942205

2195-
boolean success = sketch.exportApplet(false);
2206+
boolean success = sketchController.exportApplet(false);
21962207
if (success) {
21972208
statusNotice(tr("Done uploading."));
21982209
}
@@ -2287,7 +2298,7 @@ public void run() {
22872298

22882299
uploading = true;
22892300

2290-
boolean success = sketch.exportApplet(true);
2301+
boolean success = sketchController.exportApplet(true);
22912302
if (success) {
22922303
statusNotice(tr("Done uploading."));
22932304
}

app/src/processing/app/EditorHeader.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -82,14 +82,14 @@ public class EditorHeader extends JComponent {
8282
public class Actions {
8383
public final Action newTab = new SimpleAction(tr("New Tab"),
8484
Keys.ctrlShift(KeyEvent.VK_N),
85-
() -> editor.getSketch().handleNewCode());
85+
() -> editor.getSketchController().handleNewCode());
8686

8787
public final Action renameTab = new SimpleAction(tr("Rename"),
88-
() -> editor.getSketch().handleRenameCode());
88+
() -> editor.getSketchController().handleRenameCode());
8989

9090
public final Action deleteTab = new SimpleAction(tr("Delete"), () -> {
9191
try {
92-
editor.getSketch().handleDeleteCode();
92+
editor.getSketchController().handleDeleteCode();
9393
} catch (IOException e) {
9494
e.printStackTrace();
9595
}
@@ -184,7 +184,7 @@ public void mousePressed(MouseEvent e) {
184184
public void paintComponent(Graphics screen) {
185185
if (screen == null) return;
186186

187-
Sketch sketch = editor.getSketch();
187+
SketchController sketch = editor.getSketchController();
188188
if (sketch == null) return; // ??
189189

190190
Dimension size = getSize();

app/src/processing/app/EditorStatus.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -242,7 +242,7 @@ private void initialize() {
242242
// answering to rename/new code question
243243
if (mode == EDIT) { // this if() isn't (shouldn't be?) necessary
244244
String answer = editField.getText();
245-
editor.getSketch().nameCode(answer);
245+
editor.getSketchController().nameCode(answer);
246246
unedit();
247247
}
248248
});
@@ -281,7 +281,7 @@ public void keyTyped(KeyEvent event) {
281281

282282
if (c == KeyEvent.VK_ENTER) { // accept the input
283283
String answer = editField.getText();
284-
editor.getSketch().nameCode(answer);
284+
editor.getSketchController().nameCode(answer);
285285
unedit();
286286
event.consume();
287287

app/src/processing/app/EditorTab.java

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,8 @@ private SketchTextArea createTextArea(RSyntaxDocument document)
157157
@Override
158158
public void hyperlinkUpdate(HyperlinkEvent hyperlinkEvent) {
159159
try {
160-
editor.platform.openURL(editor.getSketch().getFolder(), hyperlinkEvent.getURL().toExternalForm());
160+
editor.platform.openURL(editor.getSketch().getFolder(),
161+
hyperlinkEvent.getURL().toExternalForm());
161162
} catch (Exception e) {
162163
Base.showWarning(e.getMessage(), e.getMessage(), e);
163164
}
@@ -357,8 +358,8 @@ public SketchTextArea getTextArea() {
357358
/**
358359
* Get the sketch this tab is editing a file from.
359360
*/
360-
public Sketch getSketch() {
361-
return editor.getSketch();
361+
public SketchController getSketch() {
362+
return editor.getSketchController();
362363
}
363364

364365
/**
@@ -415,7 +416,7 @@ private void setModified(boolean value) {
415416
if (value != modified) {
416417
modified = value;
417418
// TODO: Improve decoupling
418-
editor.getSketch().calcModified();
419+
editor.getSketchController().calcModified();
419420
}
420421
}
421422

0 commit comments

Comments
 (0)