Dynamically Changing the Display Names of Menus and Popups

Very interesting thing and handy to know when needed is the fact that "menuText" and "popupText" (from org.openide.awt.ActionRegistration) can be changed dynamically, via "putValue" as shown below for "popupText". The Action class, in this case, needs to be eager, hence you won't receive the object of interest via the constructor, but you can easily use the global Lookup for that purpose instead, as also shown below.

import java.awt.event.ActionEvent;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Collection;
import javax.swing.AbstractAction;
import org.netbeans.api.project.Project;
import org.netbeans.api.project.ProjectUtils;
import org.openide.awt.ActionID;
import org.openide.awt.ActionReference;
import org.openide.awt.ActionRegistration;
import org.openide.awt.StatusDisplayer;
import org.openide.util.Lookup;
import org.openide.util.LookupEvent;
import org.openide.util.LookupListener;
import org.openide.util.Utilities;
import org.openide.util.WeakListeners;

@ActionID(
        category = "Demo",
        id = "org.ptt.TrackProjectSelectionAction")
@ActionRegistration(
        lazy = false,
        displayName = "NOT-USED")
@ActionReference(
        path = "Projects/Actions",
        position = 0)
public final class TrackProjectSelectionAction extends AbstractAction 
    implements LookupListener {

    private Lookup.Result<Project> projects;
    private Project context;

    public TrackProjectSelectionAction() {
        projects = Utilities.actionsGlobalContext().lookupResult(Project.class);
        projects.addLookupListener(
                WeakListeners.create(LookupListener.class, this, projects));
        resultChanged(new LookupEvent(projects));
    }

    @Override
    public void resultChanged(LookupEvent le) {
        Collection<? extends Project> p = projects.allInstances();
        if (p.size() == 1) {
            Project currentProject = p.iterator().next();
            String currentProjectName = 
                    ProjectUtils.getInformation(currentProject).getDisplayName();
            putValue("popupText", "Current Project: " + currentProjectName);
            StatusDisplayer.getDefault().setStatusText(
                    "Current Project: " + currentProjectName);
            context = currentProject;
        }
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        refresh();
    }

    protected void refresh() {
        DateFormat formatter = new SimpleDateFormat("HH:mm:ss");
        String formatted = formatter.format(System.currentTimeMillis());
        putValue("popupText", "Last Click At: " + formatted + 
                " (" + ProjectUtils.getInformation(context) + ")");
    }
    
}

Comments:

Hi; Seems great ;)
What about hiding/displaying dynamically the menu Items ? for e.g hide or display some menu items based on the user profile (user, admin) by code at start up.
Regards

Posted by Djamel TORCHE on March 05, 2013 at 01:53 AM PST #

https://blogs.oracle.com/geertjan/entry/injecting_a_layer_file_into

Posted by Geertjan on March 05, 2013 at 02:25 AM PST #

Thank you so much, it's exactly what I'm looking for.

Posted by Djamel TORCHE on March 17, 2013 at 01:17 AM PDT #

Post a Comment:
  • HTML Syntax: NOT allowed
About

Geertjan Wielenga (@geertjanw) is a Principal Product Manager in the Oracle Developer Tools group living & working in Amsterdam. He is a Java technology enthusiast, evangelist, trainer, speaker, and writer. He blogs here daily.

The focus of this blog is mostly on NetBeans (a development tool primarily for Java programmers), with an occasional reference to NetBeans, and sometimes diverging to topics relating to NetBeans. And then there are days when NetBeans is mentioned, just for a change.

Search

Archives
« July 2014
SunMonTueWedThuFriSat
  
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
  
       
Today