Project

General

Profile

« Previous | Next » 

Revision d6258880

Added by Patrick Plitzner over 8 years ago

Filter out disabled commands

View differences:

eu.etaxonomy.taxeditor.store/src/main/java/eu/etaxonomy/taxeditor/view/CdmViewerContextMenu.java
43 43
                        if(selection instanceof IStructuredSelection){
44 44
                            Object firstElement = ((IStructuredSelection) selection).getFirstElement();
45 45
                            Map<String, String> availableViewers = CdmViewerUtil.getAvailableViewers(firstElement);
46
                            //if only one viewer/command is available then show only this one
47
                            if(availableViewers.size()==1){
48
                                //else show a list of possible viewers/commands
49
                                Entry<String, String> entry = availableViewers.entrySet().iterator().next();
50
                                final String commandId = entry.getKey();
46
                            Map<Command, String> enabledCommands = getEnabledCommands(availableViewers);
47

  
48
                            //check if only one or multiple viewers/commands are available
49
                            if(enabledCommands.size()==1){
50
                                Entry<Command, String> entry = enabledCommands.entrySet().iterator().next();
51
                                final Command command = entry.getKey();
51 52
                                String viewerName = entry.getValue();
53

  
52 54
                                MenuItem addItem = new MenuItem(menu, SWT.CASCADE);
53 55
                                addItem.setText(String.format("Open (%s)", viewerName));
54
                                addItem.addSelectionListener(new CommandInvoker(commandId, firstElement)) ;
56
                                addItem.addSelectionListener(new CommandInvoker(command, firstElement)) ;
55 57
                            }
56
                            else if (availableViewers.size()>1){
57
                                //else show a list of possible viewers/commands
58
                            else if(enabledCommands.size()>1){
58 59
                                MenuItem addItem = new MenuItem(menu, SWT.CASCADE);
59 60
                                addItem.setText("Open in...");
60 61
                                Menu addMenu = new Menu(menu);
61 62
                                addItem.setMenu(addMenu);
62
                                for(Entry<String, String> entry:availableViewers.entrySet()){
63
                                    final String commandId = entry.getKey();
63
                                for(Entry<Command, String> entry:enabledCommands.entrySet()){
64
                                    final Command command = entry.getKey();
64 65
                                    String viewerName = entry.getValue();
66

  
65 67
                                    MenuItem menuItem = new MenuItem(addMenu, SWT.NONE);
66 68
                                    menuItem.setText(viewerName);
67
                                    menuItem.addSelectionListener(new CommandInvoker(commandId, firstElement)) ;
69
                                    menuItem.addSelectionListener(new CommandInvoker(command, firstElement)) ;
68 70
                                }
69 71
                            }
70 72
                        }
71 73
                    }
74

  
72 75
                }
73 76
        };
74 77
        return contributionItems;
75 78
    }
76 79

  
80
    private Map<Command, String> getEnabledCommands(Map<String, String> availableViewers) {
81
        Map<Command, String> enabledCommands = new HashMap<Command, String>();
82
        for(Entry<String, String> entry:availableViewers.entrySet()){
83
            final String commandId = entry.getKey();
84
            ICommandService commandService = (ICommandService)PlatformUI.getWorkbench().getService(ICommandService.class);
85
            Command command = commandService.getCommand(commandId);
86
            if(command.isEnabled()){
87
                enabledCommands.put(command, entry.getValue());
88
            }
89
        }
90
        return enabledCommands;
91
    }
92

  
77 93
    private final class CommandInvoker extends SelectionAdapter {
78
        private final String commandId;
94
        private final Command command;
79 95
        private final Object selectedObject;
80 96

  
81
        private CommandInvoker(String commandId, Object selectedObject) {
82
            this.commandId = commandId;
97
        private CommandInvoker(Command command, Object selectedObject) {
98
            this.command = command;
83 99
            this.selectedObject = selectedObject;
84 100
        }
85 101

  
86 102
        @Override
87 103
        public void widgetSelected(SelectionEvent e) {
88
            ICommandService commandService = (ICommandService)PlatformUI.getWorkbench().getService(ICommandService.class);
89

  
90
            Command command = commandService.getCommand(commandId);
91
            if(command.isEnabled()) {
92
                IHandlerService handlerService = (IHandlerService)PlatformUI.getWorkbench().getService(IHandlerService.class);
93
                Map<String, UUID> params = new HashMap<String, UUID>();
94
                if(selectedObject instanceof ICdmBase){
95
                    params.put(commandId+".uuid", ((ICdmBase) selectedObject).getUuid());
96
                }
97
                ParameterizedCommand parameterizedCommand = ParameterizedCommand.generateCommand(command, params);
98
                try {
99
                    handlerService.executeCommand(parameterizedCommand, null);
100
                } catch (NotDefinedException nde) {
101
                    throw new RuntimeException("Could not find open command: " + commandId);
102
                } catch (Exception exception) {
103
                    MessagingUtils.error(getClass(), "An exception occured while trying execute "+commandId, exception);
104
                }
105
                PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage().getSelection();
104
            IHandlerService handlerService = (IHandlerService)PlatformUI.getWorkbench().getService(IHandlerService.class);
105
            Map<String, UUID> params = new HashMap<String, UUID>();
106
            if(selectedObject instanceof ICdmBase){
107
                params.put(command.getId()+".uuid", ((ICdmBase) selectedObject).getUuid());
108
            }
109
            ParameterizedCommand parameterizedCommand = ParameterizedCommand.generateCommand(command, params);
110
            try {
111
                handlerService.executeCommand(parameterizedCommand, null);
112
            } catch (NotDefinedException nde) {
113
                throw new RuntimeException("Could not find open command: " + command.getId());
114
            } catch (Exception exception) {
115
                MessagingUtils.error(getClass(), "An exception occured while trying execute "+command.getId(), exception);
106 116
            }
117
            PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage().getSelection();
107 118
        }
108 119
    }
109 120

  

Also available in: Unified diff