View Javadoc

1   /*
2    *  File: DefaultRenderer.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    *  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.util.ui.timebars.swt.renderer;
21  
22  import org.eclipse.swt.SWT;
23  import org.eclipse.swt.graphics.Color;
24  import org.eclipse.swt.graphics.GC;
25  import org.eclipse.swt.graphics.Rectangle;
26  import org.eclipse.swt.printing.Printer;
27  import org.eclipse.swt.widgets.Display;
28  
29  import de.jaret.util.date.Interval;
30  import de.jaret.util.swt.SwtGraphicsHelper;
31  import de.jaret.util.ui.timebars.TimeBarViewerDelegate;
32  import de.jaret.util.ui.timebars.TimeBarViewerInterface;
33  
34  /**
35   * DefaultRenderer for the TimeBarViewer widget. It renders intervals as grey/blue bars.
36   * 
37   * @author Peter Kliem
38   * @version $Id: DefaultRenderer.java 802 2008-12-28 12:30:41Z kliem $
39   */
40  public class DefaultRenderer extends AbstractTimeBarRenderer {
41      /** width or height times this factor = perentage used as the non painted border. */
42      protected static final double BORDERFACTOR = 0.2;
43  
44      /** cache for the delegate supplying the orientation information. */
45      protected TimeBarViewerDelegate _delegate;
46  
47      /**
48       * Create renderer for printing.
49       * 
50       * @param printer printer device
51       */
52      public DefaultRenderer(Printer printer) {
53          super(printer);
54      }
55  
56      /**
57       * Construct renderer for screen use.
58       * 
59       */
60      public DefaultRenderer() {
61          super(null);
62      }
63  
64      /**
65       * {@inheritDoc}
66       */
67      public void draw(GC gc, Rectangle drawingArea, TimeBarViewerDelegate delegate, Interval interval, boolean selected,
68              boolean printing, boolean overlap) {
69          _delegate = delegate;
70          if (!printing) {
71              defaultDraw(gc, drawingArea, delegate, interval, selected, printing, overlap);
72          } else {
73              print(gc, drawingArea, delegate, interval, overlap);
74          }
75      }
76  
77      /**
78       * {@inheritDoc}
79       */
80      public String getToolTipText(Interval interval, Rectangle drawingArea, int x, int y, boolean overlapping) {
81          return getToolTipText(_delegate, interval, drawingArea, x, y, overlapping);
82      }
83  
84      /**
85       * {@inheritDoc}
86       */
87      public boolean contains(Interval interval, Rectangle drawingArea, int x, int y, boolean overlapping) {
88          return contains(_delegate, interval, drawingArea, x, y, overlapping);
89      }
90  
91      /**
92       * {@inheritDoc}
93       */
94      public Rectangle getContainingRectangle(Interval interval, Rectangle drawingArea, boolean overlapping) {
95          return getContainingRectangle(_delegate, interval, drawingArea, overlapping);
96      }
97  
98      /**
99       * {@inheritDoc}. Will create print renderes for all registered renderers.
100      */
101     public TimeBarRenderer createPrintrenderer(Printer printer) {
102         DefaultRenderer renderer = new DefaultRenderer(printer);
103         return renderer;
104     }
105 
106     /**
107      * {@inheritDoc}
108      */
109     public void dispose() {
110     }
111 
112     /**
113      * Drawing method for default rendering.
114      * 
115      * @param gc GC
116      * @param drawingArea drawingArea
117      * @param delegate delegate
118      * @param interval interval to draw
119      * @param selected true for selected drawing
120      * @param printing true for printing
121      * @param overlap true if the interval overlaps with other intervals
122      */
123     private void defaultDraw(GC gc, Rectangle drawingArea, TimeBarViewerDelegate delegate, Interval interval,
124             boolean selected, boolean printing, boolean overlap) {
125         // draw focus
126         drawFocus(gc, drawingArea, delegate, interval, selected, printing, overlap);
127 
128         boolean horizontal = delegate.getOrientation() == TimeBarViewerInterface.Orientation.HORIZONTAL;
129         Rectangle iRect = getIRect(horizontal, drawingArea, overlap);
130 
131         Color bg = gc.getBackground();
132         String str = interval.toString();
133 
134         if (!selected) {
135             gc.setBackground(Display.getCurrent().getSystemColor(SWT.COLOR_GRAY));
136         } else {
137             gc.setBackground(Display.getCurrent().getSystemColor(SWT.COLOR_BLUE));
138         }
139         gc.fillRectangle(iRect);
140         gc.drawRectangle(iRect);
141         SwtGraphicsHelper.drawStringCentered(gc, str, iRect);
142 
143         gc.setBackground(bg);
144     }
145 
146     /**
147      * {@inheritDoc}
148      */
149     public String getToolTipText(TimeBarViewerDelegate delegate, Interval interval, Rectangle drawingArea, int x,
150             int y, boolean overlapping) {
151         if (contains(delegate, interval, drawingArea, x, y, overlapping)) {
152             return interval.toString();
153         }
154         return null;
155     }
156 
157     /**
158      * {@inheritDoc}
159      */
160     public boolean contains(TimeBarViewerDelegate delegate, Interval interval, Rectangle drawingArea, int x, int y,
161             boolean overlapping) {
162 
163         boolean horizontal = delegate.getOrientation() == TimeBarViewerInterface.Orientation.HORIZONTAL;
164         Rectangle iRect = getIRect(horizontal, drawingArea, overlapping);
165         return iRect.contains(drawingArea.x + x, drawingArea.y + y);
166     }
167 
168     /**
169      * {@inheritDoc}
170      */
171     public Rectangle getContainingRectangle(TimeBarViewerDelegate delegate, Interval interval, Rectangle drawingArea,
172             boolean overlapping) {
173 
174         boolean horizontal = delegate.getOrientation() == TimeBarViewerInterface.Orientation.HORIZONTAL;
175         Rectangle iRect = getIRect(horizontal, drawingArea, overlapping);
176         return iRect;
177     }
178 
179     /**
180      * Print the rendered interval.
181      * @param gc GC
182      * @param drawingArea area of the interval
183      * @param delegate the delegate
184      * @param interval the interval
185      * @param overlap true if overlapping
186      */
187     private void print(GC gc, Rectangle drawingArea, TimeBarViewerDelegate delegate, Interval interval, boolean overlap) {
188         boolean horizontal = delegate.getOrientation() == TimeBarViewerInterface.Orientation.HORIZONTAL;
189         Rectangle iRect = getIRect(horizontal, drawingArea, overlap);
190 
191         Color bg = gc.getBackground();
192         gc.setLineWidth(getDefaultLineWidth());
193         gc.setBackground(_printer.getSystemColor(SWT.COLOR_GRAY));
194         gc.fillRectangle(iRect);
195         gc.drawRectangle(iRect);
196         String str = interval.toString();
197         gc.setLineWidth(1);
198         SwtGraphicsHelper.drawStringCentered(gc, str, iRect);
199 
200         gc.setBackground(bg);
201     }
202 
203     /**
204      * Calculate the actual drawing rectangle for the interval usig the BORDERFACTOR to determine the border.
205      * 
206      * @param horizontal true for horizontal false for vertical
207      * @param drawingArea drawingArea
208      * @param overlap true if it is an overlapping interval
209      * @return the actual drawing rectangle
210      */
211     protected Rectangle getIRect(boolean horizontal, Rectangle drawingArea, boolean overlap) {
212         if (horizontal) {
213             int borderHeight = (int) (drawingArea.height * BORDERFACTOR / 2);
214             int height = drawingArea.height - (overlap ? 0 : 2 * borderHeight);
215             int y = drawingArea.y + (overlap ? 0 : borderHeight);
216             return new Rectangle(drawingArea.x, y, drawingArea.width - 1, height - 1);
217         } else {
218             int borderWidth = (int) (drawingArea.width * BORDERFACTOR / 2);
219             int width = drawingArea.width - (overlap ? 0 : 2 * borderWidth);
220             int x = drawingArea.x + (overlap ? 0 : borderWidth);
221             return new Rectangle(x, drawingArea.y, width - 1, drawingArea.height - 1);
222         }
223     }
224 
225 }