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.e4.ui.workbench.modeling.ISelectionListener;
24 import org.eclipse.jface.dialogs.MessageDialog;
25 import org.eclipse.jface.viewers.StructuredSelection;
26 import org.eclipse.swt.SWT;
27 import org.eclipse.swt.widgets.Composite;
28 import org.eclipse.swt.widgets.Display;
29 import org.simantics.Simantics;
30 import org.simantics.browsing.ui.common.AdaptableHintContext;
31 import org.simantics.db.ReadGraph;
32 import org.simantics.db.Resource;
33 import org.simantics.db.exception.DatabaseException;
34 import org.simantics.db.layer0.SelectionHints;
35 import org.simantics.db.layer0.request.ActiveModels;
36 import org.simantics.db.request.Read;
37 import org.simantics.district.network.ui.DistrictNetworkUIUtil;
38 import org.simantics.district.selection.ElementSelector;
39 import org.simantics.district.selection.ElementSelector.DiagramGenerator;
40 import org.simantics.district.selection.ElementSelector.ExplicitGenerator;
41 import org.simantics.district.selection.ElementSelector.PropertySelector;
42 import org.simantics.district.selection.ElementSelector.SelectionResult;
43 import org.simantics.district.selection.ui.ElementSelectionTools;
44 import org.simantics.district.selection.ui.ElementSelectorTableUI;
45 import org.slf4j.Logger;
46 import org.slf4j.LoggerFactory;
48 public class ElementSelectionView {
49 private static final String CONTEXT_MENU_ID = "org.simantics.district.selection.ui.contextMenu";
50 private static final String POPUP_ID = "org.simantics.district.selection.ui.selectiontable.popup";
51 private static final String CREATE_NEW_ID = "org.simantics.district.selection.ui.command.createNewSelection";
52 private static final String CREATE_NEW_LABEL = "Create New Element Selection Query";
53 private static final String CREATE_NEW_ICON = "platform:/plugin/com.famfamfam.silk/icons/add.png";
54 private static final String EDIT_ID = "org.simantics.district.selection.ui.command.editElementSelector";
55 private static final String EDIT_LABEL = "Edit Element Selection Query";
56 private static final String EDIT_ICON = "platform:/plugin/com.famfamfam.silk/icons/pencil.png";
57 private static final String DELETE_ID = "org.simantics.district.selection.ui.command.deleteElementSelector";
58 private static final String DELETE_LABEL = "Delete Element Selection Query";
59 private static final String DELETE_ICON = "platform:/plugin/com.famfamfam.silk/icons/cross.png";
62 * Name of an event that is posted to IEventBroker when a selection is made. The data value is
63 * an instance of Collection<Resource>.
65 public static final String SELECTION_EVENT_ID = "org/simantics/district/selection/elementQuerySelection";
67 private static final Logger LOGGER = LoggerFactory.getLogger(ElementSelectionView.class);
69 private ElementSelectorTableUI table;
72 private ESelectionService selectionService;
75 private IEventBroker eventBroker;
78 public void init(MPart part, MApplication app) {
79 // Command is contributed via fragment
80 MHandledToolItem createItem = MMenuFactory.INSTANCE.createHandledToolItem();
81 createItem.setCommand(app.getCommand(CREATE_NEW_ID));
82 createItem.setLabel(CREATE_NEW_LABEL);
83 createItem.setIconURI(CREATE_NEW_ICON);
84 MHandledToolItem editItem = MMenuFactory.INSTANCE.createHandledToolItem();
85 editItem.setCommand(app.getCommand(EDIT_ID));
86 editItem.setLabel(EDIT_LABEL);
87 editItem.setIconURI(EDIT_ICON);
88 MHandledToolItem deleteItem = MMenuFactory.INSTANCE.createHandledToolItem();
89 deleteItem.setCommand(app.getCommand(DELETE_ID));
90 deleteItem.setLabel(DELETE_LABEL);
91 deleteItem.setIconURI(DELETE_ICON);
93 MToolBar toolBar = MMenuFactory.INSTANCE.createToolBar();
94 toolBar.setToBeRendered(true);
96 List<MToolBarElement> children = toolBar.getChildren();
97 children.add(createItem);
98 children.add(editItem);
99 children.add(deleteItem);
101 part.setToolbar(toolBar);
103 MPopupMenu popupMenu = MMenuFactory.INSTANCE.createPopupMenu();
104 popupMenu.setElementId(POPUP_ID);
106 List<MMenuContribution> menuContributions = app.getMenuContributions();
107 for (MMenuContribution menuContribution : menuContributions) {
108 if (CONTEXT_MENU_ID.equals(menuContribution.getParentId())) {
109 popupMenu.getChildren().addAll(menuContribution.getChildren());
113 part.getMenus().add(popupMenu);
117 public void createPartControl(Composite parent, EMenuService menuService) {
118 table = new ElementSelectorTableUI(parent, SWT.BORDER, this);
119 if (!(menuService.registerContextMenu(this.table.getTree(), POPUP_ID)))
120 LOGGER.warn("Could not register context menu {}", POPUP_ID);
124 public void setFocus() {
128 public ElementSelector getSelectedItem() {
129 return table.getSelectedItem();
132 public void performSelection(Display display, ElementSelector query) {
134 Collection<Resource> models = Simantics.getSession().syncRequest(new ActiveModels(Simantics.getProjectResource()));
135 final Resource model = models.isEmpty() ? null : models.iterator().next();
137 SelectionResult result = performQuery(query, model);
139 if (result.tailCount != result.tailSize) {
140 showArbitraryResultWarning(query, result);
143 if (query.getGenerator() instanceof DiagramGenerator || query.getGenerator() instanceof ExplicitGenerator) {
144 openDiagramWithSelection(display, result);
147 StructuredSelection selection = makeSelection(model, result);
148 selectionService.setPostSelection(selection);
149 sendSelectionEvent(selection);
150 } catch (DatabaseException e) {
151 LOGGER.error("Element selection query failed", e);
155 private StructuredSelection makeSelection(final Resource model, SelectionResult result) {
156 return new StructuredSelection(result.elements.stream()
158 AdaptableHintContext selectionElement = new ElementSelectionTools.SelectionElement(SelectionHints.STD_KEYS);
159 selectionElement.setHint(SelectionHints.KEY_MAIN, p0);
160 selectionElement.setHint(SelectionHints.KEY_MODEL, model);
161 return selectionElement;
166 private void openDiagramWithSelection(Display display, SelectionResult result) throws DatabaseException {
167 DistrictNetworkUIUtil.openDNDiagramWithSelection(display, new ArrayList<>(result.elements));
170 private void sendSelectionEvent(Object selection) {
171 eventBroker.send(SELECTION_EVENT_ID, selection);
174 private void showArbitraryResultWarning(ElementSelector query, SelectionResult result) {
175 String name = query.getSelector() != null && query.getSelector() instanceof PropertySelector ? ((PropertySelector)query.getSelector()).propertyName : null;
176 String msg = "Last " + result.tailCount + " of the " + result.elements.size() + " selected elements are an arbitraty subset of " + result.tailSize + " elements with equal values" +
177 (name != null ? " for " + name : "");
178 MessageDialog.openInformation(Display.getDefault().getActiveShell(), "Note", msg);
181 private SelectionResult performQuery(ElementSelector query, final Resource model) throws DatabaseException {
182 SelectionResult result = Simantics.getSession().syncRequest(new Read<SelectionResult>() {
184 public SelectionResult perform(ReadGraph graph) throws DatabaseException {
186 LOGGER.warn("No active model");
187 return new SelectionResult(Collections.emptyList(), 0, 0);
190 return query.selectElementsFrom(graph, model);