-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathXYShapeBuilder.java
145 lines (118 loc) · 4 KB
/
XYShapeBuilder.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
/*
* jfreechart-builder: a builder pattern module for working with the jfreechart library
*
* (C) Copyright 2022, by Matt E. and project contributors
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
package com.jfcbuilder.builders;
import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Paint;
import java.awt.Shape;
import java.awt.Stroke;
import org.jfree.chart.annotations.XYAnnotation;
import org.jfree.chart.annotations.XYShapeAnnotation;
/**
* Builder for producing {@link XYShapeAnnotation} objects.
*/
public class XYShapeBuilder implements IXYAnnotationBuilder<XYShapeBuilder> {
// See XYShapeAnnotation for these defaults
public static final Color DEFAULT_LINE_COLOR = Color.BLACK;
public static final Stroke DEFAULT_LINE_STYLE = new BasicStroke(1.0f);
public static final Color DEFAULT_FILL_COLOR = null; // None specified
private Shape shape;
private Stroke lineStyle;
private Paint lineColor;
private Paint fillColor;
/**
* Hidden constructor.
*/
private XYShapeBuilder() {
shape = null;
lineStyle = DEFAULT_LINE_STYLE;
lineColor = DEFAULT_LINE_COLOR;
fillColor = DEFAULT_FILL_COLOR;
}
/**
* Factory method for obtaining new instances of this class.
*
* @return New instance of this class
*/
public static XYShapeBuilder get() {
return new XYShapeBuilder();
}
public Shape shape() {
return shape;
}
public XYShapeBuilder shape(Shape shape) {
this.shape = shape;
return this;
}
public Stroke lineStyle() {
return lineStyle;
}
public XYShapeBuilder lineStyle(Stroke style) {
lineStyle = style;
return this;
}
public Paint lineColor() {
return lineColor;
}
public XYShapeBuilder lineColor(Paint color) {
lineColor = color;
return this;
}
public Paint fillColor() {
return fillColor;
}
public XYShapeBuilder fillColor(Paint color) {
fillColor = color;
return this;
}
private void checkBuildPreconditions() throws IllegalStateException {
if (shape == null) {
throw new IllegalStateException("Shape not set");
}
}
@Override
public XYAnnotation build() throws IllegalStateException {
checkBuildPreconditions();
return new XYShapeAnnotation(shape, lineStyle, lineColor, fillColor);
}
@Override
public void mapXToTimeIndex(long[] timeData, int indexRangeStartIndex, int indexRangeEndIndex) {
// Gapless shape annotations are not currently supported due to multiple complexities.
//
// We need to somehow map the java.awt.Shape's x-coordinate(s) to time indices, but Shape is an interface and
// concrete classes can have any number of x-coordinates passed-in and in any number of ways. The interface provides
// no means to get x-coordinates polymorphically. So it requires specific logic for 30+
// different known implementing classes:
//
// https://docs.oracle.com/javase/8/docs/api/java/awt/Shape.html
//
// That is a big effort possibly with high maintenance burden.
/*
if(shape == null) {
return;
}
// +1 as the binary search method end index is exclusive.
int xIndex = Arrays.binarySearch(timeData, indexRangeStartIndex, indexRangeEndIndex + 1, (long) x());
if (xIndex >= 0) {
x((double) (xIndex - indexRangeStartIndex));
}
*/
}
}