]> gerrit.simantics Code Review - simantics/district.git/blob
16e6bb159ed2938a0a1281602b3628b7b08dab94
[simantics/district.git] /
1 package org.simantics.district.selection.ui.parts;
2
3 import java.util.ArrayList;
4 import java.util.Collection;
5 import java.util.Collections;
6 import java.util.List;
7
8 import javax.annotation.PostConstruct;
9 import javax.inject.Inject;
10
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;
46
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";
59
60         /**
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>.
63          */
64         public static final String SELECTION_EVENT_ID = "org/simantics/district/selection/elementQuerySelection";
65         
66         private static final Logger LOGGER = LoggerFactory.getLogger(ElementSelectionView.class);
67
68         private ElementSelectorTableUI table;
69
70         @Inject
71         private ESelectionService selectionService;
72         
73         @Inject
74         private IEventBroker eventBroker;
75         
76         @Inject
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);
91                 
92                 MToolBar toolBar = MMenuFactory.INSTANCE.createToolBar();
93                 toolBar.setToBeRendered(true);
94                 
95                 List<MToolBarElement> children = toolBar.getChildren();
96                 children.add(createItem);
97                 children.add(editItem);
98                 children.add(deleteItem);
99                 
100                 part.setToolbar(toolBar);
101
102                 MPopupMenu popupMenu = MMenuFactory.INSTANCE.createPopupMenu();
103                 popupMenu.setElementId(POPUP_ID);
104
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());
109                         }
110                 }
111
112                 part.getMenus().add(popupMenu);
113         }
114
115         @PostConstruct
116         public void createPartControl(Composite parent, EMenuService menuService) {
117                 table = new ElementSelectorTableUI(parent, SWT.BORDER, this);
118                 if (!(menuService.registerContextMenu(this.table.getTable(), POPUP_ID)))
119                         LOGGER.warn("Could not register context menu {}", POPUP_ID);
120         }
121
122         @Focus
123         public void setFocus() {
124                 table.setFocus();
125         }
126
127         public ElementSelector getSelectedItem() {
128                 return table.getSelectedItem();
129         }
130
131         public void performSelection(Display display, ElementSelector query) {
132                 try {
133                         Collection<Resource> models = Simantics.getSession().syncRequest(new ActiveModels(Simantics.getProjectResource()));
134                         final Resource model = models.isEmpty() ? null : models.iterator().next();
135                         
136                         SelectionResult result = performQuery(query, model);
137                         
138                         if (result.tailCount != result.tailSize) {
139                                 showArbitraryResultWarning(query, result);
140                         }
141                         
142                         if (query.getGenerator() instanceof DiagramGenerator || query.getGenerator() instanceof ExplicitGenerator) {
143                                 openDiagramWithSelection(display, result);
144                         }
145                         
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);
151                 }
152         }
153
154         private StructuredSelection makeSelection(final Resource model, SelectionResult result) {
155                 return new StructuredSelection(result.elements.stream()
156                                 .map(p0 -> {
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;
161                                 })
162                                 .toArray());
163         }
164
165         private void openDiagramWithSelection(Display display, SelectionResult result) throws DatabaseException {
166                 DistrictNetworkUIUtil.openDNDiagramWithSelection(display, new ArrayList<>(result.elements));
167         }
168
169         private void sendSelectionEvent(Object selection) {
170                 eventBroker.send(SELECTION_EVENT_ID, selection);
171         }
172
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);
178         }
179
180         private SelectionResult performQuery(ElementSelector query, final Resource model) throws DatabaseException {
181                 SelectionResult result = Simantics.getSession().syncRequest(new Read<SelectionResult>() {
182                         @Override
183                         public SelectionResult perform(ReadGraph graph) throws DatabaseException {
184                                 if (model == null) {
185                                         LOGGER.warn("No active model");
186                                         return new SelectionResult(Collections.emptyList(), 0, 0);
187                                 }
188                                 
189                                 return query.selectElementsFrom(graph, model);
190                         }
191                 });
192                 return result;
193         }
194 }