Skip to content

Commit

Permalink
FORGE-1366: Introduced UIPrompt
Browse files Browse the repository at this point in the history
  • Loading branch information
gastaldi committed Jan 9, 2014
1 parent 1813e31 commit 377cbd7
Show file tree
Hide file tree
Showing 7 changed files with 217 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import org.jboss.aesh.console.AeshConsoleBuilder;
import org.jboss.aesh.console.Console;
import org.jboss.aesh.console.Prompt;
import org.jboss.aesh.console.command.invocation.CommandInvocation;
import org.jboss.aesh.console.helper.InterruptHook;
import org.jboss.aesh.console.settings.Settings;
import org.jboss.aesh.console.settings.SettingsBuilder;
Expand All @@ -30,12 +31,17 @@
import org.jboss.forge.addon.shell.aesh.ForgeManProvider;
import org.jboss.forge.addon.shell.ui.ShellContextImpl;
import org.jboss.forge.addon.shell.ui.ShellUIOutputImpl;
import org.jboss.forge.addon.shell.ui.ShellUIPromptImpl;
import org.jboss.forge.addon.ui.AbstractCommandExecutionListener;
import org.jboss.forge.addon.ui.DefaultUIProgressMonitor;
import org.jboss.forge.addon.ui.UICommand;
import org.jboss.forge.addon.ui.UIProgressMonitor;
import org.jboss.forge.addon.ui.context.UIContext;
import org.jboss.forge.addon.ui.context.UIContextListener;
import org.jboss.forge.addon.ui.context.UIExecutionContext;
import org.jboss.forge.addon.ui.controller.CommandControllerFactory;
import org.jboss.forge.addon.ui.controller.CommandExecutionListener;
import org.jboss.forge.addon.ui.input.UIPrompt;
import org.jboss.forge.addon.ui.output.UIOutput;
import org.jboss.forge.addon.ui.spi.UIRuntime;
import org.jboss.forge.furnace.addons.AddonRegistry;
Expand All @@ -53,6 +59,8 @@
public class ShellImpl implements Shell, UIRuntime
{
private Resource<?> currentResource;
private UIPrompt prompt;

private final AddonRegistry addonRegistry;
private final AeshConsole console;
private final UIOutput output;
Expand Down Expand Up @@ -162,6 +170,7 @@ public ShellContextImpl createUIContext()
{
Imported<UIContextListener> listeners = addonRegistry.getServices(UIContextListener.class);
ShellContextImpl shellContextImpl = new ShellContextImpl(this, currentResource, listeners);
shellContextImpl.addCommandExecutionListener(new InitializePromptListener());
for (CommandExecutionListener listener : executionListeners)
{
shellContextImpl.addCommandExecutionListener(listener);
Expand All @@ -185,9 +194,26 @@ public CommandExecutionListener removeListener()
};
}

@Override
public UIPrompt getPrompt()
{
return prompt;
}

@Override
public UIProgressMonitor createProgressMonitor(UIContext context)
{
return new DefaultUIProgressMonitor();
}

private class InitializePromptListener extends AbstractCommandExecutionListener
{
@Override
public void preCommandExecuted(UICommand command, UIExecutionContext context)
{
CommandInvocation commandInvocation = (CommandInvocation) context.getUIContext().getAttributeMap()
.get(CommandInvocation.class);
ShellImpl.this.prompt = new ShellUIPromptImpl(console, commandInvocation);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
/**
* Copyright 2013 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;

import java.io.PrintStream;
import java.util.List;

import org.jboss.aesh.console.AeshConsole;
import org.jboss.aesh.console.command.CommandOperation;
import org.jboss.aesh.console.command.invocation.CommandInvocation;
import org.jboss.forge.addon.ui.input.UIPrompt;

/**
* Implementation of {@link UIPrompt}
*
* @author <a href="ggastald@redhat.com">George Gastaldi</a>
*/
public class ShellUIPromptImpl implements UIPrompt
{
private final AeshConsole console;
private final CommandInvocation commandInvocation;

public ShellUIPromptImpl(AeshConsole console, CommandInvocation commandInvocation)
{
this.console = console;
this.commandInvocation = commandInvocation;
}

private String toString(List<CommandOperation> operationList)
{
StringBuilder sb = new StringBuilder();
for (CommandOperation commandOperation : operationList)
{
sb.append(commandOperation.getInputKey().getAsChar());
}
return sb.toString();
}

@Override
public String prompt()
{
List<CommandOperation> input = commandInvocation.getInput();
String output = toString(input);
return output;
}

@Override
public boolean promptBoolean(String message)
{
PrintStream out = console.getShell().out();
out.print(message + " [y/N]");
List<CommandOperation> input = commandInvocation.getInput();
out.println();
String output = toString(input);
return "Y".equalsIgnoreCase(output);
}

}
11 changes: 8 additions & 3 deletions ui/api/src/main/java/org/jboss/forge/addon/ui/UIProvider.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package org.jboss.forge.addon.ui;

import org.jboss.forge.addon.ui.controller.CommandExecutionListener;
import org.jboss.forge.addon.ui.input.UIPrompt;
import org.jboss.forge.addon.ui.output.UIOutput;

/**
Expand All @@ -14,11 +15,15 @@ public interface UIProvider
/**
* Returns true if this {@link UIProvider} is running a Graphical User Interface.
*/
public boolean isGUI();
boolean isGUI();

/**
* Returns the output object used to display messages during a UI interation
*/
public UIOutput getOutput();
UIOutput getOutput();

}
/**
* Returns the object used to prompt for messages during a UI interaction
*/
UIPrompt getPrompt();
}
29 changes: 29 additions & 0 deletions ui/api/src/main/java/org/jboss/forge/addon/ui/input/UIPrompt.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/**
* Copyright 2013 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.ui.input;


/**
* Used when an addon needs to prompt for ad-hoc user input
*
* @author <a href="ggastald@redhat.com">George Gastaldi</a>
*/
public interface UIPrompt
{
/**
* Prompt for user input, and return as a String.
*/
String prompt();

/**
* Prompt for boolean user input (Y/n), first printing the given message, then returning user input as a boolean. The
* value returned will default to <code>true</code> if an empty or whitespace-only user input is read.
*/
boolean promptBoolean(String message);

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
/**
* Copyright 2013 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.ui.example.commands;

import org.jboss.forge.addon.ui.AbstractUICommand;
import org.jboss.forge.addon.ui.UIProvider;
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.input.UIPrompt;
import org.jboss.forge.addon.ui.metadata.UICommandMetadata;
import org.jboss.forge.addon.ui.output.UIOutput;
import org.jboss.forge.addon.ui.result.Result;
import org.jboss.forge.addon.ui.util.Metadata;

/**
*
* @author <a href="ggastald@redhat.com">George Gastaldi</a>
*/
public class PromptCommand extends AbstractUICommand
{

@Override
public void initializeUI(UIBuilder builder) throws Exception
{

}

@Override
public UICommandMetadata getMetadata(UIContext context)
{
return Metadata.forCommand(PromptCommand.class).name("prompt").description("Prompts for Information");
}

@Override
public Result execute(UIExecutionContext context) throws Exception
{
UIProvider provider = context.getUIContext().getProvider();
UIOutput output = provider.getOutput();
UIPrompt prompt = provider.getPrompt();
boolean answer = prompt.promptBoolean("Do you love Forge 2?");
output.out().println("You answered: " + answer);
return null;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/**
* Copyright 2013 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.ui.test.impl;

import org.jboss.forge.addon.ui.input.UIPrompt;

/**
*
* @author <a href="ggastald@redhat.com">George Gastaldi</a>
*/
public class UIPromptImpl implements UIPrompt
{

@Override
public String prompt()
{
return null;
}

@Override
public boolean promptBoolean(String message)
{
return false;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
package org.jboss.forge.ui.test.impl;

import org.jboss.forge.addon.ui.UIProvider;
import org.jboss.forge.addon.ui.input.UIPrompt;
import org.jboss.forge.addon.ui.output.UIOutput;

/**
Expand All @@ -18,11 +19,13 @@ public class UIProviderImpl implements UIProvider
{
private final boolean graphical;
private final UIOutput output;
private final UIPrompt prompt;

public UIProviderImpl(boolean graphical)
{
this.graphical = graphical;
this.output = new UIOutputImpl(System.out, System.err);
this.prompt = new UIPromptImpl();
}

@Override
Expand All @@ -37,4 +40,10 @@ public UIOutput getOutput()
return output;
}

@Override
public UIPrompt getPrompt()
{
return prompt;
}

}

0 comments on commit 377cbd7

Please sign in to comment.