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