]> gerrit.simantics Code Review - simantics/district.git/blob
1cddde9f7034c62c2c37c4ce186a375707f4f78b
[simantics/district.git] /
1 package org.simantics.district.selection.ui.parts;
2
3 import java.util.List;
4
5 import javax.annotation.PostConstruct;
6 import javax.inject.Inject;
7
8 import org.eclipse.e4.ui.di.Focus;
9 import org.eclipse.e4.ui.model.application.MApplication;
10 import org.eclipse.e4.ui.model.application.ui.basic.MPart;
11 import org.eclipse.e4.ui.model.application.ui.menu.MHandledToolItem;
12 import org.eclipse.e4.ui.model.application.ui.menu.MMenuContribution;
13 import org.eclipse.e4.ui.model.application.ui.menu.MMenuFactory;
14 import org.eclipse.e4.ui.model.application.ui.menu.MPopupMenu;
15 import org.eclipse.e4.ui.model.application.ui.menu.MToolBar;
16 import org.eclipse.e4.ui.model.application.ui.menu.MToolBarElement;
17 import org.eclipse.e4.ui.services.EMenuService;
18 import org.eclipse.e4.ui.workbench.modeling.ESelectionService;
19 import org.eclipse.swt.SWT;
20 import org.eclipse.swt.widgets.Composite;
21 import org.simantics.district.selection.ElementSelector;
22 import org.simantics.district.selection.ui.ElementSelectorTableUI;
23 import org.slf4j.Logger;
24 import org.slf4j.LoggerFactory;
25
26 public class ElementSelectionView {
27         private static final String CONTEXT_MENU_ID = "org.simantics.district.selection.ui.contextMenu";
28         private static final String POPUP_ID = "org.simantics.district.selection.ui.selectiontable.popup";
29         private static final String CREATE_NEW_ID = "org.simantics.district.selection.ui.command.createNewSelection";
30         private static final String CREATE_NEW_LABEL = "Create New Element Selection Query";
31         private static final String CREATE_NEW_ICON = "platform:/plugin/com.famfamfam.silk/icons/add.png";
32         private static final String EDIT_ID = "org.simantics.district.selection.ui.command.editElementSelector";
33         private static final String EDIT_LABEL = "Edit Element Selection Query";
34         private static final String EDIT_ICON = "platform:/plugin/com.famfamfam.silk/icons/pencil.png";
35         private static final String DELETE_ID = "org.simantics.district.selection.ui.command.deleteElementSelector";
36         private static final String DELETE_LABEL = "Delete Element Selection Query";
37         private static final String DELETE_ICON = "platform:/plugin/com.famfamfam.silk/icons/cross.png";
38         
39         private static final Logger LOGGER = LoggerFactory.getLogger(ElementSelectionView.class);
40
41         private ElementSelectorTableUI table;
42
43         @Inject
44         private ESelectionService selectionService;
45
46         @Inject
47         public void init(MPart part, MApplication app) {
48                 // Command is contributed via fragment
49                 MHandledToolItem createItem = MMenuFactory.INSTANCE.createHandledToolItem();
50                 createItem.setCommand(app.getCommand(CREATE_NEW_ID));
51                 createItem.setLabel(CREATE_NEW_LABEL);
52                 createItem.setIconURI(CREATE_NEW_ICON);
53                 MHandledToolItem editItem = MMenuFactory.INSTANCE.createHandledToolItem();
54                 editItem.setCommand(app.getCommand(EDIT_ID));
55                 editItem.setLabel(EDIT_LABEL);
56                 editItem.setIconURI(EDIT_ICON);
57                 MHandledToolItem deleteItem = MMenuFactory.INSTANCE.createHandledToolItem();
58                 deleteItem.setCommand(app.getCommand(DELETE_ID));
59                 deleteItem.setLabel(DELETE_LABEL);
60                 deleteItem.setIconURI(DELETE_ICON);
61                 
62                 MToolBar toolBar = MMenuFactory.INSTANCE.createToolBar();
63                 toolBar.setToBeRendered(true);
64                 
65                 List<MToolBarElement> children = toolBar.getChildren();
66                 children.add(createItem);
67                 children.add(editItem);
68                 children.add(deleteItem);
69                 
70                 part.setToolbar(toolBar);
71
72                 MPopupMenu popupMenu = MMenuFactory.INSTANCE.createPopupMenu();
73                 popupMenu.setElementId(POPUP_ID);
74
75                 List<MMenuContribution> menuContributions = app.getMenuContributions();
76                 for (MMenuContribution menuContribution : menuContributions) {
77                         if (CONTEXT_MENU_ID.equals(menuContribution.getParentId())) {
78                                 popupMenu.getChildren().addAll(menuContribution.getChildren());
79                         }
80                 }
81
82                 part.getMenus().add(popupMenu);
83         }
84
85         @PostConstruct
86         public void createPartControl(Composite parent, EMenuService menuService) {
87                 table = new ElementSelectorTableUI(selectionService, parent, SWT.BORDER);
88                 if (!(menuService.registerContextMenu(this.table.getTree(), POPUP_ID)))
89                         LOGGER.warn("Could not register context menu {}", POPUP_ID);
90         }
91
92         @Focus
93         public void setFocus() {
94                 table.setFocus();
95         }
96
97         public ElementSelector getSelectedItem() {
98                 return table.getSelectedItem();
99         }
100 }