001/*
002 * Copyright 2010-2015 Institut Pasteur.
003 * 
004 * This file is part of Icy.
005 * 
006 * Icy is free software: you can redistribute it and/or modify
007 * it under the terms of the GNU General Public License as published by
008 * the Free Software Foundation, either version 3 of the License, or
009 * (at your option) any later version.
010 * 
011 * Icy is distributed in the hope that it will be useful,
012 * but WITHOUT ANY WARRANTY; without even the implied warranty of
013 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
014 * GNU General Public License for more details.
015 * 
016 * You should have received a copy of the GNU General Public License
017 * along with Icy. If not, see <http://www.gnu.org/licenses/>.
018 */
019package icy.clipboard;
020
021import java.awt.Toolkit;
022import java.awt.datatransfer.ClipboardOwner;
023import java.awt.datatransfer.DataFlavor;
024import java.awt.datatransfer.Transferable;
025import java.awt.datatransfer.UnsupportedFlavorException;
026import java.io.IOException;
027import java.util.ArrayList;
028import java.util.List;
029
030/**
031 * Clipboard object (used for easy internal Copy/Paste operation).
032 * 
033 * @author Stephane
034 */
035public class Clipboard
036{
037    public interface ClipboardListener
038    {
039        public void clipboardChanged();
040    }
041
042    private final static java.awt.datatransfer.Clipboard systemClipboard = Toolkit.getDefaultToolkit()
043            .getSystemClipboard();
044    private final static List<ClipboardListener> listeners = new ArrayList<Clipboard.ClipboardListener>();
045
046    public static final String TYPE_SEQUENCEROILIST = "SequenceRoiList";
047    public static final String TYPE_ROILIST = "RoiList";
048    public static final String TYPE_ROILINKLIST = "RoiLinkList";
049    public static final String TYPE_SEQUENCE = "Sequence";
050    // Object should be BufferedImage
051    public static final String TYPE_IMAGE = "Image";
052
053    private static String type = "";
054    private static Object object = null;
055
056    /**
057     * Clears the clipboard.
058     */
059    public static void clear()
060    {
061        type = "";
062        object = null;
063    }
064
065    /**
066     * Returns true if the specified type match the current type stored in Clipboard
067     */
068    public static boolean isType(String t)
069    {
070        return type.equals(t);
071    }
072
073    /**
074     * Returns the type of stored object.
075     */
076    public static String getType()
077    {
078        return type;
079    }
080
081    /**
082     * Returns object actually stored in the clipboard.
083     */
084    public static Object get()
085    {
086        return object;
087    }
088
089    /**
090     * Returns object actually stored in the clipboard if it has the specified type.<br>
091     * Returns <code>null</code> otherwise.
092     */
093    public static Object get(String type)
094    {
095        if (Clipboard.type.equals(type))
096            return object;
097
098        return null;
099    }
100
101    /**
102     * Puts an object in the clipboard.<br>
103     * 
104     * @param type
105     *        object type (should not be null).
106     * @param object
107     *        object to save in clipboard.
108     */
109    public static void put(String type, Object object)
110    {
111        if (type == null)
112            throw new IllegalArgumentException("Clipboard.put(type, object): type cannot be null !");
113
114        Clipboard.type = type;
115        Clipboard.object = object;
116
117        // notify change
118        fireChangedEvent();
119    }
120
121    /**
122     * Returns if current content of the system clipboard contains specified type of data.
123     * 
124     * @param type
125     *        the requested <code>DataFlavor</code> for the contents
126     * @see java.awt.datatransfer.Clipboard#isDataFlavorAvailable(DataFlavor)
127     * @throws NullPointerException
128     *         if <code>flavor</code> is <code>null</code>
129     * @throws IllegalStateException
130     *         if this clipboard is currently unavailable
131     */
132    public static boolean hasTypeSystem(DataFlavor type)
133    {
134        return systemClipboard.isDataFlavorAvailable(type);
135    }
136
137    /**
138     * Returns all type of content available in the system clipboard.
139     * 
140     * @see java.awt.datatransfer.Clipboard#getAvailableDataFlavors()
141     * @throws IllegalStateException
142     *         if this clipboard is currently unavailable
143     */
144    public static DataFlavor[] getAllTypeSystem()
145    {
146        return systemClipboard.getAvailableDataFlavors();
147    }
148
149    /**
150     * Gets an object from the system clipboard.
151     * 
152     * @param type
153     *        the requested <code>DataFlavor</code> for the contents
154     * @throws IOException
155     * @throws NullPointerException
156     *         if <code>flavor</code> is <code>null</code>
157     * @throws IllegalStateException
158     *         if this clipboard is currently unavailable
159     * @throws UnsupportedFlavorException
160     * @throws IOException
161     *         if the data in the requested <code>DataFlavor</code>
162     *         can not be retrieved
163     * @see java.awt.datatransfer.Clipboard#getData(DataFlavor)
164     */
165    public static Object getSystem(DataFlavor type) throws UnsupportedFlavorException, IOException
166    {
167        if (hasTypeSystem(type))
168            return systemClipboard.getData(type);
169
170        return null;
171    }
172
173    /**
174     * Clears content of system clipboard.
175     */
176    public static void clearSystem()
177    {
178        systemClipboard.setContents(new TransferableNull(), null);
179    }
180
181    /**
182     * Puts an object in the system clipboard.
183     * 
184     * @param contents
185     *        the transferable object representing the
186     *        clipboard content
187     * @param owner
188     *        the object which owns the clipboard content
189     * @see java.awt.datatransfer.Clipboard#setContents(Transferable, ClipboardOwner)
190     */
191    public static void putSystem(Transferable contents, ClipboardOwner owner)
192    {
193        systemClipboard.setContents(contents, owner);
194    }
195
196    public static void addListener(ClipboardListener listener)
197    {
198        if (!listeners.contains(listener))
199            listeners.add(listener);
200    }
201
202    public static void removeListener(ClipboardListener listener)
203    {
204        listeners.remove(listener);
205    }
206
207    public static void fireChangedEvent()
208    {
209        for (ClipboardListener l : listeners)
210            l.clipboardChanged();
211    }
212}