]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.modeling.template2d.ui/src/org/simantics/modeling/template2d/ui/actions/SetBorderColor.java
Migrated source code from Simantics SVN
[simantics/platform.git] / bundles / org.simantics.modeling.template2d.ui / src / org / simantics / modeling / template2d / ui / actions / SetBorderColor.java
1 /*******************************************************************************\r
2  * Copyright (c) 2012 Association for Decentralized Information Management in\r
3  * Industry THTH ry.\r
4  * All rights reserved. This program and the accompanying materials\r
5  * are made available under the terms of the Eclipse Public License v1.0\r
6  * which accompanies this distribution, and is available at\r
7  * http://www.eclipse.org/legal/epl-v10.html\r
8  *\r
9  * Contributors:\r
10  *     VTT Technical Research Centre of Finland - initial API and implementation\r
11  *******************************************************************************/\r
12 package org.simantics.modeling.template2d.ui.actions;\r
13 \r
14 import java.awt.Color;\r
15 \r
16 import org.eclipse.swt.graphics.RGB;\r
17 import org.eclipse.swt.widgets.ColorDialog;\r
18 import org.eclipse.swt.widgets.Shell;\r
19 import org.eclipse.ui.IWorkbenchWindow;\r
20 import org.eclipse.ui.PlatformUI;\r
21 import org.simantics.db.Resource;\r
22 import org.simantics.db.WriteGraph;\r
23 import org.simantics.db.common.request.WriteRequest;\r
24 import org.simantics.db.exception.DatabaseException;\r
25 import org.simantics.db.layer0.adapter.ActionFactory;\r
26 import org.simantics.diagram.G2DUtils;\r
27 import org.simantics.diagram.stubs.G2DResource;\r
28 import org.simantics.modeling.template2d.ontology.Template2dResource;\r
29 import org.simantics.ui.SimanticsUI;\r
30 \r
31 public class SetBorderColor implements ActionFactory {\r
32 \r
33     @Override\r
34     public Runnable create(Object target) {\r
35         if(!(target instanceof Resource))\r
36             return null;\r
37         final Resource parent = (Resource)target;\r
38         \r
39         return new Runnable() {\r
40             @Override\r
41             public void run() {\r
42                 // PlatformUI.getWorkbench().getDisplay().getActiveShell()\r
43                 IWorkbenchWindow  win = PlatformUI.getWorkbench().getActiveWorkbenchWindow(); //.getDisplay().getActiveShell()\r
44                 //Shell shell = PlatformUI.getWorkbench().getDisplay().getActiveShell();\r
45                 if (win == null)\r
46                         return;\r
47                 \r
48                 Shell shell = win.getShell();\r
49                 if (shell == null)\r
50                         return;\r
51                 \r
52                 ColorDialog dia = new ColorDialog(shell); //.get Display.getDefault().getActiveShell()\r
53                 final RGB rgb = dia.open();\r
54                 if (rgb == null)\r
55                         return;\r
56 \r
57 \r
58                 SimanticsUI.getSession().asyncRequest(new WriteRequest() {\r
59                     @Override\r
60                     public void perform(WriteGraph g) throws DatabaseException {\r
61                         g.markUndoPoint();\r
62                         \r
63 //                        Layer0 L0 = Layer0.getInstance(g);\r
64                         Template2dResource TEMPLATE2D = Template2dResource.getInstance(g);\r
65                         G2DResource G2D = G2DResource.getInstance(g);\r
66                         \r
67                         if (!g.isInstanceOf(parent, TEMPLATE2D.Border))\r
68                                 return;\r
69                         \r
70                         Resource oldColor = g.getPossibleObject(parent, G2D.HasColor);\r
71                         Resource color = G2DUtils.createColor(g, new Color(rgb.red, rgb.green, rgb.blue));\r
72                         if (oldColor != null)\r
73                                 g.deny(oldColor);\r
74                         g.claim(parent, G2D.HasColor, color);\r
75                     }\r
76                 });\r
77             }\r
78         };\r
79     }\r
80 }\r