+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.dialogs.Dialog;
+import org.eclipse.jface.viewers.ISelection;
+import org.eclipse.ui.progress.UIJob;
+import org.simantics.DatabaseJob;
+import org.simantics.Simantics;
+import org.simantics.datatypes.literal.RGB;
+import org.simantics.db.Resource;
+import org.simantics.db.exception.DatabaseException;
+import org.simantics.db.layer0.SelectionHints;
+import org.simantics.db.request.Write;
+import org.simantics.district.network.DistrictNetworkUtil;
+import org.simantics.district.network.ui.internal.Activator;
+import org.simantics.utils.ui.ISelectionUtils;
+import org.simantics.utils.ui.color.Color;
+import org.simantics.utils.ui.workbench.dialogs.ColorDialog;
+import org.slf4j.LoggerFactory;
+
+public class ChangeMapBackgroundColorHandler {
+
+ private static final org.slf4j.Logger LOGGER = LoggerFactory.getLogger(ChangeMapBackgroundColorHandler.class);
+
+ @CanExecute
+ public boolean canExecute(@Named(IServiceConstants.ACTIVE_SELECTION) ISelection selection) {
+ final List<Resource> 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<Resource> diagrams = ISelectionUtils.getPossibleKeys(selection, SelectionHints.KEY_MAIN, Resource.class);
+ Job uijob = new UIJob("Change Map Background Color") {
+
+ @Override
+ public IStatus runInUIThread(IProgressMonitor monitor) {
+ ColorDialog dialog = new ColorDialog(getDisplay().getActiveShell());
+ if (dialog.open() == Dialog.OK) {
+ Color c = dialog.getColor();
+
+ LOGGER.info("Found color {}", c);
+ Job job = new DatabaseJob("Toggle draw map") {
+
+ @Override
+ protected IStatus run(IProgressMonitor monitor) {
+ try {
+ Simantics.getSession().syncRequest((Write) graph -> DistrictNetworkUtil.changeMapBackgroundColor(graph, diagrams.get(0), new RGB.Integer(c.getR(), c.getG(), c.getB())));
+ } catch (DatabaseException e) {
+ return new Status(IStatus.ERROR, Activator.PLUGIN_ID, getName() + " failed.", e);
+ }
+ return Status.OK_STATUS;
+ }
+ };
+ job.setUser(true);
+ job.schedule();
+ }
+ return Status.OK_STATUS;
+ }
+ };
+ uijob.setUser(true);
+ uijob.schedule();
+ }
+}