Search code examples
xmlxsdxsd-validationxml-validation

How to avoid to remove the root element of a XSD file


I'm trying to grey out the remove option of the root or parent element of a xsd file. It's working in its derived nodes but not in the root one.

This is working:

<xs:attribute name="user-login" type="xs:string" use="required"/>

This is not working in the parent element:

<xs:element name="MyProgram-Deploy" use="required">

XML GUI showing black, not grey, Remove command


Solution

  • Finally, what I did was to create a Java class inheriting from XMLNodeActionManager which is called from a inherit class of IMenuListener class. This IMenuListener is called every time that you show up the context up, so you have to override the method createContextMenu() of a inherit class of XMLTableTreeViewer to use the previous listener. In the XMLNodeActionManager class, I go over all options of the context menu and I remove the one that I don't want:

    XMLTableTreeViewer.createContextMenu -> IMenuListener -> XmlNodeActionManager.contributeActions

    MyTestIMenuListener:

        class MyTestNodeActionMenuListener implements IMenuListener {
        @Override
        public void menuAboutToShow(org.eclipse.jface.action.IMenuManager manager) {
            SilecsXmlNodeActionManager nodeActionManager = new MyTestXmlNodeActionManager(
                    ((IDOMDocument) getInput()).getModel(), MyTestXMLTableTreeViewer.this);
            nodeActionManager.fillContextMenu(manager, getSelection());          
        }
    }
    

    MyTestXMLTableTreeViewer extend XMLTableTreeViewer:

    @Override
    protected void createContextMenu() {
        MenuManager contextMenu = new MenuManager("#PopUp"); //$NON-NLS-1$
        contextMenu.add(new Separator("additions")); //$NON-NLS-1$
        contextMenu.setRemoveAllWhenShown(true);
        contextMenu.addMenuListener(new MyTestNodeActionMenuListener());
        Menu menu = contextMenu.createContextMenu(getControl());
        getControl().setMenu(menu);
    }
    

    MyTestXmlNodeActionManager extend XmlNodeActionManager:

    @Override
    public void contributeActions(IMenuManager menu, List selection) {
        super.contributeActions(menu, selection);
    
        Node node = (Node) selection.iterator().next();
    
        IContributionItem removeMenuItem = null;
        try {
            IContributionItem[] menuItems = menu.getItems();
            for (IContributionItem menuItem: menuItems) {
                if (menuItem instanceof ActionContributionItem) {
                    if (((ActionContributionItem) menuItem).getAction() instanceof AbstractNodeActionManager.DeleteAction) {
                        removeMenuItem = menu.remove(menuItem);
                        break;
                    }
                }
            }
        } catch (Exception e) {
    
        }
    
    }