]> gerrit.simantics Code Review - simantics/platform.git/blob
befc86cc050001ecad262e4541d8fc8371ca7948
[simantics/platform.git] /
1 /*******************************************************************************
2  * Copyright (c) 2012 Association for Decentralized Information Management in
3  * 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.template2d.ui.actions;
13
14 import java.awt.Color;
15
16 import org.eclipse.swt.graphics.RGB;
17 import org.eclipse.swt.widgets.ColorDialog;
18 import org.eclipse.swt.widgets.Shell;
19 import org.eclipse.ui.IWorkbenchWindow;
20 import org.eclipse.ui.PlatformUI;
21 import org.simantics.db.Resource;
22 import org.simantics.db.WriteGraph;
23 import org.simantics.db.common.request.WriteRequest;
24 import org.simantics.db.exception.DatabaseException;
25 import org.simantics.db.layer0.adapter.ActionFactory;
26 import org.simantics.diagram.G2DUtils;
27 import org.simantics.diagram.stubs.G2DResource;
28 import org.simantics.modeling.template2d.ontology.Template2dResource;
29 import org.simantics.ui.SimanticsUI;
30
31 public class SetBorderColor implements ActionFactory {
32
33     @Override
34     public Runnable create(Object target) {
35         if(!(target instanceof Resource))
36             return null;
37         final Resource parent = (Resource)target;
38         
39         return new Runnable() {
40             @Override
41             public void run() {
42                 // PlatformUI.getWorkbench().getDisplay().getActiveShell()
43                 IWorkbenchWindow  win = PlatformUI.getWorkbench().getActiveWorkbenchWindow(); //.getDisplay().getActiveShell()
44                 //Shell shell = PlatformUI.getWorkbench().getDisplay().getActiveShell();
45                 if (win == null)
46                         return;
47                 
48                 Shell shell = win.getShell();
49                 if (shell == null)
50                         return;
51                 
52                 ColorDialog dia = new ColorDialog(shell); //.get Display.getDefault().getActiveShell()
53                 final RGB rgb = dia.open();
54                 if (rgb == null)
55                         return;
56
57
58                 SimanticsUI.getSession().asyncRequest(new WriteRequest() {
59                     @Override
60                     public void perform(WriteGraph g) throws DatabaseException {
61                         g.markUndoPoint();
62                         
63 //                        Layer0 L0 = Layer0.getInstance(g);
64                         Template2dResource TEMPLATE2D = Template2dResource.getInstance(g);
65                         G2DResource G2D = G2DResource.getInstance(g);
66                         
67                         if (!g.isInstanceOf(parent, TEMPLATE2D.Border))
68                                 return;
69                         
70                         Resource oldColor = g.getPossibleObject(parent, G2D.HasColor);
71                         Resource color = G2DUtils.createColor(g, new Color(rgb.red, rgb.green, rgb.blue));
72                         if (oldColor != null)
73                                 g.deny(oldColor);
74                         g.claim(parent, G2D.HasColor, color);
75                     }
76                 });
77             }
78         };
79     }
80 }