mirror of
https://github.com/arduino/Arduino.git
synced 2025-01-19 08:52:15 +01:00
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.
This commit is contained in:
parent
eba1098efb
commit
629953e20d
@ -542,8 +542,8 @@ public class Base {
|
||||
Collections.reverse(reversedEditors);
|
||||
int index = 0;
|
||||
for (Editor editor : reversedEditors) {
|
||||
Sketch sketch = editor.getSketch();
|
||||
String path = sketch.getMainFilePath();
|
||||
SketchController sketch = editor.getSketchController();
|
||||
String path = sketch.getSketch().getMainFilePath();
|
||||
// Skip untitled sketches if they do not contains changes.
|
||||
if (path.startsWith(untitledPath) && !sketch.isModified()) {
|
||||
continue;
|
||||
@ -581,13 +581,13 @@ public class Base {
|
||||
return PApplet.parseInt(PApplet.split(locationStr, ','));
|
||||
}
|
||||
|
||||
protected void storeRecentSketches(Sketch sketch) {
|
||||
protected void storeRecentSketches(SketchController sketch) {
|
||||
if (sketch.isUntitled()) {
|
||||
return;
|
||||
}
|
||||
|
||||
Set<String> sketches = new LinkedHashSet<String>();
|
||||
sketches.add(sketch.getMainFilePath());
|
||||
sketches.add(sketch.getSketch().getMainFilePath());
|
||||
sketches.addAll(PreferencesData.getCollection("recent.sketches"));
|
||||
|
||||
PreferencesData.setCollection("recent.sketches", sketches);
|
||||
@ -871,7 +871,7 @@ public class Base {
|
||||
Editor editor = new Editor(this, file, storedLocation, defaultLocation, BaseNoGui.getPlatform());
|
||||
|
||||
// Make sure that the sketch actually loaded
|
||||
if (editor.getSketch() == null) {
|
||||
if (editor.getSketchController() == null) {
|
||||
return null; // Just walk away quietly
|
||||
}
|
||||
|
||||
@ -883,7 +883,7 @@ public class Base {
|
||||
// Store information on who's open and running
|
||||
// (in case there's a crash or something that can't be recovered)
|
||||
storeSketches();
|
||||
storeRecentSketches(editor.getSketch());
|
||||
storeRecentSketches(editor.getSketchController());
|
||||
rebuildRecentSketchesMenuItems();
|
||||
PreferencesData.save();
|
||||
}
|
||||
@ -1176,7 +1176,7 @@ public class Base {
|
||||
public void actionPerformed(ActionEvent event) {
|
||||
UserLibrary l = (UserLibrary) getValue("library");
|
||||
try {
|
||||
activeEditor.getSketch().importLibrary(l);
|
||||
activeEditor.getSketchController().importLibrary(l);
|
||||
} catch (IOException e) {
|
||||
showWarning(tr("Error"), I18n.format("Unable to list header files in {0}", l.getSrcFolder()), e);
|
||||
}
|
||||
@ -1714,7 +1714,7 @@ public class Base {
|
||||
public void actionPerformed(ActionEvent event) {
|
||||
UserLibrary l = (UserLibrary) getValue("library");
|
||||
try {
|
||||
activeEditor.getSketch().importLibrary(l);
|
||||
activeEditor.getSketchController().importLibrary(l);
|
||||
} catch (IOException e) {
|
||||
showWarning(tr("Error"), I18n.format("Unable to list header files in {0}", l.getSrcFolder()), e);
|
||||
}
|
||||
|
@ -85,18 +85,18 @@ public class Editor extends JFrame implements RunnerListener {
|
||||
private ArrayList<EditorTab> tabs = new ArrayList<>();
|
||||
private int currentTabIndex = -1;
|
||||
|
||||
private static class ShouldSaveIfModified implements Predicate<Sketch> {
|
||||
private static class ShouldSaveIfModified implements Predicate<SketchController> {
|
||||
|
||||
@Override
|
||||
public boolean test(Sketch sketch) {
|
||||
public boolean test(SketchController sketch) {
|
||||
return PreferencesData.getBoolean("editor.save_on_verify") && sketch.isModified() && !sketch.isReadOnly(BaseNoGui.librariesIndexer.getInstalledLibraries(), BaseNoGui.getExamplesPath());
|
||||
}
|
||||
}
|
||||
|
||||
private static class ShouldSaveReadOnly implements Predicate<Sketch> {
|
||||
private static class ShouldSaveReadOnly implements Predicate<SketchController> {
|
||||
|
||||
@Override
|
||||
public boolean test(Sketch sketch) {
|
||||
public boolean test(SketchController sketch) {
|
||||
return sketch.isReadOnly(BaseNoGui.librariesIndexer.getInstalledLibraries(), BaseNoGui.getExamplesPath());
|
||||
}
|
||||
}
|
||||
@ -158,6 +158,7 @@ public class Editor extends JFrame implements RunnerListener {
|
||||
private JSplitPane splitPane;
|
||||
|
||||
// currently opened program
|
||||
SketchController sketchController;
|
||||
Sketch sketch;
|
||||
|
||||
EditorLineStatus lineStatus;
|
||||
@ -332,7 +333,7 @@ public class Editor extends JFrame implements RunnerListener {
|
||||
|
||||
// Open the document that was passed in
|
||||
boolean loaded = handleOpenInternal(file);
|
||||
if (!loaded) sketch = null;
|
||||
if (!loaded) sketchController = null;
|
||||
}
|
||||
|
||||
|
||||
@ -358,7 +359,7 @@ public class Editor extends JFrame implements RunnerListener {
|
||||
List<File> list = (List<File>)
|
||||
transferable.getTransferData(DataFlavor.javaFileListFlavor);
|
||||
for (File file : list) {
|
||||
if (sketch.addFile(file)) {
|
||||
if (sketchController.addFile(file)) {
|
||||
successful++;
|
||||
}
|
||||
}
|
||||
@ -376,7 +377,7 @@ public class Editor extends JFrame implements RunnerListener {
|
||||
} else if (piece.startsWith("file:/")) {
|
||||
path = piece.substring(5);
|
||||
}
|
||||
if (sketch.addFile(new File(path))) {
|
||||
if (sketchController.addFile(new File(path))) {
|
||||
successful++;
|
||||
}
|
||||
}
|
||||
@ -675,7 +676,7 @@ public class Editor extends JFrame implements RunnerListener {
|
||||
item = newJMenuItemAlt(tr("Export compiled Binary"), 'S');
|
||||
item.addActionListener(new ActionListener() {
|
||||
public void actionPerformed(ActionEvent e) {
|
||||
if (new ShouldSaveReadOnly().test(sketch) && !handleSave(true)) {
|
||||
if (new ShouldSaveReadOnly().test(sketchController) && !handleSave(true)) {
|
||||
System.out.println(tr("Export canceled, changes must first be saved."));
|
||||
return;
|
||||
}
|
||||
@ -713,7 +714,7 @@ public class Editor extends JFrame implements RunnerListener {
|
||||
item = new JMenuItem(tr("Add File..."));
|
||||
item.addActionListener(new ActionListener() {
|
||||
public void actionPerformed(ActionEvent e) {
|
||||
sketch.handleAddFile();
|
||||
sketchController.handleAddFile();
|
||||
}
|
||||
});
|
||||
sketchMenu.add(item);
|
||||
@ -1562,7 +1563,14 @@ public class Editor extends JFrame implements RunnerListener {
|
||||
|
||||
|
||||
/**
|
||||
* Gets the current sketch object.
|
||||
* Gets the current sketch controller.
|
||||
*/
|
||||
public SketchController getSketchController() {
|
||||
return sketchController;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the current sketch.
|
||||
*/
|
||||
public Sketch getSketch() {
|
||||
return sketch;
|
||||
@ -1719,9 +1727,9 @@ public class Editor extends JFrame implements RunnerListener {
|
||||
handleRun(verbose, new ShouldSaveIfModified(), verboseHandler, nonVerboseHandler);
|
||||
}
|
||||
|
||||
private void handleRun(final boolean verbose, Predicate<Sketch> shouldSavePredicate, Runnable verboseHandler, Runnable nonVerboseHandler) {
|
||||
private void handleRun(final boolean verbose, Predicate<SketchController> shouldSavePredicate, Runnable verboseHandler, Runnable nonVerboseHandler) {
|
||||
internalCloseRunner();
|
||||
if (shouldSavePredicate.test(sketch)) {
|
||||
if (shouldSavePredicate.test(sketchController)) {
|
||||
handleSave(true);
|
||||
}
|
||||
toolbar.activateRun();
|
||||
@ -1762,7 +1770,7 @@ public class Editor extends JFrame implements RunnerListener {
|
||||
public void run() {
|
||||
try {
|
||||
removeAllLineHighlights();
|
||||
sketch.build(verbose, saveHex);
|
||||
sketchController.build(verbose, saveHex);
|
||||
statusNotice(tr("Done compiling."));
|
||||
} catch (PreferencesMapException e) {
|
||||
statusError(I18n.format(
|
||||
@ -1830,14 +1838,15 @@ public class Editor extends JFrame implements RunnerListener {
|
||||
* @return false if canceling the close/quit operation
|
||||
*/
|
||||
protected boolean checkModified() {
|
||||
if (!sketch.isModified()) return true;
|
||||
if (!sketchController.isModified()) return true;
|
||||
|
||||
// As of Processing 1.0.10, this always happens immediately.
|
||||
// http://dev.processing.org/bugs/show_bug.cgi?id=1456
|
||||
|
||||
toFront();
|
||||
|
||||
String prompt = I18n.format(tr("Save changes to \"{0}\"? "), sketch.getName());
|
||||
String prompt = I18n.format(tr("Save changes to \"{0}\"? "),
|
||||
sketch.getName());
|
||||
|
||||
if (!OSUtils.isMacOS()) {
|
||||
int result =
|
||||
@ -1924,7 +1933,7 @@ public class Editor extends JFrame implements RunnerListener {
|
||||
// in a folder of the same name
|
||||
String fileName = sketchFile.getName();
|
||||
|
||||
File file = SketchData.checkSketchFile(sketchFile);
|
||||
File file = Sketch.checkSketchFile(sketchFile);
|
||||
|
||||
if (file == null) {
|
||||
if (!fileName.endsWith(".ino") && !fileName.endsWith(".pde")) {
|
||||
@ -1980,11 +1989,12 @@ public class Editor extends JFrame implements RunnerListener {
|
||||
}
|
||||
|
||||
try {
|
||||
sketch = new Sketch(this, file);
|
||||
sketch = new Sketch(file);
|
||||
} catch (IOException e) {
|
||||
Base.showWarning(tr("Error"), tr("Could not create the sketch."), e);
|
||||
return false;
|
||||
}
|
||||
sketchController = new SketchController(this, sketch);
|
||||
createTabs();
|
||||
|
||||
// Disable untitled setting from previous document, if any
|
||||
@ -1995,12 +2005,13 @@ public class Editor extends JFrame implements RunnerListener {
|
||||
}
|
||||
|
||||
private void updateTitle() {
|
||||
if (sketch == null) {
|
||||
if (sketchController == null) {
|
||||
return;
|
||||
}
|
||||
SketchCode current = getCurrentTab().getSketchCode();
|
||||
if (sketch.getName().equals(current.getPrettyName())) {
|
||||
setTitle(I18n.format(tr("{0} | Arduino {1}"), sketch.getName(), BaseNoGui.VERSION_NAME_LONG));
|
||||
setTitle(I18n.format(tr("{0} | Arduino {1}"), sketch.getName(),
|
||||
BaseNoGui.VERSION_NAME_LONG));
|
||||
} else {
|
||||
setTitle(I18n.format(tr("{0} - {1} | Arduino {2}"), sketch.getName(),
|
||||
current.getFileName(), BaseNoGui.VERSION_NAME_LONG));
|
||||
@ -2045,15 +2056,15 @@ public class Editor extends JFrame implements RunnerListener {
|
||||
statusNotice(tr("Saving..."));
|
||||
boolean saved = false;
|
||||
try {
|
||||
boolean wasReadOnly = sketch.isReadOnly(BaseNoGui.librariesIndexer.getInstalledLibraries(), BaseNoGui.getExamplesPath());
|
||||
boolean wasReadOnly = sketchController.isReadOnly(BaseNoGui.librariesIndexer.getInstalledLibraries(), BaseNoGui.getExamplesPath());
|
||||
String previousMainFilePath = sketch.getMainFilePath();
|
||||
saved = sketch.save();
|
||||
saved = sketchController.save();
|
||||
if (saved) {
|
||||
statusNotice(tr("Done Saving."));
|
||||
if (wasReadOnly) {
|
||||
base.removeRecentSketchPath(previousMainFilePath);
|
||||
}
|
||||
base.storeRecentSketches(sketch);
|
||||
base.storeRecentSketches(sketchController);
|
||||
base.rebuildRecentSketchesMenuItems();
|
||||
} else {
|
||||
statusEmpty();
|
||||
@ -2090,8 +2101,8 @@ public class Editor extends JFrame implements RunnerListener {
|
||||
//public void run() {
|
||||
statusNotice(tr("Saving..."));
|
||||
try {
|
||||
if (sketch.saveAs()) {
|
||||
base.storeRecentSketches(sketch);
|
||||
if (sketchController.saveAs()) {
|
||||
base.storeRecentSketches(sketchController);
|
||||
base.rebuildRecentSketchesMenuItems();
|
||||
statusNotice(tr("Done Saving."));
|
||||
// Disabling this for 0125, instead rebuild the menu inside
|
||||
@ -2158,7 +2169,7 @@ public class Editor extends JFrame implements RunnerListener {
|
||||
*/
|
||||
synchronized public void handleExport(final boolean usingProgrammer) {
|
||||
if (PreferencesData.getBoolean("editor.save_on_verify")) {
|
||||
if (sketch.isModified() && !sketch.isReadOnly(BaseNoGui.librariesIndexer.getInstalledLibraries(), BaseNoGui.getExamplesPath())) {
|
||||
if (sketchController.isModified() && !sketchController.isReadOnly(BaseNoGui.librariesIndexer.getInstalledLibraries(), BaseNoGui.getExamplesPath())) {
|
||||
handleSave(true);
|
||||
}
|
||||
}
|
||||
@ -2185,7 +2196,7 @@ public class Editor extends JFrame implements RunnerListener {
|
||||
|
||||
uploading = true;
|
||||
|
||||
boolean success = sketch.exportApplet(false);
|
||||
boolean success = sketchController.exportApplet(false);
|
||||
if (success) {
|
||||
statusNotice(tr("Done uploading."));
|
||||
}
|
||||
@ -2282,7 +2293,7 @@ public class Editor extends JFrame implements RunnerListener {
|
||||
|
||||
uploading = true;
|
||||
|
||||
boolean success = sketch.exportApplet(true);
|
||||
boolean success = sketchController.exportApplet(true);
|
||||
if (success) {
|
||||
statusNotice(tr("Done uploading."));
|
||||
}
|
||||
|
@ -88,14 +88,14 @@ public class EditorHeader extends JComponent {
|
||||
public class Actions {
|
||||
public final Action newTab = new SimpleAction(tr("New Tab"),
|
||||
Keys.ctrlShift(KeyEvent.VK_N),
|
||||
() -> editor.getSketch().handleNewCode());
|
||||
() -> editor.getSketchController().handleNewCode());
|
||||
|
||||
public final Action renameTab = new SimpleAction(tr("Rename"),
|
||||
() -> editor.getSketch().handleRenameCode());
|
||||
() -> editor.getSketchController().handleRenameCode());
|
||||
|
||||
public final Action deleteTab = new SimpleAction(tr("Delete"), () -> {
|
||||
try {
|
||||
editor.getSketch().handleDeleteCode();
|
||||
editor.getSketchController().handleDeleteCode();
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
@ -195,7 +195,7 @@ public class EditorHeader extends JComponent {
|
||||
public void paintComponent(Graphics screen) {
|
||||
if (screen == null) return;
|
||||
|
||||
Sketch sketch = editor.getSketch();
|
||||
SketchController sketch = editor.getSketchController();
|
||||
if (sketch == null) return; // ??
|
||||
|
||||
Dimension size = getSize();
|
||||
|
@ -246,7 +246,7 @@ public class EditorStatus extends JPanel {
|
||||
// answering to rename/new code question
|
||||
if (mode == EDIT) { // this if() isn't (shouldn't be?) necessary
|
||||
String answer = editField.getText();
|
||||
editor.getSketch().nameCode(answer);
|
||||
editor.getSketchController().nameCode(answer);
|
||||
unedit();
|
||||
}
|
||||
});
|
||||
@ -285,7 +285,7 @@ public class EditorStatus extends JPanel {
|
||||
|
||||
if (c == KeyEvent.VK_ENTER) { // accept the input
|
||||
String answer = editField.getText();
|
||||
editor.getSketch().nameCode(answer);
|
||||
editor.getSketchController().nameCode(answer);
|
||||
unedit();
|
||||
event.consume();
|
||||
|
||||
|
@ -361,8 +361,8 @@ public class EditorTab extends JPanel implements SketchCode.TextStorage {
|
||||
/**
|
||||
* Get the sketch this tab is editing a file from.
|
||||
*/
|
||||
public Sketch getSketch() {
|
||||
return editor.getSketch();
|
||||
public SketchController getSketch() {
|
||||
return editor.getSketchController();
|
||||
}
|
||||
|
||||
/**
|
||||
@ -440,7 +440,7 @@ public class EditorTab extends JPanel implements SketchCode.TextStorage {
|
||||
if (value != modified) {
|
||||
modified = value;
|
||||
// TODO: Improve decoupling
|
||||
editor.getSketch().calcModified();
|
||||
editor.getSketchController().calcModified();
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -52,19 +52,15 @@ import static processing.app.I18n.tr;
|
||||
|
||||
|
||||
/**
|
||||
* Stores information about files in the current sketch
|
||||
* Handles various tasks related to a sketch, in response to user inter-action.
|
||||
*/
|
||||
public class Sketch {
|
||||
public class SketchController {
|
||||
private final Editor editor;
|
||||
private final SketchData data;
|
||||
private final Sketch sketch;
|
||||
|
||||
/**
|
||||
* path is location of the main .pde file, because this is also
|
||||
* simplest to use when opening the file from the finder/explorer.
|
||||
*/
|
||||
public Sketch(Editor _editor, File file) throws IOException {
|
||||
public SketchController(Editor _editor, Sketch _sketch) {
|
||||
editor = _editor;
|
||||
data = new SketchData(file);
|
||||
sketch = _sketch;
|
||||
}
|
||||
|
||||
private boolean renamingCode;
|
||||
@ -146,7 +142,7 @@ public class Sketch {
|
||||
|
||||
// Add the extension here, this simplifies some of the logic below.
|
||||
if (newName.indexOf('.') == -1) {
|
||||
newName += "." + getDefaultExtension();
|
||||
newName += "." + sketch.getDefaultExtension();
|
||||
}
|
||||
|
||||
// if renaming to the same thing as before, just ignore.
|
||||
@ -185,7 +181,7 @@ public class Sketch {
|
||||
// Don't let the user create the main tab as a .java file instead of .pde
|
||||
if (!isDefaultExtension(newExtension)) {
|
||||
if (renamingCode) { // If creating a new tab, don't show this error
|
||||
if (current == data.getCode(0)) { // If this is the main tab, disallow
|
||||
if (current == sketch.getCode(0)) { // If this is the main tab, disallow
|
||||
Base.showWarning(tr("Problem with rename"),
|
||||
tr("The main file can't use an extension.\n" +
|
||||
"(It may be time for your to graduate to a\n" +
|
||||
@ -207,13 +203,13 @@ public class Sketch {
|
||||
// In Arduino, we want to allow files with the same name but different
|
||||
// extensions, so compare the full names (including extensions). This
|
||||
// might cause problems: http://dev.processing.org/bugs/show_bug.cgi?id=543
|
||||
for (SketchCode c : data.getCodes()) {
|
||||
for (SketchCode c : sketch.getCodes()) {
|
||||
if (newName.equalsIgnoreCase(c.getFileName()) && OSUtils.isWindows()) {
|
||||
Base.showMessage(tr("Error"),
|
||||
I18n.format(
|
||||
tr("A file named \"{0}\" already exists in \"{1}\""),
|
||||
c.getFileName(),
|
||||
data.getFolder().getAbsolutePath()
|
||||
sketch.getFolder().getAbsolutePath()
|
||||
));
|
||||
return;
|
||||
}
|
||||
@ -222,14 +218,14 @@ public class Sketch {
|
||||
// In Arduino, don't allow a .cpp file with the same name as the sketch,
|
||||
// because the sketch is concatenated into a file with that name as part
|
||||
// of the build process.
|
||||
if (newName.equals(getName() + ".cpp")) {
|
||||
if (newName.equals(sketch.getName() + ".cpp")) {
|
||||
Base.showMessage(tr("Error"),
|
||||
tr("You can't have a .cpp file with the same name as the sketch."));
|
||||
return;
|
||||
}
|
||||
|
||||
if (renamingCode && current.isPrimary()) {
|
||||
for (SketchCode code : data.getCodes()) {
|
||||
for (SketchCode code : sketch.getCodes()) {
|
||||
if (sanitaryName.equalsIgnoreCase(code.getPrettyName()) &&
|
||||
code.isExtension("cpp")) {
|
||||
Base.showMessage(tr("Error"),
|
||||
@ -242,7 +238,7 @@ public class Sketch {
|
||||
}
|
||||
|
||||
|
||||
File newFile = new File(data.getFolder(), newName);
|
||||
File newFile = new File(sketch.getFolder(), newName);
|
||||
// if (newFile.exists()) { // yay! users will try anything
|
||||
// Base.showMessage("Error",
|
||||
// "A file named \"" + newFile + "\" already exists\n" +
|
||||
@ -264,7 +260,7 @@ public class Sketch {
|
||||
if (current.isPrimary()) {
|
||||
// get the new folder name/location
|
||||
String folderName = newName.substring(0, newName.indexOf('.'));
|
||||
File newFolder = new File(data.getFolder().getParentFile(), folderName);
|
||||
File newFolder = new File(sketch.getFolder().getParentFile(), folderName);
|
||||
if (newFolder.exists()) {
|
||||
Base.showWarning(tr("Cannot Rename"),
|
||||
I18n.format(
|
||||
@ -302,7 +298,7 @@ public class Sketch {
|
||||
|
||||
// save each of the other tabs because this is gonna be re-opened
|
||||
try {
|
||||
for (SketchCode code : data.getCodes()) {
|
||||
for (SketchCode code : sketch.getCodes()) {
|
||||
code.save();
|
||||
}
|
||||
} catch (Exception e) {
|
||||
@ -311,7 +307,7 @@ public class Sketch {
|
||||
}
|
||||
|
||||
// now rename the sketch folder and re-open
|
||||
boolean success = data.getFolder().renameTo(newFolder);
|
||||
boolean success = sketch.getFolder().renameTo(newFolder);
|
||||
if (!success) {
|
||||
Base.showWarning(tr("Error"), tr("Could not rename the sketch. (2)"), null);
|
||||
return;
|
||||
@ -356,7 +352,7 @@ public class Sketch {
|
||||
I18n.format(
|
||||
"Could not create the file \"{0}\" in \"{1}\"",
|
||||
newFile,
|
||||
data.getFolder().getAbsolutePath()
|
||||
sketch.getFolder().getAbsolutePath()
|
||||
), e);
|
||||
return;
|
||||
}
|
||||
@ -371,7 +367,7 @@ public class Sketch {
|
||||
), e);
|
||||
return;
|
||||
}
|
||||
data.addCode(code);
|
||||
sketch.addCode(code);
|
||||
}
|
||||
|
||||
// set the new guy as current
|
||||
@ -421,7 +417,7 @@ public class Sketch {
|
||||
// to do a save on the handleNew()
|
||||
|
||||
// delete the entire sketch
|
||||
Base.removeDir(data.getFolder());
|
||||
Base.removeDir(sketch.getFolder());
|
||||
|
||||
// get the changes into the sketchbook menu
|
||||
//sketchbook.rebuildMenus();
|
||||
@ -433,14 +429,14 @@ public class Sketch {
|
||||
|
||||
} else {
|
||||
// delete the file
|
||||
if (!current.deleteFile(BaseNoGui.getBuildFolder(data).toPath())) {
|
||||
if (!current.deleteFile(BaseNoGui.getBuildFolder(sketch).toPath())) {
|
||||
Base.showMessage(tr("Couldn't do it"),
|
||||
I18n.format(tr("Could not delete \"{0}\"."), current.getFileName()));
|
||||
return;
|
||||
}
|
||||
|
||||
// remove code from the list
|
||||
data.removeCode(current);
|
||||
sketch.removeCode(current);
|
||||
|
||||
// just set current tab to the main tab
|
||||
editor.selectTab(0);
|
||||
@ -468,7 +464,7 @@ public class Sketch {
|
||||
|
||||
|
||||
public boolean isModified() {
|
||||
for (SketchCode code : data.getCodes()) {
|
||||
for (SketchCode code : sketch.getCodes()) {
|
||||
if (code.isModified())
|
||||
return true;
|
||||
}
|
||||
@ -491,7 +487,7 @@ public class Sketch {
|
||||
}
|
||||
|
||||
// rename .pde files to .ino
|
||||
File mainFile = new File(getMainFilePath());
|
||||
File mainFile = new File(sketch.getMainFilePath());
|
||||
File mainFolder = mainFile.getParentFile();
|
||||
File[] pdeFiles = mainFolder.listFiles((dir, name) -> {
|
||||
return name.toLowerCase().endsWith(".pde");
|
||||
@ -527,13 +523,13 @@ public class Sketch {
|
||||
}
|
||||
}
|
||||
|
||||
data.save();
|
||||
sketch.save();
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
private boolean renameCodeToInoExtension(File pdeFile) {
|
||||
for (SketchCode c : data.getCodes()) {
|
||||
for (SketchCode c : sketch.getCodes()) {
|
||||
if (!c.getFile().equals(pdeFile))
|
||||
continue;
|
||||
|
||||
@ -566,9 +562,9 @@ public class Sketch {
|
||||
// default to the parent folder of where this was
|
||||
// on macs a .getParentFile() method is required
|
||||
|
||||
fd.setDirectory(data.getFolder().getParentFile().getAbsolutePath());
|
||||
fd.setDirectory(sketch.getFolder().getParentFile().getAbsolutePath());
|
||||
}
|
||||
String oldName = data.getName();
|
||||
String oldName = sketch.getName();
|
||||
fd.setFile(oldName);
|
||||
|
||||
fd.setVisible(true);
|
||||
@ -577,15 +573,15 @@ public class Sketch {
|
||||
|
||||
// user canceled selection
|
||||
if (newName == null) return false;
|
||||
newName = Sketch.checkName(newName);
|
||||
newName = SketchController.checkName(newName);
|
||||
|
||||
File newFolder = new File(newParentDir, newName);
|
||||
|
||||
// make sure there doesn't exist a .cpp file with that name already
|
||||
// but ignore this situation for the first tab, since it's probably being
|
||||
// resaved (with the same name) to another location/folder.
|
||||
for (int i = 1; i < data.getCodeCount(); i++) {
|
||||
SketchCode code = data.getCode(i);
|
||||
for (int i = 1; i < sketch.getCodeCount(); i++) {
|
||||
SketchCode code = sketch.getCode(i);
|
||||
if (newName.equalsIgnoreCase(code.getPrettyName())) {
|
||||
Base.showMessage(tr("Error"),
|
||||
I18n.format(tr("You can't save the sketch as \"{0}\"\n" +
|
||||
@ -596,7 +592,7 @@ public class Sketch {
|
||||
}
|
||||
|
||||
// check if the paths are identical
|
||||
if (newFolder.equals(data.getFolder())) {
|
||||
if (newFolder.equals(sketch.getFolder())) {
|
||||
// just use "save" here instead, because the user will have received a
|
||||
// message (from the operating system) about "do you want to replace?"
|
||||
return save();
|
||||
@ -605,7 +601,7 @@ public class Sketch {
|
||||
// check to see if the user is trying to save this sketch inside itself
|
||||
try {
|
||||
String newPath = newFolder.getCanonicalPath() + File.separator;
|
||||
String oldPath = data.getFolder().getCanonicalPath() + File.separator;
|
||||
String oldPath = sketch.getFolder().getCanonicalPath() + File.separator;
|
||||
|
||||
if (newPath.indexOf(oldPath) == 0) {
|
||||
Base.showWarning(tr("How very Borges of you"),
|
||||
@ -630,21 +626,21 @@ public class Sketch {
|
||||
newFolder.mkdirs();
|
||||
|
||||
// save the other tabs to their new location
|
||||
for (SketchCode code : data.getCodes()) {
|
||||
if (data.indexOfCode(code) == 0) continue;
|
||||
for (SketchCode code : sketch.getCodes()) {
|
||||
if (sketch.indexOfCode(code) == 0) continue;
|
||||
File newFile = new File(newFolder, code.getFileName());
|
||||
code.saveAs(newFile);
|
||||
}
|
||||
|
||||
// re-copy the data folder (this may take a while.. add progress bar?)
|
||||
if (data.getDataFolder().exists()) {
|
||||
if (sketch.getDataFolder().exists()) {
|
||||
File newDataFolder = new File(newFolder, "data");
|
||||
Base.copyDir(data.getDataFolder(), newDataFolder);
|
||||
Base.copyDir(sketch.getDataFolder(), newDataFolder);
|
||||
}
|
||||
|
||||
// save the main tab with its new name
|
||||
File newFile = new File(newFolder, newName + ".ino");
|
||||
data.getCode(0).saveAs(newFile);
|
||||
sketch.getCode(0).saveAs(newFile);
|
||||
|
||||
editor.handleOpenUnchecked(newFile,
|
||||
editor.getCurrentTabIndex(),
|
||||
@ -723,16 +719,16 @@ public class Sketch {
|
||||
String codeExtension = null;
|
||||
boolean replacement = false;
|
||||
|
||||
for (String extension : SketchData.EXTENSIONS) {
|
||||
for (String extension : Sketch.EXTENSIONS) {
|
||||
String lower = filename.toLowerCase();
|
||||
if (lower.endsWith("." + extension)) {
|
||||
destFile = new File(data.getFolder(), filename);
|
||||
destFile = new File(sketch.getFolder(), filename);
|
||||
codeExtension = extension;
|
||||
}
|
||||
}
|
||||
if (codeExtension == null) {
|
||||
prepareDataFolder();
|
||||
destFile = new File(data.getDataFolder(), filename);
|
||||
destFile = new File(sketch.getDataFolder(), filename);
|
||||
}
|
||||
|
||||
// check whether this file already exists
|
||||
@ -794,11 +790,11 @@ public class Sketch {
|
||||
SketchCode newCode = new SketchCode(destFile, false);
|
||||
|
||||
if (replacement) {
|
||||
data.replaceCode(newCode);
|
||||
sketch.replaceCode(newCode);
|
||||
|
||||
} else {
|
||||
ensureExistence();
|
||||
data.addCode(newCode);
|
||||
sketch.addCode(newCode);
|
||||
}
|
||||
editor.selectTab(editor.findTabIndex(filename));
|
||||
}
|
||||
@ -878,7 +874,7 @@ public class Sketch {
|
||||
* @throws RunnerException
|
||||
*/
|
||||
public String build(boolean verbose, boolean save) throws RunnerException, PreferencesMapException, IOException {
|
||||
return build(BaseNoGui.getBuildFolder(data).getAbsolutePath(), verbose, save);
|
||||
return build(BaseNoGui.getBuildFolder(sketch).getAbsolutePath(), verbose, save);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -899,7 +895,7 @@ public class Sketch {
|
||||
CompilerProgressListener progressListener = editor.status::progressUpdate;
|
||||
|
||||
boolean deleteTemp = false;
|
||||
String pathToSketch = data.getMainFilePath();
|
||||
String pathToSketch = sketch.getMainFilePath();
|
||||
if (isModified()) {
|
||||
// If any files are modified, make a copy of the sketch with the changes
|
||||
// saved, so arduino-builder will see the modifications.
|
||||
@ -908,8 +904,7 @@ public class Sketch {
|
||||
}
|
||||
|
||||
try {
|
||||
return new Compiler(pathToSketch, data, buildPath).build(progressListener,
|
||||
save);
|
||||
return new Compiler(pathToSketch, sketch, buildPath).build(progressListener, save);
|
||||
} finally {
|
||||
// Make sure we clean up any temporary sketch copy
|
||||
if (deleteTemp)
|
||||
@ -919,17 +914,17 @@ public class Sketch {
|
||||
|
||||
private String saveSketchInTempFolder() throws IOException {
|
||||
File tempFolder = FileUtils.createTempFolder("arduino_modified_sketch_");
|
||||
FileUtils.copy(getFolder(), tempFolder);
|
||||
FileUtils.copy(sketch.getFolder(), tempFolder);
|
||||
|
||||
for (SketchCode sc : Stream.of(data.getCodes()).filter(SketchCode::isModified).collect(Collectors.toList())) {
|
||||
for (SketchCode sc : Stream.of(sketch.getCodes()).filter(SketchCode::isModified).collect(Collectors.toList())) {
|
||||
Files.write(Paths.get(tempFolder.getAbsolutePath(), sc.getFileName()), sc.getProgram().getBytes());
|
||||
}
|
||||
|
||||
return Paths.get(tempFolder.getAbsolutePath(), data.getPrimaryFile().getName()).toString();
|
||||
return Paths.get(tempFolder.getAbsolutePath(), sketch.getPrimaryFile().getName()).toString();
|
||||
}
|
||||
|
||||
protected boolean exportApplet(boolean usingProgrammer) throws Exception {
|
||||
return exportApplet(BaseNoGui.getBuildFolder(data).getAbsolutePath(), usingProgrammer);
|
||||
return exportApplet(BaseNoGui.getBuildFolder(sketch).getAbsolutePath(), usingProgrammer);
|
||||
}
|
||||
|
||||
|
||||
@ -982,7 +977,7 @@ public class Sketch {
|
||||
|
||||
List<String> warningsAccumulator = new LinkedList<>();
|
||||
try {
|
||||
success = uploaderInstance.upload(data, uploader, buildPath, suggestedClassName, usingProgrammer, false, warningsAccumulator);
|
||||
success = uploaderInstance.upload(sketch, uploader, buildPath, suggestedClassName, usingProgrammer, false, warningsAccumulator);
|
||||
} finally {
|
||||
if (uploader.requiresAuthorization() && !success) {
|
||||
PreferencesData.remove(uploader.getAuthorizationKey());
|
||||
@ -1015,16 +1010,16 @@ public class Sketch {
|
||||
* but not its contents.
|
||||
*/
|
||||
private void ensureExistence() {
|
||||
if (data.getFolder().exists()) return;
|
||||
if (sketch.getFolder().exists()) return;
|
||||
|
||||
Base.showWarning(tr("Sketch Disappeared"),
|
||||
tr("The sketch folder has disappeared.\n " +
|
||||
"Will attempt to re-save in the same location,\n" +
|
||||
"but anything besides the code will be lost."), null);
|
||||
try {
|
||||
data.getFolder().mkdirs();
|
||||
sketch.getFolder().mkdirs();
|
||||
|
||||
for (SketchCode code : data.getCodes()) {
|
||||
for (SketchCode code : sketch.getCodes()) {
|
||||
code.save(); // this will force a save
|
||||
}
|
||||
calcModified();
|
||||
@ -1045,7 +1040,7 @@ public class Sketch {
|
||||
* volumes or folders without appropriate permissions.
|
||||
*/
|
||||
public boolean isReadOnly(LibraryList libraries, String examplesPath) {
|
||||
String apath = data.getFolder().getAbsolutePath();
|
||||
String apath = sketch.getFolder().getAbsolutePath();
|
||||
|
||||
Optional<UserLibrary> libraryThatIncludesSketch = libraries.stream().filter(lib -> apath.startsWith(lib.getInstalledFolder().getAbsolutePath())).findFirst();
|
||||
if (libraryThatIncludesSketch.isPresent() && !libraryThatIncludesSketch.get().onGoingDevelopment()) {
|
||||
@ -1060,7 +1055,7 @@ public class Sketch {
|
||||
}
|
||||
|
||||
private boolean sketchFilesAreReadOnly() {
|
||||
for (SketchCode code : data.getCodes()) {
|
||||
for (SketchCode code : sketch.getCodes()) {
|
||||
if (code.isModified() && code.fileReadOnly() && code.fileExists()) {
|
||||
return true;
|
||||
}
|
||||
@ -1077,7 +1072,7 @@ public class Sketch {
|
||||
* True if the specified code has the default file extension.
|
||||
*/
|
||||
private boolean hasDefaultExtension(SketchCode code) {
|
||||
return code.isExtension(getDefaultExtension());
|
||||
return code.isExtension(sketch.getDefaultExtension());
|
||||
}
|
||||
|
||||
|
||||
@ -1085,7 +1080,7 @@ public class Sketch {
|
||||
* True if the specified extension is the default file extension.
|
||||
*/
|
||||
private boolean isDefaultExtension(String what) {
|
||||
return what.equals(getDefaultExtension());
|
||||
return what.equals(sketch.getDefaultExtension());
|
||||
}
|
||||
|
||||
|
||||
@ -1094,15 +1089,7 @@ public class Sketch {
|
||||
* extensions.
|
||||
*/
|
||||
private boolean validExtension(String what) {
|
||||
return SketchData.EXTENSIONS.contains(what);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Returns the default extension for this editor setup.
|
||||
*/
|
||||
public String getDefaultExtension() {
|
||||
return data.getDefaultExtension();
|
||||
return Sketch.EXTENSIONS.contains(what);
|
||||
}
|
||||
|
||||
static private final List<String> hiddenExtensions = Arrays.asList("ino", "pde");
|
||||
@ -1111,66 +1098,15 @@ public class Sketch {
|
||||
return hiddenExtensions;
|
||||
}
|
||||
|
||||
|
||||
// . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
|
||||
|
||||
// Additional accessors added in 0136 because of package work.
|
||||
// These will also be helpful for tool developers.
|
||||
|
||||
|
||||
/**
|
||||
* Returns the name of this sketch. (The pretty name of the main tab.)
|
||||
*/
|
||||
public String getName() {
|
||||
return data.getName();
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Returns path to the main .pde file for this sketch.
|
||||
*/
|
||||
public String getMainFilePath() {
|
||||
return data.getMainFilePath();
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Returns the sketch folder.
|
||||
*/
|
||||
public File getFolder() {
|
||||
return data.getFolder();
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Create the data folder if it does not exist already. As a convenience,
|
||||
* it also returns the data folder, since it's likely about to be used.
|
||||
*/
|
||||
private File prepareDataFolder() {
|
||||
if (!data.getDataFolder().exists()) {
|
||||
data.getDataFolder().mkdirs();
|
||||
if (!sketch.getDataFolder().exists()) {
|
||||
sketch.getDataFolder().mkdirs();
|
||||
}
|
||||
return data.getDataFolder();
|
||||
}
|
||||
|
||||
|
||||
public SketchCode[] getCodes() {
|
||||
return data.getCodes();
|
||||
}
|
||||
|
||||
|
||||
public int getCodeCount() {
|
||||
return data.getCodeCount();
|
||||
}
|
||||
|
||||
|
||||
public SketchCode getCode(int index) {
|
||||
return data.getCode(index);
|
||||
}
|
||||
|
||||
|
||||
public int getCodeIndex(SketchCode who) {
|
||||
return data.indexOfCode(who);
|
||||
return sketch.getDataFolder();
|
||||
}
|
||||
|
||||
|
||||
@ -1183,8 +1119,8 @@ public class Sketch {
|
||||
return editor.untitled;
|
||||
}
|
||||
|
||||
public boolean reload() throws IOException {
|
||||
return data.reload();
|
||||
public Sketch getSketch() {
|
||||
return sketch;
|
||||
}
|
||||
|
||||
// .................................................................
|
@ -75,7 +75,7 @@ public class ThinkDifferent {
|
||||
try {
|
||||
Base.INSTANCE.handleOpen(file);
|
||||
List<Editor> editors = Base.INSTANCE.getEditors();
|
||||
if (editors.size() == 2 && editors.get(0).getSketch().isUntitled()) {
|
||||
if (editors.size() == 2 && editors.get(0).getSketchController().isUntitled()) {
|
||||
Base.INSTANCE.handleClose(editors.get(0));
|
||||
}
|
||||
} catch (Exception e) {
|
||||
|
@ -26,7 +26,7 @@ package processing.app.tools;
|
||||
import org.apache.commons.compress.utils.IOUtils;
|
||||
import processing.app.Base;
|
||||
import processing.app.Editor;
|
||||
import processing.app.Sketch;
|
||||
import processing.app.SketchController;
|
||||
|
||||
import java.awt.*;
|
||||
import java.io.File;
|
||||
@ -69,7 +69,7 @@ public class Archiver implements Tool {
|
||||
|
||||
|
||||
public void run() {
|
||||
Sketch sketch = editor.getSketch();
|
||||
SketchController sketch = editor.getSketchController();
|
||||
|
||||
// first save the sketch so that things don't archive strangely
|
||||
boolean success = false;
|
||||
@ -84,7 +84,7 @@ public class Archiver implements Tool {
|
||||
return;
|
||||
}
|
||||
|
||||
File location = sketch.getFolder();
|
||||
File location = sketch.getSketch().getFolder();
|
||||
String name = location.getName();
|
||||
File parent = new File(location.getParent());
|
||||
|
||||
|
@ -49,7 +49,7 @@ public class FixEncoding implements Tool {
|
||||
|
||||
|
||||
public void run() {
|
||||
Sketch sketch = editor.getSketch();
|
||||
SketchController sketch = editor.getSketchController();
|
||||
//SketchCode code = sketch.current;
|
||||
|
||||
if (sketch.isModified()) {
|
||||
@ -65,8 +65,8 @@ public class FixEncoding implements Tool {
|
||||
}
|
||||
}
|
||||
try {
|
||||
for (int i = 0; i < sketch.getCodeCount(); i++) {
|
||||
SketchCode code = sketch.getCode(i);
|
||||
for (int i = 0; i < sketch.getSketch().getCodeCount(); i++) {
|
||||
SketchCode code = sketch.getSketch().getCode(i);
|
||||
editor.findTab(code).setText(loadWithLocalEncoding(code.getFile()));
|
||||
}
|
||||
} catch (IOException e) {
|
||||
|
@ -111,16 +111,16 @@ public class Compiler implements MessageConsumer {
|
||||
private static final Pattern ERROR_FORMAT = Pattern.compile("(.+\\.\\w+):(\\d+)(:\\d+)*:\\s*error:\\s*(.*)\\s*", Pattern.MULTILINE | Pattern.DOTALL);
|
||||
|
||||
private final String pathToSketch;
|
||||
private final SketchData sketch;
|
||||
private final Sketch sketch;
|
||||
private final String buildPath;
|
||||
private final boolean verbose;
|
||||
private RunnerException exception;
|
||||
|
||||
public Compiler(SketchData data, String buildPath) {
|
||||
public Compiler(Sketch data, String buildPath) {
|
||||
this(data.getMainFilePath(), data, buildPath);
|
||||
}
|
||||
|
||||
public Compiler(String pathToSketch, SketchData sketch, String buildPath) {
|
||||
public Compiler(String pathToSketch, Sketch sketch, String buildPath) {
|
||||
this.pathToSketch = pathToSketch;
|
||||
this.sketch = sketch;
|
||||
this.buildPath = buildPath;
|
||||
|
@ -34,7 +34,7 @@ import cc.arduino.packages.Uploader;
|
||||
import cc.arduino.packages.UploaderFactory;
|
||||
import processing.app.BaseNoGui;
|
||||
import processing.app.PreferencesData;
|
||||
import processing.app.SketchData;
|
||||
import processing.app.Sketch;
|
||||
import processing.app.debug.TargetPlatform;
|
||||
|
||||
import java.util.LinkedList;
|
||||
@ -56,7 +56,7 @@ public class UploaderUtils {
|
||||
return new UploaderFactory().newUploader(target.getBoards().get(board), boardPort, noUploadPort);
|
||||
}
|
||||
|
||||
public boolean upload(SketchData data, Uploader uploader, String buildPath, String suggestedClassName, boolean usingProgrammer, boolean noUploadPort, List<String> warningsAccumulator) throws Exception {
|
||||
public boolean upload(Sketch data, Uploader uploader, String buildPath, String suggestedClassName, boolean usingProgrammer, boolean noUploadPort, List<String> warningsAccumulator) throws Exception {
|
||||
|
||||
if (uploader == null)
|
||||
uploader = getUploaderByPreferences(noUploadPort);
|
||||
|
@ -124,7 +124,7 @@ public class BaseNoGui {
|
||||
return path;
|
||||
}
|
||||
|
||||
static public File getBuildFolder(SketchData data) throws IOException {
|
||||
static public File getBuildFolder(Sketch data) throws IOException {
|
||||
File buildFolder;
|
||||
if (PreferencesData.get("build.path") != null) {
|
||||
buildFolder = absoluteFile(PreferencesData.get("build.path"));
|
||||
@ -507,7 +507,7 @@ public class BaseNoGui {
|
||||
// This translates here as:
|
||||
// SketchData data = new SketchData(file);
|
||||
// File tempBuildFolder = getBuildFolder();
|
||||
SketchData data = new SketchData(absoluteFile(parser.getFilenames().get(0)));
|
||||
Sketch data = new Sketch(absoluteFile(parser.getFilenames().get(0)));
|
||||
File tempBuildFolder = getBuildFolder(data);
|
||||
|
||||
// Sketch.exportApplet()
|
||||
@ -553,7 +553,7 @@ public class BaseNoGui {
|
||||
// SketchData data = new SketchData(file);
|
||||
// File tempBuildFolder = getBuildFolder();
|
||||
// data.load();
|
||||
SketchData data = new SketchData(absoluteFile(path));
|
||||
Sketch data = new Sketch(absoluteFile(path));
|
||||
File tempBuildFolder = getBuildFolder(data);
|
||||
|
||||
// Sketch.prepare() calls Sketch.ensureExistence()
|
||||
|
@ -10,7 +10,10 @@ import processing.app.helpers.FileUtils;
|
||||
|
||||
import static processing.app.I18n.tr;
|
||||
|
||||
public class SketchData {
|
||||
/**
|
||||
* This represents a single sketch, consisting of one or more files.
|
||||
*/
|
||||
public class Sketch {
|
||||
|
||||
public static final List<String> SKETCH_EXTENSIONS = Arrays.asList("ino", "pde");
|
||||
public static final List<String> OTHER_ALLOWED_EXTENSIONS = Arrays.asList("c", "cpp", "h", "hh", "hpp", "s");
|
||||
@ -57,7 +60,7 @@ public class SketchData {
|
||||
* @param file
|
||||
* The primary file for this sketch.
|
||||
*/
|
||||
SketchData(File file) throws IOException {
|
||||
Sketch(File file) throws IOException {
|
||||
primaryFile = file;
|
||||
|
||||
// get the name of the sketch by chopping .pde or .java
|
Loading…
x
Reference in New Issue
Block a user