View Javadoc

1   /*
2    *  File: BooleanCellRenderer.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.jface.resource.ImageDescriptor;
16  import org.eclipse.jface.resource.ImageRegistry;
17  import org.eclipse.swt.SWT;
18  import org.eclipse.swt.graphics.Color;
19  import org.eclipse.swt.graphics.GC;
20  import org.eclipse.swt.graphics.Image;
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.ui.ResourceImageDescriptor;
26  import de.jaret.util.ui.table.JaretTable;
27  import de.jaret.util.ui.table.model.IColumn;
28  import de.jaret.util.ui.table.model.IRow;
29  
30  /***
31   * CellRenderer rendering a Boolean to a checkbox image (default) or any other two images.
32   * 
33   * @author Peter Kliem
34   * @version $Id: BooleanCellRenderer.java 479 2007-06-01 22:01:50Z olk $
35   */
36  public class BooleanCellRenderer extends CellRendererBase implements ICellRenderer {
37      /*** rsc name for the checked state. */
38      protected String _checkedRscName = "/de/jaret/util/ui/table/resource/checked.gif";
39      /*** default rsc name for the unchecked state. */
40      protected String _uncheckedRscName = "/de/jaret/util/ui/table/resource/unchecked.gif";
41      /*** key for checked image in registry. */
42      protected static final String CHECKED = "checked";
43      /*** key for unchecked image in registry. */
44      protected static final String UNCHECKED = "unchecked";
45      /*** image registry for holding the images. */
46      private ImageRegistry _imageRegistry;
47  
48      /***
49       * Construct a boolean cell renderer for a printer device using default resources.
50       * 
51       * @param printer printer device
52       */
53      public BooleanCellRenderer(Printer printer) {
54          super(printer);
55      }
56  
57      /***
58       * Construct a boolean cell renderer for the display using default resources.
59       */
60      public BooleanCellRenderer() {
61          super(null);
62      }
63  
64      /***
65       * Construct a boolean cell renderer for a printer device providing resource names.
66       * 
67       * @param printer printer device
68       * @param checkedRscName resource path for the checked image
69       * @param uncheckedRscName resource path for the unchecked image
70       */
71      public BooleanCellRenderer(Printer printer, String checkedRscName, String uncheckedRscName) {
72          super(printer);
73          _checkedRscName = checkedRscName;
74          _uncheckedRscName = uncheckedRscName;
75      }
76  
77      /***
78       * Construct a boolean cell renderer for the display providing resource names.
79       * 
80       * @param checkedRscName resource path for the checked image
81       * @param uncheckedRscName resource path for the unchecked image
82       */
83      public BooleanCellRenderer(String checkedRscName, String uncheckedRscName) {
84          super(null);
85          _checkedRscName = checkedRscName;
86          _uncheckedRscName = uncheckedRscName;
87      }
88  
89      /***
90       * {@inheritDoc}
91       */
92      public void draw(GC gc, JaretTable jaretTable, ICellStyle cellStyle, Rectangle drawingArea, IRow row,
93              IColumn column, boolean drawFocus, boolean selected, boolean printing) {
94          drawBackground(gc, drawingArea, cellStyle, selected, printing);
95          Rectangle drect = drawBorder(gc, cellStyle, drawingArea, printing);
96          Rectangle rect = applyInsets(drect);
97          Object value = column.getValue(row);
98          if (value instanceof Boolean) {
99              Image img = null;
100             if (((Boolean) value).booleanValue()) {
101                 img = getImageRegistry().get(CHECKED);
102             } else {
103                 img = getImageRegistry().get(UNCHECKED);
104             }
105             int x = rect.x + (rect.width - scaleX(img.getBounds().width)) / 2;
106             int y = rect.y + (rect.height - scaleY(img.getBounds().height)) / 2;
107             gc.drawImage(img, 0, 0, img.getBounds().width, img.getBounds().height, x, y, scaleX(img.getBounds().width),
108                     scaleY(img.getBounds().height));
109         } else {
110             // indicate error with red fill
111             Color bg = gc.getBackground();
112             gc.setBackground(Display.getCurrent().getSystemColor(SWT.COLOR_MAGENTA));
113             gc.fillRectangle(rect);
114             gc.setBackground(bg);
115         }
116         if (drawFocus) {
117             drawFocus(gc, drect);
118         }
119         drawSelection(gc, drawingArea, cellStyle, selected, printing);
120     }
121 
122     /***
123      * {@inheritDoc}
124      */
125     public int getPreferredWidth(List<IRow> rows, IColumn column) {
126         return getImageRegistry().get(CHECKED).getBounds().width;
127     }
128 
129     /***
130      * {@inheritDoc}
131      */
132     public int getPreferredHeight(GC gc, ICellStyle cellStyle, int width, IRow row, IColumn column) {
133         return getImageRegistry().get(CHECKED).getBounds().height;
134     }
135 
136     /***
137      * Retrieve the image registry used by the renderer (lazy initializing).
138      * 
139      * @return initialized image regsitry containing the resources
140      */
141     private ImageRegistry getImageRegistry() {
142         if (_imageRegistry == null) {
143             _imageRegistry = new ImageRegistry();
144             ImageDescriptor imgDesc = new ResourceImageDescriptor(_checkedRscName, this.getClass());
145             _imageRegistry.put(CHECKED, imgDesc.createImage());
146             imgDesc = new ResourceImageDescriptor(_uncheckedRscName, this.getClass());
147             _imageRegistry.put(UNCHECKED, imgDesc.createImage());
148         }
149         return _imageRegistry;
150     }
151 
152     /***
153      * {@inheritDoc}
154      */
155     public void dispose() {
156         if (_imageRegistry != null) {
157             _imageRegistry.dispose();
158         }
159     }
160 
161     /***
162      * {@inheritDoc}
163      */
164     public ICellRenderer createPrintRenderer(Printer printer) {
165         return new BooleanCellRenderer(printer, _checkedRscName, _uncheckedRscName);
166     }
167 
168 }