Skip to content

Commit

Permalink
JBIDE-18004: Created Wizard handler to open Forge wizards
Browse files Browse the repository at this point in the history
  • Loading branch information
gastaldi committed Sep 2, 2014
1 parent b348e9d commit 176666a
Show file tree
Hide file tree
Showing 4 changed files with 141 additions and 10 deletions.
22 changes: 22 additions & 0 deletions plugins/org.jboss.tools.forge.ui/plugin.xml
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,28 @@
categoryId="org.jboss.tools.forge.ui"
id="org.jboss.tools.forge.ui.command.runForgeCommand"
name="Run a Forge Command" />
<command
categoryId="org.jboss.tools.forge.ui"
defaultHandler="org.jboss.tools.forge.ui.internal.ext.handlers.OpenWizardHandler"
description="Opens a Forge Wizard given its parameter"
id="org.jboss.tools.forge.ui.command.openWizard"
name="Open Forge Wizard">
<commandParameter
id="org.jboss.tools.forge.ui.openWizard.wizardName"
name="wizardName"
optional="false">
</commandParameter>
<commandParameter
id="org.jboss.tools.forge.ui.openWizard.wizardTitle"
name="wizardTitle"
optional="true">
</commandParameter>
<commandParameter
id="org.jboss.tools.forge.ui.command.openWizard.wizardPath"
name="path"
optional="true">
</commandParameter>
</command>
</extension>
<extension
point="org.eclipse.ui.menus">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,16 +73,7 @@ public UICommandListDialog(IWorkbenchWindow window) {
true, true, "Run a Forge command", "JBoss Forge v."
+ FurnaceRuntime.INSTANCE.getVersion()
+ " - Start typing to filter the list");
ISelection selection = window.getSelectionService().getSelection();
IStructuredSelection currentSelection = null;
if (selection instanceof TreeSelection) {
currentSelection = (TreeSelection) selection;
} else {
Object activeEditorFile = getActiveEditorInput(window);
if (activeEditorFile != null) {
currentSelection = new StructuredSelection(activeEditorFile);
}
}
IStructuredSelection currentSelection = getCurrentSelection(window);
wizardHelper = new WizardDialogHelper(getParentShell(),
currentSelection);
UISelectionImpl<?> uiSelection = wizardHelper.getContext()
Expand All @@ -99,6 +90,21 @@ public UICommandListDialog(IWorkbenchWindow window) {
}
}

public static IStructuredSelection getCurrentSelection(
IWorkbenchWindow window) {
ISelection selection = window.getSelectionService().getSelection();
IStructuredSelection currentSelection = null;
if (selection instanceof TreeSelection) {
currentSelection = (TreeSelection) selection;
} else {
Object activeEditorFile = getActiveEditorInput(window);
if (activeEditorFile != null) {
currentSelection = new StructuredSelection(activeEditorFile);
}
}
return currentSelection;
}

/**
* Retrieves the {@link IFile} represented by the active editor
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,12 @@ public List<UICommand> getAllCandidatesAsList() {
return result;
}

public UICommand getCommand(String name) {
CommandFactory commandFactory = FurnaceService.INSTANCE
.lookup(CommandFactory.class);
return commandFactory.getCommandByName(context, name);
}

public Map<String, UICommand> getAllCandidatesAsMap() {
Map<String, UICommand> result = new TreeMap<>();
CommandFactory commandFactory = FurnaceService.INSTANCE
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
/**
* 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.tools.forge.ui.internal.ext.handlers;

import java.io.File;

import org.eclipse.core.commands.AbstractHandler;
import org.eclipse.core.commands.ExecutionEvent;
import org.eclipse.core.commands.ExecutionException;
import org.eclipse.core.runtime.jobs.IJobChangeEvent;
import org.eclipse.core.runtime.jobs.Job;
import org.eclipse.core.runtime.jobs.JobChangeAdapter;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.jface.viewers.StructuredSelection;
import org.eclipse.swt.widgets.Display;
import org.eclipse.ui.IWorkbenchWindow;
import org.eclipse.ui.handlers.HandlerUtil;
import org.jboss.forge.addon.ui.command.UICommand;
import org.jboss.tools.forge.core.furnace.FurnaceRuntime;
import org.jboss.tools.forge.core.runtime.ForgeRuntimeState;
import org.jboss.tools.forge.ui.internal.ForgeUIPlugin;
import org.jboss.tools.forge.ui.internal.ext.dialog.UICommandListDialog;
import org.jboss.tools.forge.ui.internal.ext.dialog.WizardDialogHelper;
import org.jboss.tools.forge.ui.internal.part.ForgeConsoleView;
import org.jboss.tools.forge.ui.util.ForgeHelper;

/**
* A handler to open Forge wizards
*
* @author <a href="ggastald@redhat.com">George Gastaldi</a>
*/
public class OpenWizardHandler extends AbstractHandler {

@Override
public Object execute(ExecutionEvent event) throws ExecutionException {
// Parameters
final String wizardName = event
.getParameter("org.jboss.tools.forge.ui.openWizard.wizardName");
// Unused for now
final String wizardTitle = event
.getParameter("org.jboss.tools.forge.ui.openWizard.wizardTitle");
final String path = event
.getParameter("org.jboss.tools.forge.ui.command.openWizard.wizardPath");

try {
ForgeConsoleView forgeConsoleView = ForgeHelper
.findForgeConsoleView();
if (forgeConsoleView != null && forgeConsoleView.isShowing()) {
ForgeHelper.showRuntime(FurnaceRuntime.INSTANCE);
}
final IWorkbenchWindow window = HandlerUtil
.getActiveWorkbenchWindowChecked(event);
if (!ForgeRuntimeState.RUNNING.equals(FurnaceRuntime.INSTANCE
.getState())) {
Job job = ForgeHelper
.createStartRuntimeJob(FurnaceRuntime.INSTANCE);
job.addJobChangeListener(new JobChangeAdapter() {
@Override
public void done(IJobChangeEvent event) {
Display.getDefault().asyncExec(new Runnable() {
@Override
public void run() {
openWizard(wizardName, wizardTitle, path,
window);
}
});
}
});
job.schedule();
} else {
openWizard(wizardName, wizardTitle, path, window);
}
} catch (Exception e) {
ForgeUIPlugin.log(e);
}
return null;
}

private void openWizard(String wizardName, String wizardTitle, String path,
IWorkbenchWindow window) {
IStructuredSelection currentSelection;
if (path == null) {
currentSelection = UICommandListDialog.getCurrentSelection(window);
} else {
currentSelection = new StructuredSelection(new File(path));
}
WizardDialogHelper helper = new WizardDialogHelper(window.getShell(),
currentSelection);
UICommand command = helper.getCommand(wizardName);
helper.openWizard(wizardName, command);
}
}

0 comments on commit 176666a

Please sign in to comment.