1 package org.simantics.district.selection.ui.parts;
3 import java.util.ArrayList;
4 import java.util.Collection;
5 import java.util.Collections;
8 import javax.annotation.PostConstruct;
9 import javax.inject.Inject;
11 import org.eclipse.e4.core.services.events.IEventBroker;
12 import org.eclipse.e4.ui.di.Focus;
13 import org.eclipse.e4.ui.model.application.MApplication;
14 import org.eclipse.e4.ui.model.application.ui.basic.MPart;
15 import org.eclipse.e4.ui.model.application.ui.menu.MHandledToolItem;
16 import org.eclipse.e4.ui.model.application.ui.menu.MMenuContribution;
17 import org.eclipse.e4.ui.model.application.ui.menu.MMenuFactory;
18 import org.eclipse.e4.ui.model.application.ui.menu.MPopupMenu;
19 import org.eclipse.e4.ui.model.application.ui.menu.MToolBar;
20 import org.eclipse.e4.ui.model.application.ui.menu.MToolBarElement;
21 import org.eclipse.e4.ui.services.EMenuService;
22 import org.eclipse.e4.ui.workbench.modeling.ESelectionService;
23 import org.eclipse.jface.dialogs.MessageDialog;
24 import org.eclipse.jface.viewers.StructuredSelection;
25 import org.eclipse.swt.SWT;
26 import org.eclipse.swt.widgets.Composite;
27 import org.eclipse.swt.widgets.Display;
28 import org.simantics.Simantics;
29 import org.simantics.browsing.ui.common.AdaptableHintContext;
30 import org.simantics.db.ReadGraph;
31 import org.simantics.db.Resource;
32 import org.simantics.db.exception.DatabaseException;
33 import org.simantics.db.layer0.SelectionHints;
34 import org.simantics.db.layer0.request.ActiveModels;
35 import org.simantics.db.request.Read;
36 import org.simantics.district.network.ui.DistrictNetworkUIUtil;
37 import org.simantics.district.selection.ElementSelector;
38 import org.simantics.district.selection.ElementSelector.DiagramGenerator;
39 import org.simantics.district.selection.ElementSelector.ExplicitGenerator;
40 import org.simantics.district.selection.ElementSelector.PropertySelector;
41 import org.simantics.district.selection.ElementSelector.SelectionResult;
42 import org.simantics.district.selection.ui.ElementSelectionTools;
43 import org.simantics.district.selection.ui.ElementSelectorTableUI;
44 import org.slf4j.Logger;
45 import org.slf4j.LoggerFactory;
47 public class ElementSelectionView {
48 private static final String CONTEXT_MENU_ID = "org.simantics.district.selection.ui.contextMenu";
49 private static final String POPUP_ID = "org.simantics.district.selection.ui.selectiontable.popup";
50 private static final String CREATE_NEW_ID = "org.simantics.district.selection.ui.command.createNewSelection";
51 private static final String CREATE_NEW_LABEL = "Create New Element Selection Query";
52 private static final String CREATE_NEW_ICON = "platform:/plugin/com.famfamfam.silk/icons/add.png";
53 private static final String EDIT_ID = "org.simantics.district.selection.ui.command.editElementSelector";
54 private static final String EDIT_LABEL = "Edit Element Selection Query";
55 private static final String EDIT_ICON = "platform:/plugin/com.famfamfam.silk/icons/pencil.png";
56 private static final String DELETE_ID = "org.simantics.district.selection.ui.command.deleteElementSelector";
57 private static final String DELETE_LABEL = "Delete Element Selection Query";
58 private static final String DELETE_ICON = "platform:/plugin/com.famfamfam.silk/icons/cross.png";
61 * Name of an event that is posted to IEventBroker when a selection is made. The data value is
62 * an instance of Collection<Resource>.
64 public static final String SELECTION_EVENT_ID = "org/simantics/district/selection/elementQuerySelection";
66 private static final Logger LOGGER = LoggerFactory.getLogger(ElementSelectionView.class);
68 private ElementSelectorTableUI table;
71 private ESelectionService selectionService;
74 private IEventBroker eventBroker;
77 public void init(MPart part, MApplication app) {
78 // Command is contributed via fragment
79 MHandledToolItem createItem = MMenuFactory.INSTANCE.createHandledToolItem();
80 createItem.setCommand(app.getCommand(CREATE_NEW_ID));
81 createItem.setLabel(CREATE_NEW_LABEL);
82 createItem.setIconURI(CREATE_NEW_ICON);
83 MHandledToolItem editItem = MMenuFactory.INSTANCE.createHandledToolItem();
84 editItem.setCommand(app.getCommand(EDIT_ID));
85 editItem.setLabel(EDIT_LABEL);
86 editItem.setIconURI(EDIT_ICON);
87 MHandledToolItem deleteItem = MMenuFactory.INSTANCE.createHandledToolItem();
88 deleteItem.setCommand(app.getCommand(DELETE_ID));
89 deleteItem.setLabel(DELETE_LABEL);
90 deleteItem.setIconURI(DELETE_ICON);
92 MToolBar toolBar = MMenuFactory.INSTANCE.createToolBar();
93 toolBar.setToBeRendered(true);
95 List<MToolBarElement> children = toolBar.getChildren();
96 children.add(createItem);
97 children.add(editItem);
98 children.add(deleteItem);
100 part.setToolbar(toolBar);
102 MPopupMenu popupMenu = MMenuFactory.INSTANCE.createPopupMenu();
103 popupMenu.setElementId(POPUP_ID);
105 List<MMenuContribution> menuContributions = app.getMenuContributions();
106 for (MMenuContribution menuContribution : menuContributions) {
107 if (CONTEXT_MENU_ID.equals(menuContribution.getParentId())) {
108 popupMenu.getChildren().addAll(menuContribution.getChildren());
112 part.getMenus().add(popupMenu);
116 public void createPartControl(Composite parent, EMenuService menuService) {
117 table = new ElementSelectorTableUI(parent, SWT.BORDER, this);
118 if (!(menuService.registerContextMenu(this.table.getTree(), POPUP_ID)))
119 LOGGER.warn("Could not register context menu {}", POPUP_ID);
123 public void setFocus() {
127 public ElementSelector getSelectedItem() {
128 return table.getSelectedItem();
131 public void performSelection(Display display, ElementSelector query) {
133 Collection<Resource> models = Simantics.getSession().syncRequest(new ActiveModels(Simantics.getProjectResource()));
134 final Resource model = models.isEmpty() ? null : models.iterator().next();
136 SelectionResult result = performQuery(query, model);
138 if (result.tailCount != result.tailSize) {
139 showArbitraryResultWarning(query, result);
142 if (query.getGenerator() instanceof DiagramGenerator || query.getGenerator() instanceof ExplicitGenerator) {
143 openDiagramWithSelection(display, result);
146 StructuredSelection selection = makeSelection(model, result);
147 selectionService.setPostSelection(selection);
148 sendSelectionEvent(selection);
149 } catch (DatabaseException e) {
150 LOGGER.error("Element selection query failed", e);
154 private StructuredSelection makeSelection(final Resource model, SelectionResult result) {
155 return new StructuredSelection(result.elements.stream()
157 AdaptableHintContext selectionElement = new ElementSelectionTools.SelectionElement(SelectionHints.STD_KEYS);
158 selectionElement.setHint(SelectionHints.KEY_MAIN, p0);
159 selectionElement.setHint(SelectionHints.KEY_MODEL, model);
160 return selectionElement;
165 private void openDiagramWithSelection(Display display, SelectionResult result) throws DatabaseException {
166 DistrictNetworkUIUtil.openDNDiagramWithSelection(display, new ArrayList<>(result.elements));
169 private void sendSelectionEvent(Object selection) {
170 eventBroker.send(SELECTION_EVENT_ID, selection);
173 private void showArbitraryResultWarning(ElementSelector query, SelectionResult result) {
174 String name = query.getSelector() != null && query.getSelector() instanceof PropertySelector ? ((PropertySelector)query.getSelector()).propertyName : null;
175 String msg = "Last " + result.tailCount + " of the " + result.elements.size() + " selected elements are an arbitraty subset of " + result.tailSize + " elements with equal values" +
176 (name != null ? " for " + name : "");
177 MessageDialog.openInformation(Display.getDefault().getActiveShell(), "Note", msg);
180 private SelectionResult performQuery(ElementSelector query, final Resource model) throws DatabaseException {
181 SelectionResult result = Simantics.getSession().syncRequest(new Read<SelectionResult>() {
183 public SelectionResult perform(ReadGraph graph) throws DatabaseException {
185 LOGGER.warn("No active model");
186 return new SelectionResult(Collections.emptyList(), 0, 0);
189 return query.selectElementsFrom(graph, model);