]> gerrit.simantics Code Review - simantics/district.git/blob - org.simantics.district.network.ui/src/org/simantics/district/network/ui/contributions/DrawMapToggleHandler.java
Adjustments to map diagram info label rendering
[simantics/district.git] / org.simantics.district.network.ui / src / org / simantics / district / network / ui / contributions / DrawMapToggleHandler.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 DrawMapToggleHandler {
27
28     @CanExecute
29     public boolean canExecute(@Named(IServiceConstants.ACTIVE_SELECTION) ISelection selection) {
30         final List<Resource> diagrams = ISelectionUtils.getPossibleKeys(selection, SelectionHints.KEY_MAIN, Resource.class);
31         return diagrams.size() > 0;
32     }
33
34     @Execute
35     public void execute(@Named(IServiceConstants.ACTIVE_SELECTION) Object selection) {
36         final List<Resource> diagrams = ISelectionUtils.getPossibleKeys(selection, SelectionHints.KEY_MAIN, Resource.class);
37         Job job = new DatabaseJob("Toggle draw map") {
38
39             @Override
40             protected IStatus run(IProgressMonitor monitor) {
41                 try {
42                     Simantics.getSession().syncRequest(new WriteRequest() {
43
44                         @Override
45                         public void perform(WriteGraph graph) throws DatabaseException {
46                             DistrictNetworkUtil.toggleDrawMap(graph, diagrams.get(0));
47                         }
48                     });
49                 } catch (DatabaseException e) {
50                     return new Status(IStatus.ERROR, Activator.PLUGIN_ID, getName() + " failed.", e);
51                 }
52                 return Status.OK_STATUS;
53             }
54         };
55         job.setUser(true);
56         job.schedule();
57     }
58 }