]> gerrit.simantics Code Review - simantics/district.git/blob
4382e70c1e55c6efca2b284b8d632e6dd6874b79
[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.IEclipseContext;
6 import org.eclipse.e4.core.di.annotations.CanExecute;
7 import org.eclipse.e4.core.di.annotations.Execute;
8 import org.eclipse.e4.ui.model.application.ui.basic.MPart;
9 import org.eclipse.e4.ui.workbench.IWorkbench;
10 import org.eclipse.e4.ui.workbench.modeling.EPartService;
11 import org.eclipse.e4.ui.workbench.modeling.ESelectionService;
12 import org.simantics.Simantics;
13 import org.simantics.db.WriteGraph;
14 import org.simantics.db.common.request.WriteRequest;
15 import org.simantics.db.exception.DatabaseException;
16 import org.simantics.district.selection.ElementSelector;
17 import org.simantics.district.selection.ui.parts.ElementSelectionView;
18 import org.slf4j.Logger;
19 import org.slf4j.LoggerFactory;
20
21 public class DeleteElementSelector {
22
23         private static final Logger LOGGER = LoggerFactory.getLogger(DeleteElementSelector.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                 ElementSelector selectedItem = view.getSelectedItem();
52                 
53                 if (selectedItem == null)
54                         return;
55                 
56                 Simantics.getSession().async(new WriteRequest() {
57                         @Override
58                         public void perform(WriteGraph graph) throws DatabaseException {
59                                 graph.markUndoPoint();
60                                 graph.deny(selectedItem.getResource());
61                         }
62                 });
63         }
64 }