1 package org.simantics.district.selection.ui.handlers;
3 import java.util.Collection;
5 import javax.inject.Inject;
7 import org.eclipse.e4.core.contexts.IEclipseContext;
8 import org.eclipse.e4.core.di.annotations.CanExecute;
9 import org.eclipse.e4.core.di.annotations.Execute;
10 import org.eclipse.e4.ui.model.application.ui.basic.MPart;
11 import org.eclipse.e4.ui.services.IServiceConstants;
12 import org.eclipse.e4.ui.workbench.IWorkbench;
13 import org.eclipse.e4.ui.workbench.modeling.EPartService;
14 import org.eclipse.e4.ui.workbench.modeling.ESelectionService;
15 import org.eclipse.jface.window.Window;
16 import org.eclipse.swt.widgets.Shell;
17 import org.simantics.db.Resource;
18 import org.simantics.db.exception.DatabaseException;
19 import org.simantics.district.selection.ElementSelector;
20 import org.simantics.district.selection.ElementSelector.ExplicitGenerator;
21 import org.simantics.district.selection.ui.parts.EditSelectorDialog;
22 import org.simantics.district.selection.ui.parts.ElementSelectionView;
23 import org.simantics.utils.ui.AdaptionUtils;
24 import org.slf4j.Logger;
25 import org.slf4j.LoggerFactory;
27 public class EditElementSelector {
29 private static final Logger LOGGER = LoggerFactory.getLogger(EditElementSelector.class);
32 EPartService partService;
35 public boolean canExecute(ESelectionService selectionService) {
36 MPart part = partService.getActivePart();
37 if (part == null) return false;
39 Object object = part.getObject();
40 if (object == null || !(object instanceof ElementSelectionView))
43 ElementSelectionView view = (ElementSelectionView)object;
44 return view.getSelectedItem() != null;
48 public void editElementSelector(IEclipseContext context, IWorkbench workbench) {
49 Object currentSelection = context.get(IServiceConstants.ACTIVE_SELECTION);
51 MPart part = partService.getActivePart();
52 if (part == null) return;
54 Object object = part.getObject();
55 if (object == null || !(object instanceof ElementSelectionView))
58 ElementSelectionView view = (ElementSelectionView)object;
60 Collection<Resource> selection;
61 ElementSelector selectedItem = view.getSelectedItem();
62 if (selectedItem.getGenerator() instanceof ExplicitGenerator) {
63 selection = ((ExplicitGenerator)selectedItem.getGenerator()).elements;
66 selection = AdaptionUtils.adaptToCollection(currentSelection, Resource.class);
69 Shell shell = context.getActive(Shell.class);
70 EditSelectorDialog dialog = new EditSelectorDialog(shell, selectedItem, selection);
72 LOGGER.debug("Opening dialog");
73 int result = dialog.open();
74 LOGGER.debug("Dialog closed with result code " + result);
75 if (result == Window.OK) {
77 dialog.writeSelection();
78 } catch (DatabaseException e) {
79 LOGGER.error("Writing new element selection failed", e);