1 /*
2 * File: RendererBase.java
3 * Copyright (c) 2004-2007 Peter Kliem (Peter.Kliem@jaret.de)
4 * A commercial license is available, see http://www.jaret.de.
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 */
20 package de.jaret.util.ui.timebars.swt.renderer;
21
22 import org.eclipse.swt.graphics.GC;
23 import org.eclipse.swt.graphics.Point;
24 import org.eclipse.swt.graphics.Rectangle;
25 import org.eclipse.swt.printing.Printer;
26
27 import de.jaret.util.date.Interval;
28 import de.jaret.util.ui.timebars.TimeBarViewerDelegate;
29
30 /**
31 * Base implementation for renderer that support both screen and printer rendering. It's main purpose is scaling
32 * beetween screen and printer coordinates (based on a 96dpi for the screen)
33 *
34 * @author Peter Kliem
35 * @version $Id: RendererBase.java 781 2008-09-20 20:10:18Z kliem $
36 */
37 public class RendererBase {
38 /** assumed dpi value for displays. */
39 private static final double ASSUMED_DISPLAY_DPI = 96.0;
40
41 /** printer device if constructed for a printer. */
42 protected Printer _printer;
43
44 /** scale factor for valus on the x axis. */
45 protected double _scaleX = 1.0;
46
47 /** scale factor for values on the y axis. */
48 protected double _scaleY = 1.0;
49
50 /**
51 * May be constructed without printer (supplying null).
52 *
53 * @param printer or <code>null</code>
54 */
55 public RendererBase(Printer printer) {
56 _printer = printer;
57 if (_printer != null) {
58 Point dpi = _printer.getDPI();
59 _scaleX = (double) dpi.x / ASSUMED_DISPLAY_DPI;
60 _scaleY = (double) dpi.y / ASSUMED_DISPLAY_DPI;
61 }
62 }
63
64 /**
65 * Scale an x value.
66 *
67 * @param in x val to scale
68 * @return scaled value
69 */
70 public int scaleX(int in) {
71 return (int) Math.round(_scaleX * (double) in);
72 }
73
74 /**
75 * Retrieve the x scale factor.
76 *
77 * @return scale factor x
78 */
79 public double getScaleX() {
80 return _scaleX;
81 }
82
83 /**
84 * Scale an y value.
85 *
86 * @param in y val to scale
87 * @return scaled value
88 */
89 public int scaleY(int in) {
90 return (int) Math.round(_scaleY * (double) in);
91 }
92
93 /**
94 * Retruve the y scale value.
95 *
96 * @return y scale factor
97 */
98 public double getScaleY() {
99 return _scaleY;
100 }
101
102 /**
103 * Retrieve the printer device if set.
104 *
105 * @return printer device or <code>null</code>
106 */
107 public Printer getPrinter() {
108 return _printer;
109 }
110
111 /**
112 * Draw focus marking when not printing and focussed.
113 *
114 * @param gc GC
115 * @param drawingArea rectangular drawing area
116 * @param delegate tbv delegate
117 * @param interval interval that maybe in focus
118 * @param selected true if the interval is selecetd
119 * @param printing true if operating for a printer gc
120 * @param overlap true if interval overlaps with otzher intervals
121 */
122 public void drawFocus(GC gc, Rectangle drawingArea, TimeBarViewerDelegate delegate, Interval interval,
123 boolean selected, boolean printing, boolean overlap) {
124 // no selection when printing
125 if (printing) {
126 return;
127 }
128 if (delegate.isFocussed(interval)) {
129 gc.drawFocus(drawingArea.x, drawingArea.y, drawingArea.width, drawingArea.height);
130 }
131 }
132
133 /**
134 * Retrieve a correctd linewidth taking printer resolution in account when printing.
135 *
136 * @return correctd linewidth
137 */
138 public int getDefaultLineWidth() {
139 if (_printer == null) {
140 return 1;
141 } else {
142 int width = scaleX(1);
143 if (width <= 0) {
144 width = 1;
145 }
146 return width;
147 }
148 }
149
150 }