Last active
August 29, 2015 14:00
-
-
Save gastaldi/66d923899dd0dd714aa0 to your computer and use it in GitHub Desktop.
FORGE-1774: Default --help for Forge commands
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /** | |
| * Copyright 2014 Red Hat, Inc. and/or its affiliates. | |
| * | |
| * Licensed under the Eclipse Public License version 1.0, available at | |
| * http://www.eclipse.org/legal/epl-v10.html | |
| */ | |
| package org.jboss.forge.addon.shell.ui.transformer; | |
| import javax.inject.Inject; | |
| import org.jboss.forge.addon.ui.command.UICommand; | |
| import org.jboss.forge.addon.ui.command.UICommandTransformer; | |
| import org.jboss.forge.addon.ui.context.UIContext; | |
| import org.jboss.forge.addon.ui.input.InputComponentFactory; | |
| /** | |
| * Adds the "help" attribute on commands when executed in Shell | |
| * | |
| * @author <a href="ggastald@redhat.com">George Gastaldi</a> | |
| */ | |
| public class HelpUICommandTransformer implements UICommandTransformer | |
| { | |
| @Inject | |
| private InputComponentFactory inputComponentFactory; | |
| @Override | |
| public UICommand transform(UIContext context, UICommand original) | |
| { | |
| // Show in shell only | |
| if (!context.getProvider().isGUI()) | |
| { | |
| return new HelpWizard(original, inputComponentFactory); | |
| } | |
| else | |
| { | |
| return original; | |
| } | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /** | |
| * Copyright 2014 Red Hat, Inc. and/or its affiliates. | |
| * | |
| * Licensed under the Eclipse Public License version 1.0, available at | |
| * http://www.eclipse.org/legal/epl-v10.html | |
| */ | |
| package org.jboss.forge.addon.shell.ui.transformer; | |
| import javax.enterprise.inject.Vetoed; | |
| import org.jboss.forge.addon.ui.command.UICommand; | |
| import org.jboss.forge.addon.ui.context.UIBuilder; | |
| import org.jboss.forge.addon.ui.context.UIContext; | |
| import org.jboss.forge.addon.ui.context.UIExecutionContext; | |
| import org.jboss.forge.addon.ui.context.UINavigationContext; | |
| import org.jboss.forge.addon.ui.context.UIValidationContext; | |
| import org.jboss.forge.addon.ui.input.InputComponent; | |
| import org.jboss.forge.addon.ui.input.InputComponentFactory; | |
| import org.jboss.forge.addon.ui.input.UIInput; | |
| import org.jboss.forge.addon.ui.metadata.UICommandMetadata; | |
| import org.jboss.forge.addon.ui.result.NavigationResult; | |
| import org.jboss.forge.addon.ui.result.Result; | |
| import org.jboss.forge.addon.ui.wizard.UIWizard; | |
| /** | |
| * A command that adds the "help" option to an existing {@link UICommand} object | |
| * | |
| * @author <a href="ggastald@redhat.com">George Gastaldi</a> | |
| */ | |
| @Vetoed | |
| public class HelpWizard implements UIWizard | |
| { | |
| private static final String HELP_INPUT_NAME = "help"; | |
| private final UICommand command; | |
| private final InputComponentFactory inputComponentFactory; | |
| private UIInput<Boolean> helpOption; | |
| private boolean containsHelpOption; | |
| public HelpWizard(UICommand command, InputComponentFactory inputComponentFactory) | |
| { | |
| this.command = command; | |
| this.inputComponentFactory = inputComponentFactory; | |
| } | |
| @Override | |
| public UICommandMetadata getMetadata(UIContext context) | |
| { | |
| return command.getMetadata(context); | |
| } | |
| @Override | |
| public boolean isEnabled(UIContext context) | |
| { | |
| return command.isEnabled(context); | |
| } | |
| @Override | |
| public void initializeUI(final UIBuilder builder) throws Exception | |
| { | |
| UIBuilder wrapperBuilder = new UIBuilder() | |
| { | |
| @Override | |
| public UIContext getUIContext() | |
| { | |
| return builder.getUIContext(); | |
| } | |
| @Override | |
| public UIBuilder add(InputComponent<?, ?> input) | |
| { | |
| builder.add(input); | |
| if (input.getName().equals(HELP_INPUT_NAME)) | |
| { | |
| HelpWizard.this.containsHelpOption = true; | |
| } | |
| return this; | |
| } | |
| }; | |
| command.initializeUI(wrapperBuilder); | |
| if (!containsHelpOption) | |
| { | |
| helpOption = inputComponentFactory.createInput(HELP_INPUT_NAME, 'h', Boolean.class); | |
| builder.add(helpOption); | |
| } | |
| } | |
| @Override | |
| public void validate(UIValidationContext context) | |
| { | |
| if (!isHelpOptionSet()) | |
| { | |
| command.validate(context); | |
| } | |
| } | |
| @Override | |
| public Result execute(UIExecutionContext context) throws Exception | |
| { | |
| if (isHelpOptionSet()) | |
| { | |
| // TODO: Execute man | |
| return null; | |
| } | |
| else | |
| { | |
| return command.execute(context); | |
| } | |
| } | |
| private boolean isHelpOptionSet() | |
| { | |
| return (helpOption != null && helpOption.getValue()); | |
| } | |
| @Override | |
| public NavigationResult next(UINavigationContext context) throws Exception | |
| { | |
| if (isHelpOptionSet()) | |
| { | |
| return null; | |
| } | |
| else | |
| { | |
| if (command instanceof UIWizard) | |
| { | |
| return ((UIWizard) command).next(context); | |
| } | |
| else | |
| { | |
| return null; | |
| } | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment