1 package org.simantics.district.selection.ui.handlers;
3 import javax.inject.Inject;
5 import org.eclipse.e4.core.contexts.ContextInjectionFactory;
6 import org.eclipse.e4.core.contexts.IEclipseContext;
7 import org.eclipse.e4.core.di.annotations.CanExecute;
8 import org.eclipse.e4.core.di.annotations.Execute;
9 import org.eclipse.e4.ui.model.application.ui.basic.MPart;
10 import org.eclipse.e4.ui.workbench.IWorkbench;
11 import org.eclipse.e4.ui.workbench.modeling.EPartService;
12 import org.eclipse.e4.ui.workbench.modeling.ESelectionService;
13 import org.eclipse.jface.window.Window;
14 import org.eclipse.swt.widgets.Shell;
15 import org.simantics.db.exception.DatabaseException;
16 import org.simantics.district.selection.ui.parts.EditSelectorDialog;
17 import org.simantics.district.selection.ui.parts.ElementSelectionView;
18 import org.slf4j.Logger;
19 import org.slf4j.LoggerFactory;
21 public class EditElementSelector {
23 private static final Logger LOGGER = LoggerFactory.getLogger(EditElementSelector.class);
26 EPartService partService;
29 public boolean canExecute(ESelectionService selectionService) {
30 MPart part = partService.getActivePart();
31 if (part == null) return false;
33 Object object = part.getObject();
34 if (object == null || !(object instanceof ElementSelectionView))
37 ElementSelectionView view = (ElementSelectionView)object;
38 return view.getSelectedItem() != null;
42 public void editElementSelector(IEclipseContext context, IWorkbench workbench) {
43 MPart part = partService.getActivePart();
44 if (part == null) return;
46 Object object = part.getObject();
47 if (object == null || !(object instanceof ElementSelectionView))
50 ElementSelectionView view = (ElementSelectionView)object;
52 Shell shell = context.getActive(Shell.class);
53 EditSelectorDialog dialog = new EditSelectorDialog(shell, view.getSelectedItem());
55 LOGGER.debug("Opening dialog");
56 int result = dialog.open();
57 LOGGER.debug("Dialog closed with result code " + result);
58 if (result == Window.OK) {
60 dialog.writeSelection();
61 } catch (DatabaseException e) {
62 LOGGER.error("Writing new element selection failed", e);