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.utils.page;
17 * @See {@link RulerPainter} RulerPainter.RULER_MARGIN5
18 * @author Toni Kalajainen
20 public class MarginUtils {
22 public static Margin marginOf(double controlRelative, double controlAbsolute, double diagramAbsolute) {
23 return new Margin(controlRelative/100, controlAbsolute, diagramAbsolute);
27 public static final Margin NO_MARGIN = marginOf(0, 0, 0);
29 public static final Margin MARGIN5 = marginOf(5, 0, 0);
31 public static final Margin MARGIN2 = marginOf(2, 0, 0);
33 public static final Margin MARGIN_10mm = marginOf(0, 0, 10);
37 public static final Margins NO_MARGINS = new Margins(NO_MARGIN, NO_MARGIN, NO_MARGIN, NO_MARGIN);
39 public static final Margins MARGINS5 = new Margins(MARGIN5, MARGIN5, MARGIN5, MARGIN5);
41 public static final Margins MARGINS2 = new Margins(MARGIN2, MARGIN2, MARGIN2, MARGIN2);
43 public static final Margins MARGINS_10mm = new Margins(MARGIN_10mm, MARGIN_10mm, MARGIN_10mm, MARGIN_10mm);
48 public static class Margin {
49 /** Relative margins 0..100 (in control coordinates) */
50 public final double controlRelative;
51 /** Margins in absolute values of control units */
52 public final double controlAbsolute;
53 /** Margins in absolute values of diaram units*/
54 public final double diagramAbsolute;
56 public Margin(double controlRelative, double controlAbsolute, double diagramAbsolute) {
57 this.controlRelative = controlRelative;
58 this.controlAbsolute = controlAbsolute;
59 this.diagramAbsolute = diagramAbsolute;
63 public String toString() {
64 return "(" + controlRelative + " " + controlAbsolute + " " + diagramAbsolute + ")";
67 public boolean isZero() {
68 return Math.abs(controlRelative) < Double.MIN_VALUE
69 && Math.abs(controlAbsolute) < Double.MIN_VALUE
70 && Math.abs(diagramAbsolute) < Double.MIN_VALUE;
74 public int hashCode() {
78 temp = Double.doubleToLongBits(controlAbsolute);
79 result = prime * result + (int) (temp ^ (temp >>> 32));
80 temp = Double.doubleToLongBits(controlRelative);
81 result = prime * result + (int) (temp ^ (temp >>> 32));
82 temp = Double.doubleToLongBits(diagramAbsolute);
83 result = prime * result + (int) (temp ^ (temp >>> 32));
88 public boolean equals(Object obj) {
93 if (getClass() != obj.getClass())
95 Margin other = (Margin) obj;
96 if (Double.doubleToLongBits(controlAbsolute) != Double.doubleToLongBits(other.controlAbsolute))
98 if (Double.doubleToLongBits(controlRelative) != Double.doubleToLongBits(other.controlRelative))
100 if (Double.doubleToLongBits(diagramAbsolute) != Double.doubleToLongBits(other.diagramAbsolute))
107 * 4 margins: top, bottom, left, right
109 public static class Margins {
111 public static final int TOP = 1 << 0;
112 public static final int BOTTOM = 1 << 1;
113 public static final int LEFT = 1 << 2;
114 public static final int RIGHT = 1 << 3;
116 public final Margin top;
117 public final Margin bottom;
118 public final Margin left;
119 public final Margin right;
121 public Margins(Margin top, Margin bottom, Margin left, Margin right) {
123 assert bottom != null;
125 assert right != null;
128 this.bottom = bottom;
134 * @param a bitmask consisting of the values {@link #TOP},
135 * {@link #BOTTOM}, {@link #LEFT} and {@link #RIGHT}
139 public Margins withSide(int side, Margin m) {
140 Margin t = ((side & TOP) != 0) ? m : top;
141 Margin b = ((side & BOTTOM) != 0) ? m : bottom;
142 Margin l = ((side & LEFT) != 0) ? m : left;
143 Margin r = ((side & RIGHT) != 0) ? m : right;
144 return new Margins(t, b, l, r);
147 public boolean isZero() {
148 return top.isZero() && bottom.isZero() && left.isZero() && right.isZero();
152 public String toString() {
153 return "[top=" + top + ", bottom=" + bottom + ", left=" + left + ", right=" + right + "]";
157 public int hashCode() {
158 final int prime = 31;
160 result = prime * result + bottom.hashCode();
161 result = prime * result + left.hashCode();
162 result = prime * result + right.hashCode();
163 result = prime * result + top.hashCode();
168 public boolean equals(Object obj) {
173 if (getClass() != obj.getClass())
175 Margins other = (Margins) obj;
176 if (!top.equals(other.top))
178 if (!bottom.equals(other.bottom))
180 if (!left.equals(other.left))
182 if (!right.equals(other.right))