]> gerrit.simantics Code Review - simantics/district.git/blob - org.simantics.district.network.ui/src/org/simantics/district/network/ui/contributions/CopyDistrictVertexHandler.java
Initial version of dh element cut/paste (copy still WIP)
[simantics/district.git] / org.simantics.district.network.ui / src / org / simantics / district / network / ui / contributions / CopyDistrictVertexHandler.java
1 package org.simantics.district.network.ui.contributions;
2
3 import java.util.List;
4
5 import javax.inject.Named;
6
7 import org.eclipse.e4.core.di.annotations.CanExecute;
8 import org.eclipse.e4.core.di.annotations.Execute;
9 import org.eclipse.e4.ui.services.IServiceConstants;
10 import org.eclipse.jface.viewers.ISelection;
11 import org.simantics.Simantics;
12 import org.simantics.db.ReadGraph;
13 import org.simantics.db.Resource;
14 import org.simantics.db.exception.DatabaseException;
15 import org.simantics.db.layer0.SelectionHints;
16 import org.simantics.db.request.Read;
17 import org.simantics.district.network.ontology.DistrictNetworkResource;
18 import org.simantics.utils.ui.ISelectionUtils;
19 import org.slf4j.Logger;
20 import org.slf4j.LoggerFactory;
21
22 public class CopyDistrictVertexHandler {
23
24     private static final Logger LOGGER = LoggerFactory.getLogger(CopyDistrictVertexHandler.class);
25     static List<Resource> elements;
26     static boolean cut = true;
27
28     @CanExecute
29     public boolean canExecute(@Named(IServiceConstants.ACTIVE_SELECTION) ISelection selection) {
30         List<Resource> elements = ISelectionUtils.getPossibleKeys(selection, SelectionHints.KEY_MAIN, Resource.class);
31         if (elements.size() != 1)
32             return false;
33         try {
34             return Simantics.getSession().syncRequest(new Read<Boolean>() {
35
36                 @Override
37                 public Boolean perform(ReadGraph graph) throws DatabaseException {
38                     DistrictNetworkResource DN = DistrictNetworkResource.getInstance(graph);
39                     for (Resource selection : elements) {
40                         if (!graph.isInstanceOf(selection, DN.Element)) {
41                             return false;
42                         }
43                     }
44                     return true;
45                 }
46             });
47         } catch (DatabaseException e) {
48             LOGGER.error("Could not evaluate if mapping can be changed for selection {}", elements, e);
49             return false;
50         }
51     }
52
53     @Execute
54     public void execute(@Named(IServiceConstants.ACTIVE_SELECTION) Object selection) {
55         final List<Resource> elements = ISelectionUtils.getPossibleKeys(selection, SelectionHints.KEY_MAIN, Resource.class);
56         // we store these to a static variable for pasting.. maybe not the best solution 
57         CopyDistrictVertexHandler.elements = elements;
58         //CopyDistrictVertexHandler.cut = cut != null && !cut.isEmpty();
59     }
60 }