View Javadoc

1   /*
2    *  File: ITitleRenderer.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.swing.renderer;
21  
22  import java.awt.Color;
23  import java.awt.GradientPaint;
24  import java.awt.Graphics;
25  import java.awt.Graphics2D;
26  import java.awt.geom.Rectangle2D;
27  
28  import javax.swing.JComponent;
29  
30  import de.jaret.util.swing.GraphicsHelper;
31  import de.jaret.util.ui.timebars.swing.TimeBarViewer;
32  
33  /**
34   * A default title renderer rendering the name of the viewer as large as possible.
35   * 
36   * @author kliem
37   * @version $Id: DefaultTitleRenderer.java 802 2008-12-28 12:30:41Z kliem $
38   */
39  public class DefaultTitleRenderer implements ITitleRenderer {
40      /** the cached component used for rendering. */
41      protected TitleRendererComponent _titleRendererComponent;
42  
43      /**
44       * Default constructor.
45       */
46      public DefaultTitleRenderer() {
47          _titleRendererComponent = new TitleRendererComponent();
48      }
49  
50      /**
51       * {@inheritDoc}
52       */
53      public JComponent getTitleRendererComponent(TimeBarViewer tbv) {
54          _titleRendererComponent.setDisplayName(tbv.getName());
55          return _titleRendererComponent;
56      }
57  
58      /**
59       * Component used for rendering. This always tries to print out the name set at a maximum size.
60       * 
61       * @author kliem
62       * @version $Id: DefaultTitleRenderer.java 802 2008-12-28 12:30:41Z kliem $
63       */
64      public static class TitleRendererComponent extends JComponent {
65          /** the text to display. */
66          private String _name;
67          /** the last width of painting. Used to determine whether the font size has to be determined. */
68          private int _lastWidth = -1;
69          /** margin in pixel to use. */
70          private static final int MARGIN = 3;
71          /** remebered size. */
72          private float _size = 8;
73  
74          /**
75           * Default constructor.
76           */
77          public TitleRendererComponent() {
78              setLayout(null);
79              setOpaque(false);
80          }
81  
82          /**
83           * Set the name to be displayed.
84           * 
85           * @param name the name
86           */
87          public void setDisplayName(String name) {
88              _name = name;
89          }
90  
91          /**
92           * {@inheritDoc}
93           */
94          public String getToolTipText() {
95              return "<html><b>" + _name + "</b></html>";
96          }
97  
98          /**
99           * {@inheritDoc}
100          */
101         protected void paintComponent(Graphics graphics) {
102             super.paintComponent(graphics);
103             int height = getHeight();
104             int width = getWidth();
105 
106             // draw a simple gradient fill
107             Graphics2D g2 = (Graphics2D) graphics.create();
108             GradientPaint gradientPaint = new GradientPaint(0, 0, Color.BLUE, width / 2, height / 2, Color.WHITE, false);
109             g2.setPaint(gradientPaint);
110             g2.fillRect(0, 0, width, height);
111 
112             // determine the size of the font if the widht has changed
113             if (_lastWidth != width) {
114                 _lastWidth = width;
115 
116                 for (float size = 6; size < 37; size += 1) {
117                     graphics.setFont(graphics.getFont().deriveFont(size));
118                     Rectangle2D rect = graphics.getFontMetrics().getStringBounds(_name, graphics);
119                     if (rect.getWidth() > width - 2 * MARGIN) {
120                         graphics.setFont(graphics.getFont().deriveFont(size - 1));
121                         _size = size - 1;
122                         break;
123                     }
124                     _size = size;
125                 }
126 
127             } else {
128                 graphics.setFont(graphics.getFont().deriveFont(_size));
129             }
130 
131             // draw the title String
132             graphics.setColor(Color.BLACK);
133             GraphicsHelper.drawStringCenteredVCenter(graphics, _name, 0, width, height / 2);
134         }
135 
136     }
137 
138 }