Skip to content

Commit

Permalink
Add ForgeProxies utility class, refresh project and open UISelection …
Browse files Browse the repository at this point in the history
…after command execution
  • Loading branch information
adam-wyluda committed Jun 14, 2014
1 parent 9308d37 commit 51ac45e
Show file tree
Hide file tree
Showing 7 changed files with 142 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,11 @@
import com.intellij.openapi.actionSystem.AnAction;
import com.intellij.openapi.actionSystem.AnActionEvent;
import com.intellij.openapi.actionSystem.DataKeys;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.vfs.VirtualFile;
import org.jboss.forge.addon.ui.command.UICommand;
import org.jboss.forge.plugin.idea.service.callbacks.CommandListPopupCallBack;
import org.jboss.forge.plugin.idea.service.ServiceHelper;
import org.jboss.forge.plugin.idea.service.callbacks.CommandListPopupCallBack;
import org.jboss.forge.plugin.idea.ui.CommandListPopupBuilder;

/**
Expand All @@ -32,6 +33,7 @@ public void actionPerformed(AnActionEvent event)
return;
}

Project project = event.getData(DataKeys.PROJECT);
VirtualFile[] files = event.getData(DataKeys.VIRTUAL_FILE_ARRAY);

// If no file is selected, then set project directory as selection
Expand All @@ -41,6 +43,6 @@ public void actionPerformed(AnActionEvent event)
}

final VirtualFile[] selectedFiles = files;
ServiceHelper.loadFurnaceAndRun(new CommandListPopupCallBack(selectedFiles));
ServiceHelper.loadFurnaceAndRun(new CommandListPopupCallBack(project, selectedFiles));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
*/
package org.jboss.forge.plugin.idea.context;

import com.intellij.openapi.project.Project;
import com.intellij.openapi.vfs.VirtualFile;
import org.jboss.forge.addon.convert.Converter;
import org.jboss.forge.addon.convert.ConverterFactory;
Expand All @@ -28,12 +29,12 @@
*/
public class UIContextFactory
{
public static UIContext create(VirtualFile[] files)
public static UIContext create(Project project, VirtualFile[] files)
{
UIProvider provider = new UIProviderImpl();
UISelection<?> initialSelection = getSelection(files);

return new UIContextImpl(initialSelection, provider);
return new UIContextImpl(project, initialSelection, provider);
}

private static UISelection<?> getSelection(VirtualFile[] files)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
*/
package org.jboss.forge.plugin.idea.context;

import com.intellij.openapi.project.Project;
import org.jboss.forge.addon.ui.UIProvider;
import org.jboss.forge.addon.ui.context.AbstractUIContext;
import org.jboss.forge.addon.ui.context.UISelection;
Expand All @@ -15,8 +16,12 @@ public class UIContextImpl extends AbstractUIContext
private final UISelection<?> initialSelection;
private final UIProvider provider;

UIContextImpl(UISelection<?> initialSelection, UIProvider provider)
private final Project project;

UIContextImpl(Project project, UISelection<?> initialSelection, UIProvider provider)
{
this.project = project;

this.initialSelection = initialSelection;
this.provider = provider;
}
Expand All @@ -33,4 +38,9 @@ public UIProvider getProvider()
{
return provider;
}

public Project getProject()
{
return project;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

package org.jboss.forge.plugin.idea.service.callbacks;

import com.intellij.openapi.project.Project;
import com.intellij.openapi.vfs.VirtualFile;
import org.jboss.forge.addon.ui.command.CommandFactory;
import org.jboss.forge.addon.ui.command.UICommand;
Expand All @@ -24,17 +25,19 @@
*/
public class CommandListPopupCallBack implements Runnable
{
private VirtualFile[] selectedFiles;
private final Project project;
private final VirtualFile[] selectedFiles;

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

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

new CommandListPopupBuilder()
.setUIContext(uiContext)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import com.intellij.ui.wizard.WizardNavigationState;
import com.intellij.ui.wizard.WizardStep;
import net.miginfocom.swing.MigLayout;
import org.jboss.forge.addon.ui.context.UIContext;
import org.jboss.forge.addon.ui.controller.CommandController;
import org.jboss.forge.addon.ui.input.InputComponent;
import org.jboss.forge.addon.ui.result.Result;
Expand All @@ -18,6 +19,7 @@
import org.jboss.forge.plugin.idea.ui.component.ForgeComponent;
import org.jboss.forge.plugin.idea.ui.listeners.ValueChangeListener;
import org.jboss.forge.plugin.idea.util.ForgeNotifications;
import org.jboss.forge.plugin.idea.util.IDEUtil;

import javax.swing.*;
import java.util.HashMap;
Expand All @@ -32,7 +34,6 @@
public class ForgeWizardStep extends WizardStep<ForgeWizardModel>
{
private final ForgeWizardModel model;
private final CommandController controller;
private final NavigationState navigationState;

/**
Expand All @@ -43,7 +44,6 @@ public class ForgeWizardStep extends WizardStep<ForgeWizardModel>
public ForgeWizardStep(ForgeWizardModel model, CommandController controller)
{
this.model = model;
this.controller = controller;
this.navigationState = new NavigationState(model, controller);

try
Expand All @@ -53,6 +53,7 @@ public ForgeWizardStep(ForgeWizardModel model, CommandController controller)
catch (Exception e)
{
ForgeNotifications.showErrorMessage(e);
e.printStackTrace();
}
}

Expand All @@ -65,7 +66,7 @@ public JComponent prepare(WizardNavigationState state)

components = new HashMap<>();

for (InputComponent input : controller.getInputs().values())
for (InputComponent input : navigationState.getController().getInputs().values())
{
ComponentBuilder builder =
ComponentBuilderRegistry.INSTANCE.getBuilderFor(input);
Expand Down Expand Up @@ -116,6 +117,7 @@ private WizardStep navigate(boolean forward)
catch (Exception e)
{
model.getDialog().setErrorMessage(e.getMessage());
e.printStackTrace();
return this;
}
}
Expand All @@ -125,12 +127,17 @@ public boolean onFinish()
{
try
{
Result result = controller.execute();
Result result = navigationState.getController().execute();
ForgeNotifications.showExecutionResult(result);

UIContext context = navigationState.getController().getContext();
IDEUtil.refreshProject(context);
IDEUtil.openSelection(context);
}
catch (Exception e)
{
ForgeNotifications.showErrorMessage(e);
e.printStackTrace();
}

return true;
Expand All @@ -145,7 +152,7 @@ public Icon getIcon()
@Override
public String getExplanation()
{
return controller.getMetadata().getDescription();
return navigationState.getController().getMetadata().getDescription();
}

public void refreshNavigationState()
Expand Down
44 changes: 44 additions & 0 deletions src/main/java/org/jboss/forge/plugin/idea/util/ForgeProxies.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/*
* 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.util;

import org.jboss.forge.furnace.proxy.ClassLoaderAdapterBuilder;
import org.jboss.forge.furnace.proxy.Proxies;
import org.jboss.forge.plugin.idea.service.ServiceHelper;

/**
* @author Adam Wyłuda
*/
public class ForgeProxies
{
/**
* Creates CLAC proxy of given type to specified instance. Returns null if either instance is null or is not
* assignable to given type.
*/
public static <T> T proxyTo(Class<T> targetClass, Object instance)
{
if (instance == null)
{
return null;
}

instance = Proxies.unwrap(instance);
Class nativeTargetClass = ServiceHelper.getForgeService().locateNativeClass(targetClass);

if (nativeTargetClass.isAssignableFrom(instance.getClass()))
{
return (T) ClassLoaderAdapterBuilder
.callingLoader(targetClass.getClassLoader())
.delegateLoader(instance.getClass().getClassLoader())
.enhance(instance, targetClass);
}
else
{
return null;
}
}
}
62 changes: 62 additions & 0 deletions src/main/java/org/jboss/forge/plugin/idea/util/IDEUtil.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
/*
* 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.util;

import com.intellij.openapi.fileEditor.FileEditorManager;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.vfs.LocalFileSystem;
import com.intellij.openapi.vfs.VirtualFile;
import org.jboss.forge.addon.resource.FileResource;
import org.jboss.forge.addon.ui.context.UIContext;
import org.jboss.forge.furnace.proxy.Proxies;
import org.jboss.forge.furnace.util.Assert;
import org.jboss.forge.plugin.idea.context.UIContextImpl;

import java.io.File;

/**
* @author Adam Wyłuda
*/
public class IDEUtil
{
public static void refreshProject(UIContext context)
{
refreshProject(projectFromContext(context));
}

public static void refreshProject(Project project)
{
project.getBaseDir().refresh(true, true);
}

public static void openSelection(UIContext context)
{
Project project = projectFromContext(context);

FileResource resource = ForgeProxies.proxyTo(FileResource.class, context.getSelection());

if (resource != null)
{
File file = new File(resource.getFullyQualifiedName());
openFile(project, file);
}
}

public static void openFile(Project project, File file)
{
VirtualFile virtualFile = LocalFileSystem.getInstance().refreshAndFindFileByIoFile(file);
FileEditorManager.getInstance(project).openFile(virtualFile, true);
}

private static Project projectFromContext(UIContext context)
{
context = Proxies.unwrap(context);
Assert.isTrue(context instanceof UIContextImpl, "UIContext must be an instance of UIContextImpl");

return ((UIContextImpl) context).getProject();
}
}

0 comments on commit 51ac45e

Please sign in to comment.