This Bugzilla instance is a read-only archive of historic NetBeans bug reports. To report a bug in NetBeans please follow the project's instructions for reporting issues.

Bug 238018 - Menu created dynamically shows Empty after a while when MultiComponent used
Summary: Menu created dynamically shows Empty after a while when MultiComponent used
Status: RESOLVED INCOMPLETE
Alias: None
Product: platform
Classification: Unclassified
Component: Actions (show other bugs)
Version: 7.3.1
Hardware: PC Windows 7
: P3 normal (vote)
Assignee: Jan Peska
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2013-11-05 23:31 UTC by tdanard
Modified: 2014-07-14 13:55 UTC (History)
0 users

See Also:
Issue Type: DEFECT
Exception Reporter:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description tdanard 2013-11-05 23:31:13 UTC
In my application, there is a menu (the "Scripts" menu) that is only visible if script files are registered. There is one menu item for each file registered.

Here is the code to create the "Scripts" menu dynamically:

        FileSystem fs = FileUtil.createMemoryFileSystem();
        FileObject fob = null;
        OutputStream fstream = null;
        OutputStreamWriter out = null;
        try {
            fob = fs.getRoot().createData("layer", "xml");
            fstream = fob.getOutputStream();
            out = new OutputStreamWriter(fstream);
            out.write("<filesystem>");
            out.write("    <folder name=\"Menu\">\n"
                    + "        <folder name=\"Scripts\">\n"
                    + "            <attr name=\"position\" intvalue=\"1270\"/>\n"
                    + "            <file name=\"MultiComponent.shadow\">\n"
                    + "                <attr name=\"position\" intvalue=\"100\"/>\n"
                    + "                <attr name=\"originalFile\" stringvalue=\"Actions/Scripts/com-mycompany-core-actions-MultiExecutePythonScriptAction.instance\"/>\n"
                    + "            </file>\n"
                    + "        </folder>\n"
                    + "    </folder>");
            out.write("</filesystem>");

        } catch (IOException ex) {
            logger.log(Level.INFO, "Can't set scripts menu", ex);
            fob = null;
        } finally {
            try {
                if (out != null) {
                    out.close();
                }
            } catch (IOException ex) {
                // Do nothing.
            }
            try {
                if (fstream != null) {
                    fstream.close();
                }
            } catch (IOException ex) {
                // Do nothing.
            }
        }
        if (fob != null) {
            try {
                URL uri = fob.getURL();
                DynamicShowScriptsMenuLayerContent.enable(uri);
                isShown = true;
            } catch (Exception ex) {
                logger.log(Level.INFO, "Can't show scripts menu", ex);
            }
        }
    


public class DynamicShowScriptsMenuLayerContent extends MultiFileSystem {

    private static DynamicShowScriptsMenuLayerContent INSTANCE;
    private static final Logger logger = Logger.getLogger(DynamicShowScriptsMenuLayerContent.class.getName());

    public static void enable(URL url) {
        if (!hasContent()) {
            try {
                XMLFileSystem fileSystem;
                fileSystem = new XMLFileSystem(url);
                INSTANCE.setDelegates(fileSystem);
            } catch (SAXException ex) {
                logger.log(Level.WARNING, "Could not load show scripts menu", ex);
            }
        }
    }

    public DynamicShowScriptsMenuLayerContent() { // called because of declaration in META-INF/services
        // will be created on startup, exactly once
        INSTANCE = this;
        setPropagateMasks(true); // permit *_hidden masks to be used
    }

    public static boolean hasContent() {
        return INSTANCE.getDelegates().length > 0;
    }

    public static void disable() {
        INSTANCE.setDelegates();
    }


The "com.mycompany.core.actions.MultiExecutePythonScriptAction" class extends AbstractAction and implements DynamicMenuContent. It works fine the first few times the "Scripts" menu is open: registered script files are shown as menu items.

However, after a while, it stops showing any valid menu item. The word "Empty" is shown instead. The "DynamicMenuContent.getMenuPresenters" method is not even called. 

It looks like NetBeans realizes after a while that the Scripts menu is not a static menu, and forgets the dynamic registration.

It's important for the Scripts menu to be not visible if users don't have script files registered.

Is it a bug or am I registering my dynamic menu incorrectly ?
Comment 1 Jan Peska 2014-07-14 13:55:44 UTC
Can you please attach whole testcase netbeans module and reopen the bug, it is not clear to me what you are trying to do.