]> gerrit.simantics Code Review - simantics/district.git/blob - org.simantics.district.network.ui/src/org/simantics/district/network/ui/contributions/JoinVerticesHandler.java
Adjustments to map diagram info label rendering
[simantics/district.git] / org.simantics.district.network.ui / src / org / simantics / district / network / ui / contributions / JoinVerticesHandler.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.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;
25
26 public class JoinVerticesHandler {
27
28     @CanExecute
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;
32     }
33     
34     @Execute
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)
38             return;
39         Job job = new DatabaseJob("Join selected vertices") {
40             
41             @Override
42             protected IStatus run(IProgressMonitor monitor) {
43                 try {
44                     Simantics.getSession().syncRequest(new WriteRequest() {
45                         
46                         @Override
47                         public void perform(WriteGraph graph) throws DatabaseException {
48                             DistrictNetworkUtil.joinVertices(graph, vertices);
49                         }
50                     });
51                 } catch (DatabaseException e) {
52                     return new Status(IStatus.ERROR, Activator.PLUGIN_ID, getName() + " failed.", e);
53                 }
54                 return Status.OK_STATUS;
55             }
56         };
57         job.setUser(true);
58         job.schedule();
59     }
60 }