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.participant;
14 import java.awt.geom.Rectangle2D;
16 import org.simantics.g2d.canvas.Hints;
17 import org.simantics.g2d.canvas.ICanvasContext;
18 import org.simantics.g2d.canvas.impl.AbstractCanvasParticipant;
19 import org.simantics.g2d.canvas.impl.SGNodeReflection.SGCleanup;
20 import org.simantics.g2d.canvas.impl.SGNodeReflection.SGInit;
21 import org.simantics.scenegraph.g2d.G2DParentNode;
22 import org.simantics.scenegraph.g2d.nodes.PageBorderNode;
23 import org.simantics.utils.datastructures.hints.HintListenerAdapter;
24 import org.simantics.utils.datastructures.hints.IHintContext.Key;
25 import org.simantics.utils.datastructures.hints.IHintListener;
26 import org.simantics.utils.datastructures.hints.IHintObservable;
27 import org.simantics.utils.page.MarginUtils.Margins;
28 import org.simantics.utils.page.PageDesc;
31 * Paints borders or a page according to Page
33 * @author Tuukka Lehtonen
35 public class PageBorderParticipant extends AbstractCanvasParticipant {
37 IHintListener hintListener = new HintListenerAdapter() {
39 public void hintChanged(IHintObservable sender, Key key, Object oldValue, Object newValue) {
40 if (key == Hints.KEY_PAGE_DESC) {
41 //System.out.println("PAGE DESC CHANGED FROM " + oldValue + " to " + newValue);
43 } else if (key == Hints.KEY_DISPLAY_PAGE) {
44 //System.out.println("DISPLAY PAGE CHANGED: " + oldValue + " TO " + newValue);
46 } else if (key == Hints.KEY_DISPLAY_MARGINS) {
47 //System.out.println("DISPLAY MARGINS CHANGED: " + oldValue + " TO " + newValue);
49 } else if (key == Hints.KEY_DISABLE_PAINTING) {
56 public void addedToContext(ICanvasContext ctx) {
57 super.addedToContext(ctx);
58 getHintStack().addKeyHintListener(getThread(), Hints.KEY_PAGE_DESC, hintListener);
59 getHintStack().addKeyHintListener(getThread(), Hints.KEY_DISPLAY_PAGE, hintListener);
60 getHintStack().addKeyHintListener(getThread(), Hints.KEY_DISPLAY_MARGINS, hintListener);
61 getHintStack().addKeyHintListener(getThread(), Hints.KEY_DISABLE_PAINTING, hintListener);
65 public void removedFromContext(ICanvasContext ctx) {
66 getHintStack().removeKeyHintListener(getThread(), Hints.KEY_PAGE_DESC, hintListener);
67 getHintStack().removeKeyHintListener(getThread(), Hints.KEY_DISPLAY_PAGE, hintListener);
68 getHintStack().removeKeyHintListener(getThread(), Hints.KEY_DISPLAY_MARGINS, hintListener);
69 getHintStack().removeKeyHintListener(getThread(), Hints.KEY_DISABLE_PAINTING, hintListener);
70 super.removedFromContext(ctx);
73 protected void updateNode(boolean markDirty) {
74 boolean displayPage = !Boolean.FALSE.equals(getHint(Hints.KEY_DISPLAY_PAGE));
75 boolean displayMargins = !Boolean.FALSE.equals(getHint(Hints.KEY_DISPLAY_MARGINS));
76 boolean enabled = !Boolean.TRUE.equals(getHint(Hints.KEY_DISABLE_PAINTING));
78 Rectangle2D border = new Rectangle2D.Double();
79 Rectangle2D margin = new Rectangle2D.Double();
81 PageDesc desc = getHint(Hints.KEY_PAGE_DESC);
82 //System.out.println("updateNode: " + desc + ", " + displayPage + ", " + displayMargins);
85 desc.getPageRectangle(border);
87 Margins margins = desc.getMargins();
88 boolean marginsZero = margins.isZero();
89 //System.out.println("Margins: " + margins + ": zero: " + marginsZero);
90 if (displayMargins && !marginsZero) {
91 double mlx = desc.getLeftEdgePos() + margins.left.diagramAbsolute;
92 double mty = desc.getTopEdgePos() + margins.top.diagramAbsolute;
93 double w = desc.getOrientedWidth() - (margins.left.diagramAbsolute + margins.right.diagramAbsolute);
94 double h = desc.getOrientedHeight() - (margins.top.diagramAbsolute + margins.bottom.diagramAbsolute);
95 margin.setFrame(mlx, mty, w, h);
99 node.init(border, margin, enabled);
105 protected PageBorderNode node = null;
108 public void initSG(G2DParentNode parent) {
109 node = parent.addNode("page border", PageBorderNode.class);
110 node.setZIndex(-1000);
115 public void cleanupSG() {