001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017:
018: /*
019: * MediaMargins.java
020: *
021: * We launch additional printing attribute:
022: * org.apache.harmony.x.print.attributes.MediaMargins.
023: *
024: * This is a printing attribute used to set paper margins for the printed
025: * document. The MediaMargins is specified to be left, right, top and bottom
026: * paper margins. It is independent of a particular media. Sometimes it is
027: * more convenient for a client to use margins instead of printable area.
028: *
029: * This attribute can be easy calculated if we have MediaSize and
030: * MediaPrintableArea attributes for the printed document, so in this case we
031: * do not need to set it clearly. However, it can be very helpful if we do not
032: * know size of the printed document's Media size (for example, if Media
033: * attribute will be set later or it is MediaTray attribute) - we can not use
034: * MediaPrintableArea in this case.
035: *
036: * It is supposed that our default printers will support MediaMargins
037: * attribute.
038: *
039: * In case of the conflict between MediaPrintableArea and MediaMargins
040: * attribute the following way of conflict resolving is supposed:
041: *
042: * - if MediaSize, MediaPrintableArea and MediaMargins attributes are present,
043: * MediaPrintableArea should be used to calculate paper margins;
044: *
045: * - if Media attribute is not defined or it is defined, but we are unable to
046: * get paper size using it, MediaMargins attribute should be used and
047: * MediaPrintableArea should be ignored.
048: *
049: * Like MediaPrintableArea attribute, MediaMargins values are storet internally
050: * as integers in units of micrometers (please, see
051: * javax.print.attribute.standard.MediaPrintableArea class API Specification
052: * for more details).
053: */
054:
055: package org.apache.harmony.x.print.attributes;
056:
057: import javax.print.attribute.DocAttribute;
058: import javax.print.attribute.PrintJobAttribute;
059: import javax.print.attribute.PrintRequestAttribute;
060: import javax.print.attribute.Size2DSyntax;
061: import javax.print.attribute.standard.MediaPrintableArea;
062: import javax.print.attribute.standard.MediaSize;
063:
064: public final class MediaMargins implements DocAttribute,
065: PrintJobAttribute, PrintRequestAttribute {
066:
067: static final long serialVersionUID = -7745492737636484477L;
068:
069: public static final int INCH = Size2DSyntax.INCH; // 25 400
070: public static final int MM = Size2DSyntax.MM; // 1 000
071: private int x1; // left margin
072: private int x2; // right margin
073: private int y1; // top margin
074: private int y2; // bottom margin
075: private int units; // original units
076:
077: /*
078: * Constructs a MediaMargins object from integer values.
079: *
080: * Parameters:
081: * x1 - left margin
082: * y1 - top margin
083: * x2 - right margin
084: * y2 - bottom margin
085: * units - units in which the values are expressed
086: *
087: * Throws IllegalArgumentException if margins value are < 0
088: * or units < 1
089: */
090: public MediaMargins(int xx1, int yy1, int xx2, int yy2, int myunits) {
091: if ((xx1 < 0) || (yy1 < 0) || (xx2 < 0) || (yy2 < 0)
092: || (myunits < 1)) {
093: throw new IllegalArgumentException("Incorrect margins!");
094: }
095: x1 = xx1 * myunits;
096: y1 = yy1 * myunits;
097: x2 = xx2 * myunits;
098: y2 = yy2 * myunits;
099: units = myunits;
100: }
101:
102: /*
103: * Constructs a MediaMargins object from float values.
104: *
105: * Parameters:
106: * x1 - left margin
107: * y1 - top margin
108: * x2 - right margin
109: * y2 - bottom margin
110: * units - units in which the values are expressed
111: *
112: * Throws IllegalArgumentException if margins value are < 0
113: * or units < 1
114: */
115: public MediaMargins(float xx1, float yy1, float xx2, float yy2,
116: int myunits) {
117: if ((xx1 < 0) || (yy1 < 0) || (xx2 < 0) || (yy2 < 0)
118: || (myunits < 1)) {
119: throw new IllegalArgumentException("Incorrect margins!");
120: }
121: x1 = Math.round(xx1 * myunits);
122: y1 = Math.round(yy1 * myunits);
123: x2 = Math.round(xx2 * myunits);
124: y2 = Math.round(yy2 * myunits);
125: units = myunits;
126: }
127:
128: /*
129: * Constructs a MediaMargins object from MediaSize and MediaPrintableArea.
130: *
131: * Parameters:
132: * size - MediaSize attribute
133: * area - MediaPrintableArea attribute
134: *
135: * Throws IllegalArgumentException if size is null or area is null or
136: * if given MediaPrintableArea is too big for the given MediaSize.
137: */
138:
139: public MediaMargins(MediaSize size, MediaPrintableArea area) {
140: if ((size == null) || (area == null)) {
141: throw new IllegalArgumentException("Incorrect margins!");
142: }
143: this .x1 = Math.round(area.getX(MM) * MM);
144: this .x2 = Math.round((size.getX(MM) - area.getX(MM) - area
145: .getWidth(MM))
146: * MM);
147: this .y1 = Math.round(area.getY(MM) * MM);
148: this .y2 = Math.round((size.getY(MM) - area.getY(MM) - area
149: .getHeight(MM))
150: * MM);
151: if ((x1 < 0) || (y1 < 0) || (x2 < 0) || (y2 < 0)) {
152: throw new IllegalArgumentException("Incorrect margins!");
153: }
154: }
155:
156: /*
157: * Returns whether this margins attribute is equal to the passed object.
158: * The following conditions must be true:
159: * - objectis not null;
160: * - object is an instance of MediaMargins class
161: * - The margins values are the same
162: */
163: public boolean equals(Object object) {
164: if (object instanceof MediaMargins) {
165: MediaMargins m = (MediaMargins) object;
166: return (x1 == m.x1) && (y1 == m.y1) && (x2 == m.x2)
167: && (y2 == m.y2);
168: }
169: return false;
170: }
171:
172: /*
173: * Returns the printing category attribute class
174: * (an instance of java.lang.Class class).
175: * For the MediaMargins the category class is MediaMargins class itself.
176: */
177: public final Class getCategory() {
178: return MediaMargins.class;
179: }
180:
181: /*
182: * Returns the attribute category name - "media-margins"
183: */
184: public final String getName() {
185: return "media-margins";
186: }
187:
188: /*
189: * Returns the left margin value in the specified units.
190: *
191: * Throws IllegalArgumentException if units < 1.
192: */
193: public float getX1(int myunits) {
194: if (myunits < 1) {
195: throw new IllegalArgumentException("units is less than 1");
196: }
197: return ((float) x1) / myunits;
198: }
199:
200: /*
201: * Returns the top margin value in the specified units.
202: *
203: * Throws IllegalArgumentException if units < 1.
204: */
205: public float getY1(int myunits) {
206: if (myunits < 1) {
207: throw new IllegalArgumentException("units is less than 1");
208: }
209: return ((float) y1) / myunits;
210: }
211:
212: /*
213: * Returns the right margin value in the specified units.
214: *
215: * Throws IllegalArgumentException if units < 1.
216: */
217: public float getX2(int myunits) {
218: if (myunits < 1) {
219: throw new IllegalArgumentException("units is less than 1");
220: }
221: return ((float) x2) / myunits;
222: }
223:
224: /*
225: * Returns the bottom margin value in the specified units.
226: *
227: * Throws IllegalArgumentException if units < 1.
228: */
229: public float getY2(int myunits) {
230: if (myunits < 1) {
231: throw new IllegalArgumentException("units is less than 1");
232: }
233: return ((float) y2) / myunits;
234: }
235:
236: /*
237: * Returns the margins value as an array of 4 float values in the order
238: * left, top, right, bottom in given units.
239: *
240: * Throws IllegalArgumentException if units < 1.
241: */
242: public float[] getMargins(int myunits) {
243: return new float[] { getX1(myunits), getY1(myunits),
244: getX2(myunits), getY2(myunits) };
245: }
246:
247: /*
248: * Returns a hash code value for the attribute
249: */
250: public int hashCode() {
251: return ((x1 + x2) | ((y1 + y2) << 16));
252: }
253:
254: /*
255: * Returns a string version of this margins attribute in mm
256: */
257: public String toString() {
258: return toString(MM, "mm");
259: }
260:
261: /*
262: * Returns a string version of this margins attribute in the given units.
263: *
264: * Parameters:
265: * units - units conversion factor (for example, INCH or MM)
266: * unitsName - units name string. If null, no units name is appended to
267: * the result string.
268: *
269: * Throws IllegalArgumentException if units < 1.
270: */
271: public String toString(int myunits, String unitsName) {
272: StringBuffer s = new StringBuffer();
273: s.append("x1=");
274: s.append(getX1(myunits));
275: s.append(" y1=");
276: s.append(getY1(myunits));
277: s.append(" x2=");
278: s.append(getX2(myunits));
279: s.append(" y2=");
280: s.append(getY2(myunits));
281: s.append(" ");
282: s.append((unitsName == null) ? "" : unitsName);
283: return s.toString();
284: }
285:
286: /*
287: * Calculate MediaPrintableArea attribute for tis margins and the given
288: * MediaSize object.
289: */
290: public MediaPrintableArea getMediaPrintableArea(MediaSize size) {
291: return new MediaPrintableArea(getX1(MM), getY1(MM), size
292: .getX(MM)
293: - getX1(MM) - getX2(MM), size.getY(MM) - getY1(MM)
294: - getY2(MM), MM);
295: }
296:
297: } /* End of MediaMargins */
|