|
| 1 | +package com.devonfw.tools.ide.property; |
| 2 | + |
| 3 | +import java.util.function.Consumer; |
| 4 | + |
| 5 | +import com.devonfw.tools.ide.commandlet.Commandlet; |
| 6 | +import com.devonfw.tools.ide.completion.CompletionCandidateCollector; |
| 7 | +import com.devonfw.tools.ide.context.IdeContext; |
| 8 | +import com.devonfw.tools.ide.tool.ToolCommandlet; |
| 9 | +import com.devonfw.tools.ide.tool.plugin.PluginBasedCommandlet; |
| 10 | +import com.devonfw.tools.ide.tool.plugin.PluginDescriptor; |
| 11 | +import com.devonfw.tools.ide.tool.plugin.PluginMaps; |
| 12 | + |
| 13 | +/** |
| 14 | + * {@link Property} representing the plugin of a {@link PluginBasedCommandlet}. |
| 15 | + */ |
| 16 | +public class PluginProperty extends Property<String> { |
| 17 | + |
| 18 | + /** |
| 19 | + * The constructor. |
| 20 | + * |
| 21 | + * @param name the {@link #getName() property name}. |
| 22 | + * @param required the {@link #isRequired() required flag}. |
| 23 | + * @param alias the {@link #getAlias() property alias}. |
| 24 | + */ |
| 25 | + public PluginProperty(String name, boolean required, String alias) { |
| 26 | + |
| 27 | + this(name, required, alias, false, null); |
| 28 | + } |
| 29 | + |
| 30 | + /** |
| 31 | + * The constructor. |
| 32 | + * |
| 33 | + * @param name the {@link #getName() property name}. |
| 34 | + * @param required the {@link #isRequired() required flag}. |
| 35 | + * @param alias the {@link #getAlias() property alias}. |
| 36 | + * @param multivalued the boolean flag about multiple arguments |
| 37 | + * @param validator the {@link Consumer} used to {@link #validate() validate} the {@link #getValue() value}. |
| 38 | + */ |
| 39 | + public PluginProperty(String name, boolean required, String alias, boolean multivalued, Consumer<String> validator) { |
| 40 | + |
| 41 | + super(name, required, alias, multivalued, validator); |
| 42 | + } |
| 43 | + |
| 44 | + @Override |
| 45 | + public Class<String> getValueType() { |
| 46 | + |
| 47 | + return String.class; |
| 48 | + } |
| 49 | + |
| 50 | + @Override |
| 51 | + public String parse(String valueAsString, IdeContext context) { |
| 52 | + |
| 53 | + return valueAsString; |
| 54 | + } |
| 55 | + |
| 56 | + @Override |
| 57 | + protected void completeValue(String arg, IdeContext context, Commandlet commandlet, CompletionCandidateCollector collector) { |
| 58 | + |
| 59 | + ToolCommandlet cmd = commandlet.getToolForCompletion(); |
| 60 | + if (cmd instanceof PluginBasedCommandlet) { |
| 61 | + PluginMaps pluginMap = ((PluginBasedCommandlet) cmd).getPluginsMap(); |
| 62 | + for (PluginDescriptor pluginDescriptor : pluginMap.getPlugins()) { |
| 63 | + if (pluginDescriptor.getName().toLowerCase().startsWith(arg.toLowerCase())) { |
| 64 | + collector.add(pluginDescriptor.getName(), null, null, commandlet); |
| 65 | + } |
| 66 | + } |
| 67 | + } |
| 68 | + } |
| 69 | +} |
0 commit comments