Remember To Buy Dinner On The Way Home

The best thing about giving NetBeans Platform Certified Trainings is the cool challenges that students leave you with. In the process, you learn about actual business needs and you teach yourself how to implement them on the NetBeans Platform.

A great example of this during the Johannesburg training (which is now complete, after 5 days, i.e., 3 days basic training, followed by 2 day advanced course) was posed by Marcel Auret and Kobus Botha from Saab. During the presentation about the NetBeans window system, they said they need a window that:

  • is always on top
  • opens undocked
  • is modal
  • cannot be closed

Why would such a window be needed? For warning messages, or notifications, as shown below:

So, maybe a JDialog could be used instead, you might wonder. However, an additional requirement was that the window should be dockable. I.e., integration with the NetBeans window system is needed.

So, here is the new mode I created:

<?xml version="1.0" encoding="UTF-8"?>
<mode version="2.3">
  <name unique="warning" />
  <kind type="view" />
  <state type="separated" />
  <constraints>
    <path orientation="horizontal" number="100" weight="0.5"/>
  </constraints>
  <bounds x="713" y="150" width="467" height="217" />
  <frame state="0"/>
  <empty-behavior permanent="true"/>
</mode>

Note that "view" above will cause the window to be modal, while "separated" will cause it to be undocked.

Then I created a TopComponent and registered it in the layer in the above new position, which also needs to be registered in the layer.

I created an installer, with this "restored" method, which will be called when the module is installed:

@Override
public void restored() {

   WindowManager.getDefault().invokeWhenUIReady(new Runnable() {

        @Override
        public void run() {

            final TopComponent tc = WindowManager.getDefault().findTopComponent("WarningTopComponent");

            tc.toFront();

            JFrame frame = (JFrame) WindowManager.getDefault().getMainWindow();

            frame.addWindowFocusListener(new WindowFocusListener() {

                @Override
                public void windowGainedFocus(WindowEvent e) {
                    tc.toFront();
                }

                @Override
                public void windowLostFocus(WindowEvent e) {
                    tc.toFront();
                }

            });

        }

    });

}

The constructor of the TopComponent has this in its constructor:

putClientProperty(TopComponent.PROP_CLOSING_DISABLED, Boolean.TRUE);

As well as this:

@Override
public boolean canClose() {
    return false;
}

And now everything works as expected. Right-clicking on the window's tab reveals the "Dock/Undock" menu item which, when invoked, causes the window to be docked when needed. Whatever happens (except when docked, of course), the window will stay on top and will not be closeable (even when the small red button is clicked, which can remain as decoration of the fact that this is a warning window).

Comments:

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
« April 2014
SunMonTueWedThuFriSat
  
12
13
14
17
18
19
20
21
22
23
24
25
26
27
28
29
30
   
       
Today