Conditionnal opening of a TopComponent

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|

Conditionnal opening of a TopComponent

GBeans
Hi,

I'm new on netbeans RCP. I created my custom TopComponents with some annotations as follow :

Code:

@ConvertAsProperties(
                dtd = "-//project.editor.hmi//EditorShape//EN",
                autostore = false
)
@TopComponent.Description(
                preferredID = "EditorShapeTopComponent",
                iconBase = "project/editor/shape_24.png",
                persistenceType = TopComponent.PERSISTENCE_ALWAYS
)
//@TopComponent.Registration(mode = "leftSlidingSide", openAtStartup = true)
@ActionID(category = "Window", id = "project.editor.hmi.EditorShapeTopComponent")
//@ActionReference(path = "Menu/Window" /*, position = 333 */)
@TopComponent.OpenActionRegistration(
                displayName = "#CTL_EditorShapeAction",
                preferredID = "EditorShapeTopComponent"
)
@Messages({
        "CTL_EditorShapeAction=Formes",
        "CTL_EditorShapeTopComponent=Formes",
        "HINT_EditorShapeTopComponent=Formes simples importables"
})
public final class EditorShapeTopComponent extends TopComponent {
...
}




I have a configuration file (properties file) in which a parameter tells if the TopComponent must be activated or not.

To manage that, I do that :


Code:

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

                        @Override
                        public void run() {
                                // Hack to force the current Project selection when the application starts up
                                TopComponent tc = WindowManager.getDefault().findTopComponent(PROJECT_LOGICAL_TAB_ID);
                                if (tc != null) {
                                        tc.requestActive();
                                }

                                SwingUtilities.invokeLater(new Runnable() {

                        @Override
                        public void run() {
                                 // Reads the configuration file.
                                Configuration configuration = CentralLookup.getDefault().lookup(Configuration.class);
                                if (configuration.getBoolean("withShapes", false) == true) {
                                        EditorShapeTopComponent tcPalette = new EditorShapeTopComponent();
                                        Mode leftSideMode = WindowManager.getDefault().findMode("leftSlidingSide");
                                        leftSideMode.dockInto(tcPalette);

                                        FileObject menuFolder = FileUtil.getConfigFile("Menu/Window");
                                        try {
                                                registerAction(tcPalette.getClass(), "Images", new ImageIcon(getClass().getResource("/project/editor/img_24.png")), menuFolder, 333);
                                        } catch (IOException ex) {
                                                Exceptions.printStackTrace(ex);
                                        }
                                        tcPalette.open();
                                }
                        }
                });
                        }
                });
        }




With this solution, it's OK the first time I open the application. But when I reopen it, the TopComponent opens twice (one time because I create it, the second time because it was still open when I closed the application and it has been stored by the persistence manager in cache).

An other problem with this solution is that the directory "userdir\config\Windows2Local\Modes\leftSlidingSide" seems to grow again and again and is never cleaned.

What I would like is :
 - to conditionnally open the TopComponent with the parameter of the properties file.
 - If it is set to true, the component opens, if I set to false, the component does not open.
 - If the component is in the cache, I don't reopen it (i take the one from cache).

Thanks,

Bastien




Reply | Threaded
Open this post in threaded view
|

Re: Conditionnal opening of a TopComponent

geertjan.wielenga@oracle.com
You’re encouraged to move to the Apache NetBeans mailing lists as soon as possible, ask your questions and discuss any concerns, etc, there and to not write to the @netbeans.org mailing lists anymore.

https://cwiki.apache.org/confluence/display/NETBEANS/How+to+Participate

https://jaxenter.com/netbeans/january-1st-2018-end-netbeans-org-mailing-lists


Geertjan Wielenga | Principal Product Manager
Phone: +31620320056 | 
Oracle Developer Tools

ORACLE Netherlands | Hertogswetering 163-167 | 3543 AS Utrecht | Netherlands


Oracle is committed to developing practices and products that help protect the environment

On 1 Dec 2017, at 09:37, GBeans <[hidden email]> wrote:

Hi,

I'm new on netbeans RCP. I created my custom TopComponents with some annotations as follow :

Code:

@ConvertAsProperties(
dtd = "-//project.editor.hmi//EditorShape//EN",
autostore = false
)
@TopComponent.Description(
preferredID = "EditorShapeTopComponent",
iconBase = "project/editor/shape_24.png",
persistenceType = TopComponent.PERSISTENCE_ALWAYS
)
//@TopComponent.Registration(mode = "leftSlidingSide", openAtStartup = true)
@ActionID(category = "Window", id = "project.editor.hmi.EditorShapeTopComponent")
//@ActionReference(path = "Menu/Window" /*, position = 333 */)
@TopComponent.OpenActionRegistration(
displayName = "#CTL_EditorShapeAction",
preferredID = "EditorShapeTopComponent"
)
@Messages({
"CTL_EditorShapeAction=Formes",
"CTL_EditorShapeTopComponent=Formes",
"HINT_EditorShapeTopComponent=Formes simples importables"
})
public final class EditorShapeTopComponent extends TopComponent {
...
}




I have a configuration file (properties file) in which a parameter tells if the TopComponent must be activated or not.

To manage that, I do that :


Code:

public GlobalActionContextProxy() {
...

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

@Override
public void run() {
// Hack to force the current Project selection when the application starts up
TopComponent tc = WindowManager.getDefault().findTopComponent(PROJECT_LOGICAL_TAB_ID);
if (tc != null) {
tc.requestActive();
}

SwingUtilities.invokeLater(new Runnable() {

@Override
public void run() {
                                // Reads the configuration file.
Configuration configuration = CentralLookup.getDefault().lookup(Configuration.class);
if (configuration.getBoolean("withShapes", false) == true) {
EditorShapeTopComponent tcPalette = new EditorShapeTopComponent();
Mode leftSideMode = WindowManager.getDefault().findMode("leftSlidingSide");
                                       leftSideMode.dockInto(tcPalette);

FileObject menuFolder = FileUtil.getConfigFile("Menu/Window");
try {
registerAction(tcPalette.getClass(), "Images", new ImageIcon(getClass().getResource("/project/editor/img_24.png")), menuFolder, 333);
} catch (IOException ex) {
Exceptions.printStackTrace(ex);
}
tcPalette.open();
}
}
});
}
});
}




With this solution, it's OK the first time I open the application. But when I reopen it, the TopComponent opens twice (one time because I create it, the second time because it was still open when I closed the application and it has been stored by the persistence manager in cache).

An other problem with this solution is that the directory "userdir\config\Windows2Local\Modes\leftSlidingSide" seems to grow again and again and is never cleaned.

What I would like is :
- to conditionnally open the TopComponent with the parameter of the properties file. 
- If it is set to true, the component opens, if I set to false, the component does not open. 
- If the component is in the cache, I don't reopen it (i take the one from cache).

Thanks,

Bastien