View Javadoc

1   /*
2    *  File: DefaultHierarchyRenderer.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.Graphics;
24  
25  import javax.swing.JComponent;
26  
27  import de.jaret.util.ui.timebars.model.TimeBarRow;
28  import de.jaret.util.ui.timebars.swing.TimeBarViewer;
29  
30  /**
31   * Default implementation of a swing hierarchy renderer.
32   * 
33   * @author kliem
34   * @version $Id: DefaultHierarchyRenderer.java 800 2008-12-27 22:27:33Z kliem $
35   */
36  public class DefaultHierarchyRenderer implements HierarchyRenderer {
37      /**
38       * Size for the hierarchy symbols.
39       */
40      private static final int SIZE = 12;
41  
42      /** component used to render the hierarchy. */
43      private MyHierarchyRenderer _renderer = new MyHierarchyRenderer();
44  
45      /**
46       * Default constructor.
47       */
48      public DefaultHierarchyRenderer() {
49          _renderer = new MyHierarchyRenderer();
50      }
51  
52      /**
53       * {@inheritDoc}
54       */
55      public JComponent getHierarchyRendererComponent(TimeBarViewer tbv, TimeBarRow row, boolean selected,
56              boolean expanded, boolean leaf, int level, int depth) {
57          _renderer.configure(tbv, row, selected, expanded, leaf, level, depth);
58          _renderer.setToolTipText("Hierarchy");
59          return _renderer;
60      }
61  
62      /**
63       * {@inheritDoc}
64       */
65      public int getWidth() {
66          return SIZE + 4;
67      }
68  
69      class MyHierarchyRenderer extends JComponent {
70          TimeBarViewer _tbv;
71  
72          TimeBarRow _row;
73  
74          boolean _selected;
75  
76          boolean _expanded;
77  
78          boolean _leaf;
79  
80          int _level;
81  
82          int _depth;
83  
84          public void configure(TimeBarViewer tbv, TimeBarRow row, boolean selected, boolean expanded, boolean leaf,
85                  int level, int depth) {
86              _tbv = tbv;
87              _row = row;
88              _expanded = expanded;
89              _leaf = leaf;
90              _level = level;
91              _depth = depth;
92          }
93  
94          /**
95           * {@inheritDoc}
96           */
97          public void paintComponent(Graphics gc) {
98              int offx = (this.getWidth() - SIZE) / (_depth + 1);
99  
100             int x = offx * _level + SIZE / 2;
101 
102             int y = (this.getHeight() - SIZE) / 2;
103             if (_leaf) {
104                 drawLeaf(gc, SIZE, x, y);
105             } else if (_expanded) {
106                 drawMinus(gc, SIZE, x, y);
107             } else {
108                 drawPlus(gc, SIZE, x, y);
109             }
110 
111             // draw tree connections
112             boolean drawConnections = false;
113             if (drawConnections) {
114                 for (int i = 0; i <= _level - 1; i++) {
115                     x = offx * i + SIZE;
116                     gc.drawLine(x, 0, x, this.getHeight());
117 
118                 }
119                 x = offx * _level + SIZE;
120                 gc.drawLine(x, 0, x, (this.getHeight() - SIZE) / 2);
121                 gc.drawLine(x, ((this.getHeight() - SIZE) / 2) + SIZE, x, y + this.getHeight());
122 
123                 if (_level > 0) {
124                     x = offx * (_level - 1) + SIZE;
125                     int xx = offx * (_level) + SIZE;
126                     y = ((this.getHeight() - SIZE) / 2) + SIZE / 2;
127                     gc.drawLine(x, y, xx - SIZE / 2, y);
128                 }
129             }
130 
131         }
132 
133         protected void drawPlus(Graphics gc, int size, int x, int y) {
134             gc.drawLine(x + 3, y + size / 2, x + size - 3, y + size / 2);
135             gc.drawLine(x + size / 2, y + 3, x + size / 2, y + size - 3);
136             gc.drawRect(x, y, size, size);
137         }
138 
139         protected void drawMinus(Graphics gc, int size, int x, int y) {
140             gc.drawLine(x + 3, y + size / 2, x + size - 3, y + size / 2);
141             gc.drawRect(x, y, size, size);
142         }
143 
144         protected void drawLeaf(Graphics gc, int size, int x, int y) {
145             Color bg = gc.getColor();
146             gc.setColor(Color.BLACK);
147             gc.fillOval(x + size / 2, y + size / 2, size / 2, size / 2);
148             gc.setColor(bg);
149         }
150     }
151 }