]> gerrit.simantics Code Review - simantics/district.git/blob
f914d1618ce10adf7ea36243777cd8453bcf304e
[simantics/district.git] /
1 package org.simantics.district.selection.ui.handlers;
2
3 import javax.inject.Inject;
4
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;
20
21 public class EditElementSelector {
22
23         private static final Logger LOGGER = LoggerFactory.getLogger(EditElementSelector.class);
24
25         @Inject
26         EPartService partService;
27         
28         @CanExecute
29         public boolean canExecute(ESelectionService selectionService) {
30                 MPart part = partService.getActivePart();
31                 if (part == null) return false;
32                 
33                 Object object = part.getObject();
34                 if (object == null || !(object instanceof ElementSelectionView))
35                         return false;
36                 
37                 ElementSelectionView view = (ElementSelectionView)object;
38                 return view.getSelectedItem() != null;
39         }
40
41         @Execute
42         public void editElementSelector(IEclipseContext context, IWorkbench workbench) {
43                 MPart part = partService.getActivePart();
44                 if (part == null) return;
45                 
46                 Object object = part.getObject();
47                 if (object == null || !(object instanceof ElementSelectionView))
48                         return;
49                 
50                 ElementSelectionView view = (ElementSelectionView)object;
51                 
52                 Shell shell = context.getActive(Shell.class);
53                 EditSelectorDialog dialog = new EditSelectorDialog(shell, view.getSelectedItem());
54                 
55                 LOGGER.debug("Opening dialog");
56                 int result = dialog.open();
57                 LOGGER.debug("Dialog closed with result code " + result);
58                 if (result == Window.OK) {
59                         try {
60                                 dialog.writeSelection();
61                         } catch (DatabaseException e) {
62                                 LOGGER.error("Writing new element selection failed", e);
63                         }
64                 }
65         }
66 }