]> gerrit.simantics Code Review - simantics/district.git/blob - org.simantics.district.selection.ui/src/org/simantics/district/selection/ui/handlers/EditElementSelector.java
Selection queries from explicit sets of elements.
[simantics/district.git] / org.simantics.district.selection.ui / src / org / simantics / district / selection / ui / handlers / EditElementSelector.java
1 package org.simantics.district.selection.ui.handlers;
2
3 import java.util.Collection;
4
5 import javax.inject.Inject;
6
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;
26
27 public class EditElementSelector {
28
29         private static final Logger LOGGER = LoggerFactory.getLogger(EditElementSelector.class);
30
31         @Inject
32         EPartService partService;
33         
34         @CanExecute
35         public boolean canExecute(ESelectionService selectionService) {
36                 MPart part = partService.getActivePart();
37                 if (part == null) return false;
38                 
39                 Object object = part.getObject();
40                 if (object == null || !(object instanceof ElementSelectionView))
41                         return false;
42                 
43                 ElementSelectionView view = (ElementSelectionView)object;
44                 return view.getSelectedItem() != null;
45         }
46
47         @Execute
48         public void editElementSelector(IEclipseContext context, IWorkbench workbench) {
49                 Object currentSelection = context.get(IServiceConstants.ACTIVE_SELECTION);
50                 
51                 MPart part = partService.getActivePart();
52                 if (part == null) return;
53                 
54                 Object object = part.getObject();
55                 if (object == null || !(object instanceof ElementSelectionView))
56                         return;
57                 
58                 ElementSelectionView view = (ElementSelectionView)object;
59                 
60                 Collection<Resource> selection;
61                 ElementSelector selectedItem = view.getSelectedItem();
62                 if (selectedItem.getGenerator() instanceof ExplicitGenerator) {
63                         selection = ((ExplicitGenerator)selectedItem.getGenerator()).elements;
64                 }
65                 else {
66                         selection = AdaptionUtils.adaptToCollection(currentSelection, Resource.class);
67                 }
68                 
69                 Shell shell = context.getActive(Shell.class);
70                 EditSelectorDialog dialog = new EditSelectorDialog(shell, selectedItem, selection);
71                 
72                 LOGGER.debug("Opening dialog");
73                 int result = dialog.open();
74                 LOGGER.debug("Dialog closed with result code " + result);
75                 if (result == Window.OK) {
76                         try {
77                                 dialog.writeSelection();
78                         } catch (DatabaseException e) {
79                                 LOGGER.error("Writing new element selection failed", e);
80                         }
81                 }
82         }
83 }