001 package net.sourceforge.mp3dings.datatransfer;
002
003 /*
004 * Mp3dings - manage mp3 meta-information
005 * Copyright (C) 2006 Moritz Ringler
006 * $Id: TransferData.java 148 2008-12-03 22:00:50Z ringler $
007 *
008 * This program is free software; you can redistribute it and/or
009 * modify it under the terms of the GNU General Public License
010 * as published by the Free Software Foundation; either version 2
011 * of the License, or (at your option) any later version.
012 *
013 * This program is distributed in the hope that it will be useful,
014 * but WITHOUT ANY WARRANTY; without even the implied warranty of
015 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
016 * GNU General Public License for more details.
017 *
018 * You should have received a copy of the GNU General Public License
019 * along with this program; if not, write to the Free Software
020 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
021 */
022
023 import java.awt.datatransfer.DataFlavor;
024 import java.awt.datatransfer.StringSelection;
025 import java.awt.datatransfer.UnsupportedFlavorException;
026 import java.io.File;
027 import java.io.IOException;
028 import java.io.StringReader;
029 import java.util.ArrayList;
030 import java.util.Arrays;
031 import java.util.List;
032 import java.util.Iterator;
033 import java.util.logging.Level;
034 import java.util.logging.Logger;
035 import net.sourceforge.mp3dings.Globals;
036
037 /**
038 * A Transferable that wraps plain text and html data for the clipboard.*
039 *
040 *@author ringler
041 */
042 public class TransferData extends StringSelection {
043 private static final Logger logger = Logger.getLogger(TransferData.class.getPackage().getName());
044 protected static DataFlavor HTML0;
045 protected static DataFlavor HTML1;
046 protected static DataFlavor URI_FILE_LIST;
047 /** Description of the Field */
048 protected static DataFlavor OBJECT;
049 static {
050 try {
051 OBJECT = new DataFlavor(DataFlavor.javaJVMLocalObjectMimeType + ";class=java.lang.Object");
052
053 /*
054 * this exports nicely to ms office, but not to Thunderbird or
055 * OpenOffice. These would probably need a raw byte array without
056 * any Java Object overhead. But the MIME types
057 * text/html;class=[B
058 * and
059 * text/html;class=byte[].class
060 * don't work.
061 */
062 HTML0 = new DataFlavor("text/html;class=java.lang.String");
063 HTML1 = new DataFlavor("text/html;class=java.io.Reader");
064 URI_FILE_LIST = new DataFlavor("text/uri-list;class=java.lang.String");
065 } catch (Exception cle) {
066 logger.severe("Error: can't initialize TransferData!");
067 Globals.logStackTrace(logger, Level.SEVERE, cle);
068 }
069 }
070
071 private String htmlData;
072 private String strData;
073 private Object objData;
074 private List fileData;
075
076
077 /**
078 * Constructs a new TransferData.
079 * If one of the parameters is null the corresponding DataFlavor will not
080 * be supported by this Transferable.
081 *
082 *@param obj Description of the Parameter
083 *@param plaintext Description of the Parameter
084 *@param markup Description of the Parameter
085 *@param files Description of the Parameter
086 */
087 public TransferData(Object obj, String plaintext,
088 String markup, File[] files) {
089 super((plaintext == null)?"":plaintext);
090 strData = plaintext;
091 htmlData = markup;
092 objData = obj;
093 fileData = (files == null)? null : Arrays.asList(files);
094 }
095
096 private String filesAsURIList(){
097 Iterator it = fileData.iterator();
098 StringBuffer sb = new StringBuffer();
099 // refer to RFC 2483 for the text/uri-list format
100 while(it.hasNext()){
101 File file = (File) it.next();
102 sb.append(file.toURI());
103 sb.append("\r\n");
104 }
105 return sb.toString();
106 }
107
108 /**
109 * Gets the transferData attribute of the TransferData object. Guaranteed
110 * not to return null.
111 *@param df Description of the Parameter
112 *@return The transferData value
113 *@exception UnsupportedFlavorException Description of the Exception
114 *@exception IOException Description of the Exception
115 */
116 public Object getTransferData(DataFlavor df)
117 throws UnsupportedFlavorException, IOException {
118 Object result = null;
119 if(isDataFlavorSupported(df)){
120 if (df.equals(OBJECT)) {
121 result = objData;
122 } else if (df.equals(HTML0)) {
123 result = htmlData;
124 } else if (df.equals(HTML1)) {
125 result = new StringReader(htmlData);
126 } else if (df.equals(DataFlavor.javaFileListFlavor)) {
127 result = fileData;
128 } else if (df.equals(URI_FILE_LIST)){
129 result = filesAsURIList();
130 } else if(super.isDataFlavorSupported(df)){
131 result = super.getTransferData(df);
132 }
133 } else {
134 throw new UnsupportedFlavorException(df);
135 }
136 return result;
137 }
138
139
140 /**
141 * Gets the supported DataFlavors.
142 *
143 *@return The transferDataFlavors value
144 */
145 public DataFlavor[] getTransferDataFlavors() {
146 List flavors = new ArrayList(6);
147 if(objData != null) {
148 flavors.add(OBJECT);
149 }
150 if(htmlData != null){
151 flavors.add(HTML0);
152 flavors.add(HTML1);
153 }
154 if(strData != null){
155 flavors.addAll(Arrays.asList(super.getTransferDataFlavors()));
156 }
157 if(fileData != null){
158 flavors.add(DataFlavor.javaFileListFlavor);
159 flavors.add(URI_FILE_LIST);
160 }
161 return (DataFlavor[]) flavors.toArray(new DataFlavor[flavors.size()]);
162 }
163
164 public static DataFlavor[] getDefaultFlavors(){
165 return (new TransferData(new Object(), "", "", new File[0])).getTransferDataFlavors();
166 }
167
168
169 /**
170 * Gets the dataFlavorSupported attribute of the TransferData
171 * object
172 *
173 *@param flavor Description of the Parameter
174 *@return The dataFlavorSupported value
175 */
176 public boolean isDataFlavorSupported(DataFlavor flavor) {
177 return Arrays.asList(getTransferDataFlavors()).contains(flavor);
178 }
179
180 }