001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */
041: package org.netbeans.modules.print.impl.util;
042:
043: import java.awt.Color;
044: import java.awt.Font;
045: import java.awt.Graphics;
046: import java.awt.Graphics2D;
047: import java.awt.RenderingHints;
048: import java.awt.font.FontRenderContext;
049: import java.awt.geom.Rectangle2D;
050: import java.awt.print.Paper;
051: import java.awt.print.PrinterJob;
052: import java.awt.print.PageFormat;
053: import java.util.StringTokenizer;
054:
055: import java.util.prefs.Preferences;
056: import org.openide.util.NbPreferences;
057: import static org.netbeans.modules.print.impl.util.UI.*;
058:
059: /**
060: * @author Vladimir Yaroslavskiy
061: * @version 2006.03.21
062: */
063: public final class Option {
064:
065: private Option() {
066: }
067:
068: public boolean showPageSetup() {
069: PrinterJob job = PrinterJob.getPrinterJob();
070: PageFormat oldFormat = getPageFormat();
071: PageFormat newFormat = job.pageDialog(oldFormat);
072:
073: if (oldFormat == newFormat) {
074: return false;
075: }
076: myPageFormat = newFormat;
077:
078: // save
079: set(PAGE_ORIENTATION, myPageFormat.getOrientation());
080: Paper paper = myPageFormat.getPaper();
081:
082: set(PAPER_WIDTH, paper.getWidth());
083: set(PAPER_HEIGHT, paper.getHeight());
084:
085: set(AREA_X, paper.getImageableX());
086: set(AREA_Y, paper.getImageableY());
087:
088: set(AREA_WIDTH, paper.getImageableWidth());
089: set(AREA_HEIGHT, paper.getImageableHeight());
090:
091: return true;
092: }
093:
094: public PageFormat getPageFormat() {
095: PrinterJob job = PrinterJob.getPrinterJob();
096:
097: if (myPageFormat == null) {
098: myPageFormat = job.defaultPage();
099:
100: // restore
101: myPageFormat.setOrientation(round(get(PAGE_ORIENTATION,
102: PageFormat.PORTRAIT)));
103: Paper paper = myPageFormat.getPaper();
104:
105: if (get(PAPER_WIDTH, null) != null
106: && get(PAPER_HEIGHT, null) != null) {
107: paper.setSize(get(PAPER_WIDTH, INCH), get(PAPER_HEIGHT,
108: INCH));
109: }
110: if (get(AREA_X, null) != null && get(AREA_Y, null) != null
111: && get(AREA_WIDTH, null) != null
112: && get(AREA_HEIGHT, null) != null) {
113: paper.setImageableArea(get(AREA_X, INCH), get(AREA_Y,
114: INCH), get(AREA_WIDTH, INCH), get(AREA_HEIGHT,
115: INCH));
116: }
117: myPageFormat.setPaper(paper);
118: }
119: return myPageFormat;
120: }
121:
122: // paper
123: public int getPaperWidth() {
124: return (int) Math.floor(getPageFormat().getWidth());
125: }
126:
127: public int getPaperHeight() {
128: return (int) Math.floor(getPageFormat().getHeight());
129: }
130:
131: // page
132: public int getPageX() {
133: return round(getPageFormat().getImageableX());
134: }
135:
136: public int getPageY() {
137: int y = round(getPageFormat().getImageableY());
138:
139: if (hasHeader()) {
140: y += getBound(getHeaderFont()).getHeight();
141: }
142: return y;
143: }
144:
145: public int getPageWidth() {
146: return (int) Math.floor(getPageFormat().getImageableWidth());
147: }
148:
149: public int getPageHeight() {
150: int height = (int) Math.floor(getPageFormat()
151: .getImageableHeight());
152:
153: if (hasHeader()) {
154: height -= getBound(getHeaderFont()).getHeight();
155: }
156: if (hasFooter()) {
157: height -= getBound(getFooterFont()).getHeight();
158: }
159: return height;
160: }
161:
162: // header
163: public int getHeaderY() {
164: return getPageY() - round(getBound(getHeaderFont()).getMaxY());
165: }
166:
167: public int getFooterY() {
168: return getPageY()
169: + getPageHeight()
170: + round(getBound(getFooterFont()).getHeight()
171: - getBound(getFooterFont()).getMaxY());
172: }
173:
174: public boolean hasBorder() {
175: return get(BORDER, true);
176: }
177:
178: public void setBorder(boolean value) {
179: set(BORDER, value);
180: }
181:
182: public boolean isAsEditor() {
183: return get(AS_EDITOR, false);
184: }
185:
186: public void setAsEditor(boolean value) {
187: set(AS_EDITOR, value);
188: }
189:
190: public Color getBorderColor() {
191: return getColor(get(BORDER_COLOR, null), Color.black);
192: }
193:
194: public void setBorderColor(Color value) {
195: set(BORDER_COLOR, getString(value));
196: }
197:
198: public boolean hasHeader() {
199: return get(HEADER, true);
200: }
201:
202: public void setHeader(boolean value) {
203: set(HEADER, value);
204: }
205:
206: public String getHeaderLeft() {
207: return get(HEADER_LEFT, HEADER_LEFT_TEXT);
208: }
209:
210: public String getHeaderCenter() {
211: return get(HEADER_CENTER, HEADER_CENTER_TEXT);
212: }
213:
214: public String getHeaderRight() {
215: return get(HEADER_RIGHT, HEADER_RIGHT_TEXT);
216: }
217:
218: public void setHeaderLeft(String value) {
219: set(HEADER_LEFT, value);
220: }
221:
222: public void setHeaderCenter(String value) {
223: set(HEADER_CENTER, value);
224: }
225:
226: public void setHeaderRight(String value) {
227: set(HEADER_RIGHT, value);
228: }
229:
230: public Color getHeaderColor() {
231: return getColor(get(HEADER_COLOR, null), Color.black);
232: }
233:
234: public Font getHeaderFont() {
235: return getFont(get(HEADER_FONT, null), DEFAULT_TITLE_FONT);
236: }
237:
238: public void setHeaderColor(Color value) {
239: set(HEADER_COLOR, getString(value));
240: }
241:
242: public void setHeaderFont(Font value) {
243: set(HEADER_FONT, getString(value));
244: }
245:
246: // footer
247: public boolean hasFooter() {
248: return get(FOOTER, true);
249: }
250:
251: public void setFooter(boolean value) {
252: set(FOOTER, value);
253: }
254:
255: public String getFooterLeft() {
256: return get(FOOTER_LEFT, FOOTER_LEFT_TEXT);
257: }
258:
259: public String getFooterCenter() {
260: return get(FOOTER_CENTER, FOOTER_CENTER_TEXT);
261: }
262:
263: public String getFooterRight() {
264: return get(FOOTER_RIGHT, FOOTER_RIGHT_TEXT);
265: }
266:
267: public void setFooterLeft(String value) {
268: set(FOOTER_LEFT, value);
269: }
270:
271: public void setFooterCenter(String value) {
272: set(FOOTER_CENTER, value);
273: }
274:
275: public void setFooterRight(String value) {
276: set(FOOTER_RIGHT, value);
277: }
278:
279: public Color getFooterColor() {
280: return getColor(get(FOOTER_COLOR, null), Color.black);
281: }
282:
283: public Font getFooterFont() {
284: return getFont(get(FOOTER_FONT, null), DEFAULT_TITLE_FONT);
285: }
286:
287: public void setFooterColor(Color value) {
288: set(FOOTER_COLOR, getString(value));
289: }
290:
291: public void setFooterFont(Font value) {
292: set(FOOTER_FONT, getString(value));
293: }
294:
295: // scale
296: public double getScale() {
297: return get(SCALE, 1.0);
298: }
299:
300: public void setScale(double value) {
301: set(SCALE, value);
302: }
303:
304: // text
305: public void setWrapLines(boolean value) {
306: set(WRAP_LINES, value);
307: }
308:
309: public boolean isWrapLines() {
310: return get(WRAP_LINES, false);
311: }
312:
313: public boolean isLineNumbers() {
314: return get(LINE_NUMBERS, false);
315: }
316:
317: public void setLineNumbers(boolean value) {
318: set(LINE_NUMBERS, value);
319: }
320:
321: public boolean isUseFont() {
322: return get(USE_FONT, true);
323: }
324:
325: public void setUseFont(boolean value) {
326: set(USE_FONT, value);
327: }
328:
329: public boolean isUseColor() {
330: return get(USE_COLOR, true);
331: }
332:
333: public void setUseColor(boolean value) {
334: set(USE_COLOR, value);
335: }
336:
337: public Color getTextColor() {
338: return getColor(get(TEXT_COLOR, null), Color.black);
339: }
340:
341: public void setTextColor(Color value) {
342: set(TEXT_COLOR, getString(value));
343: }
344:
345: public Font getTextFont() {
346: return getFont(get(TEXT_FONT, null), DEFAULT_TEXT_FONT);
347: }
348:
349: public void setTextFont(Font value) {
350: set(TEXT_FONT, getString(value));
351: }
352:
353: public Color getBackgroundColor() {
354: return getColor(get(BACKGROUND_COLOR, null),
355: DEFAULT_BACGROUND_COLOR);
356: }
357:
358: public void setBackgroundColor(Color value) {
359: set(BACKGROUND_COLOR, getString(value));
360: }
361:
362: public double getLineSpacing() {
363: return get(LINE_SPACING, 1.0);
364: }
365:
366: public void setLineSpacing(double value) {
367: set(LINE_SPACING, value);
368: }
369:
370: // zoom
371: public void setZoom(double value) {
372: set(ZOOM, value);
373: }
374:
375: public double getZoom() {
376: return get(ZOOM, 1.0);
377: }
378:
379: // service
380: private Preferences getPreferences() {
381: return NbPreferences.forModule(Option.class);
382: }
383:
384: private String get(String name, String defaultValue) {
385: return getPreferences().get(name, defaultValue);
386: }
387:
388: private boolean get(String name, boolean defaultValue) {
389: return getPreferences().getBoolean(name, defaultValue);
390: }
391:
392: private double get(String name, double defaultValue) {
393: return getPreferences().getDouble(name, defaultValue);
394: }
395:
396: private void set(String name, String value) {
397: getPreferences().put(name, value);
398: }
399:
400: private void set(String name, boolean value) {
401: getPreferences().putBoolean(name, value);
402: }
403:
404: private void set(String name, double value) {
405: getPreferences().putDouble(name, value);
406: }
407:
408: private String getString(Color value) {
409: return value.getRed() + COMMA + value.getGreen() + COMMA
410: + value.getBlue();
411: }
412:
413: private String getString(Font value) {
414: return value.getName() + COMMA + value.getStyle() + COMMA
415: + value.getSize();
416: }
417:
418: private Color getColor(String value, Color defaultValue) {
419: if (value == null) {
420: return defaultValue;
421: }
422: StringTokenizer stk = new StringTokenizer(value, COMMA);
423:
424: int red = integer(stk);
425: int green = integer(stk);
426: int blue = integer(stk);
427:
428: if (red == -1 || green == -1 || blue == -1) {
429: return defaultValue;
430: }
431: return new Color(red, green, blue);
432: }
433:
434: private Font getFont(String value, Font defaultValue) {
435: if (value == null) {
436: return defaultValue;
437: }
438: StringTokenizer stk = new StringTokenizer(value, COMMA);
439:
440: String name = getString(stk);
441: int style = integer(stk);
442: int size = integer(stk);
443:
444: if (name == null || style == -1 || size == -1) {
445: return defaultValue;
446: }
447: return new Font(name, style, size);
448: }
449:
450: private int integer(StringTokenizer stk) {
451: if (!stk.hasMoreTokens()) {
452: return -1;
453: }
454: return getInt(stk.nextToken());
455: }
456:
457: private String getString(StringTokenizer stk) {
458: if (!stk.hasMoreTokens()) {
459: return null;
460: }
461: return stk.nextToken();
462: }
463:
464: private Rectangle2D getBound(Font font) {
465: return font.getMaxCharBounds(FONT_RENDER_CONTEXT);
466: }
467:
468: public static String getPageOfCount(String page, String count) {
469: return i18n(Option.class, "LBL_Page_of_Count", page, count); // NOI18N
470: }
471:
472: private static String getRowColumn(String row, String column) {
473: return i18n(Option.class, "LBL_Row_Column", row, column); // NOI18N
474: }
475:
476: public static Option getDefault() {
477: return DEFAULT;
478: }
479:
480: public Graphics2D getGraphics(Graphics g) {
481: Graphics2D graphics = (Graphics2D) g;
482:
483: graphics.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
484: RenderingHints.VALUE_ANTIALIAS_ON);
485:
486: graphics.setRenderingHint(RenderingHints.KEY_FRACTIONALMETRICS,
487: RenderingHints.VALUE_FRACTIONALMETRICS_ON);
488:
489: return graphics;
490: }
491:
492: private PageFormat myPageFormat;
493:
494: private static final Option DEFAULT = new Option();
495: private static final double INCH = 72.0; // .pt
496:
497: private static final Font DEFAULT_TITLE_FONT = new Font("Serif",
498: Font.PLAIN, 10); //NOI18N
499:
500: private static final Font DEFAULT_TEXT_FONT = new Font(
501: "Monospaced", Font.PLAIN, 10); // NOI18N
502:
503: public static final FontRenderContext FONT_RENDER_CONTEXT = new FontRenderContext(
504: null, true, true);
505:
506: private static final Color DEFAULT_BACGROUND_COLOR = new Color(255,
507: 250, 255);
508:
509: private static final String COMMA = ","; // NOI18N
510: private static final String EMPTY = ""; // NOI18N
511:
512: private static final String HEADER_LEFT_TEXT = Macro.NAME.getName();
513: private static final String HEADER_CENTER_TEXT = EMPTY;
514: private static final String HEADER_RIGHT_TEXT = EMPTY;
515:
516: private static final String FOOTER_LEFT_TEXT = getPageOfCount(
517: getRowColumn(Macro.ROW.getName(), Macro.COLUMN.getName()),
518: Macro.COUNT.getName());
519: private static final String FOOTER_CENTER_TEXT = EMPTY;
520: private static final String FOOTER_RIGHT_TEXT = Macro.MODIFIED_DATE
521: .getName()
522: + " " + Macro.MODIFIED_TIME.getName(); // NOI18N
523:
524: private static final String AS_EDITOR = "print.text.as.editor"; // NOI18N
525: private static final String WRAP_LINES = "print.text.wrap.lines"; // NOI18N
526: private static final String LINE_NUMBERS = "print.text.line.numbers"; // NOI18N
527: private static final String USE_FONT = "print.text.use.font"; // NOI18N
528: private static final String USE_COLOR = "print.text.use.color"; // NOI18N
529: private static final String TEXT_COLOR = "print.text.color"; // NOI18N
530: private static final String TEXT_FONT = "print.text.font"; // NOI18N
531: private static final String LINE_SPACING = "print.text.line.spacing"; // NOI18N
532: private static final String BACKGROUND_COLOR = "print.text.background.color"; // NOI18N
533:
534: private static final String SCALE = "print.scale"; // NOI18N
535: private static final String ZOOM = "print.zoom"; // NOI18N
536:
537: private static final String BORDER = "print.border"; // NOI18N
538: private static final String BORDER_COLOR = "print.border.color"; // NOI18N
539:
540: private static final String HEADER = "print.header"; // NOI18N
541: private static final String HEADER_LEFT = "print.header.left"; // NOI18N
542: private static final String HEADER_CENTER = "print.header.center"; // NOI18N
543: private static final String HEADER_RIGHT = "print.header.right"; // NOI18N
544: private static final String HEADER_COLOR = "print.header.color"; // NOI18N
545: private static final String HEADER_FONT = "print.header.font"; // NOI18N
546:
547: private static final String FOOTER = "print.footer"; // NOI18N
548: private static final String FOOTER_LEFT = "print.footer.left"; // NOI18N
549: private static final String FOOTER_CENTER = "print.footer.center"; // NOI18N
550: private static final String FOOTER_RIGHT = "print.footer.right"; // NOI18N
551: private static final String FOOTER_COLOR = "print.footer.color"; // NOI18N
552: private static final String FOOTER_FONT = "print.footer.font"; // NOI18N
553:
554: private static final String PAGE_ORIENTATION = "print.page.orientation"; // NOI18N
555: private static final String PAPER_WIDTH = "print.paper.width"; // NOI18N
556: private static final String PAPER_HEIGHT = "print.paper.height"; // NOI18N
557:
558: private static final String AREA_X = "print.area.x"; // NOI18N
559: private static final String AREA_Y = "print.area.y"; // NOI18N
560: private static final String AREA_WIDTH = "print.area.width"; // NOI18N
561: private static final String AREA_HEIGHT = "print.area.height"; // NOI18N
562: }
|