Skip to content

Commit

Permalink
FORGE-1366: Moved UIPrompt to UIExecutionContext
Browse files Browse the repository at this point in the history
  • Loading branch information
gastaldi committed Jan 9, 2014
1 parent 7926ca1 commit 69be472
Show file tree
Hide file tree
Showing 13 changed files with 89 additions and 39 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,10 @@
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;
Expand All @@ -59,7 +56,6 @@
public class ShellImpl implements Shell, UIRuntime
{
private Resource<?> currentResource;
private UIPrompt prompt;

private final AddonRegistry addonRegistry;
private final AeshConsole console;
Expand Down Expand Up @@ -170,7 +166,6 @@ 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 @@ -194,26 +189,17 @@ 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 UIPrompt createPrompt(UIContext context)
{
@Override
public void preCommandExecuted(UICommand command, UIExecutionContext context)
{
CommandInvocation commandInvocation = (CommandInvocation) context.getUIContext().getAttributeMap()
.get(CommandInvocation.class);
ShellImpl.this.prompt = new ShellUIPromptImpl(console, commandInvocation);
}
CommandInvocation commandInvocation = (CommandInvocation) context.getAttributeMap()
.get(CommandInvocation.class);
return new ShellUIPromptImpl(console, commandInvocation);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
import org.jboss.forge.addon.ui.context.UIExecutionContext;
import org.jboss.forge.addon.ui.controller.CommandController;
import org.jboss.forge.addon.ui.controller.CommandControllerFactory;
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;
Expand Down Expand Up @@ -104,6 +105,12 @@ public UIProgressMonitor createProgressMonitor(UIContext context)
{
return new DefaultUIProgressMonitor();
}

@Override
public UIPrompt createPrompt(UIContext context)
{
return null;
}
});
}
}
6 changes: 0 additions & 6 deletions ui/api/src/main/java/org/jboss/forge/addon/ui/UIProvider.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
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 @@ -21,9 +20,4 @@ public interface UIProvider
* Returns the output object used to display messages during a UI interation
*/
UIOutput getOutput();

/**
* Returns the object used to prompt for messages during a UI interaction
*/
UIPrompt getPrompt();
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
package org.jboss.forge.addon.ui.context;

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

/**
* A {@link UIExecutionContext} is created when the execution phase is requested
Expand All @@ -21,4 +22,9 @@ public interface UIExecutionContext extends UIContextProvider
*/
UIProgressMonitor getProgressMonitor();

/**
* Returns the object used to prompt for messages during a UI interaction
*/
UIPrompt getPrompt();

}
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

import org.jboss.forge.addon.ui.UIProgressMonitor;
import org.jboss.forge.addon.ui.context.UIContext;
import org.jboss.forge.addon.ui.input.UIPrompt;

/**
* Creates UI objects. Should be implemented by UI Providers
Expand All @@ -21,4 +22,9 @@ public interface UIRuntime
* Creates a new {@link UIProgressMonitor}
*/
UIProgressMonitor createProgressMonitor(UIContext context);

/**
* Create a new {@link UIPrompt}
*/
UIPrompt createPrompt(UIContext context);
}
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public Result execute(UIExecutionContext context) throws Exception
{
UIProvider provider = context.getUIContext().getProvider();
UIOutput output = provider.getOutput();
UIPrompt prompt = provider.getPrompt();
UIPrompt prompt = context.getPrompt();
boolean answer = prompt.promptBoolean("Do you love Forge 2?");
output.out().println("You answered: " + answer);
return null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import org.jboss.forge.addon.ui.UIProgressMonitor;
import org.jboss.forge.addon.ui.context.UIContext;
import org.jboss.forge.addon.ui.context.UIExecutionContext;
import org.jboss.forge.addon.ui.input.UIPrompt;

/**
* Implementation of the {@link UIExecutionContext} interface
Expand All @@ -21,11 +22,13 @@ public class UIExecutionContextImpl implements UIExecutionContext

private final UIContext context;
private final UIProgressMonitor progressMonitor;
private final UIPrompt prompt;

public UIExecutionContextImpl(UIContext context, UIProgressMonitor progressMonitor)
public UIExecutionContextImpl(UIContext context, UIProgressMonitor progressMonitor, UIPrompt prompt)
{
this.context = context;
this.progressMonitor = progressMonitor;
this.prompt = prompt;
}

@Override
Expand All @@ -40,4 +43,10 @@ public UIProgressMonitor getProgressMonitor()
return progressMonitor;
}

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

}
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import org.jboss.forge.addon.ui.impl.context.UIExecutionContextImpl;
import org.jboss.forge.addon.ui.impl.context.UIValidationContextImpl;
import org.jboss.forge.addon.ui.input.InputComponent;
import org.jboss.forge.addon.ui.input.UIPrompt;
import org.jboss.forge.addon.ui.metadata.UICommandMetadata;
import org.jboss.forge.addon.ui.output.UIMessage;
import org.jboss.forge.addon.ui.output.UIMessage.Severity;
Expand Down Expand Up @@ -67,7 +68,8 @@ public Result execute() throws Exception
assertInitialized();
assertValid();
UIProgressMonitor progressMonitor = runtime.createProgressMonitor(context);
UIExecutionContextImpl executionContext = new UIExecutionContextImpl(context, progressMonitor);
UIPrompt prompt = runtime.createPrompt(context);
UIExecutionContextImpl executionContext = new UIExecutionContextImpl(context, progressMonitor, prompt);
if (progressMonitor.isCancelled())
{
return null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import org.jboss.forge.addon.ui.impl.context.UIExecutionContextImpl;
import org.jboss.forge.addon.ui.impl.context.UINavigationContextImpl;
import org.jboss.forge.addon.ui.input.InputComponent;
import org.jboss.forge.addon.ui.input.UIPrompt;
import org.jboss.forge.addon.ui.metadata.UICommandMetadata;
import org.jboss.forge.addon.ui.output.UIMessage;
import org.jboss.forge.addon.ui.result.NavigationResult;
Expand Down Expand Up @@ -90,7 +91,8 @@ public Result execute() throws Exception

assertInitialized();
UIProgressMonitor progressMonitor = runtime.createProgressMonitor(context);
UIExecutionContextImpl executionContext = new UIExecutionContextImpl(context, progressMonitor);
UIPrompt prompt = runtime.createPrompt(context);
UIExecutionContextImpl executionContext = new UIExecutionContextImpl(context, progressMonitor, prompt);
Set<CommandExecutionListener> listeners = new LinkedHashSet<>();
listeners.addAll(context.getListeners());
for (CommandExecutionListener listener : addonRegistry
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/**
* 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.ui.impl.mock;

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

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

@Override
public String prompt()
{
// TODO Auto-generated method stub
return null;
}

@Override
public boolean promptBoolean(String message)
{
// TODO Auto-generated method stub
return false;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import org.jboss.forge.addon.ui.DefaultUIProgressMonitor;
import org.jboss.forge.addon.ui.UIProgressMonitor;
import org.jboss.forge.addon.ui.context.UIContext;
import org.jboss.forge.addon.ui.input.UIPrompt;
import org.jboss.forge.addon.ui.spi.UIRuntime;

/**
Expand All @@ -23,4 +24,11 @@ public UIProgressMonitor createProgressMonitor(UIContext context)
{
return new DefaultUIProgressMonitor();
}

@Override
public UIPrompt createPrompt(UIContext context)
{
return new MockUIPrompt();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
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 @@ -19,13 +18,11 @@ 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 @@ -39,11 +36,4 @@ public UIOutput getOutput()
{
return output;
}

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

}
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import org.jboss.forge.addon.ui.DefaultUIProgressMonitor;
import org.jboss.forge.addon.ui.UIProgressMonitor;
import org.jboss.forge.addon.ui.context.UIContext;
import org.jboss.forge.addon.ui.input.UIPrompt;
import org.jboss.forge.addon.ui.spi.UIRuntime;

/**
Expand All @@ -18,6 +19,7 @@
public class UIRuntimeImpl implements UIRuntime
{
private final UIProgressMonitor progressMonitor = new DefaultUIProgressMonitor();
private final UIPrompt prompt = new UIPromptImpl();

public UIRuntimeImpl()
{
Expand All @@ -29,4 +31,9 @@ public UIProgressMonitor createProgressMonitor(UIContext context)
return progressMonitor;
}

@Override
public UIPrompt createPrompt(UIContext context)
{
return prompt;
}
}

0 comments on commit 69be472

Please sign in to comment.