-
Notifications
You must be signed in to change notification settings - Fork 18
Expand file tree
/
Copy pathTutorials.java
More file actions
138 lines (122 loc) · 4.09 KB
/
Tutorials.java
File metadata and controls
138 lines (122 loc) · 4.09 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
/*
* IJ BAR: https://github.com/tferr/Scripts
*
* This program is free software; you can redistribute it and/or modify it under the terms of the
* GNU General Public License as published by the Free Software Foundation
* (http://www.gnu.org/licenses/gpl.txt).
* This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without
* even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*/
package bar.plugin;
import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;
import java.nio.file.FileSystem;
import java.nio.file.FileSystems;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.PathMatcher;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.Iterator;
import java.util.stream.Stream;
import net.imagej.ImageJ;
import org.scijava.Context;
import org.scijava.app.StatusService;
import org.scijava.command.Command;
import org.scijava.plugin.Menu;
import org.scijava.plugin.Parameter;
import org.scijava.plugin.Plugin;
import org.scijava.ui.DialogPrompt;
import org.scijava.ui.UIService;
import org.scijava.ui.swing.script.TextEditor;
import bar.Utils;
/** Loads a new instance of the Script editor with jarified tutorial files */
@Plugin(type = Command.class, menu = { @Menu(label = "BAR"), @Menu(label = "Help", weight = 0.01d),
@Menu(label = "Open Interactive Tutorials...") })
public class Tutorials implements Command {
@Parameter
private Context context;
@Parameter
private UIService uiService;
@Parameter
private StatusService statusService;
@Parameter(label = "Which introductory tutorial?", choices = { "Python", "BeanShell" })
private String lang;
@Override
public void run() {
final ArrayList<URL> urls = getTutorialFiles(lang);
if (urls == null || urls.isEmpty()) {
uiService.showDialog("Could not load tutorial files.", DialogPrompt.MessageType.ERROR_MESSAGE);
return;
}
// Ensure files are opened in the right order
urls.sort(new Comparator<URL>() {
@Override
public int compare(final URL u1, final URL u2) {
return u1.toString().compareTo(u2.toString());
}
});
final TextEditor editor = new TextEditor(context);
for (final URL url : urls) {
statusService.showStatus(urls.indexOf(url), urls.size(), "Opening tutorial files...");
editor.loadTemplate(url);
}
editor.switchTo(0);
editor.setVisible(true);
editor.setTitle("BAR Tutorial Files [" + lang + "]");
statusService.clearStatus();
}
private String getMatcherPattern(final String language) {
// see http://docs.oracle.com/javase/tutorial/essential/io/find.html
switch (language.toLowerCase()) {
case "python":
return "glob:**.py";
case "beanshell":
return "glob:**.bsh";
case "groovy":
return "glob:**.{groovy,gvy}";
default:
return "glob:**.*"; // any extension
}
}
/* See http://stackoverflow.com/a/28057735 for details */
private ArrayList<URL> getTutorialFiles(final String language) {
final String dir = "tutorials";
final ArrayList<URL> urlList = new ArrayList<>();
final PathMatcher matcher = FileSystems.getDefault().getPathMatcher(getMatcherPattern(language));
try {
final URI uri = Utils.getBARresource(dir).toURI();
FileSystem fileSystem = null;
Path path;
if ("jar".equals(uri.getScheme())) {
fileSystem = FileSystems.newFileSystem(uri, Collections.<String, Object>emptyMap());
path = fileSystem.getPath(dir);
} else {
path = Paths.get(uri);
}
final Stream<Path> walk = Files.walk(path, 1);
final Iterator<Path> it = walk.iterator();
while (it.hasNext()) {
final Path p = it.next();
if (matcher.matches(p)) {
urlList.add(p.toUri().toURL());
}
}
walk.close();
if (fileSystem != null)
fileSystem.close();
} catch (IOException | URISyntaxException exc) {
return null;
}
return urlList;
}
public static void main(final String... args) {
final ImageJ ij = net.imagej.Main.launch(args);
ij.command().run(Tutorials.class, true);
}
}