--- /dev/null
+/*******************************************************************************\r
+ * Copyright (c) 2007, 2010 Association for Decentralized Information Management\r
+ * in Industry THTH ry.\r
+ * All rights reserved. This program and the accompanying materials\r
+ * are made available under the terms of the Eclipse Public License v1.0\r
+ * which accompanies this distribution, and is available at\r
+ * http://www.eclipse.org/legal/epl-v10.html\r
+ *\r
+ * Contributors:\r
+ * VTT Technical Research Centre of Finland - initial API and implementation\r
+ *******************************************************************************/\r
+package org.simantics.modeling.ui.actions.e4;\r
+\r
+import java.util.Arrays;\r
+import java.util.List;\r
+\r
+import javax.inject.Named;\r
+\r
+import org.eclipse.e4.core.contexts.Active;\r
+import org.eclipse.e4.core.di.annotations.CanExecute;\r
+import org.eclipse.e4.core.di.annotations.Execute;\r
+import org.eclipse.e4.ui.model.application.ui.basic.MPart;\r
+import org.eclipse.e4.ui.services.IServiceConstants;\r
+import org.eclipse.jface.viewers.ISelection;\r
+import org.eclipse.ui.internal.e4.compatibility.CompatibilityEditor;\r
+import org.simantics.Simantics;\r
+import org.simantics.db.ReadGraph;\r
+import org.simantics.db.Resource;\r
+import org.simantics.db.common.request.UnaryRead;\r
+import org.simantics.db.common.utils.RequestUtil;\r
+import org.simantics.db.exception.DatabaseException;\r
+import org.simantics.diagram.stubs.DiagramResource;\r
+import org.simantics.modeling.ui.actions.style.EditStyle;\r
+import org.simantics.modeling.ui.diagramEditor.DiagramEditor;\r
+import org.simantics.ui.SimanticsUI;\r
+import org.simantics.ui.utils.ResourceAdaptionUtils;\r
+\r
+/**\r
+ * Style Edit\r
+ * \r
+ * TODO : should we have extension point for expanding styles?\r
+ * TODO : default ColorChooser is not suitable for this task\r
+ * TODO : how to store MetricsFormat template list\r
+ * \r
+ * @author Marko Luukkainen <marko.luukkainen@vtt.fi>\r
+ *\r
+ */\r
+public class EditStyleHandler {\r
+\r
+ private Resource[] lastResources = Resource.NONE;\r
+ private boolean cachedCanExecute;\r
+\r
+ @CanExecute\r
+ public boolean canExecute(@Named(IServiceConstants.ACTIVE_SELECTION) ISelection s, @Active MPart part) throws DatabaseException, InterruptedException {\r
+ if (!(part.getObject() instanceof CompatibilityEditor))\r
+ return false;\r
+ CompatibilityEditor editor = (CompatibilityEditor) part.getObject();\r
+ if (!(editor.getPart() instanceof DiagramEditor))\r
+ return false;\r
+ if (s == null)\r
+ return false;\r
+\r
+ final Resource[] resources = ResourceAdaptionUtils.toResources(s);\r
+ if (resources.length != 0) {\r
+ if (!Arrays.equals(resources, lastResources)) {\r
+ lastResources = resources;\r
+ cachedCanExecute = areStyleProviders(resources);\r
+ }\r
+ return cachedCanExecute;\r
+ }\r
+ return false;\r
+ }\r
+\r
+ private static boolean areStyleProviders(Resource[] resources) throws DatabaseException, InterruptedException {\r
+ return RequestUtil.trySyncRequest(\r
+ Simantics.getSession(),\r
+ SimanticsUI.UI_THREAD_REQUEST_START_TIMEOUT,\r
+ SimanticsUI.UI_THREAD_REQUEST_EXECUTION_TIMEOUT,\r
+ false,\r
+ new StyleProviderCheck(Arrays.asList(resources)));\r
+ }\r
+\r
+ private static class StyleProviderCheck extends UnaryRead<List<Resource>, Boolean> {\r
+\r
+ public StyleProviderCheck(List<Resource> parameter) {\r
+ super(parameter);\r
+ }\r
+\r
+ @Override\r
+ public Boolean perform(ReadGraph graph) throws DatabaseException {\r
+ DiagramResource DIA = DiagramResource.getInstance(graph);\r
+ // Find what kind of styles selected objects support\r
+ for (Resource r : parameter)\r
+ if (!graph.isInstanceOf(r, DIA.StyleProvider))\r
+ return false;\r
+ return true;\r
+ }\r
+\r
+ }\r
+\r
+ @Execute\r
+ public void execute(@Named(IServiceConstants.ACTIVE_SELECTION) ISelection s) {\r
+ final Resource[] resources = ResourceAdaptionUtils.toResources(s);\r
+ if (resources.length != 0)\r
+ EditStyle.openStyleDialog(resources);\r
+ }\r
+\r
+}\r