ref #9038: use QuantitativeDataFormatter for formating the label
[taxeditor.git] / eu.etaxonomy.taxeditor.store / src / main / java / eu / etaxonomy / taxeditor / view / CdmViewerContextMenuE4.java
1 package eu.etaxonomy.taxeditor.view;
2
3 import java.util.List;
4 import java.util.Map;
5 import java.util.Map.Entry;
6
7 import javax.inject.Inject;
8 import javax.inject.Named;
9
10 import org.eclipse.core.commands.Command;
11 import org.eclipse.core.commands.common.NotDefinedException;
12 import org.eclipse.e4.core.commands.ECommandService;
13 import org.eclipse.e4.core.commands.EHandlerService;
14 import org.eclipse.e4.ui.di.AboutToShow;
15 import org.eclipse.e4.ui.model.application.commands.MCommand;
16 import org.eclipse.e4.ui.model.application.commands.MCommandsFactory;
17 import org.eclipse.e4.ui.model.application.ui.menu.MHandledMenuItem;
18 import org.eclipse.e4.ui.model.application.ui.menu.MMenu;
19 import org.eclipse.e4.ui.model.application.ui.menu.MMenuElement;
20 import org.eclipse.e4.ui.model.application.ui.menu.MMenuFactory;
21 import org.eclipse.e4.ui.services.IServiceConstants;
22 import org.eclipse.jface.viewers.IStructuredSelection;
23 import org.eclipse.jface.viewers.TreeNode;
24
25 import eu.etaxonomy.cdm.model.common.ICdmBase;
26 import eu.etaxonomy.cdm.persistence.dto.UuidAndTitleCache;
27 import eu.etaxonomy.taxeditor.l10n.Messages;
28 import eu.etaxonomy.taxeditor.model.FeatureNodeContainer;
29
30 /**
31 * Generic context menu for opening elements in the taxeditor.
32 *
33 */
34 public class CdmViewerContextMenuE4 {
35
36 @Inject
37 private EHandlerService handlerService;
38
39 @Inject
40 private ECommandService commandService;
41
42 @AboutToShow
43 public void aboutToShow(List<MMenuElement> items, @Named(IServiceConstants.ACTIVE_SELECTION) Object selectedObject) {
44
45 Object firstElement = null;
46 if(selectedObject instanceof IStructuredSelection){
47 firstElement = ((IStructuredSelection) selectedObject).getFirstElement();
48 }else{
49 firstElement = selectedObject;
50 }
51
52 if (firstElement instanceof FeatureNodeContainer){
53 return ;
54 }
55 if(firstElement instanceof TreeNode){
56 firstElement = ((TreeNode) firstElement).getValue();
57 }
58
59 Map<Command, String> enabledCommands = CdmViewerUtilE4.getAvailableViewers(firstElement, commandService, handlerService);
60
61 //get UUID
62 // UUID uuid = null;
63 // List<UUID> uuids = null;
64 // //for generic UuidAndTitleCache objects try to load the object
65 // if (firstElement instanceof UuidAndTitleCache && selectedObject instanceof IStructuredSelection && ((IStructuredSelection)selectedObject).size() == 1){
66 // uuid = ((UuidAndTitleCache)firstElement).getUuid();
67 // }else if (firstElement instanceof UuidAndTitleCache && selectedObject instanceof IStructuredSelection){
68 // uuids = new ArrayList<>();
69 // Iterator<IStructuredSelection> iterator = ((IStructuredSelection)selectedObject).iterator();
70 // UuidAndTitleCache sel;
71 // UUID uuidForList;
72 // while(iterator.hasNext()){
73 // sel = (UuidAndTitleCache)iterator.next();
74 // uuidForList = sel.getUuid();
75 // uuids.add(uuidForList);
76 //
77 // }
78 //
79 // }
80
81 //TODO!!
82 // if(selectedObject instanceof ICdmBase){
83 // uuid = ((ICdmBase) selectedObject).getUuid();
84 // }
85
86 //check if only one or multiple viewers/commands are available
87 if(enabledCommands.size()==1){
88 Entry<Command, String> entry = enabledCommands.entrySet().iterator().next();
89 final Command command = entry.getKey();
90 String viewerName = entry.getValue();
91 MMenu menu = MMenuFactory.INSTANCE.createMenu();
92 menu.setLabel(Messages.CdmViewerContextMenu_OPEN_IN);
93 items.add(menu);
94 // items.add(addCommand(uuid, command, String.format(Messages.CdmViewerContextMenu_OPEN, viewerName)));
95 menu.getChildren().add(addCommand(selectedObject, command, viewerName));
96 }
97 else if(enabledCommands.size()>1){
98 MMenu menu = MMenuFactory.INSTANCE.createMenu();
99 menu.setLabel(Messages.CdmViewerContextMenu_OPEN_IN);
100 items.add(menu);
101 for(Entry<Command, String> entry:enabledCommands.entrySet()){
102 String viewerName = entry.getValue();
103 Command command = entry.getKey();
104 if (selectedObject instanceof IStructuredSelection){
105 menu.getChildren().add(addCommand(selectedObject, command, viewerName));
106 }else if(selectedObject instanceof ICdmBase){
107 // UUID uuid = ((ICdmBase) selectedObject).getUuid();
108 menu.getChildren().add(addCommand(selectedObject, command, viewerName));
109 }else if (selectedObject instanceof UuidAndTitleCache){
110 menu.getChildren().add(addCommand(selectedObject, command, viewerName));
111 }
112 }
113 }
114 }
115
116 public MHandledMenuItem addCommand(Object selection, Command command, String commandLabel) {
117 MHandledMenuItem menuItem = MMenuFactory.INSTANCE.createHandledMenuItem();
118 menuItem.setLabel(commandLabel);
119 MCommand mCommand = MCommandsFactory.INSTANCE.createCommand();
120 mCommand.setElementId(command.getId());
121 mCommand.setCommandName(commandLabel);
122 try {
123 mCommand.setCommandName(command.getName());
124 } catch (NotDefinedException e) {
125 e.printStackTrace();
126 }
127 //set params
128
129 menuItem.getTransientData().put(command.getId()+".uuid", selection);
130
131 // MParameter parameter = MCommandsFactory.INSTANCE.createParameter();
132 // parameter.setElementId(command.getId()+".uuid");
133 // parameter.setValue(uuid.toString());
134 // menuItem.getParameters().add(parameter);
135
136
137 menuItem.setCommand(mCommand);
138 return menuItem;
139 }
140
141 }