Monday Dec 17, 2012

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) + ")");
    }
    
}

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
« December 2012 »
SunMonTueWedThuFriSat
      
1
15
23
24
25
29
     
Today