-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add ForgeProxies utility class, refresh project and open UISelection …
…after command execution
- Loading branch information
1 parent
9308d37
commit 51ac45e
Showing
7 changed files
with
142 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
44 changes: 44 additions & 0 deletions
44
src/main/java/org/jboss/forge/plugin/idea/util/ForgeProxies.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
62
src/main/java/org/jboss/forge/plugin/idea/util/IDEUtil.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |