package org.simantics.district.network.ui.contributions; import java.util.List; import javax.inject.Named; import org.eclipse.e4.core.di.annotations.CanExecute; import org.eclipse.e4.core.di.annotations.Execute; import org.eclipse.e4.ui.services.IServiceConstants; import org.eclipse.jface.viewers.ISelection; import org.simantics.Simantics; import org.simantics.db.ReadGraph; import org.simantics.db.Resource; import org.simantics.db.exception.DatabaseException; import org.simantics.db.layer0.SelectionHints; import org.simantics.db.request.Read; import org.simantics.district.network.ontology.DistrictNetworkResource; import org.simantics.utils.ui.ISelectionUtils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; public class CopyDistrictVertexHandler { private static final Logger LOGGER = LoggerFactory.getLogger(CopyDistrictVertexHandler.class); static List elements; static boolean cut = true; @CanExecute public boolean canExecute(@Named(IServiceConstants.ACTIVE_SELECTION) ISelection selection) { List elements = ISelectionUtils.getPossibleKeys(selection, SelectionHints.KEY_MAIN, Resource.class); if (elements.size() != 1) return false; try { return Simantics.getSession().syncRequest(new Read() { @Override public Boolean perform(ReadGraph graph) throws DatabaseException { DistrictNetworkResource DN = DistrictNetworkResource.getInstance(graph); for (Resource selection : elements) { if (!graph.isInstanceOf(selection, DN.Element)) { return false; } } return true; } }); } catch (DatabaseException e) { LOGGER.error("Could not evaluate if mapping can be changed for selection {}", elements, e); return false; } } @Execute public void execute(@Named(IServiceConstants.ACTIVE_SELECTION) Object selection) { final List elements = ISelectionUtils.getPossibleKeys(selection, SelectionHints.KEY_MAIN, Resource.class); // we store these to a static variable for pasting.. maybe not the best solution CopyDistrictVertexHandler.elements = elements; //CopyDistrictVertexHandler.cut = cut != null && !cut.isEmpty(); } }