View Javadoc

1   /*
2    *  File: CellRendererBase.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 java.util.List;
14  
15  import org.eclipse.swt.SWT;
16  import org.eclipse.swt.graphics.Color;
17  import org.eclipse.swt.graphics.Device;
18  import org.eclipse.swt.graphics.Font;
19  import org.eclipse.swt.graphics.GC;
20  import org.eclipse.swt.graphics.RGB;
21  import org.eclipse.swt.graphics.Rectangle;
22  import org.eclipse.swt.printing.Printer;
23  import org.eclipse.swt.widgets.Display;
24  
25  import de.jaret.util.swt.ColorManager;
26  import de.jaret.util.swt.FontManager;
27  import de.jaret.util.ui.table.JaretTable;
28  import de.jaret.util.ui.table.model.IColumn;
29  import de.jaret.util.ui.table.model.IRow;
30  
31  /***
32   * Base implementation for cell renderers that support both screen and printer rendering. This base implementation
33   * contains some useful methods so that it is highly recommended to base all renderer implementations on this base.
34   * 
35   * @author Peter Kliem
36   * @version $Id: CellRendererBase.java 489 2007-06-05 17:56:08Z olk $
37   */
38  public abstract class CellRendererBase extends RendererBase implements ICellRenderer {
39      /*** selection color for overlay (non printing only). */
40      protected static final Color SELECTIONCOLOR = Display.getCurrent().getSystemColor(SWT.COLOR_GRAY);
41  
42      /*** alpha value used when drawing default selection. */
43      private static final int SELECTIONALPHA = 150;
44  
45      /*** insets used when drawing the focus. */
46      protected static final int FOCUSINSETS = 2;
47  
48      /*** default background color. */
49      protected static final RGB WHITERGB = new RGB(255, 255, 255);
50      /*** default foreground color. */
51      protected static final RGB BLACKRGB = new RGB(0, 0, 0);
52  
53      /*** cell inset used by the convenience methods. */
54      protected int _inset = 2;
55  
56      /***
57       * May be constructed without printer (supplying null).
58       * 
59       * @param printer or <code>null</code>
60       */
61      public CellRendererBase(Printer printer) {
62          super(printer);
63      }
64  
65      /***
66       * {@inheritDoc} Default implementation: no prferred width.
67       */
68      public int getPreferredWidth(List<IRow> rows, IColumn column) {
69          return -1;
70      }
71  
72      /***
73       * {@inheritDoc} Default implementation returning: no information.
74       */
75      public int getPreferredHeight(GC gc, ICellStyle cellStyle, int width, IRow row, IColumn column) {
76          return -1;
77      }
78  
79      /***
80       * {@inheritDoc} Default: no tooltip.
81       */
82      public String getTooltip(JaretTable jaretTable, Rectangle drawingArea, IRow row, IColumn column, int x, int y) {
83          return null;
84      }
85  
86      /***
87       * Target inner width (width - borders - insets).
88       * 
89       * @param width width
90       * @param cellStyle cell style
91       * @return target inner width
92       */
93      protected int getInnerWidth(int width, ICellStyle cellStyle) {
94          int sum = _inset * 2 + cellStyle.getBorderConfiguration().getBorderLeft()
95                  + cellStyle.getBorderConfiguration().getBorderRight() - 1;
96          return width - sum;
97      }
98  
99      /***
100      * Calculate the sum of all vertical spaces that could be spplied.
101      * 
102      * @param cellStyle cell style
103      * @return sum of all vertical spaces
104      */
105     protected int getVerticalSpacesSum(ICellStyle cellStyle) {
106         return _inset * 2 + cellStyle.getBorderConfiguration().getBorderTop()
107                 + cellStyle.getBorderConfiguration().getBorderBottom() - 1;
108     }
109 
110     /***
111      * Draw focus marking. Should be called with the corrected drawing area.
112      * 
113      * @param gc GC
114      * @param drawingArea corrected drawing area
115      */
116     protected void drawFocus(GC gc, Rectangle drawingArea) {
117         Color bg = gc.getBackground();
118         Color fg = gc.getForeground();
119         gc.setForeground(Display.getCurrent().getSystemColor(SWT.COLOR_BLACK));
120         gc.drawFocus(drawingArea.x + FOCUSINSETS, drawingArea.y + FOCUSINSETS, drawingArea.width - 2 * FOCUSINSETS,
121                 drawingArea.height - 2 * FOCUSINSETS - 1);
122 // gc.drawRectangle(drawingArea.x + 2, drawingArea.y + 2, drawingArea.width - 4, drawingArea.height - 3);
123         gc.setForeground(fg);
124         gc.setBackground(bg);
125     }
126 
127     /***
128      * Calculate the resulting rectangle after applying the insets.
129      * 
130      * @param rect cell drawing area
131      * @return corrected rectangle
132      */
133     protected Rectangle applyInsets(Rectangle rect) {
134         int d = _inset;
135         return new Rectangle(rect.x + d, rect.y + d, rect.width - 2 * d, rect.height - 2 * d);
136     }
137 
138     /***
139      * Draw the border for the cell according to the cell style.
140      * 
141      * @param gc GC
142      * @param cellStyle th style
143      * @param drawingArea the drawing area of the cell
144      * @param printing true marks operation for a printer
145      * @return the corrected drawing area (the thickness of the border has been substracted)
146      */
147     protected Rectangle drawBorder(GC gc, ICellStyle cellStyle, Rectangle drawingArea, boolean printing) {
148         IBorderConfiguration borderConfiguration = cellStyle.getBorderConfiguration();
149         int x = drawingArea.x;
150         int y = drawingArea.y;
151         int width = drawingArea.width;
152         int height = drawingArea.height;
153 
154         Color fg = gc.getForeground();
155         gc.setForeground(getBorderColor(cellStyle, printing));
156 
157         int lineWidth = gc.getLineWidth();
158         if (borderConfiguration.getBorderLeft() > 0) {
159             int lw = scaleX(borderConfiguration.getBorderLeft());
160             gc.setLineWidth(lw);
161             gc.drawLine(drawingArea.x, drawingArea.y, drawingArea.x, drawingArea.y + drawingArea.height);
162             x += lw;
163             width -= lw;
164         }
165         if (borderConfiguration.getBorderRight() > 0) {
166             int lw = scaleX(borderConfiguration.getBorderRight());
167             gc.setLineWidth(lw);
168             gc.drawLine(drawingArea.x + drawingArea.width, drawingArea.y, drawingArea.x + drawingArea.width,
169                     drawingArea.y + drawingArea.height);
170             width -= lw;
171         }
172         if (borderConfiguration.getBorderTop() > 0) {
173             int lw = scaleY(borderConfiguration.getBorderTop());
174             gc.setLineWidth(lw);
175             gc.drawLine(drawingArea.x, drawingArea.y, drawingArea.x + drawingArea.width - 1, drawingArea.y);
176             y += lw;
177             height -= lw;
178         }
179         if (borderConfiguration.getBorderBottom() > 0) {
180             int lw = scaleY(borderConfiguration.getBorderBottom());
181             gc.setLineWidth(lw);
182             gc.drawLine(drawingArea.x, drawingArea.y + drawingArea.height, drawingArea.x + drawingArea.width,
183                     drawingArea.y + drawingArea.height);
184             height -= lw;
185         }
186         gc.setLineWidth(lineWidth);
187         gc.setForeground(fg);
188 
189         return new Rectangle(x, y, width, height);
190 
191     }
192 
193     /***
194      * Draw the cell background.
195      * 
196      * @param gc GC
197      * @param area cell drawing area
198      * @param style cell style
199      * @param selected true for selected
200      * @param printing true if printing
201      */
202     protected void drawBackground(GC gc, Rectangle area, ICellStyle style, boolean selected, boolean printing) {
203         Color c = gc.getBackground();
204         Color bg;
205         bg = getBackgroundColor(style, printing);
206         gc.setBackground(bg);
207         gc.fillRectangle(area);
208         gc.setBackground(c);
209     }
210 
211     /***
212      * Draws a cell selection by overlaying alpha blended area using SELECTIONCOLOR.
213      * 
214      * @param gc GC
215      * @param area area of the cell
216      * @param style cellstyle
217      * @param selected true if selecetd
218      * @param printing true if printing - no selection will be drawn when printing
219      */
220     protected void drawSelection(GC gc, Rectangle area, ICellStyle style, boolean selected, boolean printing) {
221         Color c = gc.getBackground();
222         Color bg;
223 
224         if (selected && !printing) {
225             bg = SELECTIONCOLOR;
226             gc.setBackground(bg);
227             int alpha = gc.getAlpha();
228             gc.setAlpha(SELECTIONALPHA);
229             gc.fillRectangle(area);
230             gc.setAlpha(alpha);
231             gc.setBackground(c);
232         }
233     }
234 
235     /***
236      * Draw a marker in upper left corner for indicating a cell comment.
237      * 
238      * @param gc GC
239      * @param area drawing area
240      * @param color color of the marker
241      * @param size size of the marker
242      */
243     protected void drawCommentMarker(GC gc, Rectangle area, Color color, int size) {
244         Color bg = gc.getBackground();
245         gc.setBackground(color);
246         gc.fillRectangle(area.x + area.width - size, area.y, size, size);
247         gc.setBackground(bg);
248     }
249 
250     /***
251      * Check whether a position is in the area of the commetn marker.
252      * 
253      * @param area drawing area of the cell
254      * @param size size of the marker
255      * @param x x coordinate to check
256      * @param y y coordinate to check
257      * @return true if the position is in the area of the marker
258      */
259     protected boolean isInCommentMarkerArea(Rectangle area, int size, int x, int y) {
260         Rectangle r = new Rectangle(area.x + area.width - size, area.y, size, size);
261         return r.contains(x, y);
262     }
263 
264     /***
265      * Get the background color according to a cell style.
266      * 
267      * @param style cell style
268      * @param printing true for printing
269      * @return the color
270      */
271     protected Color getBackgroundColor(ICellStyle style, boolean printing) {
272         Device device = printing ? _printer : Display.getCurrent();
273         ColorManager cm = ColorManager.getColorManager(device);
274         Color bg = cm.getColor(style.getBackgroundColor() != null ? style.getBackgroundColor() : WHITERGB);
275         return bg;
276     }
277 
278     /***
279      * Get the foreground color according to the cell style.
280      * 
281      * @param style cell style
282      * @param printing true for printing
283      * @return the foreground color
284      */
285     protected Color getForegroundColor(ICellStyle style, boolean printing) {
286         Device device = printing ? _printer : Display.getCurrent();
287         ColorManager cm = ColorManager.getColorManager(device);
288         Color bg = cm.getColor(style.getForegroundColor() != null ? style.getForegroundColor() : BLACKRGB);
289         return bg;
290     }
291 
292     /***
293      * Get the border color according to the cell style.
294      * 
295      * @param style cell style
296      * @param printing true for printing
297      * @return the border color
298      */
299     protected Color getBorderColor(ICellStyle style, boolean printing) {
300         Device device = printing ? _printer : Display.getCurrent();
301         ColorManager cm = ColorManager.getColorManager(device);
302         Color bg = cm.getColor(style.getBorderColor() != null ? style.getBorderColor() : new RGB(0, 0, 0));
303         return bg;
304     }
305 
306     /***
307      * Retrieve the font accrding to the cell style.
308      * 
309      * @param style cell style
310      * @param printing true for printing
311      * @param defaultFont a default font used if no font can be retrieved
312      * @return font according to style or default font
313      */
314     protected Font getFont(ICellStyle style, boolean printing, Font defaultFont) {
315         Device device = printing ? _printer : Display.getCurrent();
316         FontManager fm = FontManager.getFontManager(device);
317         Font f = style.getFont() != null ? fm.getFont(style.getFont()) : defaultFont;
318         return f;
319     }
320 
321 }