One thing I would do differently is use the layer.xml file rather than doing it like this:
CentralLookup.getDefault().lookup(Configuration.class); I.e., if you use FileUtile.getConfigFile, you'll be able to get a folder in the virtual file system: http://bits.netbeans.org/dev/javadoc/org-openide-filesystems/org/openide/ filesystems/FileUtil.html#getConfigFile-java.lang.String- And then you can enable/disable something there. But, better than discussing this via e-mail back and forth would be for you to put a small app together with the code from your e-mail in a repo on GitHub so that others can take a look at it and suggest alternative approaches. You should also think about whether it is good practice in terms of usability to have a window that appears and disappears automatically -- normally that's not something that you'd want to do programmatically. Gj On Mon, Jan 8, 2018 at 7:13 PM, Geertjan Wielenga < geertjan.wiele...@googlemail.com> wrote: > One thing I would do differently is use the layer.xml file rather than > doing it like this: > > CentralLookup.getDefault().lookup(Configuration.class); > > I.e., if you use FileUtile.getConfigFile, you'll be able to get a folder > in the virtual file system: > > http://bits.netbeans.org/dev/javadoc/org-openide-filesystems/org/openide/ > filesystems/FileUtil.html#getConfigFile-java.lang.String- > > And then you can enable/disable something there. > > But, better than discussing this via e-mail back and forth would be for > you to put a small app together with the code from your e-mail in a repo on > GitHub so that others can take a look at it and suggest alternative > approaches. > > You should also think about whether it is good practice in terms of > usability to have a window that appears and disappears automatically -- > normally that's not something that you'd want to do programmatically. > > Gj > > On Mon, Jan 8, 2018 at 5:30 PM, GRYSPEERDT Bastien [CLEMESSY] < > bastien.gryspee...@eiffage.com> wrote: > >> Hi, >> >> >> >> I'm new on netbeans RCP. I created my custom TopComponents with some >> annotations as follow : >> >> >> >> @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.EditorShap >> eTopComponent") >> >> //@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 the visibility of the TopComponent, I do that : >> >> 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); >> >> >> >> // Here we check >> that the parameter is the configuration file is to true. >> >> >> if (configuration.getBoolean("withImages", false) == true) { >> >> >> // Adds to Menu. >> >> >> FileObject menuFolder = FileUtil.getConfigFile("Menu/Window"); >> >> >> try { >> >> >> >> registerAction(SycloneEditorImagePaletteTopComponent.class, >> Bundle.LBL_SycloneEditorImagePaletteTopComponent(), new >> ImageIcon(getClass().getResource("/fr/clemessy/ast/syclone/ >> project/synoptic/editor/img_24.png")), menuFolder, 333); >> >> >> } catch (IOException ex) { >> >> >> Exceptions.printStackTrace(ex); >> >> >> } >> >> >> >> >> // Adds to left dock. >> >> >> if >> (!isTopComponentInUserDirCache(SycloneEditorImagePaletteTopComponent.class)) >> { >> >> >> SycloneEditorImagePaletteTopComponent >> tcPalette = new SycloneEditorImagePaletteTopComponent(); >> >> >> Mode leftSideMode = >> WindowManager.getDefault().findMode("leftSlidingSide"); >> >> >> leftSideMode.dockInto(tcPalette); >> >> >> tcPalette.open(); >> >> >> } >> >> } >> else { >> >> >> // Removes the file from the cache : avoid it to be reopened by the >> persistence manager. >> >> >> removeTopComponentInUserDirCac >> he(SycloneEditorImagePaletteTopComponent.class); >> >> } >> >> } >> >> }); >> >> } >> >> }); >> >> } >> >> >> >> >> >> >> >> The method isTopComponentInUserDirCache is as follow : >> >> /** >> >> * Checks if a component appears in the userdir cache of >> Netbeans. >> >> * >> >> * @param <T> : child class of TopComponent. >> >> * @param classType : class type. >> >> * @return true if the component is in the cache, false >> otherwise. >> >> */ >> >> private <T extends TopComponent> boolean >> isTopComponentInUserDirCache(final Class<T> classType) { >> >> boolean isInCache = false; >> >> >> >> // Gets the directory which contains cache >> on Components. >> >> Path compDirPath = >> getUserDirComponentsSettingPath(); >> >> >> >> // It the directory exists, checks if the >> component has already been opened. >> >> File compDirFile = compDirPath.toFile(); >> >> if (compDirFile.exists()) { >> >> // Gets the files >> referencing the component. >> >> File[] compFiles = >> compDirFile.listFiles(new FilenameFilter() { >> >> @Override >> >> public >> boolean accept(File dir, String name) { >> >> >> StringBuilder filenameMatcher = new StringBuilder("^"); >> >> >> filenameMatcher.append(classType.getSimpleName()); >> >> >> filenameMatcher.append("(_\\d*)?"); >> >> >> filenameMatcher.append(".settings$"); >> >> >> return name != null && name.matches(filenameMatcher.toString()); >> >> } >> >> }); >> >> >> >> // If there is at least 1 >> file, the component has already been opened. >> >> if (compFiles != null && >> compFiles.length > 0) { >> >> isInCache >> = true; >> >> } >> >> } >> >> return isInCache; >> >> } >> >> >> >> With this solution, it seems to work but it is not very clean. And when I >> change the language, the menu item (linked to the TopComponent), which is >> read from the cache, is not translated. >> >> >> >> How could I do it in a cleanest way ? >> >> >> >> The only reason I don’t use annotations on the TopComponent is because I >> need to tell “open the TopComponent only if it is defined in the >> configuration file” : is there a way to do it properly ? >> >> >> >> Thanks, >> >> >> >> Bastien >> >> Cet e-mail et ses éventuelles pièces jointes peuvent contenir des >> informations confidentielles et sont exclusivement adressés au(x) >> destinataire(s) mentionné(s) ci-dessus. Toute diffusion, exploitation ou >> copie sans autorisation de cet e-mail et de ses pièces jointes est >> strictement interdite. Si vous recevez ce message par erreur, merci de le >> détruire et d' avertir immédiatement l'expéditeur. EIFFAGE décline toute >> responsabilité si ce message a été modifié ou falsifié. >> This message and any attachments may contain confidential information and >> are established exclusively for his or its recipients. Any use of this >> message, for which it was not intended, any distribution or any total or >> partial publication is prohibited unless previously approved. If you >> receive this message in error, please destroy it and immediately notify the >> sender thereof. The EIFFAGE Group declines all responsibility concerning >> this message if it has been altered or tampered with. >> > >