Skip to content

Commit

Permalink
Refactoring: extract callback
Browse files Browse the repository at this point in the history
  • Loading branch information
danielsoro committed Jun 8, 2014
1 parent 04fdfc1 commit 6872f93
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 42 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,11 @@
import com.intellij.openapi.actionSystem.AnActionEvent;
import com.intellij.openapi.actionSystem.DataKeys;
import com.intellij.openapi.vfs.VirtualFile;
import org.jboss.forge.addon.ui.command.CommandFactory;
import org.jboss.forge.addon.ui.command.UICommand;
import org.jboss.forge.addon.ui.context.UIContext;
import org.jboss.forge.addon.ui.wizard.UIWizardStep;
import org.jboss.forge.plugin.idea.context.UIContextFactory;
import org.jboss.forge.plugin.idea.service.callbacks.CommandListPopupCallBack;
import org.jboss.forge.plugin.idea.service.ServiceHelper;
import org.jboss.forge.plugin.idea.ui.CommandListPopupBuilder;

import java.util.ArrayList;
import java.util.List;

/**
* Creates a popup list and displays all the currently registered
* {@link UICommand} instances
Expand All @@ -47,40 +41,6 @@ public void actionPerformed(AnActionEvent event)
}

final VirtualFile[] selectedFiles = files;
ServiceHelper.loadFurnaceAndRun(new Runnable()
{
@Override
public void run()
{
UIContext uiContext = UIContextFactory.create(selectedFiles);

new CommandListPopupBuilder()
.setUIContext(uiContext)
.setCommands(getAllCandidates(uiContext))
.build()
.showInFocusCenter();
}
});
}

private List<UICommand> getAllCandidates(UIContext uiContext)
{
List<UICommand> commands = new ArrayList<UICommand>();
CommandFactory commandFactory = ServiceHelper.getForgeService().getCommandFactory();

for (UICommand command : commandFactory.getCommands())
{
if (isCandidate(command, uiContext))
{
commands.add(command);
}
}

return commands;
}

private boolean isCandidate(UICommand command, UIContext uiContext)
{
return !(command instanceof UIWizardStep) && command.isEnabled(uiContext);
ServiceHelper.loadFurnaceAndRun(new CommandListPopupCallBack(selectedFiles));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
/*
* 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.plugin.idea.service.callbacks;

import com.intellij.openapi.vfs.VirtualFile;
import org.jboss.forge.addon.ui.command.CommandFactory;
import org.jboss.forge.addon.ui.command.UICommand;
import org.jboss.forge.addon.ui.context.UIContext;
import org.jboss.forge.addon.ui.wizard.UIWizardStep;
import org.jboss.forge.plugin.idea.context.UIContextFactory;
import org.jboss.forge.plugin.idea.service.ServiceHelper;
import org.jboss.forge.plugin.idea.ui.CommandListPopupBuilder;

import java.util.ArrayList;
import java.util.List;

/**
* @author <a href="mailto:danielsoro@gmail.com">Daniel Cunha (soro)</a>
*/
public class CommandListPopupCallBack implements Runnable
{
private VirtualFile[] selectedFiles;

public CommandListPopupCallBack(VirtualFile... selectedFiles)
{
this.selectedFiles = selectedFiles;
}

@Override
public void run()
{
UIContext uiContext = UIContextFactory.create(selectedFiles);

new CommandListPopupBuilder()
.setUIContext(uiContext)
.setCommands(getAllCandidates(uiContext))
.build()
.showInFocusCenter();
}

private List<UICommand> getAllCandidates(UIContext uiContext)
{
List<UICommand> commands = new ArrayList<UICommand>();
CommandFactory commandFactory = ServiceHelper.getForgeService().getCommandFactory();

for (UICommand command : commandFactory.getCommands())
{
if (isCandidate(command, uiContext))
{
commands.add(command);
}
}

return commands;
}

private boolean isCandidate(UICommand command, UIContext uiContext)
{
return !(command instanceof UIWizardStep) && command.isEnabled(uiContext);
}
}

0 comments on commit 6872f93

Please sign in to comment.