KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jfree > chart > StandardLegendItemLayout


1 /* ======================================
2  * JFreeChart : a free Java chart library
3  * ======================================
4  *
5  * Project Info: http://www.jfree.org/jfreechart/index.html
6  * Project Lead: David Gilbert (david.gilbert@object-refinery.com);
7  *
8  * (C) Copyright 2000-2003, by Object Refinery Limited and Contributors.
9  *
10  * This library is free software; you can redistribute it and/or modify it under the terms
11  * of the GNU Lesser General Public License as published by the Free Software Foundation;
12  * either version 2.1 of the License, or (at your option) any later version.
13  *
14  * This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
15  * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
16  * See the GNU Lesser General Public License for more details.
17  *
18  * You should have received a copy of the GNU Lesser General Public License along with this
19  * library; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330,
20  * Boston, MA 02111-1307, USA.
21  *
22  * -----------------------------
23  * StandardLegendItemLayout.java
24  * -----------------------------
25  * (C) Copyright 2002, 2003, by Object Refinery Limited.
26  *
27  * Original Author: David Gilbert (for Object Refinery Limited);
28  * Contributor(s): -;
29  *
30  * $Id: StandardLegendItemLayout.java,v 1.2 2003/06/05 08:26:09 mungady Exp $
31  *
32  * Changes
33  * -------
34  * 07-Feb-2002 : Version 1 (DG);
35  * 02-Oct-2002 : Fixed errors reported by Checkstyle (DG);
36  *
37  */

38
39 package org.jfree.chart;
40
41 import java.util.Iterator JavaDoc;
42
43 /**
44  * A class for arranging legend items.
45  *
46  * @author David Gilbert
47  */

48 public class StandardLegendItemLayout implements LegendItemLayout {
49
50     /** Useful constant for vertical orientation. */
51     public static final int VERTICAL = 0;
52
53     /** Useful constant for horizontal orientation. */
54     public static final int HORIZONTAL = 1;
55
56     /** The orientation of the layout (HORIZONTAL or VERTICAL). */
57     private int orientation;
58
59     /** A constraint on one of the dimesions in the layout. */
60     private double dimension;
61
62     /**
63      * Constructs a new layout class for legend items.
64      *
65      * @param orientation the orientation of the layout (HORIZONTAL or VERTICAL).
66      * @param dimension the constrained dimension.
67      */

68     public StandardLegendItemLayout(int orientation, double dimension) {
69
70         this.orientation = orientation;
71         this.dimension = dimension;
72
73     }
74
75     /**
76      * Performs a layout on the items in the collection.
77      *
78      * @param collection the collection to be laid out.
79      */

80     public void layoutLegendItems(LegendItemCollection collection) {
81
82         if (orientation == HORIZONTAL) {
83             doHorizontalLayout(collection);
84         }
85         else if (orientation == VERTICAL) {
86             doVerticalLayout(collection);
87         }
88
89     }
90
91     /**
92      * Lays out the items horizontally, with a constraint on the width.
93      * @param collection The collection to be laid out.
94      */

95     private void doHorizontalLayout(LegendItemCollection collection) {
96
97         // run through the items in the collection and set their coordinates
98
// relative to (0, 0)
99
Iterator JavaDoc iterator = collection.iterator();
100
101        // int rowCount = 0;
102
// double totalHeight = 0.0;
103
boolean first = true;
104         double currentRowX = 0.0;
105         double currentRowY = 0.0;
106         double currentRowHeight = 0.0;
107
108         while (iterator.hasNext()) {
109             DrawableLegendItem item = (DrawableLegendItem) iterator.next();
110             if ((first) || (item.getWidth() < (this.dimension - currentRowX))) {
111                 item.setX(currentRowX);
112                 item.setY(currentRowY);
113                 currentRowX = currentRowX + item.getWidth();
114                 currentRowHeight = Math.max(currentRowHeight, item.getHeight());
115                 first = false;
116             }
117             else { // start new row
118
currentRowY = currentRowY + currentRowHeight;
119                 currentRowHeight = item.getHeight();
120                 item.setX(0.0);
121                 currentRowX = item.getWidth();
122             }
123
124         }
125
126     }
127
128     /**
129      * Lays out the items vertically, with a constraint on the height.
130      * @param collection The collection to be laid out.
131      */

132     private void doVerticalLayout(LegendItemCollection collection) {
133
134         // run through the items in the collection and set their coordinates
135
// relative to (0, 0)
136
Iterator JavaDoc iterator = collection.iterator();
137
138         //int columnCount = 0;
139
//double totalWidth = 0.0;
140
boolean first = true;
141         double currentColumnX = 0.0;
142         double currentColumnY = 0.0;
143         double currentColumnWidth = 0.0;
144
145         while (iterator.hasNext()) {
146             DrawableLegendItem item = (DrawableLegendItem) iterator.next();
147             if ((first) || (item.getHeight() < (this.dimension - currentColumnY))) {
148                 item.setX(currentColumnX);
149                 item.setY(currentColumnY);
150                 currentColumnY = currentColumnY + item.getHeight();
151                 currentColumnWidth = Math.max(currentColumnWidth, item.getWidth());
152                 first = false;
153             }
154             else { // start new column
155
currentColumnX = currentColumnX + currentColumnWidth;
156                 currentColumnWidth = item.getWidth();
157                 item.setY(0.0);
158                 currentColumnY = item.getHeight();
159             }
160
161         }
162     }
163
164 }
165
Popular Tags