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
10 * VTT Technical Research Centre of Finland - initial API and implementation
\r
11 *******************************************************************************/
\r
12 package org.simantics.g2d.gallery;
\r
14 import java.awt.Font;
\r
15 import java.awt.Point;
\r
16 import java.awt.geom.AffineTransform;
\r
17 import java.awt.geom.Rectangle2D;
\r
19 import org.simantics.g2d.diagram.IDiagram;
\r
20 import org.simantics.g2d.element.ElementUtils;
\r
21 import org.simantics.g2d.element.IElement;
\r
22 import org.simantics.g2d.element.SceneGraphNodeKey;
\r
23 import org.simantics.g2d.element.ElementClass.Single;
\r
24 import org.simantics.g2d.element.handler.LifeCycle;
\r
25 import org.simantics.g2d.element.handler.SceneGraph;
\r
26 import org.simantics.g2d.element.handler.Transform;
\r
27 import org.simantics.g2d.image.Image;
\r
28 import org.simantics.scenegraph.Node;
\r
29 import org.simantics.scenegraph.g2d.G2DParentNode;
\r
30 import org.simantics.scenegraph.g2d.nodes.GalleryItemNode;
\r
31 import org.simantics.utils.datastructures.hints.IHintContext.Key;
\r
32 import org.simantics.utils.datastructures.hints.IHintContext.KeyOf;
\r
35 public class GalleryItemSGNode implements SceneGraph, LifeCycle {
\r
37 private static final long serialVersionUID = 6625834518019163965L;
\r
39 public final static Key KEY_IMAGE = new KeyOf(Image.class, "GALLERY_IMAGE");
\r
40 public final static Key KEY_IMAGE_SHADOW = new KeyOf(Image.class, "GALLERY_IMAGE_SHADOW");
\r
41 public final static Key KEY_TARGET_IMAGE_SIZE = new KeyOf(Point.class, "GALLERY_TARGET_IMAGE_SIZE");
\r
43 public final static Point DEFAULT_TARGET_IMAGE_SIZE = new Point(50, 50);
\r
45 public static final Key KEY_GALLERY_ITEM_NODE = new SceneGraphNodeKey(GalleryItemNode.class, "GALLERY_ITEM_NODE");
\r
49 public static Image getImage(IElement e) {
\r
50 return e.getHint(KEY_IMAGE);
\r
53 public GalleryItemSGNode(Font font) {
\r
57 public void update(IElement e) {
\r
58 GalleryItemNode node = (GalleryItemNode) e.getHint(KEY_GALLERY_ITEM_NODE);
\r
60 Rectangle2D bounds = ElementUtils.getElementBounds(e);
\r
61 String str = ElementUtils.getText(e);
\r
62 Image img = e.getHint(KEY_IMAGE);
\r
64 Rectangle2D imageBounds = img.getBounds();
\r
65 node.init(str, bounds, font, imageBounds);
\r
67 AffineTransform at = e.getElementClass().getSingleItem(Transform.class).getTransform(e);
\r
68 node.setTransform((AffineTransform) at.clone());
\r
74 public void setFont(Font font) {
\r
79 public void init(final IElement e, G2DParentNode parent) {
\r
80 GalleryItemNode node = (GalleryItemNode) e.getHint(KEY_GALLERY_ITEM_NODE);
\r
82 node = parent.addNode(GalleryItemNode.class);
\r
83 e.setHint(KEY_GALLERY_ITEM_NODE, node);
\r
89 public void cleanup(IElement e) {
\r
90 Node node = e.removeHint(KEY_GALLERY_ITEM_NODE);
\r
96 public void onElementActivated(IDiagram d, IElement e) {
\r
100 public void onElementCreated(IElement e) {
\r
104 public void onElementDeactivated(IDiagram d, IElement e) {
\r
108 public void onElementDestroyed(IElement e) {
\r