View Javadoc

1   /*
2    *  File: IntervalListTransferable.java 
3    *  Copyright (c) 2004-2007  Peter Kliem (Peter.Kliem@jaret.de)
4    *  A commercial license is available, see http://www.jaret.de.
5    *
6    *  This program is free software; you can redistribute it and/or modify
7    *  it under the terms of the GNU General Public License as published by
8    *  the Free Software Foundation; either version 2 of the License, or
9    *  (at your option) any later version.
10   *
11   *  This program is distributed in the hope that it will be useful,
12   *  but WITHOUT ANY WARRANTY; without even the implied warranty of
13   *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14   *  GNU General Public License for more details.
15   *
16   *  You should have received a copy of the GNU General Public License
17   *  along with this program; if not, write to the Free Software
18   *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19   */
20  package de.jaret.util.ui.timebars.swing.dnd;
21  
22  import java.awt.datatransfer.DataFlavor;
23  import java.awt.datatransfer.Transferable;
24  import java.awt.datatransfer.UnsupportedFlavorException;
25  import java.util.Iterator;
26  import java.util.List;
27  
28  /**
29   * Transferable for a list of intervals and
30   * 
31   * @author Peter Kliem
32   * @version $Id: IntervalListTransferable.java 800 2008-12-27 22:27:33Z kliem $
33   */
34  public class IntervalListTransferable implements Transferable {
35      public static DataFlavor intervalListFlavor = new DataFlavor(List.class, "IntervalListFavor");
36  
37      List data;
38  
39      static DataFlavor supportedFlavors[] = {intervalListFlavor, DataFlavor.stringFlavor};
40  
41      public IntervalListTransferable(List rowIntervalTuples) {
42          data = rowIntervalTuples;
43      }
44  
45      public Object getTransferData(DataFlavor flavor) throws UnsupportedFlavorException {
46          if (!isDataFlavorSupported(flavor)) {
47              throw new UnsupportedFlavorException(flavor);
48          }
49          if (flavor.equals(DataFlavor.stringFlavor)) {
50              return toString();
51          }
52          return data;
53      }
54  
55      public DataFlavor[] getTransferDataFlavors() {
56          return supportedFlavors;
57      }
58  
59      public boolean isDataFlavorSupported(DataFlavor flavor) {
60          for (int i = 0; i < supportedFlavors.length; i++) {
61              if (supportedFlavors[i].equals(flavor)) {
62                  return true;
63              }
64          }
65          return false;
66      }
67  
68      public String toString() {
69          StringBuffer buf = new StringBuffer();
70          Iterator it = data.iterator();
71          while (it.hasNext()) {
72              RowIntervalTuple tuple = (RowIntervalTuple) it.next();
73              buf.append(tuple.toString() + " ");
74          }
75          return buf.toString();
76      }
77  }