package org.simantics.district.route.ui.handlers;
+import java.util.Collections;
import java.util.List;
import java.util.concurrent.CompletableFuture;
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.model.application.ui.basic.MPart;
import org.eclipse.e4.ui.services.IServiceConstants;
import org.eclipse.jface.viewers.ISelection;
+import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
-import org.simantics.db.layer0.variable.Variable;
+import org.simantics.Simantics;
+import org.simantics.db.Resource;
+import org.simantics.db.exception.DatabaseException;
+import org.simantics.district.network.ui.DistrictNetworkUIUtil;
import org.simantics.district.route.Route;
import org.simantics.district.route.RouteJob;
import org.simantics.district.route.RouterConfiguration;
import org.simantics.utils.ui.ISelectionUtils;
+import org.simantics.utils.ui.SWTUtils;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
/**
* @author Tuukka Lehtonen
*/
public class SelectRouteOnDiagram {
+ private static final Logger LOGGER = LoggerFactory.getLogger(SelectRouteOnDiagram.class);
+
+ @CanExecute
+ public boolean canExecute(@Named(IServiceConstants.ACTIVE_SELECTION) ISelection selection) {
+ return ISelectionUtils.filterSingleSelection(selection, Route.class) != null;
+ }
+
@Execute
public void execute(@Named(IServiceConstants.ACTIVE_SHELL) Shell activeShell,
@Named(IServiceConstants.ACTIVE_PART) MPart part,
if (route == null)
return;
- CompletableFuture<List<Variable>> result = new CompletableFuture<>();
+ Display display = activeShell.getDisplay();
+ CompletableFuture<List<Resource>> result = new CompletableFuture<>();
+ result
+ .exceptionally(e -> Collections.emptyList())
+ .thenAccept(dnElements -> {
+ if (!dnElements.isEmpty()) {
+ try {
+ openDiagram(display, dnElements);
+ } catch (DatabaseException e) {
+ LOGGER.error("Failed to open district network diagram with selection {}", dnElements); //$NON-NLS-1$
+ }
+ }
+ });
+
RouterConfiguration config = new RouterConfiguration();
new RouteJob(config, route, result).schedule();
}
+ public static boolean openDiagram(Display display, List<Resource> dnElements) throws DatabaseException {
+ if (dnElements.isEmpty())
+ return false;
+
+ DistrictNetworkUIUtil.Input in = Simantics.getSession().syncRequest(
+ new DistrictNetworkUIUtil.ElementToInput(dnElements.get(0)));
+ if (in == null)
+ return false;
+
+ SWTUtils.asyncExec(display,
+ () -> DistrictNetworkUIUtil.openDNDiagramEditorWithSelection(in, dnElements.toArray()));
+ return true;
+ }
+
}
\ No newline at end of file