View Javadoc

1   /*
2    *  File: ImageCellRenderer.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.Image;
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.ui.table.JaretTable;
22  import de.jaret.util.ui.table.model.IColumn;
23  import de.jaret.util.ui.table.model.IRow;
24  
25  /***
26   * CellRenderer rendering an image.
27   * 
28   * @author Peter Kliem
29   * @version $Id: ImageCellRenderer.java 179 2007-01-07 17:37:50Z olk $
30   */
31  public class ImageCellRenderer extends CellRendererBase implements ICellRenderer {
32  
33      /***
34       * Construct an image cell renderer for use with a printer.
35       * 
36       * @param printer printer
37       */
38      public ImageCellRenderer(Printer printer) {
39          super(printer);
40      }
41  
42      /***
43       * Construct an image cell renderer for display use.
44       * 
45       */
46      public ImageCellRenderer() {
47          super(null);
48      }
49  
50      /***
51       * {@inheritDoc}
52       */
53      public void draw(GC gc, JaretTable jaretTable, ICellStyle cellStyle, Rectangle drawingArea, IRow row,
54              IColumn column, boolean drawFocus, boolean selected, boolean printing) {
55          drawBackground(gc, drawingArea, cellStyle, selected, printing);
56          Rectangle drect = drawBorder(gc, cellStyle, drawingArea, printing);
57          Rectangle rect = applyInsets(drect);
58          Object value = column.getValue(row);
59          if (value != null) {
60              if (value instanceof Image) {
61                  Image img = (Image) value;
62                  int x = rect.x + (rect.width - img.getBounds().width) / 2;
63                  int y = rect.y + (rect.height - img.getBounds().height) / 2;
64                  gc.drawImage(img, x, y);
65              } else {
66                  // indicate error with red fill
67                  Color bg = gc.getBackground();
68                  gc.setBackground(Display.getCurrent().getSystemColor(SWT.COLOR_RED));
69                  gc.fillRectangle(rect);
70                  gc.setBackground(bg);
71              }
72          }
73          if (drawFocus) {
74              drawFocus(gc, drawingArea);
75          }
76          drawSelection(gc, drawingArea, cellStyle, selected, printing);
77  
78      }
79  
80      // public int getPreferredWidth(List<IRow> rows, IColumn column) {
81      // int max = 0;
82      // for (IRow row : rows) {
83      // Image img = (Image)column.getValue(row);
84      // if (img.getBounds().width > max) {
85      // max = img.getBounds().width;
86      // }
87      // }
88      // return max;
89      // }
90      // public int getPreferredHeight(IRow row, IColumn column) {
91      // Image img = (Image)column.getValue(row);
92      // return img.getBounds().height;
93      // }
94      //
95  
96      /***
97       * {@inheritDoc}
98       */
99      public void dispose() {
100         // nothing to dispose
101     }
102 
103     /***
104      * {@inheritDoc}
105      */
106     public ICellRenderer createPrintRenderer(Printer printer) {
107         return new ImageCellRenderer(printer);
108     }
109 
110 }