package org.simantics.district.network.ui.contributions; import java.util.List; import javax.inject.Named; import org.eclipse.core.runtime.IProgressMonitor; import org.eclipse.core.runtime.IStatus; import org.eclipse.core.runtime.Status; import org.eclipse.core.runtime.jobs.Job; 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.DatabaseJob; import org.simantics.Simantics; import org.simantics.db.Resource; import org.simantics.db.WriteGraph; import org.simantics.db.common.request.WriteRequest; import org.simantics.db.exception.DatabaseException; import org.simantics.db.layer0.SelectionHints; import org.simantics.district.network.DistrictNetworkUtil; import org.simantics.district.network.ui.internal.Activator; import org.simantics.utils.ui.ISelectionUtils; public class DrawMapToggleHandler { @CanExecute public boolean canExecute(@Named(IServiceConstants.ACTIVE_SELECTION) ISelection selection) { final List diagrams = ISelectionUtils.getPossibleKeys(selection, SelectionHints.KEY_MAIN, Resource.class); return diagrams.size() > 0; } @Execute public void execute(@Named(IServiceConstants.ACTIVE_SELECTION) Object selection) { final List diagrams = ISelectionUtils.getPossibleKeys(selection, SelectionHints.KEY_MAIN, Resource.class); Job job = new DatabaseJob("Toggle draw map") { @Override protected IStatus run(IProgressMonitor monitor) { try { Simantics.getSession().syncRequest(new WriteRequest() { @Override public void perform(WriteGraph graph) throws DatabaseException { DistrictNetworkUtil.toggleDrawMap(graph, diagrams.get(0)); } }); } catch (DatabaseException e) { return new Status(IStatus.ERROR, Activator.PLUGIN_ID, getName() + " failed.", e); } return Status.OK_STATUS; } }; job.setUser(true); job.schedule(); } }