|
| 1 | +/* |
| 2 | + * #%L |
| 3 | + * Discoverable commands, with the metadata a menu is built from. |
| 4 | + * %% |
| 5 | + * Copyright (C) 2026 SciJava developers. |
| 6 | + * %% |
| 7 | + * Redistribution and use in source and binary forms, with or without |
| 8 | + * modification, are permitted provided that the following conditions are met: |
| 9 | + * |
| 10 | + * 1. Redistributions of source code must retain the above copyright notice, |
| 11 | + * this list of conditions and the following disclaimer. |
| 12 | + * 2. Redistributions in binary form must reproduce the above copyright notice, |
| 13 | + * this list of conditions and the following disclaimer in the documentation |
| 14 | + * and/or other materials provided with the distribution. |
| 15 | + * |
| 16 | + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
| 17 | + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 18 | + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 19 | + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE |
| 20 | + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
| 21 | + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
| 22 | + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
| 23 | + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
| 24 | + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
| 25 | + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
| 26 | + * POSSIBILITY OF SUCH DAMAGE. |
| 27 | + * #L% |
| 28 | + */ |
| 29 | + |
| 30 | +package org.scijava.command; |
| 31 | + |
| 32 | +import java.util.Map; |
| 33 | +import java.util.Optional; |
| 34 | + |
| 35 | +import org.scijava.context.Access; |
| 36 | +import org.scijava.discovery.Discovery; |
| 37 | +import org.scijava.execute.Executable; |
| 38 | +import org.scijava.execute.ExecutableInstance; |
| 39 | +import org.scijava.execute.Executables; |
| 40 | +import org.scijava.struct.Struct; |
| 41 | + |
| 42 | +/** |
| 43 | + * A command as the application knows it before running one: its identity, its |
| 44 | + * parameters, and how it should appear in a menu. |
| 45 | + * <p> |
| 46 | + * The presentation metadata is read from the annotation index, so listing |
| 47 | + * every command and building a menu from them loads no command classes at all. |
| 48 | + * {@link #struct()} and {@link #create()} are the points at which the class is |
| 49 | + * finally needed. |
| 50 | + * </p> |
| 51 | + * <p> |
| 52 | + * NB: SciJava Common called this {@code ModuleInfo}, and its |
| 53 | + * {@code ClassCommandInfo} was one implementation. The names have shifted by one: |
| 54 | + * "module" is now a JPMS module, so the general description is |
| 55 | + * {@link Executable}, and this is the command-shaped view of it. |
| 56 | + * </p> |
| 57 | + * <p> |
| 58 | + * NB: a command whose implementation is a <em>class</em> - which is not the |
| 59 | + * same as a Java class, Kotlin and Groovy and Scala classes being just as |
| 60 | + * welcome. The other kind is a script, which reaches the same menus as a |
| 61 | + * {@link CommandInfo} built by {@link CommandInfo#of}. |
| 62 | + * </p> |
| 63 | + * |
| 64 | + * @author Curtis Rueden |
| 65 | + */ |
| 66 | +public class ClassCommandInfo implements CommandInfo { |
| 67 | + |
| 68 | + private final Discovery<Command> discovery; |
| 69 | + private final Map<String, String> menu; |
| 70 | + private Executable delegate; |
| 71 | + |
| 72 | + ClassCommandInfo(final Discovery<Command> discovery, |
| 73 | + final Map<String, String> menu) |
| 74 | + { |
| 75 | + this.discovery = discovery; |
| 76 | + this.menu = menu; |
| 77 | + } |
| 78 | + |
| 79 | + // -- Metadata, readable without loading the class -- |
| 80 | + |
| 81 | + /** Gets the command's implementation class name. */ |
| 82 | + public String className() { |
| 83 | + return discovery.implClassName(); |
| 84 | + } |
| 85 | + |
| 86 | + /** Gets where this command sits in the menus, if anywhere. */ |
| 87 | + @Override |
| 88 | + public Optional<String> menuPath() { |
| 89 | + return attr("path"); |
| 90 | + } |
| 91 | + |
| 92 | + /** Gets the label to display, defaulting to the last menu path element. */ |
| 93 | + @Override |
| 94 | + public String label() { |
| 95 | + return menuPath() // |
| 96 | + .map(path -> path.substring(path.lastIndexOf('>') + 1)) // |
| 97 | + .orElseGet(this::name); |
| 98 | + } |
| 99 | + |
| 100 | + /** Gets this command's keyboard shortcut, if it has one. */ |
| 101 | + @Override |
| 102 | + public Optional<String> accelerator() { |
| 103 | + return attr("accelerator"); |
| 104 | + } |
| 105 | + |
| 106 | + /** Gets this command's icon resource, if it has one. */ |
| 107 | + @Override |
| 108 | + public Optional<String> iconPath() { |
| 109 | + return attr("iconPath"); |
| 110 | + } |
| 111 | + |
| 112 | + /** Gets how this command sorts among its menu siblings. */ |
| 113 | + @Override |
| 114 | + public double weight() { |
| 115 | + return attr("weight").map(Double::parseDouble) // |
| 116 | + .orElse(Double.POSITIVE_INFINITY); |
| 117 | + } |
| 118 | + |
| 119 | + /** Gets whether this command should appear in menus. */ |
| 120 | + @Override |
| 121 | + public boolean isVisible() { |
| 122 | + return !"false".equals(menu.get("visible")); |
| 123 | + } |
| 124 | + |
| 125 | + /** Gets the priority with which this command was declared. */ |
| 126 | + public double priority() { |
| 127 | + return discovery.priority(); |
| 128 | + } |
| 129 | + |
| 130 | + /** Gets a menu attribute by name. */ |
| 131 | + public Optional<String> attr(final String key) { |
| 132 | + final String value = menu.get(key); |
| 133 | + return value == null || value.isEmpty() ? Optional.empty() // |
| 134 | + : Optional.of(value); |
| 135 | + } |
| 136 | + |
| 137 | + // -- Executable methods -- |
| 138 | + |
| 139 | + @Override |
| 140 | + public String name() { |
| 141 | + return className(); |
| 142 | + } |
| 143 | + |
| 144 | + @Override |
| 145 | + public Struct struct() { |
| 146 | + return delegate().struct(); |
| 147 | + } |
| 148 | + |
| 149 | + @Override |
| 150 | + public ExecutableInstance create() { |
| 151 | + return delegate().create(); |
| 152 | + } |
| 153 | + |
| 154 | + @Override |
| 155 | + public String toString() { |
| 156 | + return className() + menuPath().map(p -> " [" + p + "]").orElse(""); |
| 157 | + } |
| 158 | + |
| 159 | + /** |
| 160 | + * Gets the executable behind this command, loading the class on first use. |
| 161 | + * <p> |
| 162 | + * NB: everything above this line answers from the index alone. This is |
| 163 | + * where a menu stops being free. |
| 164 | + * </p> |
| 165 | + */ |
| 166 | + private synchronized Executable delegate() { |
| 167 | + if (delegate == null) { |
| 168 | + final Class<? extends Command> type = discovery.type(); |
| 169 | + // NB: the lookup comes from the context, so a command's package need |
| 170 | + // only be opened to org.scijava.context -- not additionally to the |
| 171 | + // execution layer that reads its @Parameter fields. |
| 172 | + delegate = Executables.of(type, Access.lookupIn(type)); |
| 173 | + } |
| 174 | + return delegate; |
| 175 | + } |
| 176 | +} |
0 commit comments