Skip to content

Commit

Permalink
JBIDE-18004: Allowing pre-populated parameter values
Browse files Browse the repository at this point in the history
  • Loading branch information
gastaldi committed Sep 3, 2014
1 parent d33d403 commit 89a5c19
Show file tree
Hide file tree
Showing 4 changed files with 83 additions and 12 deletions.
9 changes: 7 additions & 2 deletions plugins/org.jboss.tools.forge.ui/plugin.xml
Expand Up @@ -74,12 +74,17 @@
name="Open Forge Wizard">
<commandParameter
id="org.jboss.tools.forge.ui.runForgeCommand.commandName"
name="commandName"
name="Command Name"
optional="false">
</commandParameter>
<commandParameter
id="org.jboss.tools.forge.ui.runForgeCommand.commandTitle"
name="commandTitle"
name="Command Title"
optional="true">
</commandParameter>
<commandParameter
id="org.jboss.tools.forge.ui.runForgeCommand.commandValues"
name="Values (in key=value format)"
optional="true">
</commandParameter>
</command>
Expand Down
Expand Up @@ -38,6 +38,7 @@
import org.eclipse.swt.widgets.Text;
import org.eclipse.ui.IEditorInput;
import org.eclipse.ui.IEditorPart;
import org.eclipse.ui.ISelectionService;
import org.eclipse.ui.IWorkbenchPage;
import org.eclipse.ui.IWorkbenchPart;
import org.eclipse.ui.IWorkbenchWindow;
Expand Down Expand Up @@ -92,7 +93,8 @@ public UICommandListDialog(IWorkbenchWindow window) {

public static IStructuredSelection getCurrentSelection(
IWorkbenchWindow window) {
ISelection selection = window.getSelectionService().getSelection();
ISelectionService selectionService = window.getSelectionService();
ISelection selection = selectionService.getSelection();
IStructuredSelection currentSelection = null;
if (selection instanceof TreeSelection) {
currentSelection = (TreeSelection) selection;
Expand All @@ -102,6 +104,12 @@ public static IStructuredSelection getCurrentSelection(
currentSelection = new StructuredSelection(activeEditorFile);
}
}
if (currentSelection == null) {
// Try to get from Package Explorer
currentSelection = (IStructuredSelection) selectionService
.getSelection("org.eclipse.jdt.ui.PackageExplorer");
}

return currentSelection;
}

Expand Down
Expand Up @@ -10,6 +10,7 @@
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.TreeMap;

import org.eclipse.jface.dialogs.IPageChangedListener;
Expand All @@ -25,6 +26,7 @@
import org.jboss.forge.addon.ui.controller.CommandController;
import org.jboss.forge.addon.ui.controller.CommandControllerFactory;
import org.jboss.forge.addon.ui.controller.WizardCommandController;
import org.jboss.forge.addon.ui.input.InputComponent;
import org.jboss.forge.addon.ui.metadata.UICommandMetadata;
import org.jboss.forge.addon.ui.wizard.UIWizardStep;
import org.jboss.tools.forge.core.furnace.FurnaceService;
Expand Down Expand Up @@ -97,13 +99,16 @@ public Map<String, UICommand> getAllCandidatesAsMap() {
}

public void openWizard(String windowTitle, UICommand selectedCommand) {
openWizard(windowTitle, selectedCommand, null);
}

public void openWizard(String windowTitle, UICommand selectedCommand,
Map<String, ?> values) {
CommandControllerFactory controllerFactory = FurnaceService.INSTANCE
.lookup(CommandControllerFactory.class);
ForgeUIRuntime runtime = new ForgeUIRuntime();
CommandController controller = controllerFactory.createController(
context, runtime, selectedCommand);

ForgeWizard wizard = new ForgeWizard(windowTitle, controller, context);
try {
controller.initialize();
} catch (Exception e) {
Expand All @@ -113,12 +118,49 @@ public void openWizard(String windowTitle, UICommand selectedCommand) {
ForgeUIPlugin.log(e);
return;
}
ForgeWizard wizard = new ForgeWizard(windowTitle, controller, context);
final WizardDialog wizardDialog;
// TODO: Review this
if (controller instanceof WizardCommandController) {
WizardCommandController wizardController = (WizardCommandController) controller;
if (values != null) {
Map<String, InputComponent<?, ?>> inputs = wizardController
.getInputs();
for (String key : inputs.keySet()) {
Object value = values.get(key);
if (value != null)
wizardController.setValueFor(key, value);
}
while (wizardController.canMoveToNextStep()) {
try {
wizardController.next().initialize();
} catch (Exception e) {
ForgeUIPlugin.log(e);
break;
}
inputs = wizardController.getInputs();
for (String key : inputs.keySet()) {
Object value = values.get(key);
if (value != null)
wizardController.setValueFor(key, value);
}
}
// Rewind
while (wizardController.canMoveToPreviousStep()) {
try {
wizardController.previous();
} catch (Exception e) {
ForgeUIPlugin.log(e);
}
}
}
wizardDialog = new ForgeWizardDialog(parentShell, wizard,
(WizardCommandController) controller);
wizardController);
} else {
if (values != null) {
for (Entry<String, ?> entry : values.entrySet()) {
controller.setValueFor(entry.getKey(), entry.getValue());
}
}
wizardDialog = new ForgeCommandDialog(parentShell, wizard);
}
// TODO: Show help button when it's possible to display the docs for
Expand Down
Expand Up @@ -7,6 +7,9 @@

package org.jboss.tools.forge.ui.internal.ext.handlers;

import java.util.HashMap;
import java.util.Map;

import org.eclipse.core.commands.AbstractHandler;
import org.eclipse.core.commands.ExecutionEvent;
import org.eclipse.core.commands.ExecutionException;
Expand Down Expand Up @@ -39,6 +42,18 @@ public Object execute(ExecutionEvent event) throws ExecutionException {
.getParameter("org.jboss.tools.forge.ui.runForgeCommand.commandName");
final String wizardTitle = event
.getParameter("org.jboss.tools.forge.ui.runForgeCommand.commandTitle");
final String wizardValues = event
.getParameter("org.jboss.tools.forge.ui.runForgeCommand.commandValues");
final Map<String, Object> values;
if (wizardValues == null) {
values = null;
} else {
values = new HashMap<>();
for (String entry : wizardValues.split(",")) {
String[] split = entry.split("=");
values.put(split[0], split[1]);
}
}
try {
final IWorkbenchWindow window = HandlerUtil
.getActiveWorkbenchWindowChecked(event);
Expand All @@ -52,29 +67,30 @@ public void done(IJobChangeEvent event) {
Display.getDefault().asyncExec(new Runnable() {
@Override
public void run() {
openWizard(wizardName, wizardTitle, window);
openWizard(window, wizardName, wizardTitle,
values);
}
});
}
});
job.schedule();
} else {
openWizard(wizardName, wizardTitle, window);
openWizard(window, wizardName, wizardTitle, values);
}
} catch (Exception e) {
ForgeUIPlugin.log(e);
}
return null;
}

private void openWizard(String wizardName, String wizardTitle,
IWorkbenchWindow window) {
private void openWizard(IWorkbenchWindow window, String wizardName,
String wizardTitle, Map<String, ?> values) {
IStructuredSelection currentSelection = UICommandListDialog
.getCurrentSelection(window);
WizardDialogHelper helper = new WizardDialogHelper(window.getShell(),
currentSelection);
UICommand command = helper.getCommand(wizardName);
helper.openWizard(wizardTitle == null ? wizardName : wizardTitle,
command);
command, values);
}
}

0 comments on commit 89a5c19

Please sign in to comment.