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