View Javadoc

1   /*
2    *  File: GlobalBreakRenderer.java 
3    *  Copyright (c) 2004-2009  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.millis.swt.renderer;
21  
22  import java.awt.Rectangle;
23  
24  import org.eclipse.swt.SWT;
25  import org.eclipse.swt.graphics.Color;
26  import org.eclipse.swt.graphics.Device;
27  import org.eclipse.swt.graphics.GC;
28  import org.eclipse.swt.printing.Printer;
29  
30  import de.jaret.util.date.Interval;
31  import de.jaret.util.date.IntervalImpl;
32  import de.jaret.util.ui.timebars.TimeBarViewerDelegate;
33  import de.jaret.util.ui.timebars.model.PPSInterval;
34  import de.jaret.util.ui.timebars.swt.renderer.GlobalAssistantRenderer;
35  import de.jaret.util.ui.timebars.swt.renderer.RendererBase;
36  
37  /***
38   * GlobalAssistantRenderer that renders breaks in the time line.
39   * 
40   * @author kliem
41   * @version $Id: GlobalBreakRenderer.java 836 2009-02-14 21:24:39Z kliem $
42   */
43  public class GlobalBreakRenderer extends RendererBase implements GlobalAssistantRenderer {
44      /*** Color used to draw the gaps. */
45      protected Color _gapColor;
46  
47      /***
48       * Construct the renderer for a printer.
49       * 
50       * @param printer printer device
51       */
52      public GlobalBreakRenderer(Printer printer) {
53          super(printer);
54      }
55  
56      /***
57       * Construct the renderer for diplay use.
58       */
59      public GlobalBreakRenderer() {
60          super(null);
61      }
62  
63      /***
64       * {@inheritDoc}
65       */
66      public GlobalAssistantRenderer createPrintRenderer(Printer printer) {
67          return new GlobalBreakRenderer(printer);
68      }
69  
70      /***
71       * {@inheritDoc}
72       */
73      public void dispose() {
74          if (_gapColor != null) {
75              _gapColor.dispose();
76          }
77      }
78  
79      /***
80       * {@inheritDoc} Nothing to do.
81       */
82      public void doRenderingBeforeIntervals(TimeBarViewerDelegate delegate, GC gc, boolean printing) {
83          // do nothing
84      }
85  
86      /***
87       * {@inheritDoc} Renders the gaps as gray areas.
88       */
89      public void doRenderingLast(TimeBarViewerDelegate delegate, GC gc, boolean printing) {
90          // safety check
91          if (!delegate.hasVariableXScale()) {
92              return;
93          }
94          aquireColors(gc.getDevice());
95          Color bg = gc.getBackground();
96  
97          gc.setBackground(_gapColor);
98  
99          Rectangle rect = delegate.getDiagramRect();
100         Interval display = new IntervalImpl(delegate.getStartDate().copy(), delegate.getEndDate().copy());
101         for (Interval interval : delegate.getPpsRow().getIntervals()) {
102             PPSInterval ppsInterval = (PPSInterval) interval;
103             if (ppsInterval.isBreak() && display.intersects(ppsInterval)) {
104                 int startx = delegate.xForDate(ppsInterval.getBegin());
105                 int endx = delegate.xForDate(ppsInterval.getEnd());
106                 if (startx < rect.x) {
107                     startx = rect.x;
108                 }
109                 if (endx > rect.x + rect.width) {
110                     endx = rect.x + rect.width;
111                 }
112 
113                 int starty = rect.y;
114                 int endy = rect.y + rect.height;
115 
116                 // draw the area
117                 org.eclipse.swt.graphics.Rectangle r = new org.eclipse.swt.graphics.Rectangle(startx, starty, endx
118                         - startx, endy);
119                 gc.fillRectangle(r);
120 
121                 // draw dashed lines
122                 gc.setLineStyle(SWT.LINE_DASH);
123                 gc.drawLine(startx, starty, startx, endy);
124                 gc.drawLine(endx, starty, endx, endy);
125 
126             }
127         }
128 
129         gc.setLineStyle(SWT.LINE_SOLID);
130         gc.setBackground(bg);
131 
132     }
133 
134     /***
135      * Get the colors needed.
136      * 
137      * @param device device to aquire the colors for
138      */
139     private void aquireColors(Device device) {
140         if (_gapColor == null) {
141             _gapColor = new Color(device, 220, 220, 220);
142         }
143     }
144 
145 }