-
-
Notifications
You must be signed in to change notification settings - Fork 454
/
ChartTransferable.java
277 lines (253 loc) · 8.67 KB
/
ChartTransferable.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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
/* ===========================================================
* JFreeChart : a free chart library for the Java(tm) platform
* ===========================================================
*
* (C) Copyright 2000-2020, by Object Refinery Limited and Contributors.
*
* Project Info: http://www.jfree.org/jfreechart/index.html
*
* 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.
*
* [Oracle and Java are registered trademarks of Oracle and/or its affiliates.
* Other names may be trademarks of their respective owners.]
*
* ----------------------
* ChartTransferable.java
* ----------------------
* (C) Copyright 2009-2016, by Object Refinery Limited.
*
* Original Author: David Gilbert (for Object Refinery Limited);
* Contributor(s): -;
*
* Changes
* -------
* 08-Apr-2009 : Version 1, with inspiration from patch 1460845 (DG);
* 05-May-2009 : Match the scaling options provided by the ChartPanel
* class (DG);
*
*/
package org.jfree.chart;
import java.awt.Graphics2D;
import java.awt.datatransfer.DataFlavor;
import java.awt.datatransfer.Transferable;
import java.awt.datatransfer.UnsupportedFlavorException;
import java.awt.geom.AffineTransform;
import java.awt.geom.Rectangle2D;
import java.awt.image.BufferedImage;
import java.io.IOException;
/**
* A class used to represent a chart on the clipboard.
*
* @since 1.0.13
*/
public class ChartTransferable implements Transferable {
/** The data flavor. */
final DataFlavor imageFlavor = new DataFlavor(
"image/x-java-image; class=java.awt.Image", "Image");
/** The chart. */
private JFreeChart chart;
/** The width of the chart on the clipboard. */
private int width;
/** The height of the chart on the clipboard. */
private int height;
/**
* The smallest width at which the chart will be drawn (if necessary, the
* chart will then be scaled down to fit the requested width).
*
* @since 1.0.14
*/
private int minDrawWidth;
/**
* The smallest height at which the chart will be drawn (if necessary, the
* chart will then be scaled down to fit the requested height).
*
* @since 1.0.14
*/
private int minDrawHeight;
/**
* The largest width at which the chart will be drawn (if necessary, the
* chart will then be scaled up to fit the requested width).
*
* @since 1.0.14
*/
private int maxDrawWidth;
/**
* The largest height at which the chart will be drawn (if necessary, the
* chart will then be scaled up to fit the requested height).
*
* @since 1.0.14
*/
private int maxDrawHeight;
/**
* Creates a new chart selection.
*
* @param chart the chart.
* @param width the chart width.
* @param height the chart height.
*/
public ChartTransferable(JFreeChart chart, int width, int height) {
this(chart, width, height, true);
}
/**
* Creates a new chart selection.
*
* @param chart the chart.
* @param width the chart width.
* @param height the chart height.
* @param cloneData clone the dataset(s)?
*/
public ChartTransferable(JFreeChart chart, int width, int height,
boolean cloneData) {
this(chart, width, height, 0, 0, Integer.MAX_VALUE, Integer.MAX_VALUE,
true);
}
/**
* Creates a new chart selection. The minimum and maximum drawing
* dimensions are used to match the scaling behaviour in the
* {@link ChartPanel} class.
*
* @param chart the chart.
* @param width the chart width.
* @param height the chart height.
* @param minDrawW the minimum drawing width.
* @param minDrawH the minimum drawing height.
* @param maxDrawW the maximum drawing width.
* @param maxDrawH the maximum drawing height.
* @param cloneData clone the dataset(s)?
*
* @since 1.0.14
*/
public ChartTransferable(JFreeChart chart, int width, int height,
int minDrawW, int minDrawH, int maxDrawW, int maxDrawH,
boolean cloneData) {
// we clone the chart because presumably there can be some delay
// between putting this instance on the system clipboard and
// actually having the getTransferData() method called...
try {
this.chart = (JFreeChart) chart.clone();
}
catch (CloneNotSupportedException e) {
this.chart = chart;
}
// FIXME: we've cloned the chart, but the dataset(s) aren't cloned
// and we should do that
this.width = width;
this.height = height;
this.minDrawWidth = minDrawW;
this.minDrawHeight = minDrawH;
this.maxDrawWidth = maxDrawW;
this.maxDrawHeight = maxDrawH;
}
/**
* Returns the data flavors supported.
*
* @return The data flavors supported.
*/
@Override
public DataFlavor[] getTransferDataFlavors() {
return new DataFlavor[] {this.imageFlavor};
}
/**
* Returns {@code true} if the specified flavor is supported.
*
* @param flavor the flavor.
*
* @return A boolean.
*/
@Override
public boolean isDataFlavorSupported(DataFlavor flavor) {
return this.imageFlavor.equals(flavor);
}
/**
* Returns the content for the requested flavor, if it is supported.
*
* @param flavor the requested flavor.
*
* @return The content.
*
* @throws java.awt.datatransfer.UnsupportedFlavorException if the flavor
* is not supported.
* @throws java.io.IOException if there is an IO problem.
*/
@Override
public Object getTransferData(DataFlavor flavor)
throws UnsupportedFlavorException, IOException {
if (this.imageFlavor.equals(flavor)) {
return createBufferedImage(this.chart, this.width, this.height,
this.minDrawWidth, this.minDrawHeight, this.maxDrawWidth,
this.maxDrawHeight);
} else {
throw new UnsupportedFlavorException(flavor);
}
}
/**
* A utility method that creates an image of a chart, with scaling.
*
* @param chart the chart.
* @param w the image width.
* @param h the image height.
* @param minDrawW the minimum width for chart drawing.
* @param minDrawH the minimum height for chart drawing.
* @param maxDrawW the maximum width for chart drawing.
* @param maxDrawH the maximum height for chart drawing.
*
* @return A chart image.
*
* @since 1.0.14
*/
private BufferedImage createBufferedImage(JFreeChart chart, int w, int h,
int minDrawW, int minDrawH, int maxDrawW, int maxDrawH) {
BufferedImage image = new BufferedImage(w, h,
BufferedImage.TYPE_INT_ARGB);
Graphics2D g2 = image.createGraphics();
// work out if scaling is required...
boolean scale = false;
double drawWidth = w;
double drawHeight = h;
double scaleX = 1.0;
double scaleY = 1.0;
if (drawWidth < minDrawW) {
scaleX = drawWidth / minDrawW;
drawWidth = minDrawW;
scale = true;
}
else if (drawWidth > maxDrawW) {
scaleX = drawWidth / maxDrawW;
drawWidth = maxDrawW;
scale = true;
}
if (drawHeight < minDrawH) {
scaleY = drawHeight / minDrawH;
drawHeight = minDrawH;
scale = true;
}
else if (drawHeight > maxDrawH) {
scaleY = drawHeight / maxDrawH;
drawHeight = maxDrawH;
scale = true;
}
Rectangle2D chartArea = new Rectangle2D.Double(0.0, 0.0, drawWidth,
drawHeight);
if (scale) {
AffineTransform st = AffineTransform.getScaleInstance(scaleX,
scaleY);
g2.transform(st);
}
chart.draw(g2, chartArea, null, null);
g2.dispose();
return image;
}
}