View Javadoc

1   /*
2    *  File: DefaultTimeBarMarkerRenderer.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.printing.Printer;
26  import org.eclipse.swt.widgets.Display;
27  
28  import de.jaret.util.ui.timebars.TimeBarMarker;
29  import de.jaret.util.ui.timebars.TimeBarViewerDelegate;
30  import de.jaret.util.ui.timebars.TimeBarViewerInterface;
31  
32  /**
33   * Simple default renderer for time bar markers. Renders them as a simple vertical line.
34   * 
35   * @author Peter Kliem
36   * @version $Id: DefaultTimeBarMarkerRenderer.java 821 2009-02-04 21:12:16Z kliem $
37   */
38  public class DefaultTimeBarMarkerRenderer extends RendererBase implements TimeBarMarkerRenderer {
39      /** color when dragging. */
40      private static final Color MARKER_ACTIVE_COLOR = Display.getCurrent().getSystemColor(SWT.COLOR_BLUE);
41  
42      /** normal color. */
43      private static final Color MARKER_COLOR = Display.getCurrent().getSystemColor(SWT.COLOR_RED);
44  
45      /**
46       * Cunstructor for printer use.
47       * 
48       * @param printer printer device
49       */
50      public DefaultTimeBarMarkerRenderer(Printer printer) {
51          super(printer);
52      }
53  
54      /**
55       * Constructor for screen use.
56       * 
57       */
58      public DefaultTimeBarMarkerRenderer() {
59          super(null);
60      }
61  
62      /**
63       * {@inheritDoc}
64       */
65      public void draw(GC gc, TimeBarViewerDelegate tbv, TimeBarMarker marker, boolean isDragged, boolean printing) {
66          boolean horizontal = tbv.getOrientation().equals(TimeBarViewerInterface.Orientation.HORIZONTAL);
67          Color oldCol = gc.getForeground();
68  
69          if (!printing) {
70              if (isDragged) {
71                  gc.setForeground(MARKER_ACTIVE_COLOR);
72              } else {
73                  gc.setForeground(MARKER_COLOR);
74              }
75          } else {
76              gc.setForeground(_printer.getSystemColor(SWT.COLOR_DARK_MAGENTA));
77              gc.setLineWidth(getDefaultLineWidth());
78          }
79  
80          if (horizontal) {
81              int startY = Math.min(tbv.getXAxisRect().y, tbv.getDiagramRect().y);
82              int x = tbv.xForDate(marker.getDate());
83              int height = tbv.getXAxisRect().height + tbv.getDiagramRect().height;
84              gc.drawLine(x, startY, x, startY + height);
85          } else {
86              int startX = Math.min(tbv.getXAxisRect().x, tbv.getDiagramRect().x);
87              int y = tbv.xForDate(marker.getDate());
88              int width = tbv.getXAxisRect().width + tbv.getDiagramRect().width;
89              gc.drawLine(startX, y, startX + width, y);
90          }
91          gc.setLineWidth(1);
92          gc.setForeground(oldCol);
93  
94      }
95  
96      /**
97       * {@inheritDoc}
98       */
99      public void dispose() {
100         // nothing to dispose
101     }
102 
103     /**
104      * {@inheritDoc}
105      */
106     public int getWidth(TimeBarMarker marker) {
107         return scaleX(4);
108     }
109 
110     /**
111      * {@inheritDoc}
112      */
113     public TimeBarMarkerRenderer createPrintRenderer(Printer printer) {
114         return new DefaultTimeBarMarkerRenderer(printer);
115     }
116 
117 }