View Javadoc

1   /*
2    *  File: FancyGlobalRenderer.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 java.util.ArrayList;
23  import java.util.List;
24  
25  import org.eclipse.swt.SWT;
26  import org.eclipse.swt.graphics.Color;
27  import org.eclipse.swt.graphics.GC;
28  import org.eclipse.swt.graphics.Rectangle;
29  import org.eclipse.swt.printing.Printer;
30  
31  import de.jaret.examples.timebars.fancy.model.FancyInterval;
32  import de.jaret.util.date.Interval;
33  import de.jaret.util.ui.timebars.TimeBarViewerDelegate;
34  import de.jaret.util.ui.timebars.model.TimeBarRow;
35  import de.jaret.util.ui.timebars.swt.TimeBarViewer;
36  import de.jaret.util.ui.timebars.swt.renderer.GlobalAssistantRenderer;
37  import de.jaret.util.ui.timebars.swt.renderer.RendererBase;
38  
39  /***
40   * Gloabl renderer that marks intervals by drawing an oval around them.
41   * 
42   * @author kliem
43   * @version $Id: FancyGlobalRenderer.java 558 2007-09-08 07:40:22Z olk $
44   */
45  public class FancyGlobalRenderer extends RendererBase implements GlobalAssistantRenderer {
46  
47      private List<Interval> _intervalsToMark;
48      private boolean _drawHistory = false;
49  
50      public FancyGlobalRenderer() {
51          super(null);
52      }
53  
54      public FancyGlobalRenderer(Printer printer) {
55          super(printer);
56      }
57  
58      public GlobalAssistantRenderer createPrintRenderer(Printer printer) {
59          return new FancyGlobalRenderer(printer);
60      }
61  
62      public void dispose() {
63  
64      }
65  
66      public void doRenderingBeforeIntervals(TimeBarViewerDelegate delegate, GC gc, boolean printing) {
67          if (_drawHistory) {
68              for (int r = 0; r < delegate.getModel().getRowCount(); r++) {
69                  TimeBarRow row = delegate.getModel().getRow(r);
70                  if (delegate.isRowDisplayed(row)) {
71                      for (Interval interval : row.getIntervals()) {
72                          drawHistory(delegate, row, interval, gc);
73                      }
74                  }
75              }
76          }
77      }
78  
79      private void drawHistory(TimeBarViewerDelegate delegate, TimeBarRow row, Interval interval, GC gc) {
80          if (interval instanceof FancyInterval) {
81              Rectangle bounds = TimeBarViewer.convertRect(delegate.getIntervalBounds(row, interval));
82              int alpha = gc.getAlpha();
83              Color bg = gc.getBackground();
84              gc.setAlpha(30);
85              gc.setBackground(gc.getDevice().getSystemColor(SWT.COLOR_DARK_GREEN));
86              if (bounds != null) {
87                  for (Interval i : ((FancyInterval) interval).getHistory()) {
88                      Rectangle b = TimeBarViewer.convertRect(delegate.getIntervalBounds(row, i));
89                      gc.fillRectangle(b);
90                  }
91              }
92              gc.setAlpha(alpha);
93              gc.setBackground(bg);
94          }
95      }
96  
97      public void doRenderingLast(TimeBarViewerDelegate delegate, GC gc, boolean printing) {
98          if (_intervalsToMark != null) {
99              for (Interval interval : _intervalsToMark) {
100                 Rectangle bounds = TimeBarViewer.convertRect(delegate.getIntervalBounds(interval));
101                 if (bounds != null) {
102                     gc.setLineWidth(5);
103                     Color fg = gc.getForeground();
104                     gc.setForeground(gc.getDevice().getSystemColor(SWT.COLOR_GREEN));
105 
106                     gc.drawOval(bounds.x - 20, bounds.y - 20, bounds.width + 40, bounds.height + 40);
107 
108                     gc.setLineWidth(1);
109                     gc.setForeground(fg);
110                 }
111             }
112         }
113 
114     }
115 
116     public void setIntervalsToMark(List<Interval> intervals) {
117         _intervalsToMark = new ArrayList<Interval>();
118         _intervalsToMark.addAll(intervals);
119     }
120 
121     public boolean isDrawHistory() {
122         return _drawHistory;
123     }
124 
125     public void setDrawHistory(boolean drawHistory) {
126         _drawHistory = drawHistory;
127     }
128 
129 }