View Javadoc

1   /*
2    *  File: CheckBoxHeaderRenderer.java 
3    *  Copyright (c) 2004-2008  Peter Kliem (Peter.Kliem@jaret.de)
4    *  A commercial license is available, see http://www.jaret.de.
5    *
6    *  This program is free software; you can redistribute it and/or modify
7    *  it under the terms of the GNU General Public License as published by
8    *  the Free Software Foundation; either version 2 of the License, or
9    *  (at your option) any later version.
10   *
11   *  This program is distributed in the hope that it will be useful,
12   *  but WITHOUT ANY WARRANTY; without even the implied warranty of
13   *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14   *  GNU General Public License for more details.
15   *
16   *  You should have received a copy of the GNU General Public License
17   *  along with this program; if not, write to the Free Software
18   *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19   */
20  package de.jaret.examples.timebars.simple.swt.renderer;
21  
22  import org.eclipse.jface.resource.ImageDescriptor;
23  import org.eclipse.jface.resource.ImageRegistry;
24  import org.eclipse.swt.SWT;
25  import org.eclipse.swt.graphics.Color;
26  import org.eclipse.swt.graphics.GC;
27  import org.eclipse.swt.graphics.Image;
28  import org.eclipse.swt.graphics.Rectangle;
29  import org.eclipse.swt.printing.Printer;
30  
31  import de.jaret.util.swt.SwtGraphicsHelper;
32  import de.jaret.util.ui.ResourceImageDescriptor;
33  import de.jaret.util.ui.timebars.TimeBarViewerDelegate;
34  import de.jaret.util.ui.timebars.TimeBarViewerInterface;
35  import de.jaret.util.ui.timebars.model.TimeBarRow;
36  import de.jaret.util.ui.timebars.model.TimeBarRowHeader;
37  import de.jaret.util.ui.timebars.swt.renderer.HeaderRenderer;
38  import de.jaret.util.ui.timebars.swt.renderer.RendererBase;
39  
40  /***
41   * Simple heder renderer rendering the selection with an imitated checkbox. Obviously best used with 
42   * the sleection models row selction toggle mode.
43   * 
44   * @author Peter Kliem
45   * @version $Id: CheckBoxHeaderRenderer.java 751 2008-04-22 20:10:24Z kliem $
46   */
47  public class CheckBoxHeaderRenderer extends RendererBase implements HeaderRenderer {
48      /*** line width when printing. */
49      private static final int PRINTING_LINEWIDTH = 3;
50      /*** distance of the checkbox. */
51      private static final int INSETS = 2;
52  
53      /*** rsc name for the checked state. */
54      protected String _checkedRscName = "/de/jaret/examples/timebars/simple/swt/renderer/checked.gif";
55      /*** default rsc name for the unchecked state. */
56      protected String _uncheckedRscName = "/de/jaret/examples/timebars/simple/swt/renderer/unchecked.gif";
57      /*** key for checked image in registry. */
58      protected static final String CHECKED = "checked";
59      /*** key for unchecked image in registry. */
60      protected static final String UNCHECKED = "unchecked";
61      /*** image registry for holding the images. */
62      private ImageRegistry _imageRegistry;
63  
64      /***
65       * Constructor for printing use.
66       * 
67       * @param printer printing device
68       */
69      public CheckBoxHeaderRenderer(Printer printer) {
70          super(printer);
71      }
72  
73      /***
74       * Constructor for screen use.
75       * 
76       */
77      public CheckBoxHeaderRenderer() {
78          super(null);
79      }
80  
81      /***
82       * {@inheritDoc}
83       */
84      public void draw(GC gc, Rectangle drawingArea, TimeBarViewerDelegate delegate, TimeBarRowHeader header,
85              boolean selected, boolean printing) {
86  
87          if (printing) {
88              gc.setLineWidth(PRINTING_LINEWIDTH);
89          }
90  
91          // draw background
92          Color bg = gc.getBackground();
93          Color fg = gc.getForeground();
94          gc.setForeground(gc.getDevice().getSystemColor(SWT.COLOR_WHITE));
95          gc.fillRectangle(drawingArea);
96          gc.setForeground(fg);
97  
98          // retrieve the checkbox image and draw it
99          Image img = null;
100         if (selected) {
101             img = getImageRegistry().get(CHECKED);
102         } else {
103             img = getImageRegistry().get(UNCHECKED);
104         }
105         int x = drawingArea.x + scaleX(INSETS);
106         int y = drawingArea.y + scaleY(INSETS);
107         gc.drawImage(img, 0, 0, img.getBounds().width, img.getBounds().height, x, y, scaleX(img.getBounds().width),
108                 scaleY(img.getBounds().height));
109 
110         int offx = scaleX(img.getBounds().width) + scaleX(INSETS);
111         String str = header.toString();
112         SwtGraphicsHelper.drawStringCenteredVCenter(gc, str, drawingArea.x + offx, drawingArea.x + drawingArea.width
113                 - offx, drawingArea.y + drawingArea.height / 2);
114 
115         gc.setBackground(bg);
116         if (delegate.getDrawRowGrid()) {
117             if (delegate.getOrientation() == TimeBarViewerInterface.Orientation.HORIZONTAL) {
118                 gc.drawLine(drawingArea.x, drawingArea.y + drawingArea.height - 1, drawingArea.x + drawingArea.width,
119                         drawingArea.y + drawingArea.height - 1);
120             } else {
121                 gc.drawLine(drawingArea.x + drawingArea.width - 1, drawingArea.y,
122                         drawingArea.x + drawingArea.width - 1, drawingArea.y + drawingArea.height - 1);
123             }
124         }
125     }
126 
127     /***
128      * {@inheritDoc}
129      */
130     public String getToolTipText(TimeBarRow row, Rectangle drawingArea, int x, int y) {
131         if (row == null) {
132             return null;
133         }
134         return row.getRowHeader().getLabel();
135     }
136 
137     /***
138      * {@inheritDoc}
139      */
140     public boolean contains(Rectangle drawingArea, int x, int y) {
141         return true;
142     }
143 
144     /***
145      * Retrieve the image registry used by the renderer (lazy initializing).
146      * 
147      * @return initialized image regsitry containing the resources
148      */
149     private ImageRegistry getImageRegistry() {
150         if (_imageRegistry == null) {
151             _imageRegistry = new ImageRegistry();
152             ImageDescriptor imgDesc = new ResourceImageDescriptor(_checkedRscName, this.getClass());
153             _imageRegistry.put(CHECKED, imgDesc.createImage());
154             imgDesc = new ResourceImageDescriptor(_uncheckedRscName, this.getClass());
155             _imageRegistry.put(UNCHECKED, imgDesc.createImage());
156         }
157         return _imageRegistry;
158     }
159 
160     /***
161      * {@inheritDoc}
162      */
163     public void dispose() {
164         if (_imageRegistry != null) {
165             _imageRegistry.dispose();
166         }
167     }
168 
169     /***
170      * {@inheritDoc}
171      */
172     public HeaderRenderer createPrintRenderer(Printer printer) {
173         return new CheckBoxHeaderRenderer(printer);
174     }
175 }