View Javadoc

1   /*
2    *  File: EventMonitorHeaderRenderer.java 
3    *  Copyright (c) 2004-2008  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.eventmonitoring.swing.renderer;
21  
22  import java.awt.AlphaComposite;
23  import java.awt.Color;
24  import java.awt.Dimension;
25  import java.awt.GradientPaint;
26  import java.awt.Graphics;
27  import java.awt.Graphics2D;
28  import java.awt.geom.Rectangle2D;
29  
30  import javax.swing.JComponent;
31  
32  import de.jaret.util.ui.timebars.model.TimeBarRowHeader;
33  import de.jaret.util.ui.timebars.swing.TimeBarViewer;
34  import de.jaret.util.ui.timebars.swing.renderer.HeaderRenderer;
35  
36  /***
37   * A header renderer that renders the label always on top of the supplied space and doing a gradient fill of the
38   * background.
39   * 
40   * @author kliem
41   * @version $Id: EventMonitorHeaderRenderer.java 805 2008-12-31 10:01:13Z kliem $
42   */
43  public class EventMonitorHeaderRenderer implements HeaderRenderer {
44      private HeaderComponent _headerComponent;
45  
46      public EventMonitorHeaderRenderer() {
47          _headerComponent = new HeaderComponent();
48      }
49  
50      /***
51       * {@inheritDoc}
52       */
53      public JComponent getHeaderRendererComponent(TimeBarViewer tbv, TimeBarRowHeader value, boolean isSelected) {
54          _headerComponent.setSelected(isSelected);
55          _headerComponent.setTimeBarRowHeader(value);
56          _headerComponent.setTimeBarViewer(tbv);
57          return _headerComponent;
58      }
59  
60      /***
61       * {@inheritDoc}
62       */
63      public int getWidth() {
64          return 100;
65      }
66  
67      /***
68       * The Component to be painted as the header.
69       * 
70       * @author kliem
71       * @version $Id: EventMonitorHeaderRenderer.java 805 2008-12-31 10:01:13Z kliem $
72       */
73      public class HeaderComponent extends JComponent {
74          private final static int MARGIN = 3;
75  
76          private TimeBarRowHeader _header;
77          private boolean _selected;
78          private TimeBarViewer _tbv;
79  
80          public HeaderComponent() {
81              setLayout(null);
82              setOpaque(false);
83          }
84  
85          public void setTimeBarViewer(TimeBarViewer tbv) {
86              _tbv = tbv;
87          }
88  
89          public void setTimeBarRowHeader(TimeBarRowHeader header) {
90              _header = header;
91          }
92  
93          public String getToolTipText() {
94              return "<html><b>" + _header.getLabel() + "</b></html>";
95          }
96  
97          public void setSelected(boolean selected) {
98              _selected = selected;
99          }
100 
101         /***
102          * {@inheritDoc} The header renderer tries to get as much space as possible.
103          */
104         public Dimension getPreferredSize() {
105             return new Dimension(100, 1000);
106         }
107 
108         protected void paintComponent(Graphics graphics) {
109             super.paintComponent(graphics);
110             int height = getHeight();
111             int width = getWidth();
112 
113             // Colors
114             int startGray = 200;
115             int endGray = 230;
116             Color sColor = new Color(startGray, startGray, startGray);
117             Color eColor = new Color(endGray, endGray, endGray);
118 
119             // background plain fill
120             graphics.setColor(Color.RED);
121             graphics.fillRect(0, 0, width, height);
122 
123             // background gradient fill
124             Graphics2D g2 = (Graphics2D) graphics.create();
125             GradientPaint gradientPaint = new GradientPaint(width / 2, 0, sColor, width / 2, height / 2, eColor, false);
126             g2.setPaint(gradientPaint);
127             g2.fillRect(0, 0, width, height);
128 
129             graphics.setColor(Color.BLACK);
130 
131             // row grid
132             if (_tbv.getDrawRowGrid()) {
133                 graphics.drawLine(0, height - 1, width, height - 1);
134             }
135 
136             // text
137             Rectangle2D rect = graphics.getFontMetrics().getStringBounds("Q", graphics);
138             int textHeight = (int) rect.getHeight();
139             graphics.drawString(_header.getLabel(), MARGIN, MARGIN + textHeight);
140 
141             // selection
142             if (_selected) {
143                 graphics.setColor(Color.BLUE);
144                 g2 = (Graphics2D) graphics;
145                 g2.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 0.2f));
146                 graphics.fillRect(0, 0, width, height);
147                 g2.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 1.0f));
148             }
149         }
150     }
151 }