View Javadoc

1   /*
2    *  File: EventRenderer.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.examples.timebars.fancy.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  
28  import de.jaret.examples.timebars.fancy.model.FancyEvent;
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  import de.jaret.util.ui.timebars.swt.renderer.RendererBase;
34  import de.jaret.util.ui.timebars.swt.renderer.TimeBarRenderer;
35  import de.jaret.util.ui.timebars.swt.renderer.TimeBarRenderer2;
36  
37  /***
38   * Renderer rendering a point in time as a simple diamod and a label (using extended painting area).
39   * 
40   * @author Peter Kliem
41   * @version $Id: FancyEventRenderer.java 565 2007-09-16 13:25:48Z olk $
42   */
43  public class FancyEventRenderer extends RendererBase implements TimeBarRenderer, TimeBarRenderer2 {
44      /*** size of the drawn element. */
45      private static final int SIZE = 10;
46      /*** extend for the label. */
47      private static final int MAXLABELWIDTH = 50;
48      /*** pixeloffset for the label drawing. */
49      private static final int LABELOFFSET = 3;
50  
51      /*** corrected size (for printing). */
52      private int _size = SIZE;
53  
54      /*** cache for the delegate supplying the orientation information. */
55      protected TimeBarViewerDelegate _delegate;
56  
57      /***
58       * Create renderer for printing.
59       * 
60       * @param printer printer device
61       */
62      public FancyEventRenderer(Printer printer) {
63          super(printer);
64          _size = scaleX(SIZE);
65      }
66  
67      /***
68       * Construct renderer for screen use.
69       * 
70       */
71      public FancyEventRenderer() {
72          super(null);
73      }
74  
75      /***
76       * {@inheritDoc}
77       */
78      public Rectangle getPreferredDrawingBounds(Rectangle intervalDrawingArea, TimeBarViewerDelegate delegate,
79              Interval interval, boolean selected, boolean printing, boolean overlap) {
80  
81          boolean horizontal = delegate.getOrientation() == TimeBarViewerInterface.Orientation.HORIZONTAL;
82          if (horizontal) {
83              return new Rectangle(intervalDrawingArea.x - _size, intervalDrawingArea.y, intervalDrawingArea.width + 2
84                      * _size + scaleX(MAXLABELWIDTH), intervalDrawingArea.height);
85          } else {
86              return new Rectangle(intervalDrawingArea.x, intervalDrawingArea.y - _size, intervalDrawingArea.width,
87                      intervalDrawingArea.height + 2 * _size + scaleY(MAXLABELWIDTH));
88          }
89      }
90  
91      /***
92       * {@inheritDoc}
93       */
94      public void draw(GC gc, Rectangle drawingArea, TimeBarViewerDelegate delegate, Interval interval, boolean selected,
95              boolean printing, boolean overlap) {
96          _delegate = delegate;
97          defaultDraw(gc, drawingArea, delegate, interval, selected, printing, overlap);
98      }
99  
100     /***
101      * {@inheritDoc}
102      */
103     public String getToolTipText(Interval interval, Rectangle drawingArea, int x, int y, boolean overlapping) {
104         return getToolTipText(_delegate, interval, drawingArea, x, y, overlapping);
105     }
106 
107     /***
108      * {@inheritDoc}
109      */
110     public boolean contains(Interval interval, Rectangle drawingArea, int x, int y, boolean overlapping) {
111         return contains(_delegate, interval, drawingArea, x, y, overlapping);
112     }
113 
114     /***
115      * {@inheritDoc}
116      */
117     public Rectangle getContainingRectangle(Interval interval, Rectangle drawingArea, boolean overlapping) {
118         return getContainingRectangle(_delegate, interval, drawingArea, overlapping);
119     }
120 
121     /***
122      * {@inheritDoc}. Will create print renderes for all registered renderers.
123      */
124     public TimeBarRenderer createPrintrenderer(Printer printer) {
125         FancyEventRenderer renderer = new FancyEventRenderer(printer);
126         return renderer;
127     }
128 
129     /***
130      * {@inheritDoc}
131      */
132     public void dispose() {
133     }
134 
135     /***
136      * Drawing method for default rendering.
137      * 
138      * @param gc GC
139      * @param drawingArea drawingArea
140      * @param delegate delegate
141      * @param interval interval to draw
142      * @param selected true for selected drawing
143      * @param printing true for printing
144      * @param overlap true if the interval overlaps with other intervals
145      */
146     private void defaultDraw(GC gc, Rectangle drawingArea, TimeBarViewerDelegate delegate, Interval interval,
147             boolean selected, boolean printing, boolean overlap) {
148 
149         boolean horizontal = delegate.getOrientation() == TimeBarViewerInterface.Orientation.HORIZONTAL;
150         Rectangle da = getDrawingRect(drawingArea, horizontal);
151 
152         // draw focus
153         drawFocus(gc, da, delegate, interval, selected, printing, overlap);
154 
155         Color bg = gc.getBackground();
156 
157         // draw the diamond
158         gc.setBackground(gc.getDevice().getSystemColor(SWT.COLOR_GRAY));
159 
160         int[] points = new int[] {da.x, da.y + da.height / 2, da.x + da.width / 2, da.y, da.x + da.width,
161                 da.y + da.height / 2, da.x + da.width / 2, da.y + da.height};
162 
163         gc.fillPolygon(points);
164         gc.drawPolygon(points);
165 
166         if (selected) {
167             gc.setBackground(gc.getDevice().getSystemColor(SWT.COLOR_BLUE));
168             gc.setAlpha(60);
169             gc.fillPolygon(points);
170             gc.setAlpha(255);
171         }
172         gc.setBackground(bg);
173 
174         FancyEvent fe = (FancyEvent) interval;
175 
176         // draw the label
177         if (horizontal) {
178             SwtGraphicsHelper.drawStringVCentered(gc, fe.getLabel(), da.x + da.width + scaleX(LABELOFFSET), da.y, da.y
179                     + da.height);
180         } else {
181             SwtGraphicsHelper.drawStringCentered(gc, fe.getLabel(), da.x + da.width / 2, da.y + da.height
182                     + scaleY(LABELOFFSET) + gc.textExtent(fe.getLabel()).y);
183         }
184 
185     }
186 
187     /***
188      * Calculate the drawing area for the marking symbol.
189      * 
190      * @param drawingArea drawing area as given for the time
191      * @return Rectangle for drawing the main symbol
192      */
193     private Rectangle getDrawingRect(Rectangle drawingArea, boolean horizontal) {
194         if (horizontal) {
195             int y = drawingArea.y + (drawingArea.height - 2 * _size) / 2;
196             return new Rectangle(drawingArea.x - _size, y, 2 * _size, 2 * _size);
197         } else {
198             int x = drawingArea.x + (drawingArea.width - 2 * _size) / 2;
199             return new Rectangle(x, drawingArea.y - _size, 2 * _size, 2 * _size);
200         }
201     }
202 
203     public String getToolTipText(TimeBarViewerDelegate delegate, Interval interval, Rectangle drawingArea, int x,
204             int y, boolean overlapping) {
205         if (contains(delegate, interval, drawingArea, x, y, overlapping)) {
206             return interval.toString();
207         }
208         return null;
209     }
210 
211     public boolean contains(TimeBarViewerDelegate delegate, Interval interval, Rectangle drawingArea, int x, int y,
212             boolean overlapping) {
213         boolean horizontal = delegate.getOrientation() == TimeBarViewerInterface.Orientation.HORIZONTAL;
214         Rectangle da = getDrawingRect(drawingArea, horizontal);
215         return da.contains(drawingArea.x + x, drawingArea.y + y);
216     }
217 
218     public Rectangle getContainingRectangle(TimeBarViewerDelegate delegate, Interval interval, Rectangle drawingArea,
219             boolean overlapping) {
220         boolean horizontal = delegate.getOrientation() == TimeBarViewerInterface.Orientation.HORIZONTAL;
221         Rectangle da = getDrawingRect(drawingArea, horizontal);
222         return da;
223     }
224 
225 }