View Javadoc

1   /*
2    *  File: BarCellRenderer.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.SWT;
14  import org.eclipse.swt.graphics.Color;
15  import org.eclipse.swt.graphics.GC;
16  import org.eclipse.swt.graphics.Rectangle;
17  import org.eclipse.swt.printing.Printer;
18  import org.eclipse.swt.widgets.Display;
19  
20  import de.jaret.util.ui.table.JaretTable;
21  import de.jaret.util.ui.table.model.IColumn;
22  import de.jaret.util.ui.table.model.IRow;
23  
24  /***
25   * CellRenderer rendering bar according to the cell value that is expected to be of type Integer. Min and max can be
26   * configured.
27   * 
28   * @author Peter Kliem
29   * @version $Id: BarCellRenderer.java 179 2007-01-07 17:37:50Z olk $
30   */
31  public class BarCellRenderer extends CellRendererBase implements ICellRenderer {
32      /*** min value. */
33      protected int _min = 0;
34  
35      /*** max value. */
36      protected int _max = 100;
37  
38      /*** color for rendering. */
39      protected Color _barColor;
40  
41      /***
42       * Constructor for BarCellRenderer.
43       * 
44       * @param printer Printer or <code>null</code>
45       */
46      public BarCellRenderer(Printer printer) {
47          super(printer);
48          if (printer != null) {
49              _barColor = printer.getSystemColor(SWT.COLOR_DARK_RED);
50          }
51      }
52  
53      /***
54       * Default constructor.
55       * 
56       */
57      public BarCellRenderer() {
58          super(null);
59          _barColor = Display.getCurrent().getSystemColor(SWT.COLOR_DARK_RED);
60      }
61  
62      /***
63       * @return Returns the max.
64       */
65      public int getMax() {
66          return _max;
67      }
68  
69      /***
70       * @param max The max to set.
71       */
72      public void setMax(int max) {
73          this._max = max;
74      }
75  
76      /***
77       * @return Returns the min.
78       */
79      public int getMin() {
80          return _min;
81      }
82  
83      /***
84       * @param min The min to set.
85       */
86      public void setMin(int min) {
87          this._min = min;
88      }
89  
90      /***
91       * {@inheritDoc}
92       */
93      public void draw(GC gc, JaretTable jaretTable, ICellStyle cellStyle, Rectangle drawingArea, IRow row,
94              IColumn column, boolean drawFocus, boolean selected, boolean printing) {
95          drawBackground(gc, drawingArea, cellStyle, selected, printing);
96          Rectangle drect = drawBorder(gc, cellStyle, drawingArea, printing);
97          Rectangle rect = applyInsets(drect);
98          Object value = column.getValue(row);
99          if (value instanceof Integer) {
100             int val = ((Integer) value).intValue();
101             double pixPer = (double) rect.width / (double) (_max - _min);
102             int correctedValue = val - _min;
103             int drawingWidth = (int) (correctedValue * pixPer);
104 
105             Color bg = gc.getBackground();
106             gc.setBackground(_barColor);
107             gc.fillRectangle(rect.x, rect.y, drawingWidth, rect.height);
108 
109             gc.setBackground(bg);
110         } else {
111             // indicate error with red fill
112             Color bg = gc.getBackground();
113             gc.setBackground(Display.getCurrent().getSystemColor(SWT.COLOR_RED));
114             gc.fillRectangle(rect);
115             gc.setBackground(bg);
116         }
117         if (drawFocus) {
118             drawFocus(gc, drawingArea);
119         }
120         drawSelection(gc, drawingArea, cellStyle, selected, printing);
121     }
122 
123     /***
124      * {@inheritDoc}
125      */
126     public void dispose() {
127     }
128 
129     /***
130      * {@inheritDoc}
131      */
132     public ICellRenderer createPrintRenderer(Printer printer) {
133         return new BarCellRenderer(printer);
134     }
135 
136 }