]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.modeling.ui/src/org/simantics/modeling/ui/actions/e4/EditStyleHandler.java
(refs #7571) Fixed incorrect handling of effects in edo
[simantics/platform.git] / bundles / org.simantics.modeling.ui / src / org / simantics / modeling / ui / actions / e4 / EditStyleHandler.java
1 /*******************************************************************************
2  * Copyright (c) 2007, 2010 Association for Decentralized Information Management
3  * in Industry THTH ry.
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
8  *
9  * Contributors:
10  *     VTT Technical Research Centre of Finland - initial API and implementation
11  *******************************************************************************/
12 package org.simantics.modeling.ui.actions.e4;
13
14 import java.util.Arrays;
15 import java.util.List;
16
17 import javax.inject.Named;
18
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;
37
38 /**
39  * Style Edit
40  * 
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
44  * 
45  * @author Marko Luukkainen <marko.luukkainen@vtt.fi>
46  *
47  */
48 public class EditStyleHandler {
49
50     private Resource[] lastResources = Resource.NONE;
51     private boolean cachedCanExecute;
52
53     @CanExecute
54     public boolean canExecute(@Named(IServiceConstants.ACTIVE_SELECTION) ISelection s, @Active MPart part) throws DatabaseException, InterruptedException {
55         if (!(part.getObject() instanceof CompatibilityEditor))
56             return false;
57         CompatibilityEditor editor = (CompatibilityEditor) part.getObject();
58         if (!(editor.getPart() instanceof DiagramEditor))
59             return false;
60         if (s == null)
61             return false;
62
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);
68             }
69             return cachedCanExecute;
70         }
71         return false;
72     }
73
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,
79                 false,
80                 new StyleProviderCheck(Arrays.asList(resources)));
81     }
82
83     private static class StyleProviderCheck extends UnaryRead<List<Resource>, Boolean> {
84
85         public StyleProviderCheck(List<Resource> parameter) {
86             super(parameter);
87         }
88
89         @Override
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))
95                     return false;
96             return true;
97         }
98
99     }
100
101     @Execute
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);
106     }
107
108 }