1 /*******************************************************************************
2 * Copyright (c) 2007, 2010 Association for Decentralized Information Management
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
10 * VTT Technical Research Centre of Finland - initial API and implementation
11 *******************************************************************************/
12 package org.simantics.g2d.gallery;
15 import java.awt.Point;
16 import java.awt.geom.AffineTransform;
17 import java.awt.geom.Rectangle2D;
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;
35 public class GalleryItemSGNode implements SceneGraph, LifeCycle {
37 private static final long serialVersionUID = 6625834518019163965L;
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");
43 public final static Point DEFAULT_TARGET_IMAGE_SIZE = new Point(50, 50);
45 public static final Key KEY_GALLERY_ITEM_NODE = new SceneGraphNodeKey(GalleryItemNode.class, "GALLERY_ITEM_NODE");
49 public static Image getImage(IElement e) {
50 return e.getHint(KEY_IMAGE);
53 public GalleryItemSGNode(Font font) {
57 public void update(IElement e) {
58 GalleryItemNode node = (GalleryItemNode) e.getHint(KEY_GALLERY_ITEM_NODE);
60 Rectangle2D bounds = ElementUtils.getElementBounds(e);
61 String str = ElementUtils.getText(e);
62 Image img = e.getHint(KEY_IMAGE);
64 Rectangle2D imageBounds = img.getBounds();
65 node.init(str, bounds, font, imageBounds);
67 AffineTransform at = e.getElementClass().getSingleItem(Transform.class).getTransform(e);
68 node.setTransform((AffineTransform) at.clone());
74 public void setFont(Font font) {
79 public void init(final IElement e, G2DParentNode parent) {
80 GalleryItemNode node = (GalleryItemNode) e.getHint(KEY_GALLERY_ITEM_NODE);
82 node = parent.addNode(GalleryItemNode.class);
83 e.setHint(KEY_GALLERY_ITEM_NODE, node);
89 public void cleanup(IElement e) {
90 Node node = e.removeHint(KEY_GALLERY_ITEM_NODE);
96 public void onElementActivated(IDiagram d, IElement e) {
100 public void onElementCreated(IElement e) {
104 public void onElementDeactivated(IDiagram d, IElement e) {
108 public void onElementDestroyed(IElement e) {