]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.utils/src/org/simantics/utils/page/MarginUtils.java
Fixed all line endings of the repository
[simantics/platform.git] / bundles / org.simantics.utils / src / org / simantics / utils / page / MarginUtils.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.utils.page;
13
14
15 /**
16  * 
17  * @See {@link RulerPainter} RulerPainter.RULER_MARGIN5
18  * @author Toni Kalajainen
19  */
20 public class MarginUtils {
21
22     public static Margin marginOf(double controlRelative, double controlAbsolute, double diagramAbsolute) {
23         return new Margin(controlRelative/100, controlAbsolute, diagramAbsolute);
24     }
25
26     /** No margin */
27     public static final Margin NO_MARGIN = marginOf(0, 0, 0);
28     /** 5% margin */
29     public static final Margin MARGIN5 = marginOf(5, 0, 0);
30     /** 2% margin */
31     public static final Margin MARGIN2 = marginOf(2, 0, 0);
32     /** 10mm margin */
33     public static final Margin MARGIN_10mm = marginOf(0, 0, 10);
34
35     
36     /** No margins */
37     public static final Margins NO_MARGINS = new Margins(NO_MARGIN, NO_MARGIN, NO_MARGIN, NO_MARGIN);
38     /** 5% margin */
39     public static final Margins MARGINS5 = new Margins(MARGIN5, MARGIN5, MARGIN5, MARGIN5);
40     /** 2% margin */
41     public static final Margins MARGINS2 = new Margins(MARGIN2, MARGIN2, MARGIN2, MARGIN2);
42     /** 10mm margin */
43     public static final Margins MARGINS_10mm = new Margins(MARGIN_10mm, MARGIN_10mm, MARGIN_10mm, MARGIN_10mm);
44
45     /**
46      * A margin.
47      */
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;
55
56         public Margin(double controlRelative, double controlAbsolute, double diagramAbsolute) {
57             this.controlRelative = controlRelative;
58             this.controlAbsolute = controlAbsolute;
59             this.diagramAbsolute = diagramAbsolute;
60         }
61
62         @Override
63         public String toString() {
64             return "(" + controlRelative + " " + controlAbsolute + " " + diagramAbsolute + ")";
65         }
66
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;
71         }
72
73         @Override
74         public int hashCode() {
75             final int prime = 31;
76             int result = 1;
77             long temp;
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));
84             return result;
85         }
86
87         @Override
88         public boolean equals(Object obj) {
89             if (this == obj)
90                 return true;
91             if (obj == null)
92                 return false;
93             if (getClass() != obj.getClass())
94                 return false;
95             Margin other = (Margin) obj;
96             if (Double.doubleToLongBits(controlAbsolute) != Double.doubleToLongBits(other.controlAbsolute))
97                 return false;
98             if (Double.doubleToLongBits(controlRelative) != Double.doubleToLongBits(other.controlRelative))
99                 return false;
100             if (Double.doubleToLongBits(diagramAbsolute) != Double.doubleToLongBits(other.diagramAbsolute))
101                 return false;
102             return true;
103         }
104     }
105
106     /**
107      * 4 margins: top, bottom, left, right
108      */
109     public static class Margins {
110
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;
115
116         public final Margin top;
117         public final Margin bottom;
118         public final Margin left;
119         public final Margin right;
120
121         public Margins(Margin top, Margin bottom, Margin left, Margin right) {
122             assert top != null;
123             assert bottom != null;
124             assert left != null;
125             assert right != null;
126
127             this.top = top;
128             this.bottom = bottom;
129             this.left = left;
130             this.right = right;
131         }
132
133         /**
134          * @param a bitmask consisting of the values {@link #TOP},
135          *        {@link #BOTTOM}, {@link #LEFT} and {@link #RIGHT}
136          * @param top
137          * @return
138          */
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);
145         }
146
147         public boolean isZero() {
148             return top.isZero() && bottom.isZero() && left.isZero() && right.isZero();
149         }
150
151         @Override
152         public String toString() {
153             return "[top=" + top + ", bottom=" + bottom + ", left=" + left + ", right=" + right + "]";
154         }
155
156         @Override
157         public int hashCode() {
158             final int prime = 31;
159             int result = 1;
160             result = prime * result + bottom.hashCode();
161             result = prime * result + left.hashCode();
162             result = prime * result + right.hashCode();
163             result = prime * result + top.hashCode();
164             return result;
165         }
166
167         @Override
168         public boolean equals(Object obj) {
169             if (this == obj)
170                 return true;
171             if (obj == null)
172                 return false;
173             if (getClass() != obj.getClass())
174                 return false;
175             Margins other = (Margins) obj;
176             if (!top.equals(other.top))
177                 return false;
178             if (!bottom.equals(other.bottom))
179                 return false;
180             if (!left.equals(other.left))
181                 return false;
182             if (!right.equals(other.right))
183                 return false;
184             return true;
185         }
186     }
187
188 }