]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.g2d/src/org/simantics/g2d/participant/PageBorderParticipant.java
Fixed invalid comparisons which were identified by Eclipse IDE 2018-09
[simantics/platform.git] / bundles / org.simantics.g2d / src / org / simantics / g2d / participant / PageBorderParticipant.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.participant;
13
14 import java.awt.geom.Rectangle2D;
15
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;
29
30 /**
31  * Paints borders or a page according to Page
32  *
33  * @author Tuukka Lehtonen
34  */
35 public class PageBorderParticipant extends AbstractCanvasParticipant {
36
37     IHintListener hintListener = new HintListenerAdapter() {
38         @Override
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);
42                 updateNode(true);
43             } else if (key == Hints.KEY_DISPLAY_PAGE) {
44                 //System.out.println("DISPLAY PAGE CHANGED: " + oldValue + " TO " + newValue);
45                 updateNode(true);
46             } else if (key == Hints.KEY_DISPLAY_MARGINS) {
47                 //System.out.println("DISPLAY MARGINS CHANGED: " + oldValue + " TO " + newValue);
48                 updateNode(true);
49             } else if (key == Hints.KEY_DISABLE_PAINTING) {
50                 updateNode(true);
51             }
52         }
53     };
54
55     @Override
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);
62     }
63
64     @Override
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);
71     }
72
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));
77
78         Rectangle2D border = new Rectangle2D.Double();
79         Rectangle2D margin = new Rectangle2D.Double();
80
81         PageDesc desc = getHint(Hints.KEY_PAGE_DESC);
82         //System.out.println("updateNode: " + desc + ", " + displayPage + ", " + displayMargins);
83         if (desc != null) {
84             if (displayPage)
85                 desc.getPageRectangle(border);
86
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);
96             }
97         }
98
99         node.init(border, margin, enabled);
100
101         if (markDirty)
102             setDirty();
103     }
104
105     protected PageBorderNode node = null;
106
107     @SGInit
108     public void initSG(G2DParentNode parent) {
109         node = parent.addNode("page border", PageBorderNode.class);
110         node.setZIndex(-1000);
111         updateNode(false);
112     }
113
114     @SGCleanup
115     public void cleanupSG() {
116         node.remove();
117     }
118 }