Need non-global toolbar to use global action (NB 6.7)

View: New views
4 Messages — Rating Filter:   Alert me  

Need non-global toolbar to use global action (NB 6.7)

by NB-n00b :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

It seems that this should be easy to do, but I haven't been able to figure it out...



I have a TopComponent window in the Explorer position (but the window isn't an explorer -- I just want it in that position).



I want this window to have a toolbar, and a few other components, so I added the toolbar in the GUI designer.  I can easily add buttons to it, and they work.



The problem: I have created an action which is in the global File menu which I want to add to this toolbar.



I can't figure how how to get the instance of the Action object in order to add it to the toolbar.  I know one must have been created, because the action appears in the global File menu, and it does work.



I can find lots of documentation about making this work with a global toolbar, but it doesn't seem to apply to a "local" toolbar.



Thanks for your help!





Re: Need non-global toolbar to use global action (NB 6.7)

by gbivins :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

I asked a similar question a couple of weeks back.
http://forums.netbeans.org/viewtopic.php?t=18592&highlight=

Sounds like you could do something similar.
Gerrick
On Oct 24, 2009, at 2:18 PM, NB-n00b wrote:

> It seems that this should be easy to do, but I haven't been able to  
> figure it out...
>
>
>
> I have a TopComponent window in the Explorer position (but the  
> window isn't an explorer -- I just want it in that position).
>
>
>
> I want this window to have a toolbar, and a few other components, so  
> I added the toolbar in the GUI designer.  I can easily add buttons  
> to it, and they work.
>
>
>
> The problem: I have created an action which is in the global File  
> menu which I want to add to this toolbar.
>
>
>
> I can't figure how how to get the instance of the Action object in  
> order to add it to the toolbar.  I know one must have been created,  
> because the action appears in the global File menu, and it does work.
>
>
>
> I can find lots of documentation about making this work with a  
> global toolbar, but it doesn't seem to apply to a "local" toolbar.
>
>
>
> Thanks for your help!
>
>
>
>


Re: Need non-global toolbar to use global action (NB 6.7)

by Fabrizio Giudici :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

NB-n00b wrote:
>
>
> I can't figure how how to get the instance of the Action object in order to add it to the toolbar.  I know one must have been created, because the action appears in the global File menu, and it does work.
>
>  
I see a number of ways to get it, perhaps the simpler is
Lookups.forPath(path), where path is the path of the global action to
get. Of course you need to know it, but you can find it with the "Open
layer in context" menu in the project explorer. This sounds to me better
than depending on the class name.


PS For such task, I generally advice about using code such as the
attached. It's simpler and allows to define actions in a declarative way
such as:

    <folder name="LocalToolbar">
        <folder name="it.tidalwave.geo.viewer.GeoViewerPresentation">
            <file
name="it-tidalwave-geo-viewer-impl-action-RemoveAllWaypointsAction.shadow">
                <attr name="originalFile"
stringvalue="Actions/Geo/it-tidalwave-geo-viewer-impl-action-RemoveAllWaypointsAction.instance"/>
            </file>
        </folder>
    </folder>



In this way, both your actions and those taken from the platform are
managed in the same way.


package it.tidalwave.netbeans.swing;

import java.util.logging.Logger;
import java.awt.GridLayout;
import javax.swing.Action;
import javax.swing.BorderFactory;
import javax.swing.JPanel;
import javax.swing.JSeparator;
import org.openide.util.actions.SystemAction;
import org.openide.util.lookup.Lookups;

public class LocalToolbar extends JPanel // or JToolBar, whatever
  {
    private final static String CLASS = LocalToolbar.class.getName();
   
    private final static Logger logger = Logger.getLogger(CLASS);
   
    class Button extends javax.swing.JButton
      {
        private static final int DEFAULT_TOOLBAR_BUTTON_SIZE = 16;

        private Action action;

        public Button (SystemAction action)
          {
            this.action = action;
            setBorder(BorderFactory.createEmptyBorder(2, 2, 2, 2));
            setOpaque(false);
            //
            // Needed for testing. Inherited getAction() returns null
(can't change it).
            //
            putClientProperty("test.action", action);
          }
      }

    public LocalToolbar()
      {
        setLayout(new GridLayout(1, 0, 4, 0));
        setOpaque(false);
      }

    public LocalToolbar (final String name)
      {
        this();
        load(name);
      }

    public void load (final String name)
      {
        for (final Object object : Lookups.forPath("LocalToolbar/" +
name).lookupAll(Object.class))
          {
            if (object instanceof SystemAction)
              {
                add(new Button((SystemAction)object));
              }
            else if (object instanceof JSeparator)
              {
                add((JSeparator)object);
              }
          }
      }
   
    public void addAction (final Class actionClass)
      {
        final SystemAction action = SystemAction.get(actionClass);
        add(new Button(action));
      }
  }


--
Fabrizio Giudici - Java Architect, Project Manager
Tidalwave s.a.s. - "We make Java work. Everywhere."
weblogs.java.net/blog/fabriziogiudici - www.tidalwave.it/people
Fabrizio.Giudici@...


Re: Need non-global toolbar to use global action (NB 6.7)

by Geertjan Wielenga :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message


Great questions and thanks Fabrizio for the answer, I provided some more
here today:

http://blogs.sun.com/geertjan/entry/calling_an_action_in_the

Gj

Fabrizio Giudici wrote:

> NB-n00b wrote:
>>
>>
>> I can't figure how how to get the instance of the Action object in
>> order to add it to the toolbar.  I know one must have been created,
>> because the action appears in the global File menu, and it does work.
>>
>>  
> I see a number of ways to get it, perhaps the simpler is
> Lookups.forPath(path), where path is the path of the global action to
> get. Of course you need to know it, but you can find it with the "Open
> layer in context" menu in the project explorer. This sounds to me
> better than depending on the class name.
>
>
> PS For such task, I generally advice about using code such as the
> attached. It's simpler and allows to define actions in a declarative
> way such as:
>
>    <folder name="LocalToolbar">
>        <folder name="it.tidalwave.geo.viewer.GeoViewerPresentation">
>            <file
> name="it-tidalwave-geo-viewer-impl-action-RemoveAllWaypointsAction.shadow">
>
>                <attr name="originalFile"
> stringvalue="Actions/Geo/it-tidalwave-geo-viewer-impl-action-RemoveAllWaypointsAction.instance"/>
>
>            </file>
>        </folder>
>    </folder>
>
>
>
> In this way, both your actions and those taken from the platform are
> managed in the same way.
>
>
> package it.tidalwave.netbeans.swing;
>
> import java.util.logging.Logger;
> import java.awt.GridLayout;
> import javax.swing.Action;
> import javax.swing.BorderFactory;
> import javax.swing.JPanel;
> import javax.swing.JSeparator;
> import org.openide.util.actions.SystemAction;
> import org.openide.util.lookup.Lookups;
>
> public class LocalToolbar extends JPanel // or JToolBar, whatever
>  {
>    private final static String CLASS = LocalToolbar.class.getName();
>      private final static Logger logger = Logger.getLogger(CLASS);
>      class Button extends javax.swing.JButton
>      {
>        private static final int DEFAULT_TOOLBAR_BUTTON_SIZE = 16;
>
>        private Action action;
>
>        public Button (SystemAction action)
>          {
>            this.action = action;
>            setBorder(BorderFactory.createEmptyBorder(2, 2, 2, 2));
>            setOpaque(false);
>            //
>            // Needed for testing. Inherited getAction() returns null
> (can't change it).
>            //
>            putClientProperty("test.action", action);
>          }
>      }
>
>    public LocalToolbar()
>      {
>        setLayout(new GridLayout(1, 0, 4, 0));
>        setOpaque(false);
>      }
>
>    public LocalToolbar (final String name)
>      {
>        this();
>        load(name);
>      }
>
>    public void load (final String name)
>      {
>        for (final Object object : Lookups.forPath("LocalToolbar/" +
> name).lookupAll(Object.class))
>          {
>            if (object instanceof SystemAction)
>              {
>                add(new Button((SystemAction)object));
>              }
>            else if (object instanceof JSeparator)
>              {
>                add((JSeparator)object);
>              }
>          }
>      }
>      public void addAction (final Class actionClass)
>      {
>        final SystemAction action = SystemAction.get(actionClass);
>        add(new Button(action));
>      }
>  }
>
>