001 /* =========================================================== 002 * JFreeChart : a free chart library for the Java(tm) platform 003 * =========================================================== 004 * 005 * (C) Copyright 2000-2009, by Object Refinery Limited and Contributors. 006 * 007 * Project Info: http://www.jfree.org/jfreechart/index.html 008 * 009 * This library is free software; you can redistribute it and/or modify it 010 * under the terms of the GNU Lesser General Public License as published by 011 * the Free Software Foundation; either version 2.1 of the License, or 012 * (at your option) any later version. 013 * 014 * This library is distributed in the hope that it will be useful, but 015 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY 016 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public 017 * License for more details. 018 * 019 * You should have received a copy of the GNU Lesser General Public 020 * License along with this library; if not, write to the Free Software 021 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, 022 * USA. 023 * 024 * [Java is a trademark or registered trademark of Sun Microsystems, Inc. 025 * in the United States and other countries.] 026 * 027 * ------------------- 028 * ChartSelection.java 029 * ------------------- 030 * (C) Copyright 2009, by Object Refinery Limited. 031 * 032 * Original Author: David Gilbert (for Object Refinery Limited); 033 * Contributor(s): -; 034 * 035 * Changes 036 * ------- 037 * 08-Apr-2009 : Version 1, with inspiration from patch 1460845 (DG); 038 * 039 */ 040 041 package org.jfree.chart; 042 043 import java.awt.datatransfer.DataFlavor; 044 import java.awt.datatransfer.Transferable; 045 import java.awt.datatransfer.UnsupportedFlavorException; 046 import java.io.IOException; 047 048 /** 049 * A class used to represent a chart on the clipboard. 050 * 051 * @since 1.0.13 052 */ 053 public class ChartTransferable implements Transferable { 054 055 /** The data flavor. */ 056 final DataFlavor imageFlavor = new DataFlavor( 057 "image/x-java-image; class=java.awt.Image", "Image"); 058 059 /** The chart. */ 060 private JFreeChart chart; 061 062 /** The width of the chart on the clipboard. */ 063 private int width; 064 065 /** The height of the chart on the clipboard. */ 066 private int height; 067 068 /** 069 * Creates a new chart selection. 070 * 071 * @param chart the chart. 072 * @param width the chart width. 073 * @param height the chart height. 074 */ 075 public ChartTransferable(JFreeChart chart, int width, int height) { 076 this(chart, width, height, true); 077 } 078 079 /** 080 * Creates a new chart selection. 081 * 082 * @param chart the chart. 083 * @param width the chart width. 084 * @param height the chart height. 085 * @param cloneData clone the dataset(s)? 086 */ 087 public ChartTransferable(JFreeChart chart, int width, int height, 088 boolean cloneData) { 089 090 // we clone the chart because presumably there can be some delay 091 // between putting this instance on the system clipboard and 092 // actually having the getTransferData() method called... 093 try { 094 this.chart = (JFreeChart) chart.clone(); 095 } 096 catch (CloneNotSupportedException e) { 097 this.chart = chart; 098 } 099 this.width = width; 100 this.height = height; 101 // FIXME: we've cloned the chart, but the dataset(s) aren't cloned 102 // and we should do that 103 } 104 105 /** 106 * Returns the data flavors supported. 107 * 108 * @return The data flavors supported. 109 */ 110 public DataFlavor[] getTransferDataFlavors() { 111 return new DataFlavor[] {this.imageFlavor}; 112 } 113 114 /** 115 * Returns <code>true</code> if the specified flavor is supported. 116 * 117 * @param flavor the flavor. 118 * 119 * @return A boolean. 120 */ 121 public boolean isDataFlavorSupported(DataFlavor flavor) { 122 return this.imageFlavor.equals(flavor); 123 } 124 125 /** 126 * Returns the content for the requested flavor, if it is supported. 127 * 128 * @param flavor the requested flavor. 129 * 130 * @return The content. 131 * 132 * @throws java.awt.datatransfer.UnsupportedFlavorException 133 * @throws java.io.IOException 134 */ 135 public Object getTransferData(DataFlavor flavor) 136 throws UnsupportedFlavorException, IOException { 137 138 if (this.imageFlavor.equals(flavor)) { 139 return this.chart.createBufferedImage(width, height); 140 } 141 else { 142 throw new UnsupportedFlavorException(flavor); 143 } 144 } 145 146 }