View Javadoc

1   /*
2    *  File: TextCellRenderer.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.Font;
16  import org.eclipse.swt.graphics.GC;
17  import org.eclipse.swt.graphics.Rectangle;
18  import org.eclipse.swt.printing.Printer;
19  import org.eclipse.swt.widgets.Display;
20  
21  import de.jaret.util.swt.TextRenderer;
22  import de.jaret.util.ui.table.JaretTable;
23  import de.jaret.util.ui.table.model.IColumn;
24  import de.jaret.util.ui.table.model.IRow;
25  import de.jaret.util.ui.table.model.ITableViewState;
26  
27  /***
28   * TextCellRenderer for the jaret table. Features an integrated comment marker (tooltip), Override getComment() to use
29   * this. This CellRenderer may be used as the basis for a lot of toText-CellRenderers (see the DateCellRenderer)
30   * 
31   * @author Peter Kliem
32   * @version $Id: TextCellRenderer.java 385 2007-04-29 20:31:49Z olk $
33   */
34  public class TextCellRenderer extends CellRendererBase implements ICellRenderer {
35      /*** size of the comment arker. */
36      private static final int COMMENTMARKER_SIZE = 5;
37      
38      /*** color of the comment marker. */
39      protected Color _commentColor;
40  
41      /***
42       * Create a text cell renderer for printing.
43       * 
44       * @param printer printer device
45       */
46      public TextCellRenderer(Printer printer) {
47          super(printer);
48      }
49  
50      /***
51       * Create a text cell renderer for display.
52       */
53      public TextCellRenderer() {
54          super(null);
55          _commentColor = Display.getCurrent().getSystemColor(SWT.COLOR_RED);
56      }
57  
58      /***
59       * {@inheritDoc}
60       */
61      public String getTooltip(JaretTable jaretTable, Rectangle drawingArea, IRow row, IColumn column, int x, int y) {
62          if (getComment(row, column) != null && isInCommentMarkerArea(drawingArea, COMMENTMARKER_SIZE, x, y)) {
63              return getComment(row, column);
64          }
65          return null;
66      }
67  
68      /***
69       * Convert the value specified by row, column to a string. This method is ideally suited to be overidden by
70       * extensions of the textcellrenderer.
71       * 
72       * @param row row of the cell
73       * @param column column of the cell
74       * @return String for the value
75       */
76      protected String convertValue(IRow row, IColumn column) {
77          Object value = column.getValue(row);
78          return value != null ? value.toString() : null;
79      }
80  
81      /***
82       * Override for using content marker and tooltip.
83       * 
84       * @param row row of the cell
85       * @param column column of the cell
86       * @return comment as String or <code>null</code>
87       */
88      protected String getComment(IRow row, IColumn column) {
89          return null;
90      }
91  
92      /***
93       * {@inheritDoc}
94       */
95      public void draw(GC gc, JaretTable jaretTable, ICellStyle cellStyle, Rectangle drawingArea, IRow row,
96              IColumn column, boolean drawFocus, boolean selected, boolean printing) {
97          
98          drawBackground(gc, drawingArea, cellStyle, selected, printing);
99          Rectangle drect = drawBorder(gc, cellStyle, drawingArea, printing);
100         Rectangle rect = applyInsets(drect);
101 
102         // convert the value to a string
103         String s = convertValue(row, column);
104 
105         Color fg = gc.getForeground();
106         Color bg = gc.getBackground();
107         Font font = gc.getFont();
108 
109         if (s != null) {
110             if (selected && !printing) {
111                 gc.setBackground(SELECTIONCOLOR);
112             } else {
113                 gc.setBackground(getBackgroundColor(cellStyle, printing));
114             }
115             gc.setForeground(getForegroundColor(cellStyle, printing));
116             gc.setFont(getFont(cellStyle, printing, gc.getFont()));
117 
118             drawCellString(gc, rect, s, cellStyle);
119         }
120 
121         // draw comment marker if comment is present and not printing
122         if (!printing && getComment(row, column) != null) {
123             drawCommentMarker(gc, drawingArea, _commentColor, COMMENTMARKER_SIZE);
124         }
125 
126         if (drawFocus) {
127             drawFocus(gc, drect);
128         }
129         drawSelection(gc, drawingArea, cellStyle, selected, printing);
130 
131         gc.setForeground(fg);
132         gc.setBackground(bg);
133         gc.setFont(font);
134 
135     }
136 
137     /***
138      * Draw the string.
139      * 
140      * @param gc gc
141      * @param rect drawing area
142      * @param s String to drw
143      * @param cellStyle the cell style
144      */
145     private void drawCellString(GC gc, Rectangle rect, String s, ICellStyle cellStyle) {
146         if (cellStyle.getMultiLine()) {
147             drawCellStringMulti(gc, rect, s, cellStyle);
148         } else {
149             drawCellStringSingle(gc, rect, s);
150         }
151     }
152 
153     /***
154      * Draw single line String.
155      * 
156      * @param gc gc
157      * @param rect drawing area
158      * @param s String to draw
159      */
160     private void drawCellStringSingle(GC gc, Rectangle rect, String s) {
161         gc.drawString(s, rect.x, rect.y + 10, true);
162     }
163 
164     /***
165      * Draw a String in the drawing area, splitting it into multiple lines.
166      * 
167      * @param gc gc
168      * @param rect drawing area
169      * @param s String to draw
170      * @param cellStyle cell style determing alignment
171      */
172     private void drawCellStringMulti(GC gc, Rectangle rect, String s, ICellStyle cellStyle) {
173         int halign = TextRenderer.LEFT;
174         if (cellStyle.getHorizontalAlignment() == ITableViewState.HAlignment.RIGHT) {
175             halign = TextRenderer.RIGHT;
176         } else if (cellStyle.getHorizontalAlignment() == ITableViewState.HAlignment.CENTER) {
177             halign = TextRenderer.CENTER;
178         }
179         int valign = TextRenderer.TOP;
180         if (cellStyle.getVerticalAlignment() == ITableViewState.VAlignment.BOTTOM) {
181             valign = TextRenderer.BOTTOM;
182         } else if (cellStyle.getVerticalAlignment() == ITableViewState.VAlignment.CENTER) {
183             valign = TextRenderer.CENTER;
184         }
185 
186         TextRenderer.renderText(gc, rect, true, false, s, halign, valign);
187     }
188 
189     /***
190      * {@inheritDoc}
191      */
192     public int getPreferredHeight(GC gc, ICellStyle cellStyle, int width, IRow row, IColumn column) {
193         Object value = convertValue(row, column);
194         Font font = gc.getFont();
195         int height = -1;
196         if (value != null) {
197             String s = value.toString();
198             gc.setFont(getFont(cellStyle, false, gc.getFont()));
199             height = TextRenderer.getHeight(gc, getInnerWidth(width, cellStyle), true, s);
200         }
201         gc.setFont(font);
202         return height + getVerticalSpacesSum(cellStyle);
203     }
204 
205     /***
206      * {@inheritDoc}
207      */
208     public void dispose() {
209         // nothing to dispose
210     }
211 
212     /***
213      * {@inheritDoc}
214      */
215     public ICellRenderer createPrintRenderer(Printer printer) {
216         return new TextCellRenderer(printer);
217     }
218 
219 }