Skip to content

Commit

Permalink
Refactor CommandListPopup
Browse files Browse the repository at this point in the history
  • Loading branch information
adam-wyluda committed Jun 6, 2014
1 parent 0310fa0 commit 4f72a0d
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 47 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,16 @@
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.ServiceHelper;
import org.jboss.forge.plugin.idea.ui.CommandListPopup;
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
Expand All @@ -28,7 +33,7 @@ public class ShowCommandListAction extends AnAction
@Override
public void actionPerformed(AnActionEvent event)
{
if (CommandListPopup.isActive())
if (CommandListPopupBuilder.isActive())
{
return;
}
Expand All @@ -48,9 +53,34 @@ public void actionPerformed(AnActionEvent event)
public void run()
{
UIContext uiContext = UIContextFactory.create(selectedFiles);
CommandListPopup popup = new CommandListPopup(uiContext);
popup.show();

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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,9 @@
import com.intellij.ui.ListCellRendererWrapper;
import com.intellij.ui.components.JBList;
import com.intellij.util.Function;
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.metadata.UICommandMetadata;
import org.jboss.forge.addon.ui.wizard.UIWizardStep;
import org.jboss.forge.plugin.idea.service.ServiceHelper;

import javax.swing.*;
import java.util.*;
Expand All @@ -25,38 +22,49 @@
*
* @author Adam Wyłuda
*/
public class CommandListPopup
public class CommandListPopupBuilder
{
// TODO Refactor to builder pattern

private static final Icon FORGE_ICON = new ImageIcon(CommandListPopup.class.getResource("/icons/forge.png"));
private static final Icon FORGE_ICON = new ImageIcon(CommandListPopupBuilder.class.getResource("/icons/forge.png"));
private static volatile boolean active;

private final UIContext uiContext;
private UIContext uiContext;
private List<UICommand> commands;

public CommandListPopup(UIContext uiContext)
public CommandListPopupBuilder setUIContext(UIContext uiContext)
{
this.uiContext = uiContext;
return this;
}

public CommandListPopupBuilder setCommands(List<UICommand> commands)
{
this.commands = commands;
return this;
}

public static boolean isActive()
{
return active;
}

public void show()
public JBPopup build()
{
if (active)
return;
active = true;

Map<UICommand, UICommandMetadata> metadataIndex = indexMetadata(commands);
Map<String, List<UICommand>> categories = categorizeCommands(commands, metadataIndex);
List<Object> elements = categoriesToList(sortCategories(categories, metadataIndex));

JBList list = buildJBList(elements, metadataIndex);
JBPopup popup = buildPopup(list, categories, metadataIndex);

return popup;
}

private JBList buildJBList(List<Object> elements, final Map<UICommand, UICommandMetadata> metadataIndex)
{
final JBList list = new JBList();
DefaultListModel model = new DefaultListModel();

List<UICommand> candidates = getAllCandidates();
final Map<UICommand, UICommandMetadata> metadataIndex = indexMetadata(candidates);
final Map<String, List<UICommand>> categories = categorizeCommands(candidates, metadataIndex);
final List<Object> elements = categoriesToList(sortCategories(categories, metadataIndex));
model.setSize(elements.size());

list.setCellRenderer(new ListCellRendererWrapper<Object>()
Expand Down Expand Up @@ -89,9 +97,15 @@ else if (data instanceof String)
{
model.set(i, elements.get(i));
}

list.setModel(model);

return list;
}

private JBPopup buildPopup(final JBList list,
final Map<String, List<UICommand>> categories,
final Map<UICommand, UICommandMetadata> metadataIndex)
{
final PopupChooserBuilder listPopupBuilder = JBPopupFactory.getInstance().createListPopupBuilder(list);
listPopupBuilder.setTitle("Run a Forge command");
listPopupBuilder.setResizable(true);
Expand All @@ -100,7 +114,7 @@ else if (data instanceof String)
@Override
public void onClosed(LightweightWindowEvent event)
{
CommandListPopup.this.active = false;
CommandListPopupBuilder.this.active = false;
}
});
listPopupBuilder.setItemChoosenCallback(new Runnable()
Expand Down Expand Up @@ -147,29 +161,7 @@ else if (object instanceof String)
}
});

JBPopup popup = listPopupBuilder.createPopup();
popup.showInFocusCenter();
}

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

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

return commands;
}

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

private Map<UICommand, UICommandMetadata> indexMetadata(List<UICommand> commands)
Expand Down Expand Up @@ -262,6 +254,6 @@ private void openWizard(UICommand command)
// TODO Use CommandController to obtain UICommand metadata
// ForgeWizardModel model = new ForgeWizardModel(command.getMetadata().getName(), command, files);
// ForgeWizardDialog dialog = new ForgeWizardDialog(model);
// dialog.show();
// dialog.build();
}
}

0 comments on commit 4f72a0d

Please sign in to comment.