Skip to content

Commit

Permalink
Refactoring still going on...
Browse files Browse the repository at this point in the history
  • Loading branch information
gastaldi committed Aug 19, 2013
1 parent 7f65f07 commit a8dab86
Show file tree
Hide file tree
Showing 9 changed files with 99 additions and 141 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ public class CommandManager

private Imported<UICommand> allCommands;
private CommandLineUtil commandLineUtil;
private ConverterFactory converterFactory;

public CommandManager(AddonRegistry addonRegistry)
{
Expand All @@ -44,15 +45,28 @@ public Map<String, ShellCommand> getEnabledShellCommands(ShellContext shellConte
public Iterable<UICommand> getAllCommands()
{
if (allCommands == null)
{
allCommands = addonRegistry.getServices(UICommand.class);
}
return allCommands;
}

private CommandLineUtil getCommandLineUtil()
{
if (commandLineUtil == null)
commandLineUtil = new CommandLineUtil(addonRegistry.getServices(ConverterFactory.class).get());
{
commandLineUtil = new CommandLineUtil(getConverterFactory());
}
return commandLineUtil;
}

ConverterFactory getConverterFactory()
{
if (converterFactory == null)
{
converterFactory = addonRegistry.getServices(ConverterFactory.class).get();
}
return converterFactory;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,13 @@
import javax.annotation.PreDestroy;
import javax.enterprise.inject.Vetoed;

import org.jboss.aesh.console.Config;
import org.jboss.aesh.console.Console;
import org.jboss.aesh.console.Prompt;
import org.jboss.aesh.console.settings.Settings;
import org.jboss.aesh.terminal.CharacterType;
import org.jboss.aesh.terminal.Color;
import org.jboss.aesh.terminal.TerminalCharacter;
import org.jboss.forge.addon.convert.ConverterFactory;
import org.jboss.forge.addon.resource.FileResource;
import org.jboss.forge.addon.shell.aesh.ForgeConsoleCallback;
import org.jboss.forge.addon.shell.aesh.ShellCommand;
Expand Down Expand Up @@ -188,22 +188,46 @@ public Collection<ShellCommand> findMatchingCommands(ShellContext shellContext,

public Result execute(ShellCommand shellCommand)
{
// TODO: Fire pre/post listeners
Result result = null;
try
{
Result result = shellCommand.execute();
if (result != null && result.getMessage() != null)
{
getConsole().pushToStdOut(result.getMessage() + Config.getLineSeparator());
}
return result;
firePreCommandListeners(shellCommand);
result = shellCommand.execute();
}
catch (Exception e)
{
return Results.fail(e.getMessage(), e);
result = Results.fail(e.getMessage(), e);
}
finally
{
firePostCommandListeners(shellCommand, result);
}
return result;
}

/**
* @param shellCommand
*/
private void firePreCommandListeners(ShellCommand shellCommand)
{
for (CommandExecutionListener listener : listeners)
{
listener.preCommandExecuted(shellCommand.getCommand(), shellCommand.getContext());
}
}

/**
* @param shellCommand
*/
private void firePostCommandListeners(ShellCommand shellCommand, Result result)
{
for (CommandExecutionListener listener : listeners)
{
listener.postCommandExecuted(shellCommand.getCommand(), shellCommand.getContext(), result);
}

}

public ShellContext newShellContext()
{
return new ShellContextImpl(this, currentResource);
Expand All @@ -223,4 +247,9 @@ public void close()
}
}

public ConverterFactory getConverterFactory()
{
return commandManager.getConverterFactory();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,14 @@ public String getName()
return name;
}

/**
* @return the context
*/
public ShellContext getContext()
{
return context;
}

public Map<String, InputComponent<?, Object>> getInputs()
{
return inputs;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package org.jboss.forge.addon.shell.aesh.completion;

import org.jboss.aesh.complete.CompleteOperation;
import org.jboss.forge.addon.convert.ConverterFactory;
import org.jboss.forge.addon.shell.ui.ShellContext;
import org.jboss.forge.addon.ui.input.InputComponent;

Expand All @@ -18,7 +19,8 @@ public interface CompletionStrategy
* @param input
* @param context
* @param typedValue
* @param converterFactory TODO
*/
public void complete(CompleteOperation completeOperation, InputComponent<?, Object> input, ShellContext context,
String typedValue);
String typedValue, ConverterFactory converterFactory);
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import org.jboss.forge.addon.ui.facets.HintsFacet;
import org.jboss.forge.addon.ui.hints.InputType;
import org.jboss.forge.addon.ui.input.InputComponent;
import org.jboss.forge.addon.ui.input.SelectComponent;

/**
* Returns the completion based on the input component
Expand All @@ -13,15 +14,25 @@ public class CompletionStrategyFactory
{
public static CompletionStrategy getCompletionFor(InputComponent<?, Object> component)
{
boolean isUISelect = (component instanceof SelectComponent);
InputType inputType = component.getFacet(HintsFacet.class).getInputType();
switch (inputType)
final CompletionStrategy strategy;
if (inputType == InputType.FILE_PICKER)
{
case FILE_PICKER:
return new FileInputCompletionStrategy(false);
case DIRECTORY_PICKER:
return new FileInputCompletionStrategy(true);
default:
return new DefaultInputCompletionStrategy();
strategy = new FileInputCompletionStrategy(false);
}
else if (inputType == InputType.DIRECTORY_PICKER)
{
strategy = new FileInputCompletionStrategy(true);
}
else if (isUISelect)
{
strategy = new SelectComponentCompletionStrategy();
}
else
{
strategy = new DefaultInputCompletionStrategy();
}
return strategy;
}
}
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
package org.jboss.forge.addon.shell.aesh.completion;

import org.jboss.aesh.complete.CompleteOperation;
import org.jboss.forge.addon.convert.ConverterFactory;
import org.jboss.forge.addon.shell.ui.ShellContext;
import org.jboss.forge.addon.ui.input.InputComponent;
import org.jboss.forge.addon.ui.input.UICompleter;
import org.jboss.forge.addon.ui.util.InputComponents;

public class DefaultInputCompletionStrategy implements CompletionStrategy
class DefaultInputCompletionStrategy implements CompletionStrategy
{
@Override
public void complete(CompleteOperation completeOperation, InputComponent<?, Object> input, ShellContext context,
String typedValue)
String typedValue, ConverterFactory converterFactory)
{
UICompleter<Object> completer = InputComponents.getCompleterFor(input);
if (completer != null)
Expand All @@ -22,7 +23,7 @@ public void complete(CompleteOperation completeOperation, InputComponent<?, Obje
}
else
{

}
}
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,16 @@
/**
* 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.aesh.completion;

import java.io.File;

import org.jboss.aesh.complete.CompleteOperation;
import org.jboss.aesh.util.FileLister;
import org.jboss.forge.addon.convert.ConverterFactory;
import org.jboss.forge.addon.resource.FileResource;
import org.jboss.forge.addon.shell.ui.ShellContext;
import org.jboss.forge.addon.ui.context.UISelection;
Expand All @@ -23,7 +30,7 @@ public FileInputCompletionStrategy(boolean directory)

@Override
public void complete(CompleteOperation completeOperation, InputComponent<?, Object> input, ShellContext context,
String typedValue)
String typedValue, ConverterFactory converterFactory)
{
completeOperation.setOffset(completeOperation.getCursor());

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

package org.jboss.forge.addon.shell.aesh.completion;

import java.io.File;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;
Expand All @@ -17,18 +16,10 @@
import org.jboss.aesh.cl.internal.ParameterInt;
import org.jboss.aesh.complete.CompleteOperation;
import org.jboss.aesh.complete.Completion;
import org.jboss.aesh.util.FileLister;
import org.jboss.forge.addon.resource.DirectoryResource;
import org.jboss.forge.addon.resource.FileResource;
import org.jboss.forge.addon.shell.ShellImpl;
import org.jboss.forge.addon.shell.aesh.ShellCommand;
import org.jboss.forge.addon.shell.ui.ShellContext;
import org.jboss.forge.addon.ui.hints.InputType;
import org.jboss.forge.addon.ui.input.HasCompleter;
import org.jboss.forge.addon.ui.input.InputComponent;
import org.jboss.forge.addon.ui.input.SelectComponent;
import org.jboss.forge.addon.ui.input.SingleValued;
import org.jboss.forge.addon.ui.util.InputComponents;

/**
* @author <a href="ggastald@redhat.com">George Gastaldi</a>
Expand Down Expand Up @@ -107,7 +98,8 @@ else if (completeObject.isArgument())
if (input != null)
{
CompletionStrategy completionObj = CompletionStrategyFactory.getCompletionFor(input);
completionObj.complete(completeOperation, input, shellContext, completeObject.getValue());
completionObj.complete(completeOperation, input, shellContext, completeObject.getValue(),
shell.getConverterFactory());
}
}
if (completeOperation.getCompletionCandidates().size() == 1)
Expand All @@ -134,111 +126,4 @@ private void removeExistingOptions(String commandLine, Iterable<String> availabl
}
}
}

// try to complete an option value. Eg: "--xxx"
@SuppressWarnings({ "rawtypes", "unchecked" })
private void optionCompletion(ShellContext shellContext, CompleteOperation completeOperation,
ParsedCompleteObject completeObject,
ShellCommand shellCommand)
{
InputComponent inputOption = shellCommand.getInputs().get(completeObject.getName());
Object value = InputComponents.getValueFor(inputOption);
InputType inputType = InputComponents.getInputType(inputOption);
// atm the FileLister requires the CompleteOperation object so it need
// to be handled here and not for each inputcomponents.setCompleter
if (inputOption != null &&
((inputOption.getValueType() == File.class) || (inputOption.getValueType() == FileResource.class)))
{
completeOperation.setOffset(completeOperation.getCursor());
if (completeObject.getValue() == null)
{
// use default value if its set
if (inputOption.getValueType() == SingleValued.class &&
((SingleValued) inputOption).getValue() != null)
{
new FileLister("", new File(((SingleValued) inputOption).getValue().toString()))
.findMatchingDirectories(completeOperation);
}
else
new FileLister("", new File(System.getProperty("user.dir")))
.findMatchingDirectories(completeOperation);
}
else
new FileLister(completeObject.getValue(), new File(System.getProperty("user.dir")))
.findMatchingDirectories(completeOperation);
}
else if (inputOption != null && inputOption.getValueType() == DirectoryResource.class)
{
completeOperation.setOffset(completeOperation.getCursor());
if (completeObject.getValue() == null)
{

if (((SingleValued) inputOption).getValue() != null)
{
new FileLister("", new File(((SingleValued) inputOption).getValue().toString()),
FileLister.Filter.DIRECTORY).findMatchingDirectories(completeOperation);
}
else
new FileLister("", new File(System.getProperty("user.dir")), FileLister.Filter.DIRECTORY)
.findMatchingDirectories(completeOperation);
}
else
new FileLister(completeObject.getValue(), new File(System.getProperty("user.dir")),
FileLister.Filter.DIRECTORY).findMatchingDirectories(completeOperation);
}

if (inputOption != null && (inputOption instanceof SingleValued &&
((SingleValued) inputOption).getValue() != null))
{
// need to check if the default matches the complete value
if (completeObject.getValue().length() == 0)
{
completeOperation.addCompletionCandidate(((SingleValued) inputOption).getValue().toString());
return;
}
else
{
String defaultValue = ((SingleValued) inputOption).getValue().toString();
if (defaultValue.startsWith(completeObject.getValue()))
{
completeOperation.addCompletionCandidate(defaultValue.substring(completeObject.getValue().length()));
return;
}
}
}
if (inputOption != null && inputOption instanceof SelectComponent)
{
if (completeObject.getValue() == null || completeObject.getValue().length() == 0)
{
for (Object o : ((SelectComponent) inputOption).getValueChoices())
{
completeOperation.addCompletionCandidate(o.toString());
}
}
else
{
for (Object o : ((SelectComponent) inputOption).getValueChoices())
{
if (o.toString().startsWith(completeObject.getValue()))
completeOperation.addCompletionCandidate(o.toString());
}
}
}
if (inputOption != null
&& (inputOption instanceof HasCompleter && ((HasCompleter) inputOption).getCompleter() != null))
{
Iterable iter = ((HasCompleter) inputOption).getCompleter().getCompletionProposals(null, inputOption,
completeObject.getValue());
if (iter != null)
{
for (Object s : iter)
completeOperation.addCompletionCandidate(s.toString());
}
if (completeOperation.getCompletionCandidates().size() == 1)
{
completeOperation.setOffset(completeOperation.getCursor() - completeObject.getOffset());
}
}

}
}
Loading

0 comments on commit a8dab86

Please sign in to comment.