Conditionnal opening of a TopComponent

"GBeans" <[email protected]> Mon, 04 Dec 2017 09:03:53 +0000
Newsgroups gmane.comp.java.netbeans.modules.openide.devel
Message-ID <[email protected]>
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