1 package org.simantics.district.network.ui.contributions;
5 import javax.inject.Named;
7 import org.eclipse.core.runtime.IProgressMonitor;
8 import org.eclipse.core.runtime.IStatus;
9 import org.eclipse.core.runtime.Status;
10 import org.eclipse.core.runtime.jobs.Job;
11 import org.eclipse.e4.core.di.annotations.CanExecute;
12 import org.eclipse.e4.core.di.annotations.Execute;
13 import org.eclipse.e4.ui.services.IServiceConstants;
14 import org.eclipse.jface.viewers.ISelection;
15 import org.simantics.DatabaseJob;
16 import org.simantics.Simantics;
17 import org.simantics.db.Resource;
18 import org.simantics.db.WriteGraph;
19 import org.simantics.db.common.request.WriteRequest;
20 import org.simantics.db.exception.DatabaseException;
21 import org.simantics.db.layer0.SelectionHints;
22 import org.simantics.district.network.DistrictNetworkUtil;
23 import org.simantics.district.network.ui.internal.Activator;
24 import org.simantics.utils.ui.ISelectionUtils;
26 public class JoinVerticesHandler {
29 public boolean canExecute(@Named(IServiceConstants.ACTIVE_SELECTION) ISelection selection) {
30 List<Resource> vertices = ISelectionUtils.getPossibleKeys(selection, SelectionHints.KEY_MAIN, Resource.class);
31 return vertices.size() > 1;
35 public void execute(@Named(IServiceConstants.ACTIVE_SELECTION) Object selection) {
36 final List<Resource> vertices = ISelectionUtils.getPossibleKeys(selection, SelectionHints.KEY_MAIN, Resource.class);
37 if (vertices.size() <= 1)
39 Job job = new DatabaseJob("Join selected vertices") {
42 protected IStatus run(IProgressMonitor monitor) {
44 Simantics.getSession().syncRequest(new WriteRequest() {
47 public void perform(WriteGraph graph) throws DatabaseException {
48 DistrictNetworkUtil.joinVertices(graph, vertices);
51 } catch (DatabaseException e) {
52 return new Status(IStatus.ERROR, Activator.PLUGIN_ID, getName() + " failed.", e);
54 return Status.OK_STATUS;