2014-01-30 15:50:09 +01:00
|
|
|
package processing.app;
|
|
|
|
|
2014-04-07 18:20:17 +02:00
|
|
|
import java.io.File;
|
|
|
|
import java.io.IOException;
|
2015-05-25 12:53:51 +02:00
|
|
|
import java.util.*;
|
2015-08-05 12:09:24 +02:00
|
|
|
import java.util.stream.Collectors;
|
|
|
|
import java.util.stream.Stream;
|
|
|
|
|
Clean up sketch loading
Previously, the Sketch constructor called its `load()` function, which
called the `SketchData.load()` function to load files and then
`Editor.sketchLoaded()` to initialize the GUI with the loaded files.
When external editing was enabled, `Sketch.load()` was called again
when activating the Arduino app, to reload the entire sketch.
With this commit, the `Sketch.load()` function is removed, and
`SketchData.load()` is called from the SketchData constructor. Instead
of Sketch calling `Editor.sketchLoaded()`, that method is renamed
to `createTabs()` and called by `Editor.HandleOpenInternal()` directly
after creating the Sketch object.
Handling of external editor mode has also changed. When the Arduino
application is activated, instead of fully reloading the sketch (through
the now-absent `Sketch.load()` method), the new `SketchData.reload()`
method is called to reload the list of files in the sketch. If it
changed, all tabs are re-created. If not, only the current tab is
reloaded. When the user switches from one tab to another, that tab is
also reloaded. This ensures that the visible tab is always up-to-date,
without needlessly reloading all tabs all the time. When external
editing mode is enabled or disabled, all tabs are reloaded too, to make
sure they are up-to-date.
When re-creating all tabs, no attempt is made to preserve the currently
selected tab. Since adding or removing files happens rarely, this should
not be a problem. When files are changed, the currently selected tab is
implicitly preserved (because the tab is reloaded, not recreated). The
caret (and thus scroll) position is preserved by temporarily changing
the caret update policy, so the caret does not move while the text is
swapped out. This happens in `EditorTab.setText()` now, so other callers
can also profit from it.
To support checking for a changed list of files in
`SketchData.reload()`, a `SketchCode.equals()` method is added, that
just checks if the filenames are equal. Additionally, the loading of the
file list for a sketch has now moved from `SketchData.load()` to
`SketchData.listSketchFiles()`, so `reload()` can also use it. At the
same time, this loading is greatly simplified by using a sorted Set and
`FileUtils.listFiles()`.
In external editor mode, to ensure that during compilation the version
from disk is always used instead of the in-memory version, EditorTab
detaches itself from its SketchCode, so SketchCode has no access to the
(possibly outdated) in-memory contents of the file.
2015-12-09 09:27:25 +01:00
|
|
|
import processing.app.helpers.FileUtils;
|
|
|
|
|
2015-08-05 12:09:24 +02:00
|
|
|
import static processing.app.I18n.tr;
|
2014-01-30 15:50:09 +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.
2015-12-17 11:05:16 +01:00
|
|
|
/**
|
|
|
|
* This represents a single sketch, consisting of one or more files.
|
|
|
|
*/
|
|
|
|
public class Sketch {
|
2015-12-17 12:50:09 +01:00
|
|
|
public static final String DEFAULT_SKETCH_EXTENSION = "ino";
|
2015-12-21 16:48:59 +01:00
|
|
|
public static final List<String> OLD_SKETCH_EXTENSIONS = Arrays.asList("pde");
|
|
|
|
public static final List<String> SKETCH_EXTENSIONS = Stream.concat(Stream.of(DEFAULT_SKETCH_EXTENSION), OLD_SKETCH_EXTENSIONS.stream()).collect(Collectors.toList());
|
2015-07-09 04:23:33 +10:00
|
|
|
public static final List<String> OTHER_ALLOWED_EXTENSIONS = Arrays.asList("c", "cpp", "h", "hh", "hpp", "s");
|
2015-08-05 12:09:24 +02:00
|
|
|
public static final List<String> EXTENSIONS = Stream.concat(SKETCH_EXTENSIONS.stream(), OTHER_ALLOWED_EXTENSIONS.stream()).collect(Collectors.toList());
|
2015-05-25 12:53:51 +02:00
|
|
|
|
2015-08-05 12:09:24 +02:00
|
|
|
/**
|
|
|
|
* folder that contains this sketch
|
|
|
|
*/
|
2014-04-07 18:20:17 +02:00
|
|
|
private File folder;
|
|
|
|
|
2015-12-17 16:06:02 +01:00
|
|
|
private List<SketchFile> files = new ArrayList<SketchFile>();
|
2014-02-01 20:47:00 +01:00
|
|
|
|
2015-12-17 16:06:02 +01:00
|
|
|
private static final Comparator<SketchFile> CODE_DOCS_COMPARATOR = new Comparator<SketchFile>() {
|
2014-01-30 15:50:09 +01:00
|
|
|
@Override
|
2015-12-17 16:06:02 +01:00
|
|
|
public int compare(SketchFile x, SketchFile y) {
|
2015-12-08 19:04:18 +01:00
|
|
|
if (x.isPrimary() && !y.isPrimary())
|
|
|
|
return -1;
|
|
|
|
if (y.isPrimary() && !x.isPrimary())
|
|
|
|
return 1;
|
2014-02-01 20:47:00 +01:00
|
|
|
return x.getFileName().compareTo(y.getFileName());
|
2014-01-30 15:50:09 +01:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
Clean up sketch loading
Previously, the Sketch constructor called its `load()` function, which
called the `SketchData.load()` function to load files and then
`Editor.sketchLoaded()` to initialize the GUI with the loaded files.
When external editing was enabled, `Sketch.load()` was called again
when activating the Arduino app, to reload the entire sketch.
With this commit, the `Sketch.load()` function is removed, and
`SketchData.load()` is called from the SketchData constructor. Instead
of Sketch calling `Editor.sketchLoaded()`, that method is renamed
to `createTabs()` and called by `Editor.HandleOpenInternal()` directly
after creating the Sketch object.
Handling of external editor mode has also changed. When the Arduino
application is activated, instead of fully reloading the sketch (through
the now-absent `Sketch.load()` method), the new `SketchData.reload()`
method is called to reload the list of files in the sketch. If it
changed, all tabs are re-created. If not, only the current tab is
reloaded. When the user switches from one tab to another, that tab is
also reloaded. This ensures that the visible tab is always up-to-date,
without needlessly reloading all tabs all the time. When external
editing mode is enabled or disabled, all tabs are reloaded too, to make
sure they are up-to-date.
When re-creating all tabs, no attempt is made to preserve the currently
selected tab. Since adding or removing files happens rarely, this should
not be a problem. When files are changed, the currently selected tab is
implicitly preserved (because the tab is reloaded, not recreated). The
caret (and thus scroll) position is preserved by temporarily changing
the caret update policy, so the caret does not move while the text is
swapped out. This happens in `EditorTab.setText()` now, so other callers
can also profit from it.
To support checking for a changed list of files in
`SketchData.reload()`, a `SketchCode.equals()` method is added, that
just checks if the filenames are equal. Additionally, the loading of the
file list for a sketch has now moved from `SketchData.load()` to
`SketchData.listSketchFiles()`, so `reload()` can also use it. At the
same time, this loading is greatly simplified by using a sorted Set and
`FileUtils.listFiles()`.
In external editor mode, to ensure that during compilation the version
from disk is always used instead of the in-memory version, EditorTab
detaches itself from its SketchCode, so SketchCode has no access to the
(possibly outdated) in-memory contents of the file.
2015-12-09 09:27:25 +01:00
|
|
|
/**
|
|
|
|
* Create a new SketchData object, and looks at the sketch directory
|
|
|
|
* on disk to get populate the list of files in this sketch.
|
|
|
|
*
|
|
|
|
* @param file
|
Do not store the sketch name in Sketch
Sketch already stores the sketch folder, and the sketch name should be
identical to the folder name. In the case where the filename passed to
the sketch constructor is not the primary .ino file (named after the
sketch), this will slightly change behaviour. However, the calling code
should prevent this from happening, and with the old code, some internal
assumptions were probably violated, so this changes makes handling this
situation a bit more robust.
Since the actual filename passed to Sketch is no longer used, it is no
longer required to pass the name of the primary .ino file. At some
point, the constructor should probably be changed to accept a folder
name instead of a filename, but that would require a lot of changes
to trace this back through the code, so this is something for later.
2015-12-18 16:27:16 +01:00
|
|
|
* Any file inside the sketch directory.
|
Clean up sketch loading
Previously, the Sketch constructor called its `load()` function, which
called the `SketchData.load()` function to load files and then
`Editor.sketchLoaded()` to initialize the GUI with the loaded files.
When external editing was enabled, `Sketch.load()` was called again
when activating the Arduino app, to reload the entire sketch.
With this commit, the `Sketch.load()` function is removed, and
`SketchData.load()` is called from the SketchData constructor. Instead
of Sketch calling `Editor.sketchLoaded()`, that method is renamed
to `createTabs()` and called by `Editor.HandleOpenInternal()` directly
after creating the Sketch object.
Handling of external editor mode has also changed. When the Arduino
application is activated, instead of fully reloading the sketch (through
the now-absent `Sketch.load()` method), the new `SketchData.reload()`
method is called to reload the list of files in the sketch. If it
changed, all tabs are re-created. If not, only the current tab is
reloaded. When the user switches from one tab to another, that tab is
also reloaded. This ensures that the visible tab is always up-to-date,
without needlessly reloading all tabs all the time. When external
editing mode is enabled or disabled, all tabs are reloaded too, to make
sure they are up-to-date.
When re-creating all tabs, no attempt is made to preserve the currently
selected tab. Since adding or removing files happens rarely, this should
not be a problem. When files are changed, the currently selected tab is
implicitly preserved (because the tab is reloaded, not recreated). The
caret (and thus scroll) position is preserved by temporarily changing
the caret update policy, so the caret does not move while the text is
swapped out. This happens in `EditorTab.setText()` now, so other callers
can also profit from it.
To support checking for a changed list of files in
`SketchData.reload()`, a `SketchCode.equals()` method is added, that
just checks if the filenames are equal. Additionally, the loading of the
file list for a sketch has now moved from `SketchData.load()` to
`SketchData.listSketchFiles()`, so `reload()` can also use it. At the
same time, this loading is greatly simplified by using a sorted Set and
`FileUtils.listFiles()`.
In external editor mode, to ensure that during compilation the version
from disk is always used instead of the in-memory version, EditorTab
detaches itself from its SketchCode, so SketchCode has no access to the
(possibly outdated) in-memory contents of the file.
2015-12-09 09:27:25 +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.
2015-12-17 11:05:16 +01:00
|
|
|
Sketch(File file) throws IOException {
|
2015-12-21 16:54:56 +01:00
|
|
|
folder = file.getParentFile();
|
2015-12-17 16:06:02 +01:00
|
|
|
files = listSketchFiles(true);
|
2014-04-07 18:20:17 +02:00
|
|
|
}
|
|
|
|
|
2014-08-22 17:43:41 +02:00
|
|
|
static public File checkSketchFile(File file) {
|
2014-08-22 16:21:07 +02:00
|
|
|
// check to make sure that this .pde file is
|
|
|
|
// in a folder of the same name
|
|
|
|
String fileName = file.getName();
|
|
|
|
File parent = file.getParentFile();
|
|
|
|
String parentName = parent.getName();
|
|
|
|
String pdeName = parentName + ".pde";
|
|
|
|
File altPdeFile = new File(parent, pdeName);
|
|
|
|
String inoName = parentName + ".ino";
|
|
|
|
File altInoFile = new File(parent, inoName);
|
|
|
|
|
2014-08-22 17:43:41 +02:00
|
|
|
if (pdeName.equals(fileName) || inoName.equals(fileName))
|
|
|
|
return file;
|
|
|
|
|
|
|
|
if (altPdeFile.exists())
|
|
|
|
return altPdeFile;
|
|
|
|
|
|
|
|
if (altInoFile.exists())
|
|
|
|
return altInoFile;
|
|
|
|
|
|
|
|
return null;
|
2014-08-22 16:21:07 +02:00
|
|
|
}
|
|
|
|
|
2014-04-07 18:20:17 +02:00
|
|
|
/**
|
Clean up sketch loading
Previously, the Sketch constructor called its `load()` function, which
called the `SketchData.load()` function to load files and then
`Editor.sketchLoaded()` to initialize the GUI with the loaded files.
When external editing was enabled, `Sketch.load()` was called again
when activating the Arduino app, to reload the entire sketch.
With this commit, the `Sketch.load()` function is removed, and
`SketchData.load()` is called from the SketchData constructor. Instead
of Sketch calling `Editor.sketchLoaded()`, that method is renamed
to `createTabs()` and called by `Editor.HandleOpenInternal()` directly
after creating the Sketch object.
Handling of external editor mode has also changed. When the Arduino
application is activated, instead of fully reloading the sketch (through
the now-absent `Sketch.load()` method), the new `SketchData.reload()`
method is called to reload the list of files in the sketch. If it
changed, all tabs are re-created. If not, only the current tab is
reloaded. When the user switches from one tab to another, that tab is
also reloaded. This ensures that the visible tab is always up-to-date,
without needlessly reloading all tabs all the time. When external
editing mode is enabled or disabled, all tabs are reloaded too, to make
sure they are up-to-date.
When re-creating all tabs, no attempt is made to preserve the currently
selected tab. Since adding or removing files happens rarely, this should
not be a problem. When files are changed, the currently selected tab is
implicitly preserved (because the tab is reloaded, not recreated). The
caret (and thus scroll) position is preserved by temporarily changing
the caret update policy, so the caret does not move while the text is
swapped out. This happens in `EditorTab.setText()` now, so other callers
can also profit from it.
To support checking for a changed list of files in
`SketchData.reload()`, a `SketchCode.equals()` method is added, that
just checks if the filenames are equal. Additionally, the loading of the
file list for a sketch has now moved from `SketchData.load()` to
`SketchData.listSketchFiles()`, so `reload()` can also use it. At the
same time, this loading is greatly simplified by using a sorted Set and
`FileUtils.listFiles()`.
In external editor mode, to ensure that during compilation the version
from disk is always used instead of the in-memory version, EditorTab
detaches itself from its SketchCode, so SketchCode has no access to the
(possibly outdated) in-memory contents of the file.
2015-12-09 09:27:25 +01:00
|
|
|
* Reload the list of files. This checks the sketch directory on disk,
|
|
|
|
* to see if any files were added or removed. This does *not* check
|
|
|
|
* the contents of the files, just their presence.
|
|
|
|
*
|
|
|
|
* @return true when the list of files was changed, false when it was
|
|
|
|
* not.
|
2014-04-07 18:20:17 +02:00
|
|
|
*/
|
Clean up sketch loading
Previously, the Sketch constructor called its `load()` function, which
called the `SketchData.load()` function to load files and then
`Editor.sketchLoaded()` to initialize the GUI with the loaded files.
When external editing was enabled, `Sketch.load()` was called again
when activating the Arduino app, to reload the entire sketch.
With this commit, the `Sketch.load()` function is removed, and
`SketchData.load()` is called from the SketchData constructor. Instead
of Sketch calling `Editor.sketchLoaded()`, that method is renamed
to `createTabs()` and called by `Editor.HandleOpenInternal()` directly
after creating the Sketch object.
Handling of external editor mode has also changed. When the Arduino
application is activated, instead of fully reloading the sketch (through
the now-absent `Sketch.load()` method), the new `SketchData.reload()`
method is called to reload the list of files in the sketch. If it
changed, all tabs are re-created. If not, only the current tab is
reloaded. When the user switches from one tab to another, that tab is
also reloaded. This ensures that the visible tab is always up-to-date,
without needlessly reloading all tabs all the time. When external
editing mode is enabled or disabled, all tabs are reloaded too, to make
sure they are up-to-date.
When re-creating all tabs, no attempt is made to preserve the currently
selected tab. Since adding or removing files happens rarely, this should
not be a problem. When files are changed, the currently selected tab is
implicitly preserved (because the tab is reloaded, not recreated). The
caret (and thus scroll) position is preserved by temporarily changing
the caret update policy, so the caret does not move while the text is
swapped out. This happens in `EditorTab.setText()` now, so other callers
can also profit from it.
To support checking for a changed list of files in
`SketchData.reload()`, a `SketchCode.equals()` method is added, that
just checks if the filenames are equal. Additionally, the loading of the
file list for a sketch has now moved from `SketchData.load()` to
`SketchData.listSketchFiles()`, so `reload()` can also use it. At the
same time, this loading is greatly simplified by using a sorted Set and
`FileUtils.listFiles()`.
In external editor mode, to ensure that during compilation the version
from disk is always used instead of the in-memory version, EditorTab
detaches itself from its SketchCode, so SketchCode has no access to the
(possibly outdated) in-memory contents of the file.
2015-12-09 09:27:25 +01:00
|
|
|
public boolean reload() throws IOException {
|
2015-12-17 16:06:02 +01:00
|
|
|
List<SketchFile> reloaded = listSketchFiles(false);
|
|
|
|
if (!reloaded.equals(files)) {
|
|
|
|
files = reloaded;
|
Clean up sketch loading
Previously, the Sketch constructor called its `load()` function, which
called the `SketchData.load()` function to load files and then
`Editor.sketchLoaded()` to initialize the GUI with the loaded files.
When external editing was enabled, `Sketch.load()` was called again
when activating the Arduino app, to reload the entire sketch.
With this commit, the `Sketch.load()` function is removed, and
`SketchData.load()` is called from the SketchData constructor. Instead
of Sketch calling `Editor.sketchLoaded()`, that method is renamed
to `createTabs()` and called by `Editor.HandleOpenInternal()` directly
after creating the Sketch object.
Handling of external editor mode has also changed. When the Arduino
application is activated, instead of fully reloading the sketch (through
the now-absent `Sketch.load()` method), the new `SketchData.reload()`
method is called to reload the list of files in the sketch. If it
changed, all tabs are re-created. If not, only the current tab is
reloaded. When the user switches from one tab to another, that tab is
also reloaded. This ensures that the visible tab is always up-to-date,
without needlessly reloading all tabs all the time. When external
editing mode is enabled or disabled, all tabs are reloaded too, to make
sure they are up-to-date.
When re-creating all tabs, no attempt is made to preserve the currently
selected tab. Since adding or removing files happens rarely, this should
not be a problem. When files are changed, the currently selected tab is
implicitly preserved (because the tab is reloaded, not recreated). The
caret (and thus scroll) position is preserved by temporarily changing
the caret update policy, so the caret does not move while the text is
swapped out. This happens in `EditorTab.setText()` now, so other callers
can also profit from it.
To support checking for a changed list of files in
`SketchData.reload()`, a `SketchCode.equals()` method is added, that
just checks if the filenames are equal. Additionally, the loading of the
file list for a sketch has now moved from `SketchData.load()` to
`SketchData.listSketchFiles()`, so `reload()` can also use it. At the
same time, this loading is greatly simplified by using a sorted Set and
`FileUtils.listFiles()`.
In external editor mode, to ensure that during compilation the version
from disk is always used instead of the in-memory version, EditorTab
detaches itself from its SketchCode, so SketchCode has no access to the
(possibly outdated) in-memory contents of the file.
2015-12-09 09:27:25 +01:00
|
|
|
return true;
|
2015-05-20 16:32:27 +02:00
|
|
|
}
|
Clean up sketch loading
Previously, the Sketch constructor called its `load()` function, which
called the `SketchData.load()` function to load files and then
`Editor.sketchLoaded()` to initialize the GUI with the loaded files.
When external editing was enabled, `Sketch.load()` was called again
when activating the Arduino app, to reload the entire sketch.
With this commit, the `Sketch.load()` function is removed, and
`SketchData.load()` is called from the SketchData constructor. Instead
of Sketch calling `Editor.sketchLoaded()`, that method is renamed
to `createTabs()` and called by `Editor.HandleOpenInternal()` directly
after creating the Sketch object.
Handling of external editor mode has also changed. When the Arduino
application is activated, instead of fully reloading the sketch (through
the now-absent `Sketch.load()` method), the new `SketchData.reload()`
method is called to reload the list of files in the sketch. If it
changed, all tabs are re-created. If not, only the current tab is
reloaded. When the user switches from one tab to another, that tab is
also reloaded. This ensures that the visible tab is always up-to-date,
without needlessly reloading all tabs all the time. When external
editing mode is enabled or disabled, all tabs are reloaded too, to make
sure they are up-to-date.
When re-creating all tabs, no attempt is made to preserve the currently
selected tab. Since adding or removing files happens rarely, this should
not be a problem. When files are changed, the currently selected tab is
implicitly preserved (because the tab is reloaded, not recreated). The
caret (and thus scroll) position is preserved by temporarily changing
the caret update policy, so the caret does not move while the text is
swapped out. This happens in `EditorTab.setText()` now, so other callers
can also profit from it.
To support checking for a changed list of files in
`SketchData.reload()`, a `SketchCode.equals()` method is added, that
just checks if the filenames are equal. Additionally, the loading of the
file list for a sketch has now moved from `SketchData.load()` to
`SketchData.listSketchFiles()`, so `reload()` can also use it. At the
same time, this loading is greatly simplified by using a sorted Set and
`FileUtils.listFiles()`.
In external editor mode, to ensure that during compilation the version
from disk is always used instead of the in-memory version, EditorTab
detaches itself from its SketchCode, so SketchCode has no access to the
(possibly outdated) in-memory contents of the file.
2015-12-09 09:27:25 +01:00
|
|
|
return false;
|
|
|
|
}
|
2014-04-07 18:20:17 +02:00
|
|
|
|
Clean up sketch loading
Previously, the Sketch constructor called its `load()` function, which
called the `SketchData.load()` function to load files and then
`Editor.sketchLoaded()` to initialize the GUI with the loaded files.
When external editing was enabled, `Sketch.load()` was called again
when activating the Arduino app, to reload the entire sketch.
With this commit, the `Sketch.load()` function is removed, and
`SketchData.load()` is called from the SketchData constructor. Instead
of Sketch calling `Editor.sketchLoaded()`, that method is renamed
to `createTabs()` and called by `Editor.HandleOpenInternal()` directly
after creating the Sketch object.
Handling of external editor mode has also changed. When the Arduino
application is activated, instead of fully reloading the sketch (through
the now-absent `Sketch.load()` method), the new `SketchData.reload()`
method is called to reload the list of files in the sketch. If it
changed, all tabs are re-created. If not, only the current tab is
reloaded. When the user switches from one tab to another, that tab is
also reloaded. This ensures that the visible tab is always up-to-date,
without needlessly reloading all tabs all the time. When external
editing mode is enabled or disabled, all tabs are reloaded too, to make
sure they are up-to-date.
When re-creating all tabs, no attempt is made to preserve the currently
selected tab. Since adding or removing files happens rarely, this should
not be a problem. When files are changed, the currently selected tab is
implicitly preserved (because the tab is reloaded, not recreated). The
caret (and thus scroll) position is preserved by temporarily changing
the caret update policy, so the caret does not move while the text is
swapped out. This happens in `EditorTab.setText()` now, so other callers
can also profit from it.
To support checking for a changed list of files in
`SketchData.reload()`, a `SketchCode.equals()` method is added, that
just checks if the filenames are equal. Additionally, the loading of the
file list for a sketch has now moved from `SketchData.load()` to
`SketchData.listSketchFiles()`, so `reload()` can also use it. At the
same time, this loading is greatly simplified by using a sorted Set and
`FileUtils.listFiles()`.
In external editor mode, to ensure that during compilation the version
from disk is always used instead of the in-memory version, EditorTab
detaches itself from its SketchCode, so SketchCode has no access to the
(possibly outdated) in-memory contents of the file.
2015-12-09 09:27:25 +01:00
|
|
|
/**
|
|
|
|
* Scan this sketch's directory for files that should be loaded as
|
|
|
|
* part of this sketch. Doesn't modify this SketchData instance, just
|
|
|
|
* returns a filtered and sorted list of File objects ready to be
|
2015-12-17 16:06:02 +01:00
|
|
|
* passed to the SketchFile constructor.
|
2015-12-09 11:18:38 +01:00
|
|
|
*
|
|
|
|
* @param showWarnings
|
|
|
|
* When true, any invalid filenames will show a warning.
|
Clean up sketch loading
Previously, the Sketch constructor called its `load()` function, which
called the `SketchData.load()` function to load files and then
`Editor.sketchLoaded()` to initialize the GUI with the loaded files.
When external editing was enabled, `Sketch.load()` was called again
when activating the Arduino app, to reload the entire sketch.
With this commit, the `Sketch.load()` function is removed, and
`SketchData.load()` is called from the SketchData constructor. Instead
of Sketch calling `Editor.sketchLoaded()`, that method is renamed
to `createTabs()` and called by `Editor.HandleOpenInternal()` directly
after creating the Sketch object.
Handling of external editor mode has also changed. When the Arduino
application is activated, instead of fully reloading the sketch (through
the now-absent `Sketch.load()` method), the new `SketchData.reload()`
method is called to reload the list of files in the sketch. If it
changed, all tabs are re-created. If not, only the current tab is
reloaded. When the user switches from one tab to another, that tab is
also reloaded. This ensures that the visible tab is always up-to-date,
without needlessly reloading all tabs all the time. When external
editing mode is enabled or disabled, all tabs are reloaded too, to make
sure they are up-to-date.
When re-creating all tabs, no attempt is made to preserve the currently
selected tab. Since adding or removing files happens rarely, this should
not be a problem. When files are changed, the currently selected tab is
implicitly preserved (because the tab is reloaded, not recreated). The
caret (and thus scroll) position is preserved by temporarily changing
the caret update policy, so the caret does not move while the text is
swapped out. This happens in `EditorTab.setText()` now, so other callers
can also profit from it.
To support checking for a changed list of files in
`SketchData.reload()`, a `SketchCode.equals()` method is added, that
just checks if the filenames are equal. Additionally, the loading of the
file list for a sketch has now moved from `SketchData.load()` to
`SketchData.listSketchFiles()`, so `reload()` can also use it. At the
same time, this loading is greatly simplified by using a sorted Set and
`FileUtils.listFiles()`.
In external editor mode, to ensure that during compilation the version
from disk is always used instead of the in-memory version, EditorTab
detaches itself from its SketchCode, so SketchCode has no access to the
(possibly outdated) in-memory contents of the file.
2015-12-09 09:27:25 +01:00
|
|
|
*/
|
2015-12-17 16:06:02 +01:00
|
|
|
private List<SketchFile> listSketchFiles(boolean showWarnings) throws IOException {
|
|
|
|
Set<SketchFile> result = new TreeSet<>(CODE_DOCS_COMPARATOR);
|
Clean up sketch loading
Previously, the Sketch constructor called its `load()` function, which
called the `SketchData.load()` function to load files and then
`Editor.sketchLoaded()` to initialize the GUI with the loaded files.
When external editing was enabled, `Sketch.load()` was called again
when activating the Arduino app, to reload the entire sketch.
With this commit, the `Sketch.load()` function is removed, and
`SketchData.load()` is called from the SketchData constructor. Instead
of Sketch calling `Editor.sketchLoaded()`, that method is renamed
to `createTabs()` and called by `Editor.HandleOpenInternal()` directly
after creating the Sketch object.
Handling of external editor mode has also changed. When the Arduino
application is activated, instead of fully reloading the sketch (through
the now-absent `Sketch.load()` method), the new `SketchData.reload()`
method is called to reload the list of files in the sketch. If it
changed, all tabs are re-created. If not, only the current tab is
reloaded. When the user switches from one tab to another, that tab is
also reloaded. This ensures that the visible tab is always up-to-date,
without needlessly reloading all tabs all the time. When external
editing mode is enabled or disabled, all tabs are reloaded too, to make
sure they are up-to-date.
When re-creating all tabs, no attempt is made to preserve the currently
selected tab. Since adding or removing files happens rarely, this should
not be a problem. When files are changed, the currently selected tab is
implicitly preserved (because the tab is reloaded, not recreated). The
caret (and thus scroll) position is preserved by temporarily changing
the caret update policy, so the caret does not move while the text is
swapped out. This happens in `EditorTab.setText()` now, so other callers
can also profit from it.
To support checking for a changed list of files in
`SketchData.reload()`, a `SketchCode.equals()` method is added, that
just checks if the filenames are equal. Additionally, the loading of the
file list for a sketch has now moved from `SketchData.load()` to
`SketchData.listSketchFiles()`, so `reload()` can also use it. At the
same time, this loading is greatly simplified by using a sorted Set and
`FileUtils.listFiles()`.
In external editor mode, to ensure that during compilation the version
from disk is always used instead of the in-memory version, EditorTab
detaches itself from its SketchCode, so SketchCode has no access to the
(possibly outdated) in-memory contents of the file.
2015-12-09 09:27:25 +01:00
|
|
|
for (File file : FileUtils.listFiles(folder, false, EXTENSIONS)) {
|
|
|
|
if (BaseNoGui.isSanitaryName(file.getName())) {
|
2015-12-18 15:21:30 +01:00
|
|
|
FileUtils.SplitFile split = FileUtils.splitFilename(file);
|
|
|
|
boolean isPrimary = split.basename.equals(folder.getName()) && SKETCH_EXTENSIONS.contains(split.extension);
|
2015-12-29 16:42:14 +01:00
|
|
|
result.add(new SketchFile(this, file, isPrimary));
|
2015-12-09 11:18:38 +01:00
|
|
|
} else if (showWarnings) {
|
Clean up sketch loading
Previously, the Sketch constructor called its `load()` function, which
called the `SketchData.load()` function to load files and then
`Editor.sketchLoaded()` to initialize the GUI with the loaded files.
When external editing was enabled, `Sketch.load()` was called again
when activating the Arduino app, to reload the entire sketch.
With this commit, the `Sketch.load()` function is removed, and
`SketchData.load()` is called from the SketchData constructor. Instead
of Sketch calling `Editor.sketchLoaded()`, that method is renamed
to `createTabs()` and called by `Editor.HandleOpenInternal()` directly
after creating the Sketch object.
Handling of external editor mode has also changed. When the Arduino
application is activated, instead of fully reloading the sketch (through
the now-absent `Sketch.load()` method), the new `SketchData.reload()`
method is called to reload the list of files in the sketch. If it
changed, all tabs are re-created. If not, only the current tab is
reloaded. When the user switches from one tab to another, that tab is
also reloaded. This ensures that the visible tab is always up-to-date,
without needlessly reloading all tabs all the time. When external
editing mode is enabled or disabled, all tabs are reloaded too, to make
sure they are up-to-date.
When re-creating all tabs, no attempt is made to preserve the currently
selected tab. Since adding or removing files happens rarely, this should
not be a problem. When files are changed, the currently selected tab is
implicitly preserved (because the tab is reloaded, not recreated). The
caret (and thus scroll) position is preserved by temporarily changing
the caret update policy, so the caret does not move while the text is
swapped out. This happens in `EditorTab.setText()` now, so other callers
can also profit from it.
To support checking for a changed list of files in
`SketchData.reload()`, a `SketchCode.equals()` method is added, that
just checks if the filenames are equal. Additionally, the loading of the
file list for a sketch has now moved from `SketchData.load()` to
`SketchData.listSketchFiles()`, so `reload()` can also use it. At the
same time, this loading is greatly simplified by using a sorted Set and
`FileUtils.listFiles()`.
In external editor mode, to ensure that during compilation the version
from disk is always used instead of the in-memory version, EditorTab
detaches itself from its SketchCode, so SketchCode has no access to the
(possibly outdated) in-memory contents of the file.
2015-12-09 09:27:25 +01:00
|
|
|
System.err.println(I18n.format(tr("File name {0} is invalid: ignored"), file.getName()));
|
2014-04-07 18:20:17 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
Clean up sketch loading
Previously, the Sketch constructor called its `load()` function, which
called the `SketchData.load()` function to load files and then
`Editor.sketchLoaded()` to initialize the GUI with the loaded files.
When external editing was enabled, `Sketch.load()` was called again
when activating the Arduino app, to reload the entire sketch.
With this commit, the `Sketch.load()` function is removed, and
`SketchData.load()` is called from the SketchData constructor. Instead
of Sketch calling `Editor.sketchLoaded()`, that method is renamed
to `createTabs()` and called by `Editor.HandleOpenInternal()` directly
after creating the Sketch object.
Handling of external editor mode has also changed. When the Arduino
application is activated, instead of fully reloading the sketch (through
the now-absent `Sketch.load()` method), the new `SketchData.reload()`
method is called to reload the list of files in the sketch. If it
changed, all tabs are re-created. If not, only the current tab is
reloaded. When the user switches from one tab to another, that tab is
also reloaded. This ensures that the visible tab is always up-to-date,
without needlessly reloading all tabs all the time. When external
editing mode is enabled or disabled, all tabs are reloaded too, to make
sure they are up-to-date.
When re-creating all tabs, no attempt is made to preserve the currently
selected tab. Since adding or removing files happens rarely, this should
not be a problem. When files are changed, the currently selected tab is
implicitly preserved (because the tab is reloaded, not recreated). The
caret (and thus scroll) position is preserved by temporarily changing
the caret update policy, so the caret does not move while the text is
swapped out. This happens in `EditorTab.setText()` now, so other callers
can also profit from it.
To support checking for a changed list of files in
`SketchData.reload()`, a `SketchCode.equals()` method is added, that
just checks if the filenames are equal. Additionally, the loading of the
file list for a sketch has now moved from `SketchData.load()` to
`SketchData.listSketchFiles()`, so `reload()` can also use it. At the
same time, this loading is greatly simplified by using a sorted Set and
`FileUtils.listFiles()`.
In external editor mode, to ensure that during compilation the version
from disk is always used instead of the in-memory version, EditorTab
detaches itself from its SketchCode, so SketchCode has no access to the
(possibly outdated) in-memory contents of the file.
2015-12-09 09:27:25 +01:00
|
|
|
if (result.size() == 0)
|
2015-08-05 09:04:53 +02:00
|
|
|
throw new IOException(tr("No valid code files found"));
|
2014-04-07 18:20:17 +02:00
|
|
|
|
Clean up sketch loading
Previously, the Sketch constructor called its `load()` function, which
called the `SketchData.load()` function to load files and then
`Editor.sketchLoaded()` to initialize the GUI with the loaded files.
When external editing was enabled, `Sketch.load()` was called again
when activating the Arduino app, to reload the entire sketch.
With this commit, the `Sketch.load()` function is removed, and
`SketchData.load()` is called from the SketchData constructor. Instead
of Sketch calling `Editor.sketchLoaded()`, that method is renamed
to `createTabs()` and called by `Editor.HandleOpenInternal()` directly
after creating the Sketch object.
Handling of external editor mode has also changed. When the Arduino
application is activated, instead of fully reloading the sketch (through
the now-absent `Sketch.load()` method), the new `SketchData.reload()`
method is called to reload the list of files in the sketch. If it
changed, all tabs are re-created. If not, only the current tab is
reloaded. When the user switches from one tab to another, that tab is
also reloaded. This ensures that the visible tab is always up-to-date,
without needlessly reloading all tabs all the time. When external
editing mode is enabled or disabled, all tabs are reloaded too, to make
sure they are up-to-date.
When re-creating all tabs, no attempt is made to preserve the currently
selected tab. Since adding or removing files happens rarely, this should
not be a problem. When files are changed, the currently selected tab is
implicitly preserved (because the tab is reloaded, not recreated). The
caret (and thus scroll) position is preserved by temporarily changing
the caret update policy, so the caret does not move while the text is
swapped out. This happens in `EditorTab.setText()` now, so other callers
can also profit from it.
To support checking for a changed list of files in
`SketchData.reload()`, a `SketchCode.equals()` method is added, that
just checks if the filenames are equal. Additionally, the loading of the
file list for a sketch has now moved from `SketchData.load()` to
`SketchData.listSketchFiles()`, so `reload()` can also use it. At the
same time, this loading is greatly simplified by using a sorted Set and
`FileUtils.listFiles()`.
In external editor mode, to ensure that during compilation the version
from disk is always used instead of the in-memory version, EditorTab
detaches itself from its SketchCode, so SketchCode has no access to the
(possibly outdated) in-memory contents of the file.
2015-12-09 09:27:25 +01:00
|
|
|
return new ArrayList<>(result);
|
2014-04-07 18:20:17 +02:00
|
|
|
}
|
|
|
|
|
2015-12-17 13:00:47 +01:00
|
|
|
/**
|
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
public File prepareDataFolder() {
|
2015-12-18 15:13:54 +01:00
|
|
|
File dataFolder = getDataFolder();
|
2015-12-17 13:00:47 +01:00
|
|
|
if (!dataFolder.exists()) {
|
|
|
|
dataFolder.mkdirs();
|
|
|
|
}
|
|
|
|
return dataFolder;
|
|
|
|
}
|
|
|
|
|
2014-04-07 18:20:17 +02:00
|
|
|
public void save() throws IOException {
|
2015-12-17 16:06:02 +01:00
|
|
|
for (SketchFile file : getFiles()) {
|
|
|
|
if (file.isModified())
|
|
|
|
file.save();
|
2014-04-07 18:20:17 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-01-30 15:50:09 +01:00
|
|
|
public int getCodeCount() {
|
2015-12-17 16:06:02 +01:00
|
|
|
return files.size();
|
2014-01-30 15:50:09 +01:00
|
|
|
}
|
|
|
|
|
2015-12-17 16:06:02 +01:00
|
|
|
public SketchFile[] getFiles() {
|
|
|
|
return files.toArray(new SketchFile[0]);
|
2014-01-30 15:50:09 +01:00
|
|
|
}
|
|
|
|
|
2014-04-07 18:20:17 +02:00
|
|
|
/**
|
|
|
|
* Returns a file object for the primary .pde of this sketch.
|
|
|
|
*/
|
2015-12-18 15:21:30 +01:00
|
|
|
public SketchFile getPrimaryFile() {
|
|
|
|
return files.get(0);
|
2014-04-07 18:20:17 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns path to the main .pde file for this sketch.
|
|
|
|
*/
|
|
|
|
public String getMainFilePath() {
|
2015-12-18 15:21:30 +01:00
|
|
|
return getPrimaryFile().getFile().getAbsolutePath();
|
2014-04-07 18:20:17 +02:00
|
|
|
}
|
|
|
|
|
2015-12-17 16:06:02 +01:00
|
|
|
public SketchFile getFile(int i) {
|
|
|
|
return files.get(i);
|
2014-01-30 15:50:09 +01:00
|
|
|
}
|
|
|
|
|
2015-12-17 16:06:02 +01:00
|
|
|
protected void removeFile(SketchFile which) {
|
|
|
|
if (!files.remove(which))
|
2015-12-08 18:57:57 +01:00
|
|
|
System.err.println("removeCode: internal error.. could not find code");
|
2014-01-30 15:50:09 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public String getName() {
|
Do not store the sketch name in Sketch
Sketch already stores the sketch folder, and the sketch name should be
identical to the folder name. In the case where the filename passed to
the sketch constructor is not the primary .ino file (named after the
sketch), this will slightly change behaviour. However, the calling code
should prevent this from happening, and with the old code, some internal
assumptions were probably violated, so this changes makes handling this
situation a bit more robust.
Since the actual filename passed to Sketch is no longer used, it is no
longer required to pass the name of the primary .ino file. At some
point, the constructor should probably be changed to accept a folder
name instead of a filename, but that would require a lot of changes
to trace this back through the code, so this is something for later.
2015-12-18 16:27:16 +01:00
|
|
|
return folder.getName();
|
2014-01-30 15:50:09 +01:00
|
|
|
}
|
|
|
|
|
2014-04-07 18:20:17 +02:00
|
|
|
public File getFolder() {
|
|
|
|
return folder;
|
|
|
|
}
|
|
|
|
|
|
|
|
public File getDataFolder() {
|
2015-12-18 15:13:54 +01:00
|
|
|
return new File(folder, "data");
|
2014-04-07 18:20:17 +02:00
|
|
|
}
|
2015-12-17 11:30:44 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Is any of the files in this sketch modified?
|
|
|
|
*/
|
|
|
|
public boolean isModified() {
|
2015-12-17 16:06:02 +01:00
|
|
|
for (SketchFile file : files) {
|
|
|
|
if (file.isModified())
|
2015-12-17 11:30:44 +01:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
Refactor file adding and renaming, and save as handling
This commits replaces a significant part of the code handling these
features. A lot of responsibilities are moved from SketchController to
Sketch, though the code involved is rewritten mostly.
Most of the handling now happens inside Sketch, including various checks
against the new filename. Basically SketchController processes the user
input to decide what needs to be done, and Sketch checks if it can be
done and does it.
If problems occur, an IOException is thrown, using a translated error
message that is shown by SketchController as-is. This might not be the
best way to transfer error messages (regular IOExceptions might contain
less-friendly messages), so this might need further improvement later.
In addition to moving around code and responsibilities, this code also
changes behaviour in some places:
- Because Sketch and SketchFile are now in control of renames and
saves, they can update their internal state after a rename. This
removes the need for reloading the entire sketch after a rename or
save as and allows `Editor.handleOpenUnchecked()` to be removed.
- When renaming the entire sketch, all files used to be saved before
renaming, since the sketch would be re-opened after renaming. Since
the re-opening no longer happens, there is no longer a need to save
the sketch, so any unsaved changes remain unsaved in the editor after
renaming the sketch.
- When renaming or adding new files, duplicate filenames are detected.
Initially, this happened case sensitively, but it was later changed to
use case insensitive matching to prevent problems on Windows (where
filenames cannot differ in just case). To prevent complexity, this
did not distinguish between systems. In commit 5fbf9621f6 (Sketch
rename: allowig a case change rename if NOT on windows), the
intention was to only do case insensitive checking on Windows, but it
effectively disabled all checking on other systems, making the check
not catch duplicate filenames at all.
With this commit, all these checks are done using `File.equals()`
instead of comparing strings, which is already aware of the case
sensitivity of the platform and should act accordingly.
- Some error messages were changed.
- When adding a file, an empty file is not created directly, but only a
SketchFile and EditorTab is added. When the sketch is saved, the file
is created.
- When importing a file that already exists (thus overwriting it),
instead of replacing the SketchFile instance, this just lets the
EditorTab reload its contents. This was broken since the introduction
of EditorTab. The file would be replaced, but not this was not
reflected in the editor, which is now fixed. This change allows
`Sketch.replaceFile()` to be removed.
- When importing a file that does not exist yet (thus adding it), a tab
is now also added for it (in addition to a SketchFile). This was
broken since the introduction of EditorTab, and would result in the
file being added, but not shown in the editor.
This commit adds a `Sketch.renameFileTo()` method, to rename a single
file within the sketch. It would be better to integrate its contents
into `Sketch.renameTo()`, but that does not have access to the `Sketch`
instance it is contained in. This will be changed in a future commit.
2015-12-21 17:00:50 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Finds the file with the given filename and returns its index.
|
|
|
|
* Returns -1 when the file was not found.
|
|
|
|
*/
|
|
|
|
public int findFileIndex(File filename) {
|
|
|
|
int i = 0;
|
|
|
|
for (SketchFile file : files) {
|
|
|
|
if (file.getFile().equals(filename))
|
|
|
|
return i;
|
|
|
|
i++;
|
|
|
|
}
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Check if renaming/saving this sketch to the given folder would
|
|
|
|
* cause a problem because: 1. The new folder already exists 2.
|
|
|
|
* Renaming the primary file would cause a conflict with an existing
|
|
|
|
* file. If so, an IOEXception is thrown. If not, the name of the new
|
|
|
|
* primary file is returned.
|
|
|
|
*/
|
|
|
|
protected File checkNewFoldername(File newFolder) throws IOException {
|
|
|
|
String newPrimary = FileUtils.addExtension(newFolder.getName(), DEFAULT_SKETCH_EXTENSION);
|
|
|
|
// Verify the new folder does not exist yet
|
|
|
|
if (newFolder.exists()) {
|
|
|
|
String msg = I18n.format(tr("Sorry, the folder \"{0}\" already exists."), newFolder.getAbsoluteFile());
|
|
|
|
throw new IOException(msg);
|
|
|
|
}
|
|
|
|
|
|
|
|
// If the folder is actually renamed (as opposed to moved somewhere
|
|
|
|
// else), check for conflicts using the new filename, but the
|
|
|
|
// existing folder name.
|
|
|
|
if(newFolder.getName() != folder.getName())
|
|
|
|
checkNewFilename(new File(folder, newPrimary));
|
|
|
|
|
|
|
|
return new File(newFolder, newPrimary);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Check if renaming or adding a file would cause a problem because
|
|
|
|
* the file already exists in this sketch. If so, an IOEXception is
|
|
|
|
* thrown.
|
|
|
|
*
|
|
|
|
* @param newFile
|
|
|
|
* The filename of the new file, or the new name for an
|
|
|
|
* existing file.
|
|
|
|
*/
|
|
|
|
protected void checkNewFilename(File newFile) throws IOException {
|
|
|
|
// Verify that the sketch doesn't have a filem with the new name
|
|
|
|
// already, other than the current primary (index 0)
|
|
|
|
if (findFileIndex(newFile) >= 0) {
|
|
|
|
String msg = I18n.format(tr("The sketch already contains a file named \"{0}\""), newFile.getName());
|
|
|
|
throw new IOException(msg);
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Rename this sketch' folder to the given name. Unlike saveAs(), this
|
|
|
|
* moves the sketch directory, not leaving anything in the old place.
|
|
|
|
* This operation does not *save* the sketch, so the files on disk are
|
|
|
|
* moved, but not modified.
|
|
|
|
*
|
|
|
|
* @param newFolder
|
|
|
|
* The new folder name for this sketch. The new primary
|
|
|
|
* file's name will be derived from this.
|
|
|
|
*
|
|
|
|
* @throws IOException
|
|
|
|
* When a problem occurs. The error message should be
|
|
|
|
* already translated.
|
|
|
|
*/
|
|
|
|
public void renameTo(File newFolder) throws IOException {
|
|
|
|
// Check intended rename (throws if there is a problem)
|
|
|
|
File newPrimary = checkNewFoldername(newFolder);
|
|
|
|
|
|
|
|
// Rename the sketch folder
|
|
|
|
if (!getFolder().renameTo(newFolder))
|
|
|
|
throw new IOException(tr("Failed to rename sketch folder"));
|
|
|
|
|
|
|
|
folder = newFolder;
|
|
|
|
|
|
|
|
// Tell each file about its new name
|
|
|
|
for (SketchFile file : files)
|
|
|
|
file.renamedTo(new File(newFolder, file.getFileName()));
|
|
|
|
|
|
|
|
// And finally, rename the primary file
|
2015-12-29 16:51:49 +01:00
|
|
|
getPrimaryFile().renameTo(newPrimary.getName());
|
Refactor file adding and renaming, and save as handling
This commits replaces a significant part of the code handling these
features. A lot of responsibilities are moved from SketchController to
Sketch, though the code involved is rewritten mostly.
Most of the handling now happens inside Sketch, including various checks
against the new filename. Basically SketchController processes the user
input to decide what needs to be done, and Sketch checks if it can be
done and does it.
If problems occur, an IOException is thrown, using a translated error
message that is shown by SketchController as-is. This might not be the
best way to transfer error messages (regular IOExceptions might contain
less-friendly messages), so this might need further improvement later.
In addition to moving around code and responsibilities, this code also
changes behaviour in some places:
- Because Sketch and SketchFile are now in control of renames and
saves, they can update their internal state after a rename. This
removes the need for reloading the entire sketch after a rename or
save as and allows `Editor.handleOpenUnchecked()` to be removed.
- When renaming the entire sketch, all files used to be saved before
renaming, since the sketch would be re-opened after renaming. Since
the re-opening no longer happens, there is no longer a need to save
the sketch, so any unsaved changes remain unsaved in the editor after
renaming the sketch.
- When renaming or adding new files, duplicate filenames are detected.
Initially, this happened case sensitively, but it was later changed to
use case insensitive matching to prevent problems on Windows (where
filenames cannot differ in just case). To prevent complexity, this
did not distinguish between systems. In commit 5fbf9621f6 (Sketch
rename: allowig a case change rename if NOT on windows), the
intention was to only do case insensitive checking on Windows, but it
effectively disabled all checking on other systems, making the check
not catch duplicate filenames at all.
With this commit, all these checks are done using `File.equals()`
instead of comparing strings, which is already aware of the case
sensitivity of the platform and should act accordingly.
- Some error messages were changed.
- When adding a file, an empty file is not created directly, but only a
SketchFile and EditorTab is added. When the sketch is saved, the file
is created.
- When importing a file that already exists (thus overwriting it),
instead of replacing the SketchFile instance, this just lets the
EditorTab reload its contents. This was broken since the introduction
of EditorTab. The file would be replaced, but not this was not
reflected in the editor, which is now fixed. This change allows
`Sketch.replaceFile()` to be removed.
- When importing a file that does not exist yet (thus adding it), a tab
is now also added for it (in addition to a SketchFile). This was
broken since the introduction of EditorTab, and would result in the
file being added, but not shown in the editor.
This commit adds a `Sketch.renameFileTo()` method, to rename a single
file within the sketch. It would be better to integrate its contents
into `Sketch.renameTo()`, but that does not have access to the `Sketch`
instance it is contained in. This will be changed in a future commit.
2015-12-21 17:00:50 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public SketchFile addFile(String newName) throws IOException {
|
|
|
|
// Check the name will not cause any conflicts
|
|
|
|
File newFile = new File(folder, newName);
|
|
|
|
checkNewFilename(newFile);
|
|
|
|
|
|
|
|
// Add a new sketchFile
|
2015-12-29 16:42:14 +01:00
|
|
|
SketchFile sketchFile = new SketchFile(this, newFile, false);
|
Refactor file adding and renaming, and save as handling
This commits replaces a significant part of the code handling these
features. A lot of responsibilities are moved from SketchController to
Sketch, though the code involved is rewritten mostly.
Most of the handling now happens inside Sketch, including various checks
against the new filename. Basically SketchController processes the user
input to decide what needs to be done, and Sketch checks if it can be
done and does it.
If problems occur, an IOException is thrown, using a translated error
message that is shown by SketchController as-is. This might not be the
best way to transfer error messages (regular IOExceptions might contain
less-friendly messages), so this might need further improvement later.
In addition to moving around code and responsibilities, this code also
changes behaviour in some places:
- Because Sketch and SketchFile are now in control of renames and
saves, they can update their internal state after a rename. This
removes the need for reloading the entire sketch after a rename or
save as and allows `Editor.handleOpenUnchecked()` to be removed.
- When renaming the entire sketch, all files used to be saved before
renaming, since the sketch would be re-opened after renaming. Since
the re-opening no longer happens, there is no longer a need to save
the sketch, so any unsaved changes remain unsaved in the editor after
renaming the sketch.
- When renaming or adding new files, duplicate filenames are detected.
Initially, this happened case sensitively, but it was later changed to
use case insensitive matching to prevent problems on Windows (where
filenames cannot differ in just case). To prevent complexity, this
did not distinguish between systems. In commit 5fbf9621f6 (Sketch
rename: allowig a case change rename if NOT on windows), the
intention was to only do case insensitive checking on Windows, but it
effectively disabled all checking on other systems, making the check
not catch duplicate filenames at all.
With this commit, all these checks are done using `File.equals()`
instead of comparing strings, which is already aware of the case
sensitivity of the platform and should act accordingly.
- Some error messages were changed.
- When adding a file, an empty file is not created directly, but only a
SketchFile and EditorTab is added. When the sketch is saved, the file
is created.
- When importing a file that already exists (thus overwriting it),
instead of replacing the SketchFile instance, this just lets the
EditorTab reload its contents. This was broken since the introduction
of EditorTab. The file would be replaced, but not this was not
reflected in the editor, which is now fixed. This change allows
`Sketch.replaceFile()` to be removed.
- When importing a file that does not exist yet (thus adding it), a tab
is now also added for it (in addition to a SketchFile). This was
broken since the introduction of EditorTab, and would result in the
file being added, but not shown in the editor.
This commit adds a `Sketch.renameFileTo()` method, to rename a single
file within the sketch. It would be better to integrate its contents
into `Sketch.renameTo()`, but that does not have access to the `Sketch`
instance it is contained in. This will be changed in a future commit.
2015-12-21 17:00:50 +01:00
|
|
|
files.add(sketchFile);
|
|
|
|
Collections.sort(files, CODE_DOCS_COMPARATOR);
|
|
|
|
|
|
|
|
return sketchFile;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Save this sketch under the new name given. Unlike renameTo(), this
|
|
|
|
* leaves the existing sketch in place.
|
|
|
|
*
|
|
|
|
* @param newFolder
|
|
|
|
* The new folder name for this sketch. The new primary
|
|
|
|
* file's name will be derived from this.
|
|
|
|
*
|
|
|
|
* @throws IOException
|
|
|
|
* When a problem occurs. The error message should be
|
|
|
|
* already translated.
|
|
|
|
*/
|
|
|
|
public void saveAs(File newFolder) throws IOException {
|
|
|
|
// Check intented rename (throws if there is a problem)
|
|
|
|
File newPrimary = checkNewFoldername(newFolder);
|
|
|
|
|
|
|
|
// Create the folder
|
|
|
|
if (!newFolder.mkdirs()) {
|
|
|
|
String msg = I18n.format(tr("Could not create directory \"{0}\""), newFolder.getAbsolutePath());
|
|
|
|
throw new IOException(msg);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Save the files to their new location
|
|
|
|
for (SketchFile file : files) {
|
|
|
|
if (file.isPrimary())
|
|
|
|
file.saveAs(newPrimary);
|
|
|
|
else
|
|
|
|
file.saveAs(new File(newFolder, file.getFileName()));
|
|
|
|
}
|
|
|
|
|
|
|
|
folder = newFolder;
|
|
|
|
|
|
|
|
// Copy the data folder (this may take a while.. add progress bar?)
|
|
|
|
if (getDataFolder().exists()) {
|
|
|
|
File newDataFolder = new File(newFolder, "data");
|
|
|
|
FileUtils.copy(getDataFolder(), newDataFolder);
|
|
|
|
}
|
|
|
|
}
|
2014-01-30 15:50:09 +01:00
|
|
|
}
|