Create IntelliJ IDEA plugin that submit a form - intellij-idea

I'm learning about how to create an IntelliJ plugin. I was reading through some of the documentation on JetBrains. With that documentation, I've created a sample project and now I understand little bits and pieces of SDK. What I'm struggling with right now is how can I create a form that takes some input from the user, submit the form and show the response that it got from the server.
This can be under the tools window. Any sample GitHub project that does something like this?

in plugin.xml you should add
<extensions defaultExtensionNs="com.intellij">
...
<toolWindow factoryClass="SomeClass" id="someUniqueID" />
</extensions>
and then create the factory class like this
public class SomeClass implements ToolWindowFactory {
#Override
public void createToolWindowContent(#NotNull Project p, #NotNull ToolWindow w) {
ContentFactory contentFactory = ContentFactory.SERVICE.getInstance();
JComponent form = new MyMagicForm(...);
Content content = contentFactory.createContent(form, "My form", false);
content.setCloseable(false);
w.getComponent().putClientProperty(ToolWindowContentUi.HIDE_ID_LABEL, "true");
w.getContentManager().addContent(content);
}
}

Related

Null pointer when adding action listeners in IntelliJ GUI form

I'm using an IntelliJ GUI form to create a toolwindow as part of an IntelliJ plugin. This is some code in the class bound to the form:
private JButton checkNifi;
NifiToolWindow(ToolWindow toolWindow) {
checkNifi.addActionListener(e -> toolWindow.hide(null));
}
I understand that when this action listener is added the button is still null and this is the issue, however even if I do checkNifi = new JButton("Some text");, the null pointer instead gets thrown on this line.
I should add I also have a ToolWindowFactory class which looks like this:
#Override
public void createToolWindowContent(#NotNull Project project, #NotNull com.intellij.openapi.wm.ToolWindow toolWindow) {
NifiToolWindow nifiToolWindow = new NifiToolWindow(toolWindow);
ContentFactory contentFactory = new ContentFactoryImpl();
Content content = contentFactory.createContent(nifiToolWindow.getContent(), "", false);
toolWindow.getContentManager().addContent(content);
}
This is taken from the example here https://github.com/JetBrains/intellij-sdk-docs/tree/master/code_samples/tool_window/src/myToolWindow
Any help or ideas would be great.
I found the solution, I had create custom box ticked in the gui designer, but an empty createGuiComponents() method. Therefore it was null.

IntelliJ IDEA plugin: associating a source psi file with a icon in the package structure

I'm building a idea plugin which needs to create source file with the extension of ".java". I have created a file template and used it in an implementation of JavaCreateTemplateInPackageAction<PsiElement> class. In the constructor of the above mentioned class I called the constructor of the super class with a icon(which I loaded using IconLoader.getIcon before) like this
protected JavaCreateTemplateInPackageAction(String text, String description, Icon icon, boolean inSourceOnly) {
super(text, description, icon, inSourceOnly ? JavaModuleSourceRootTypes.SOURCES : null);
}
Finally I registered the implementation in plugin.xml as an action. The code works as a charm to create the source file with given template but the issue is in the package structure it doesn't shows the given custom icon, instead it shows the default icon for java classes(letter 'c'). But the given icon appears in the new menu when right click on the source package to create a source file. Can anybody help me out please.? Thanks.
PS: I tried to change the file extension something other than .java and it still doesn't show the expected icon but instead it shows the generic icon for java(letter 'j' icon)
The icon you provide in your JavaCreateTemplateInPackageAction is only used for that action. Icons in the project view can be overridden using an IconProvider, that you can register using the <iconProvider> tag in your plugin.xml:
<iconProvider implementation="org.intellij.plugins.ceylon.ide.presentation.CeylonIconProvider"/>
Java code:
public class CeylonIconProvider extends IconProvider {
#Nullable
#Override
public Icon getIcon(#NotNull PsiElement element, int flags) {
if (element instanceof CeylonFile) {
return ...
}
if (element instanceof CeyLightClass) {
...
}
return null;
}
}

Display ViewPart in eclipse

I am currently developing an eclipse plugin which displays DOT-Graphs. For this purpose I make use of this plugin. However, I have no idea how to actually display the graph which I built. I want to display it in the middle of the eclipse window as an Editor.
To get this done I created a custom Editor class which needs some code in its createPartControl(Composite) code in order to make use of the DotGraphView which is provided by the plugin.
The question is, how can I display this DotGraphView?
The code of my Editor looks like this:
#Override
public void createPartControl(Composite container) {
DotImport importer = new DotImport(TEST_GRAPH);
Graph graph = importer.newGraphInstance();
DotGraphView dotGraphView = new DotGraphView();
dotGraphView.setGraph(graph);
// add dotGraphView as a child to container and display it
// What todo here?
}
To use the graph in your own custom view, check out the implementation of ZestFxUiView, the superclass of DotGraphView. You could probably subclass ZestFxUiView and call setGraph with your graph object.

Attach my action to F5/Refresh

i'm writing a plugin for Eclipse and i would like to attach one of my actions to Eclipse F5/Refresh event.
Can anyone help me?
Thanks!
You can attach a IExecutionListener to the ICommandService. You will get notification of all the commands executed. You can look for the command id that you want (in this case org.eclipse.ui.file.refresh) and do your operation
I'm assuming you're writing this for Eclipse Helios (3.6).
In Eclipse help, in the Platform Plug-in Developer Guide -> Programmer's Guide -> Advanced resource concepts -> Refresh providers, there's an extension point.
org.eclipse.core.resources.refreshProviders
Your class has to extend RefreshProvider to use this extension.
According to Prakash G. R., I show the sample code.
Because the initialization code in Activator does not work if we need to use Workbench, therefore I use the startup extension point. The plugin.xml is
<extension
point="org.eclipse.ui.startup">
<startup
class="sampleplugin.MyStartUp">
</startup>
</extension>
Therefore in MyStartUp class we add the ExecutionListener to ICommandService.
The important thing is that the ExecutionEvent in the preExecute method is not able to
extract the selection. This is different from usual ExecutionEvent in Command.
Therefore , the MyStartUp.java is
public class MyStartUp implements IStartup {
#Override
public void earlyStartup() {
ICommandService service = (ICommandService) PlatformUI.getWorkbench().getService(ICommandService .class);
service.addExecutionListener(
new IExecutionListener() {
...
#Override
public void postExecuteSuccess(String commandId,
Object returnValue) {
// do something post
}
#Override
public void preExecute(String commandId,
final ExecutionEvent event) {
if (org.eclipse.ui.IWorkbenchCommandConstants.FILE_REFRESH.equals(commandId) ) {
IWorkbench wb = PlatformUI.getWorkbench();
IWorkbenchWindow win = wb.getActiveWorkbenchWindow();
IWorkbenchPage page = win.getActivePage();
ISelection selection = page.getSelection();
// do something using selection
}
}
});
}
}
I use the
IWorkbench wb = PlatformUI.getWorkbench();
IWorkbenchWindow win = wb.getActiveWorkbenchWindow();
IWorkbenchPage page = win.getActivePage();
ISelection selection = page.getSelection();
instead of
IStructuredSelection selection = (IStructuredSelection) HandlerUtil.getCurrentSelectionChecked(event);
because the above reason. However, this is due to the Eclipse inner mechanism.
The refresh event uses an old action mechanism and The ExternalActionManager call
preExecute method directly in which the event has no data for selection.
I want the second formula
IStructuredSelection selection = (IStructuredSelection) HandlerUtil.getCurrentSelectionChecked(event);
may be available in preExecute method in the future.

Eclipse: Within a plug-in, how to access another plug-ins preference store?

I have an Eclipse plug-in with a checkbox in the plug-in's preference page.
This checkbox is used for enabling and disabling an editor, which is being launched from this plug-in.
However, the problem is, I would also like to be able to enable and disable this 'editor-launch' from another plug-in, by having actions which change the value of the checkbox in the above mentioned preference page.
Here's the problem, how do I access that local preference store from another plug-in?
I've tried things like..
View myView = (View) PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage().findView("ViewID");
But this 'myView' always seems to be null.. And also, what would I do with the view since it's the Plug-in I want.
Platform.getBundle('bundleName')...
Same here, want the Plugin, not the bundle corresponding to is.
No matter what I try nothing seems to work.
Does anyone have any ideas?
There are two ways of doing this:
Please refer to http://www.vogella.com/tutorials/EclipsePreferences/article.html#preferences_pluginaccess
Using .getPluginPreferences(). For example, there is a plugin class "com.xxx.TestPlugin" which extends org.eclipse.ui.plugin.AbstractUIPlugin.Plugin, in order to get access to the preferences of TestPlugin. The plugin code could be below:
public class TestPlugin extends AbstractUIPlugin {
private static TestPlugin plugin;
public static final String PREF_TEST = "test_preference";
/**
* The constructor.
*/
public TestPlugin() {
plugin = this;
}
/**
* This method is called upon plug-in activation
*/
public void start(BundleContext context) throws Exception {
super.start(context);
}
/**
* This method is called when the plug-in is stopped
*/
public void stop(BundleContext context) throws Exception {
super.stop(context);
plugin = null;
}
/**
* Returns the shared instance.
*/
public static TestPlugin getDefault() {
return plugin;
}
}
To access the preference of TestPlugin, the code could be:
TestPlugin.getDefault().getPluginPreferences().getDefaultBoolean(TestPlugin.PREF_TEST);
Or have a look at this answer: Writing Eclipse plugin to modify Editor Preferences
This thread recommend the use of a Service tracker:
ServiceTracker tracker = new ServiceTracker(ToolkitPlugin.getDefault().getBundle().getBundleContext(),
IProxyService.class.getName(), null);
tracker.open();
proxyService = (IProxyService) tracker.getService();
proxyService.addProxyChangeListener(this);
This may work.
Prefs stores are found per plugin. This is one way to get a prefs store for the plugin whose activator class is ActivatorA.
IPreferenceStore store = ActivatorA.getDefault().getPreferenceStore();
If you want another plugin to refer to the same store, perhaps you could expose some api on ActivatorA for it to get there, e.g.
public IPreferenceStore getSharedPrefs() {
return ActivatorA.getDefault().getPreferenceStore();
}
The second plugin would find the shared store by doing this
IPreferenceStore sharedPrefs = ActivatorA.getSharedPrefs();
Good luck.