View Javadoc

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    * All rights reserved. This program and the accompanying materials
7    * are made available under the terms of the Common Public License v1.0
8    * which accompanies this distribution, and is available at
9    * http://www.eclipse.org/legal/cpl-v10.html
10   */
11  package de.jaret.util.ui.table.renderer;
12  
13  import org.eclipse.swt.graphics.Color;
14  import org.eclipse.swt.graphics.Font;
15  import org.eclipse.swt.graphics.GC;
16  import org.eclipse.swt.graphics.Point;
17  import org.eclipse.swt.printing.Printer;
18  
19  /***
20   * Base implementation for renderers that support both screen and printer rendering. It's main purpose is scaling
21   * beetween screen and printer coordinates (based on 96dpi for the screen).
22   * 
23   * @author Peter Kliem
24   * @version $Id: RendererBase.java 179 2007-01-07 17:37:50Z olk $
25   */
26  public abstract class RendererBase {
27      /*** printer if used. */
28      protected Printer _printer;
29  
30      /*** constant for scaling: screen resolution x. */
31      protected static final double SCREEN_DPI_X = 96.0;
32      /*** constant for scaling: screen resolution y. */
33      protected static final double SCREEN_DPI_Y = 96.0;
34  
35      /*** x scaling for transformation beetwenn screen and printer. */
36      protected double _scaleX = 1.0;
37  
38      /*** y scaling for transformation beetwenn screen and printer. */
39      protected double _scaleY = 1.0;
40  
41      /*** for saving gc attribute. */
42      private Color _bgColor;
43      /*** for saving gc attribute. */
44      private Color _fgColor;
45      /*** for saving gc attribute. */
46      private int _lineWidth;
47      /*** for saving gc attribute. */
48      private Font _font;
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 / SCREEN_DPI_X;
60              _scaleY = (double) dpi.y / SCREEN_DPI_Y;
61          }
62      }
63  
64      /***
65       * Scale an x coordinate/size from screen to printer.
66       * 
67       * @param in corodinate/size 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 x scale factor
78       */
79      public double getScaleX() {
80          return _scaleX;
81      }
82  
83      /***
84       * Scale an y coordinate/size from screen to printer.
85       * 
86       * @param in corodinate/size 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       * Retrieve the y scale factor.
95       * 
96       * @return y scale factor
97       */
98      public double getScaleY() {
99          return _scaleY;
100     }
101 
102     /***
103      * Retrieve the printer device.
104      * 
105      * @return printer device if set <code>null</code> otherwise
106      */
107     public Printer getPrinter() {
108         return _printer;
109     }
110 
111     /***
112      * Helper method saving several GC attributes to loal variables. The values can be restored with
113      * <code>restoreGCAttributes</code>.
114      * 
115      * @param gc GC to save attributes for
116      */
117     protected void saveGCAttributes(GC gc) {
118         _bgColor = gc.getBackground();
119         _fgColor = gc.getForeground();
120         _font = gc.getFont();
121         _lineWidth = gc.getLineWidth();
122     }
123 
124     /***
125      * Helper method to restore attribute values saved with <code>saveGCAttributes</code>.
126      * 
127      * @param gc GC to restore attributes for
128      */
129     protected void restoreGCAttributes(GC gc) {
130         if (_bgColor == null) {
131             throw new RuntimeException("no attributes saved");
132         }
133         gc.setBackground(_bgColor);
134         gc.setForeground(_fgColor);
135         gc.setFont(_font);
136         gc.setLineWidth(_lineWidth);
137     }
138 
139 }