]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.g2d/src/org/simantics/g2d/participant/BackgroundPainter.java
Migrated source code from Simantics SVN
[simantics/platform.git] / bundles / org.simantics.g2d / src / org / simantics / g2d / participant / BackgroundPainter.java
1 /*******************************************************************************\r
2  * Copyright (c) 2007, 2010 Association for Decentralized Information Management\r
3  * in 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.g2d.participant;\r
13 \r
14 import java.awt.Color;\r
15 import java.awt.Paint;\r
16 \r
17 import org.simantics.g2d.canvas.Hints;\r
18 import org.simantics.g2d.canvas.SGDesignation;\r
19 import org.simantics.g2d.canvas.impl.AbstractCanvasParticipant;\r
20 import org.simantics.g2d.canvas.impl.HintReflection.HintListener;\r
21 import org.simantics.g2d.canvas.impl.SGNodeReflection.SGCleanup;\r
22 import org.simantics.g2d.canvas.impl.SGNodeReflection.SGInit;\r
23 import org.simantics.scenegraph.g2d.G2DParentNode;\r
24 import org.simantics.scenegraph.g2d.nodes.BackgroundNode;\r
25 import org.simantics.utils.datastructures.hints.IHintContext.Key;\r
26 import org.simantics.utils.datastructures.hints.IHintObservable;\r
27 \r
28 /**\r
29  * Paints background wither with KEY_BACKGROUND_COLOR or KEY_BACKGROUND_PAINT.\r
30  * \r
31  * @see Hints#KEY_BACKGROUND_COLOR\r
32  * @see Hints#KEY_BACKGROUND_PAINT\r
33  * @author Toni Kalajainen\r
34  */\r
35 public class BackgroundPainter extends AbstractCanvasParticipant {\r
36 \r
37     protected BackgroundNode node = null;\r
38 \r
39     @HintListener(Class=Hints.class, Field="KEY_BACKGROUND_COLOR")\r
40     public void colorChanged(IHintObservable sender, Key key, Object oldValue, Object newValue) {\r
41         if (node != null) {\r
42             Color c = getHint(Hints.KEY_BACKGROUND_COLOR);\r
43             node.setColor(c);\r
44             setDirty();\r
45         }\r
46     }\r
47 \r
48     @HintListener(Class=Hints.class, Field="KEY_BACKGROUND_PAINT")\r
49     public void paintChanged(IHintObservable sender, Key key, Object oldValue, Object newValue) {\r
50         if (node != null) {\r
51             Paint p = getHint(Hints.KEY_BACKGROUND_PAINT);\r
52             node.setPaint(p);\r
53             setDirty();\r
54         }\r
55     }\r
56 \r
57     @SGInit(designation = SGDesignation.CONTROL)\r
58     public void initSG(G2DParentNode parent) {\r
59         node = parent.addNode("background", BackgroundNode.class);\r
60         node.setZIndex(Integer.MIN_VALUE / 2);\r
61 \r
62         Color c = getHint(Hints.KEY_BACKGROUND_COLOR);\r
63         Paint p = getHint(Hints.KEY_BACKGROUND_PAINT);\r
64         node.setColor(c);\r
65         node.setPaint(p);\r
66     }\r
67 \r
68     @SGCleanup\r
69     public void cleanupSG() {\r
70         if (node != null) {\r
71             node.remove();\r
72             node = null;\r
73         }\r
74     }\r
75 \r
76 }