]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.g2d/src/org/simantics/g2d/gallery/GalleryItemSGNode.java
Fixed invalid comparisons which were identified by Eclipse IDE 2018-09
[simantics/platform.git] / bundles / org.simantics.g2d / src / org / simantics / g2d / gallery / GalleryItemSGNode.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.gallery;
13
14 import java.awt.Font;
15 import java.awt.Point;
16 import java.awt.geom.AffineTransform;
17 import java.awt.geom.Rectangle2D;
18
19 import org.simantics.g2d.diagram.IDiagram;
20 import org.simantics.g2d.element.ElementUtils;
21 import org.simantics.g2d.element.IElement;
22 import org.simantics.g2d.element.SceneGraphNodeKey;
23 import org.simantics.g2d.element.ElementClass.Single;
24 import org.simantics.g2d.element.handler.LifeCycle;
25 import org.simantics.g2d.element.handler.SceneGraph;
26 import org.simantics.g2d.element.handler.Transform;
27 import org.simantics.g2d.image.Image;
28 import org.simantics.scenegraph.Node;
29 import org.simantics.scenegraph.g2d.G2DParentNode;
30 import org.simantics.scenegraph.g2d.nodes.GalleryItemNode;
31 import org.simantics.utils.datastructures.hints.IHintContext.Key;
32 import org.simantics.utils.datastructures.hints.IHintContext.KeyOf;
33
34 @Single
35 public class GalleryItemSGNode implements SceneGraph, LifeCycle {
36
37     private static final long serialVersionUID = 6625834518019163965L;
38
39     public final static Key   KEY_IMAGE                 = new KeyOf(Image.class, "GALLERY_IMAGE");
40     public final static Key   KEY_IMAGE_SHADOW          = new KeyOf(Image.class, "GALLERY_IMAGE_SHADOW");
41     public final static Key   KEY_TARGET_IMAGE_SIZE     = new KeyOf(Point.class, "GALLERY_TARGET_IMAGE_SIZE");
42
43     public final static Point DEFAULT_TARGET_IMAGE_SIZE = new Point(50, 50);
44
45     public static final Key   KEY_GALLERY_ITEM_NODE     = new SceneGraphNodeKey(GalleryItemNode.class, "GALLERY_ITEM_NODE");
46
47     private Font              font;
48
49     public static Image getImage(IElement e) {
50         return e.getHint(KEY_IMAGE);
51     }
52
53     public GalleryItemSGNode(Font font) {
54         this.font = font;
55     }
56
57     public void update(IElement e) {
58         GalleryItemNode node = (GalleryItemNode) e.getHint(KEY_GALLERY_ITEM_NODE);
59         if (node != null) {
60             Rectangle2D bounds = ElementUtils.getElementBounds(e);
61             String str = ElementUtils.getText(e);
62             Image img = e.getHint(KEY_IMAGE);
63
64             Rectangle2D imageBounds = img.getBounds();
65             node.init(str, bounds, font, imageBounds);
66
67             AffineTransform at = e.getElementClass().getSingleItem(Transform.class).getTransform(e);
68             node.setTransform((AffineTransform) at.clone());
69
70             img.init(node);
71         }
72     }
73
74     public void setFont(Font font) {
75         this.font = font;
76     }
77
78     @Override
79     public void init(final IElement e, G2DParentNode parent) {
80         GalleryItemNode node = (GalleryItemNode) e.getHint(KEY_GALLERY_ITEM_NODE);
81         if (node == null) {
82             node = parent.addNode(GalleryItemNode.class);
83             e.setHint(KEY_GALLERY_ITEM_NODE, node);
84         }
85         update(e);
86     }
87
88     @Override
89     public void cleanup(IElement e) {
90         Node node = e.removeHint(KEY_GALLERY_ITEM_NODE);
91         if (node != null)
92             node.remove();
93     }
94
95     @Override
96     public void onElementActivated(IDiagram d, IElement e) {
97     }
98
99     @Override
100     public void onElementCreated(IElement e) {
101     }
102
103     @Override
104     public void onElementDeactivated(IDiagram d, IElement e) {
105     }
106
107     @Override
108     public void onElementDestroyed(IElement e) {
109         cleanup(e);
110     }
111
112 }