1 /*******************************************************************************
2 * Copyright (c) 2007, 2010 Association for Decentralized Information Management
4 * All rights reserved. This program and the accompanying materials
5 * are made available under the terms of the Eclipse Public License v1.0
6 * which accompanies this distribution, and is available at
7 * http://www.eclipse.org/legal/epl-v10.html
10 * VTT Technical Research Centre of Finland - initial API and implementation
11 *******************************************************************************/
12 package org.simantics.modeling.ui.actions.e4;
14 import java.util.Arrays;
15 import java.util.List;
17 import javax.inject.Named;
19 import org.eclipse.e4.core.contexts.Active;
20 import org.eclipse.e4.core.di.annotations.CanExecute;
21 import org.eclipse.e4.core.di.annotations.Execute;
22 import org.eclipse.e4.ui.model.application.ui.basic.MPart;
23 import org.eclipse.e4.ui.services.IServiceConstants;
24 import org.eclipse.jface.viewers.ISelection;
25 import org.eclipse.ui.internal.e4.compatibility.CompatibilityEditor;
26 import org.simantics.Simantics;
27 import org.simantics.db.ReadGraph;
28 import org.simantics.db.Resource;
29 import org.simantics.db.common.request.UnaryRead;
30 import org.simantics.db.common.utils.RequestUtil;
31 import org.simantics.db.exception.DatabaseException;
32 import org.simantics.diagram.stubs.DiagramResource;
33 import org.simantics.modeling.ui.actions.style.EditStyle;
34 import org.simantics.modeling.ui.diagramEditor.DiagramEditor;
35 import org.simantics.ui.SimanticsUI;
36 import org.simantics.ui.utils.ResourceAdaptionUtils;
41 * TODO : should we have extension point for expanding styles?
42 * TODO : default ColorChooser is not suitable for this task
43 * TODO : how to store MetricsFormat template list
45 * @author Marko Luukkainen <marko.luukkainen@vtt.fi>
48 public class EditStyleHandler {
50 private Resource[] lastResources = Resource.NONE;
51 private boolean cachedCanExecute;
54 public boolean canExecute(@Named(IServiceConstants.ACTIVE_SELECTION) ISelection s, @Active MPart part) throws DatabaseException, InterruptedException {
55 if (!(part.getObject() instanceof CompatibilityEditor))
57 CompatibilityEditor editor = (CompatibilityEditor) part.getObject();
58 if (!(editor.getPart() instanceof DiagramEditor))
63 final Resource[] resources = ResourceAdaptionUtils.toResources(s);
64 if (resources.length != 0) {
65 if (!Arrays.equals(resources, lastResources)) {
66 lastResources = resources;
67 cachedCanExecute = areStyleProviders(resources);
69 return cachedCanExecute;
74 private static boolean areStyleProviders(Resource[] resources) throws DatabaseException, InterruptedException {
75 return RequestUtil.trySyncRequest(
76 Simantics.getSession(),
77 SimanticsUI.UI_THREAD_REQUEST_START_TIMEOUT,
78 SimanticsUI.UI_THREAD_REQUEST_EXECUTION_TIMEOUT,
80 new StyleProviderCheck(Arrays.asList(resources)));
83 private static class StyleProviderCheck extends UnaryRead<List<Resource>, Boolean> {
85 public StyleProviderCheck(List<Resource> parameter) {
90 public Boolean perform(ReadGraph graph) throws DatabaseException {
91 DiagramResource DIA = DiagramResource.getInstance(graph);
92 // Find what kind of styles selected objects support
93 for (Resource r : parameter)
94 if (!graph.isInstanceOf(r, DIA.StyleProvider))
102 public void execute(@Named(IServiceConstants.ACTIVE_SELECTION) ISelection s) {
103 final Resource[] resources = ResourceAdaptionUtils.toResources(s);
104 if (resources.length != 0)
105 EditStyle.openStyleDialog(resources);