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