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