Cannot add programmatically action to NavBarToolBar

It works from plugin XML, like:

<actions>
<group id="org.jetbrains.tutorials.actions.ExampleCustomDefaultActionGroup"
text="Example Custom DefaultActionGroup" description="Custom DefaultActionGroup Demo">
<add-to-group group-id="NavBarToolBar" anchor="first"/>
<action class="com.github.clouddevdesktopplugin.actions.DeployAnAction"
id="com.gtihub.clouddevdesktopplugin.actions.DeployAnAction"/>
<action class="com.github.clouddevdesktopplugin.actions.RestartAnAction"
id="com.github.clouddevdesktopplugin.actions.RestartAnAction"/>
<action class="com.github.clouddevdesktopplugin.actions.BuildAnAction"
id="com.github.clouddevdesktopplugin.actions.BuildAnAction"/>
</group>
</actions>

But doesn't work from Project Component like:

DefaultActionGroup windowM = (DefaultActionGroup) actionManager.getAction("NavBarToolBar");

DefaultActionGroup defaultActionGroup = new DefaultActionGroup("Opa", Arrays.asList(deployAnAction, restartAnAction, buildAnAction));
windowM.add(defaultActionGroup);

just no actions in toolbar, at the same time if I use NavBarToolBarOthers I can see actions in slightly different place. 

How to do that?

 

0
4 comments
Avatar
Permanently deleted user

Thx for link, I tried that, and it even works for other toolbars, however I'm trying to add a actions, to plane on screen:

I can do that when I define actions in XML, but not from code. Why?

0

Could you please post link to full plugin sources? Anyway it is preferrable to use XML registration for Actions if possible to avoid unnecessary class-loading.

0
Avatar
Permanently deleted user

  I use this to add buttons to the toolbars with LivePlugin, since I can't use a plugin.xml then

// Add actions to this action group using the regular IntelliJ mechanisms and they will show up in the toolbar/navbar toolbar
// By default, all actions are shown next to each other
// Set popup to true if you want
DefaultActionGroup actionGroupInMainToolbarAndNavBar(String uniqueId, String description = "", boolean popup = false) {
DefaultActionGroup actionGroup = registerAction(uniqueId, new DefaultActionGroup(description, popup))
// Originally, I tried adding the actions directly to the Main toolbar/navbar toolbar action groups, but this didn't really work, even with this
//ActionToolbarImpl.updateAllToolbarsImmediately()


// Instead, we use the same mechanism in which the user can add buttons/menus to the toolbars
// It's stored in customization.xml and accessible via CustomActionsSchema.getInstance

def instance = CustomActionsSchema.getInstance()
// Without this, customization.xml gets spammed with duplicate entries. I used to have duplicates within the toolbar as well, but can't reproduce that anymore. Maybe older IntelliJ behaved differently?
removeOldEntriesInCustomizationXML([uniqueId])


// position = 100 to add the action at the end
instance.addAction(new ActionUrl(["root", "Main Toolbar"], uniqueId, ActionUrl.ADDED, 100))
// When the toolbar is disabled, some actions show to the right of the navigation bar, let's show those first (position = 0)
instance.addAction(new ActionUrl(["root", "Navigation Bar Toolbar"], uniqueId, ActionUrl.ADDED, 0))

// This will force the toolbars to update and show the new buttons
CustomActionsSchema.setCustomizationSchemaForCurrentProjects()

return actionGroup
}

DefaultActionGroup myActionGroup = actionGroupInMainToolbarAndNavBar("my-action-group", "Action Name (search in Ctrl-Shift-A)", false)
myActionGroup.getTemplatePresentation().setDescription("Description in status bar")


 

These are all the imports, just remove those you don't need/are invalid

 

import PopupMenu
import com.intellij.execution.Executor
import com.intellij.execution.configurations.JavaParameters
import com.intellij.execution.configurations.RunProfile
import com.intellij.execution.runners.JavaProgramPatcher
import com.intellij.ide.ui.customization.ActionUrl
import com.intellij.ide.ui.customization.CustomActionsSchema
import com.intellij.openapi.actionSystem.*
import com.intellij.openapi.actionSystem.AnAction
import com.intellij.openapi.actionSystem.AnActionEvent
import com.intellij.openapi.actionSystem.DefaultActionGroup
import com.intellij.openapi.actionSystem.Presentation
import com.intellij.openapi.actionSystem.ex.ComboBoxAction
import com.intellij.openapi.actionSystem.ex.ComboBoxAction.ComboBoxButton
import com.intellij.openapi.actionSystem.impl.ActionToolbarImpl
import com.intellij.openapi.extensions.Extensions
import com.intellij.ui.components.panels.NonOpaquePanel
import com.intellij.util.ui.JBUI
import com.intellij.util.ui.UIUtil
import groovy.transform.Field
import org.jetbrains.annotations.NotNull

import javax.swing.*
import javax.swing.border.Border
import java.awt.*
import java.awt.event.ActionEvent
import java.nio.file.Files
import java.nio.file.Path
import java.nio.file.Paths
import java.util.List
import java.util.function.BiConsumer
import java.util.function.Consumer

import static liveplugin.PluginUtil.*
0

Please sign in to leave a comment.