How would you greet the current Person?

How would you greet the current Person in this scenario?


As you can see, the "PersonInput" window is where you can type a greeting. The greeting should be displayed, when "Greet!" is clicked, in the currently selected window in the "editor" mode, which in the above case would be "Jack", while "Mary" and "Jane" should be empty, since the greeting is not intended for them. The three windows shown in the editor mode are all instances of the "PersonEditorTopComponent".

In a real scenario, you could imagine the "PersonInput" window to be a piano or guitar, with the "PersonEditor" windows being instances of a music score window. I.e., these two windows are independent of each other, potentially coming from different modules altogether. Hence, this approach in the JButton of the "PersonInput" window is not correct:

PersonEditorTopComponent ptc = (PersonEditorTopComponent) WindowManager.getDefault().findTopComponent("PersonEditorTopComponent");
ptc.setGreeting(greetingText.getText());

And this is also incorrect:

Mode editorMode = WindowManager.getDefault().findMode("editor");
PersonEditorTopComponent ptc = (PersonEditorTopComponent) editorMode.getSelectedTopComponent();
ptc.setGreeting(greetingText.getText());

Somehow, Lookup is the correct answer, but how to implement it in this scenario?

Firstly, in the "greetButton", add the String, or even better, a business object constructed from the String, to the Lookup via new AbstractLookup(instanceContent) in associateLookup:

private void greetButtonActionPerformed(java.awt.event.ActionEvent evt) {
    instanceContent.add(greetingText.getText());
}

OK, so now we have our String available. In the "PersonEditorTopComponent", we listen for the String object in the global Lookup and then:

@Override
public void resultChanged(LookupEvent le) {
    if (!greetingResult.allInstances().isEmpty()) {
        String greeting = greetingResult.allInstances().iterator().next();
        Mode editorMode = WindowManager.getDefault().findMode("editor");
        if (editorMode.getSelectedTopComponent().equals(this)) {
            greetingLabel.setText(greeting);
            PersonUtilities.ic.remove(greeting);
        }
    }
}
 

Above, I get the selected TopComponent from the "editor" mode. Then I set the label, after which the String is immediately removed from the InstanceContent. That InstanceContent is the same InstanceContent that was used in the "PersonInput" window for adding the String. The above approach means that the currently shown window is the one where the greeting is put and none of the other windows, i.e., those that are not currently shown, are changed.

It's not a perfect solution. Does anyone have a better one?


Update. I got a few interesting suggestions from Toni and Sven in Chieftains pub, San Francisco, this evening. The one that is simplest and works best is by Toni:

@Override
public void componentShowing() {
    greetingResult = Utilities.actionsGlobalContext().lookupResult(String.class);
    greetingResult.addLookupListener(this);
}

@Override
public void componentHidden() {
    greetingResult.removeLookupListener(this);
}

@Override
public void resultChanged(LookupEvent le) {
    if (!greetingResult.allInstances().isEmpty()) {
        String greeting = greetingResult.allInstances().iterator().next();
        greetingLabel.setText(greeting);
        PersonUtilities.ic.remove(greeting);
    }
}

Via componentShowing/Hidden, we end up with the only TopComponent that is visible. Even when multiple TopComponents are undocked, these are all "editor" TopComponents, so if undocked, they'll disappear behind the frame of the application and the remaining shown TopComponent will be used, i.e., that one will be docked. The final statement above ensures that as soon as a greeting is used, it is removed, so that other TopComponents, which may be shown after the currently shown TopComponent, will not pick up the same greeting automatically.

Another approach, suggested by Sven, is to create a JList in the "PersonInput" window, where open TopComponents are listed. Then, depending on which TopComponent is selected, the greeting is made available to that specific TopComponent. Not completely sure how to do the latter part of that. Yet another approach would be to somehow, via a PropertyChangeListener on the TopComponent Registry, detect the last active TopComponent, which would be the TopComponent that the user would like to work with.

Comments:

One of the most useful things about netbeans is the palette.
I'm creating an application that would benefit from the use of the palette design. Is there a tutorial or guide or sample code of how to implement the netbeans palette into my app?

I'd like a palette item that drops a palette. :)

Posted by Jeff Groom on October 07, 2011 at 01:02 AM PDT #

http://bits.netbeans.org/dev/javadoc/org-netbeans-spi-palette/overview-summary.html

Posted by Geertjan on October 07, 2011 at 03:00 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
« March 2015
SunMonTueWedThuFriSat
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
    
       
Today