Search in sources :

Example 1 with HandledContributionItem

use of org.eclipse.e4.ui.workbench.renderers.swt.HandledContributionItem in project core by jcryptool.

the class FileExplorerView method init.

@Override
public void init(IViewSite site, IMemento memento) throws PartInitException {
    super.init(site, memento);
    if (memento != null && memento.getBoolean(MEMENTOKEY_INVISIBLE_FILES_HIDDEN) != null) {
        boolean invis_files_hidden = memento.getBoolean(MEMENTOKEY_INVISIBLE_FILES_HIDDEN);
        this.setHideInvisible(invis_files_hidden);
    } else {
        setHideInvisible(getDefaultForHideInvisibleFiles());
    }
    getViewSite().getActionBars().getMenuManager().addMenuListener(new IMenuListener() {

        public void menuAboutToShow(IMenuManager manager) {
            if (!menuButtonInitialized) {
                try {
                    IContributionItem[] items = getViewSite().getActionBars().getMenuManager().getItems();
                    for (IContributionItem item : items) {
                        if ("org.jcryptool.fileexplorer.invisibleToggle".equals(item.getId())) {
                            if (item instanceof CommandContributionItem) {
                                CommandContributionItem invisibleToggle = (CommandContributionItem) item;
                                State state = invisibleToggle.getCommand().getCommand().getState(RegistryToggleState.STATE_ID);
                                if (state == null)
                                    throw new UnsupportedOperationException(// $NON-NLS-1$
                                    "The 'show invisible files' command does not have a toggle state");
                                else if (!(state.getValue() instanceof Boolean))
                                    throw new UnsupportedOperationException(// $NON-NLS-1$
                                    "The 'show invisible files' command's toggle state doesn't contain a boolean value");
                                else {
                                    state.setValue(!isHideInvisible());
                                }
                            } else if (item instanceof HandledContributionItem) {
                                HandledContributionItem invisibleToggle = (HandledContributionItem) item;
                                // The MCommand's element ID seems to be the command ID
                                // Knowing the command ID, we can retrieve the Command (not
                                // MCommand!)
                                // from the ECommandService
                                String commandId = invisibleToggle.getModel().getCommand().getElementId();
                                ECommandService commandService = (ECommandService) PlatformUI.getWorkbench().getService(ECommandService.class);
                                State state = commandService.getCommand(commandId).getState(RegistryToggleState.STATE_ID);
                                if (state == null)
                                    throw new UnsupportedOperationException(// $NON-NLS-1$
                                    "The 'show invisible files' command does not have a toggle state");
                                else if (!(state.getValue() instanceof Boolean))
                                    throw new UnsupportedOperationException(// $NON-NLS-1$
                                    "The 'show invisible files' command's toggle state doesn't contain a boolean value");
                                else {
                                    state.setValue(!isHideInvisible());
                                }
                            }
                        }
                    }
                    menuButtonInitialized = true;
                } catch (UnsupportedOperationException e) {
                    LogUtil.logError(e.getMessage());
                }
            }
        }
    });
}
Also used : IContributionItem(org.eclipse.jface.action.IContributionItem) State(org.eclipse.core.commands.State) RegistryToggleState(org.eclipse.ui.handlers.RegistryToggleState) IMenuManager(org.eclipse.jface.action.IMenuManager) HandledContributionItem(org.eclipse.e4.ui.workbench.renderers.swt.HandledContributionItem) CommandContributionItem(org.eclipse.ui.menus.CommandContributionItem) IMenuListener(org.eclipse.jface.action.IMenuListener) ECommandService(org.eclipse.e4.core.commands.ECommandService)

Aggregations

State (org.eclipse.core.commands.State)1 ECommandService (org.eclipse.e4.core.commands.ECommandService)1 HandledContributionItem (org.eclipse.e4.ui.workbench.renderers.swt.HandledContributionItem)1 IContributionItem (org.eclipse.jface.action.IContributionItem)1 IMenuListener (org.eclipse.jface.action.IMenuListener)1 IMenuManager (org.eclipse.jface.action.IMenuManager)1 RegistryToggleState (org.eclipse.ui.handlers.RegistryToggleState)1 CommandContributionItem (org.eclipse.ui.menus.CommandContributionItem)1