Skip to content

Commit

Permalink
Refresh wizard dialog navigation state
Browse files Browse the repository at this point in the history
  • Loading branch information
adam-wyluda committed Jun 6, 2014
1 parent d7d8c0b commit 49517e0
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 34 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,7 @@
package org.jboss.forge.plugin.idea.ui.wizard;

import com.intellij.ui.wizard.WizardModel;
import com.intellij.ui.wizard.WizardStep;
import org.jboss.forge.addon.ui.controller.CommandController;
import org.jboss.forge.addon.ui.controller.WizardCommandController;

/**
* Represents the model of a wizard
Expand All @@ -19,41 +17,17 @@
*/
public class ForgeWizardModel extends WizardModel
{
private CommandController originalController;

@SuppressWarnings("unchecked")
public ForgeWizardModel(CommandController originalController)
{
super(originalController.getMetadata().getName());
this.originalController = originalController;

addWizardSteps();
addWizardSteps(originalController);
((ForgeWizardStep) getCurrentStep()).refreshNavigationState(getCurrentNavigationState());
}

private void addWizardSteps()
private void addWizardSteps(CommandController originalController)
{
add(new ForgeWizardStep(originalController));
}

@Override
public boolean isFirst(WizardStep step)
{
CommandController controller = ((ForgeWizardStep) step).getController();
return controller.getCommand() == originalController.getCommand();
}

@Override
public boolean isLast(WizardStep step)
{
CommandController controller = ((ForgeWizardStep) step).getController();

boolean result = true;

if (controller instanceof WizardCommandController)
{
result = ((WizardCommandController) controller).canExecute();
}

return result;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
*/
public class ForgeWizardStep extends WizardStep<ForgeWizardModel>
{
private CommandController controller;
private final CommandController controller;

public ForgeWizardStep(CommandController controller)
{
Expand Down Expand Up @@ -67,14 +67,17 @@ public JComponent prepare(WizardNavigationState state)
public ForgeWizardStep onNext(ForgeWizardModel model)
{
// If it's not a wizard, we don't care
if (!(controller instanceof WizardCommandController))
if (!(isWizardController()))
{
return null;
}

try
{
return new ForgeWizardStep(((WizardCommandController) controller).next());
CommandController nextController = getWizardCommandController().next();
ForgeWizardStep nextStep = new ForgeWizardStep(nextController);
refreshNavigationState(model.getCurrentNavigationState(), nextController);
return nextStep;
}
catch (Exception e)
{
Expand All @@ -88,14 +91,17 @@ public ForgeWizardStep onNext(ForgeWizardModel model)
public WizardStep onPrevious(ForgeWizardModel model)
{
// If it's not a wizard, we don't care
if (!(controller instanceof WizardCommandController))
if (!(isWizardController()))
{
return null;
}

try
{
return new ForgeWizardStep(((WizardCommandController) controller).previous());
CommandController previousController = getWizardCommandController().previous();
ForgeWizardStep previousStep = new ForgeWizardStep(previousController);
refreshNavigationState(model.getCurrentNavigationState(), previousController);
return previousStep;
}
catch (Exception e)
{
Expand All @@ -120,4 +126,67 @@ public boolean onFinish()

return true;
}

public void refreshNavigationState(WizardNavigationState navigationState)
{
refreshNavigationState(navigationState, this.controller);
}

private static void refreshNavigationState(WizardNavigationState navigationState, CommandController controller)
{
navigationState.CANCEL.setEnabled(true);
navigationState.PREVIOUS.setEnabled(isPreviousEnabled(controller));
navigationState.NEXT.setEnabled(isNextEnabled(controller));
navigationState.FINISH.setEnabled(isFinishEnabled(controller));
}

private static boolean isPreviousEnabled(CommandController controller)
{
if (!isWizardController(controller))
{
return false;
}

return getWizardCommandController(controller).canMoveToPreviousStep();
}

private static boolean isNextEnabled(CommandController controller)
{
if (!isWizardController(controller))
{
return false;
}

return getWizardCommandController(controller).canMoveToNextStep();
}

private static boolean isFinishEnabled(CommandController controller)
{
if (!isWizardController(controller))
{
return false;
}

return getWizardCommandController(controller).canExecute();
}

private boolean isWizardController()
{
return isWizardController(this.controller);
}

private static boolean isWizardController(CommandController controller)
{
return controller instanceof WizardCommandController;
}

private WizardCommandController getWizardCommandController()
{
return getWizardCommandController(this.controller);
}

private static WizardCommandController getWizardCommandController(CommandController controller)
{
return (WizardCommandController) controller;
}
}

0 comments on commit 49517e0

Please sign in to comment.