Question about Actions

Hi all,
Now I'm using CreateElementActionBase class, as base for my action class and override invokeDialog in it. The directory object passed to it, always returns the containing package when calling directory.getVirtualFile() and directory.getName(). I need to get the file path and name, not the containing folder.
Probably, I'm using the wrong base class. So, my questions:
1- How to get the file name and absolute path, when right clicking on it for example in Project tool window.
2- How to have Context Sensitive Popup Menus? For exmaple, I want to be able to control showing or not showing some items in right click menu, depending on the selected file.

Best regards,
<Farshid/>

4 comments
Comment actions Permalink

All actions in IDEA have update method. You can overload it.
In with method you can disable/enable/show/hide this action depends on context. Look here:

 0;
        // Visibility
        e.getPresentation().setVisible(visible);
        // Enable or disable
        e.getPresentation().setEnabled(visible);
    }
]]>


Every action have invocation context. You can get it from AnActionEvent.getDataContext(). This context contains some context values such as selected virtual files, current active project, module, editor etc (see interface com.intellij.openapi.actionSystem.DataConstants).

To get VirtualFile just use:

Or, multiple selection:


To operate with VirtualFile path you can use methods from VirtualFile, or use VfsUtil class.

Thanks!

0
Comment actions Permalink

Hi,

I am not sure that my question is totally relevant to the one above, but I am trying to get the project scope and current @todo items, so here's my code:

public void actionPerformed(AnActionEvent e) {
    Project project = e.getProject();
    
AllTodosTreeStructure treeStructure = new AllTodosTreeStructure(project);
    ...
}


But I just constantly get NullPointerException :(

Stack trace:

[  49476]  ERROR - llij.ide.plugins.PluginManager - null
java.lang.NullPointerException
    at TodoExporter.actionPerformed(TodoExporter.java:14)
    at com.intellij.openapi.actionSystem.ex.ActionUtil.performActionDumbAware(ActionUtil.java:182)
    at com.intellij.openapi.actionSystem.impl.ActionMenuItem$ActionTransmitter$1.run(ActionMenuItem.java:312)
    at com.intellij.openapi.wm.impl.FocusManagerImpl.runOnOwnContext(FocusManagerImpl.java:958)
    at com.intellij.openapi.wm.impl.IdeFocusManagerImpl.runOnOwnContext(IdeFocusManagerImpl.java:124)
    at com.intellij.openapi.actionSystem.impl.ActionMenuItem$ActionTransmitter.actionPerformed(ActionMenuItem.java:282)
    at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2022)
    at com.intellij.openapi.actionSystem.impl.ActionMenuItem.fireActionPerformed(ActionMenuItem.java:109)
    at com.intellij.ui.plaf.beg.BegMenuItemUI.doClick(BegMenuItemUI.java:512)
    at com.intellij.ui.plaf.beg.BegMenuItemUI.access$300(BegMenuItemUI.java:44)
    at com.intellij.ui.plaf.beg.BegMenuItemUI$MyMouseInputHandler.mouseReleased(BegMenuItemUI.java:532)
    at java.awt.Component.processMouseEvent(Component.java:6525)
    at javax.swing.JComponent.processMouseEvent(JComponent.java:3324)
    at java.awt.Component.processEvent(Component.java:6290)
    at java.awt.Container.processEvent(Container.java:2234)
    at java.awt.Component.dispatchEventImpl(Component.java:4881)
    at java.awt.Container.dispatchEventImpl(Container.java:2292)
    at java.awt.Component.dispatchEvent(Component.java:4703)
    at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4898)
    at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4533)
    at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4462)
    at java.awt.Container.dispatchEventImpl(Container.java:2278)
    at java.awt.Window.dispatchEventImpl(Window.java:2750)
    at java.awt.Component.dispatchEvent(Component.java:4703)
    at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:758)
    at java.awt.EventQueue.access$500(EventQueue.java:97)
    at java.awt.EventQueue$3.run(EventQueue.java:709)
    at java.awt.EventQueue$3.run(EventQueue.java:703)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:75)
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:86)
    at java.awt.EventQueue$4.run(EventQueue.java:731)
    at java.awt.EventQueue$4.run(EventQueue.java:729)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:75)
    at java.awt.EventQueue.dispatchEvent(EventQueue.java:728)
    at com.intellij.ide.IdeEventQueue.defaultDispatchEvent(IdeEventQueue.java:734)
    at com.intellij.ide.IdeEventQueue._dispatchEvent(IdeEventQueue.java:565)
    at com.intellij.ide.IdeEventQueue.dispatchEvent(IdeEventQueue.java:382)
    at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:201)
    at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:116)
    at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:105)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:93)
    at java.awt.EventDispatchThread.run(EventDispatchThread.java:82)
[  49482]  ERROR - llij.ide.plugins.PluginManager - IntelliJ IDEA 14.1.3  Build #IC-141.1010.3
[  49482]  ERROR - llij.ide.plugins.PluginManager - JDK: 1.8.0_45
[  49483]  ERROR - llij.ide.plugins.PluginManager - VM: Java HotSpot(TM) 64-Bit Server VM
[  49483]  ERROR - llij.ide.plugins.PluginManager - Vendor: Oracle Corporation
[  49483]  ERROR - llij.ide.plugins.PluginManager - OS: Linux
[  49483]  ERROR - llij.ide.plugins.PluginManager - Last Action: TodoExporter
[  84546]   WARN - api.vfs.impl.local.FileWatcher - Watcher terminated with exit code 0

So the questions is - how should I handle this correctly?

0
Comment actions Permalink

In your update() method, you should check if project is null and disable the action (e.getPresentation().setEnabled(false)) if it's null.

0
Comment actions Permalink

I have an action associated with the NewGroup and I want to know which module was selected when the user selected my action.  You state...

Every action have invocation context. You can get it from AnActionEvent.getDataContext(). This context contains some context values such as selected virtual files, current active project, module, editor etc (see interface com.intellij.openapi.actionSystem.DataConstants).

And then imply that there's a way to get the context data, but there's no documentation there.  How is this done and what data context is associated with an action for the new group?

 

 

0

Please sign in to leave a comment.