Spec-Zone .ru
спецификации, руководства, описания, API
001/*
002 * Copyright (c) 2011, 2013, Oracle and/or its affiliates. All rights reserved.
003 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
004 *
005 * This code is free software; you can redistribute it and/or modify it
006 * under the terms of the GNU General Public License version 2 only, as
007 * published by the Free Software Foundation.  Oracle designates this
008 * particular file as subject to the "Classpath" exception as provided
009 * by Oracle in the LICENSE file that accompanied this code.
010 *
011 * This code is distributed in the hope that it will be useful, but WITHOUT
012 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
013 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
014 * version 2 for more details (a copy is included in the LICENSE file that
015 * accompanied this code).
016 *
017 * You should have received a copy of the GNU General Public License version
018 * 2 along with this work; if not, write to the Free Software Foundation,
019 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
020 *
021 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
022 * or visit www.oracle.com if you need additional information or have any
023 * questions.
024 */
025
026package javafx.scene.input;
027
028import java.io.File;
029import java.util.ArrayList;
030import java.util.HashMap;
031import java.util.List;
032
033import javafx.scene.image.Image;
034
035/**
036 * Data container for {@link Clipboard} data. It can hold multiple data in 
037 * several data formats.
038 */
039public class ClipboardContent extends HashMap<DataFormat, Object> {
040    /**
041     * Gets whether a plain text String (DataFormat.PLAIN_TEXT) has been registered
042     * on this Clipboard.
043     * @return true if <code>hasContent(DataFormat.PLAIN_TEXT)</code> returns true, false otherwise
044     */
045    public final boolean hasString() {
046        return containsKey(DataFormat.PLAIN_TEXT);
047    }
048
049    /**
050     * Puts a plain text String onto the Clipboard. This is equivalent to
051     * invoking <code>setContent(DataFormat.PLAIN_TEXT, s)</code>. Setting this value
052     * to null effectively clears it from the clipboard.
053     * @param s The string to place. This may be null.
054     * @return True if the string was successfully placed on the clipboard.
055     * @throws NullPointerException if null reference is passed
056     */
057    public final boolean putString(String s) {
058        if (s == null) {
059            throw new NullPointerException("Null string put on ClipboardContent");
060        }
061        return put(DataFormat.PLAIN_TEXT, s) == s;
062    }
063
064    /**
065     * Gets the plain text String from the clipboard which had previously
066     * been registered. This is equivalent to invoking
067     * <code>getContent(DataFormat.PLAIN_TEXT)</code>. If no such entry exists,
068     * null is returned.
069     * @return The String on the clipboard associated with DataFormat.PLAIN_TEXT,
070     * or null if there is not one.
071     */
072    public final String getString() {
073        return (String) get(DataFormat.PLAIN_TEXT);
074    }
075
076    /**
077     * Gets whether a url String (DataFormat.URL) has been registered
078     * on this Clipboard.
079     * @return true if hasContent(DataFormat.URL) returns true, false otherwise
080     */
081    public final boolean hasUrl() {
082        return containsKey(DataFormat.URL);
083    }
084
085    /**
086     * Puts a URL String onto the Clipboard. This is equivalent to
087     * invoking <code>setContent(DataFormat.URL, s)</code>. Setting this value
088     * to null effectively clears it from the clipboard.
089     * @param url The string to place. This may be null.
090     * @return True if the string was successfully placed on the clipboard.
091     * @throws NullPointerException if null reference is passed
092     */
093    public final boolean putUrl(String url) {
094        if (url == null) {
095            throw new NullPointerException("Null URL put on ClipboardContent");
096        }
097        return put(DataFormat.URL, url) == url;
098    }
099
100    /**
101     * Gets the URL String from the clipboard which had previously
102     * been registered. This is equivalent to invoking
103     * <code>getContent(DataFormat.URL)</code>. If no such entry exists,
104     * null is returned.
105     * @return The String on the clipboard associated with DataFormat.URL,
106     * or null if there is not one.
107     */
108    public final String getUrl() {
109        return (String) get(DataFormat.URL);
110    }
111
112    /**
113     * Gets whether an HTML text String (DataFormat.HTML) has been registered
114     * on this Clipboard.
115     * @return true if <code>hasContent(DataFormat.HTML)</code> returns true, false otherwise
116     */
117    public final boolean hasHtml() {
118        return containsKey(DataFormat.HTML);
119    }
120
121    /**
122     * Puts an HTML text String onto the Clipboard. This is equivalent to
123     * invoking <code>setContent(DataFormat.HTML, s)</code>. Setting this value
124     * to null effectively clears it from the clipboard.
125     * @param s The string to place. This may be null.
126     * @return True if the string was successfully placed on the clipboard.
127     * @throws NullPointerException if null reference is passed
128     */
129    public final boolean putHtml(String s) {
130        if (s == null) {
131            throw new NullPointerException("Null HTML put on ClipboardContent");
132        }
133        return put(DataFormat.HTML, s) == s;
134    }
135
136    /**
137     * Gets the HTML text String from the clipboard which had previously
138     * been registered. This is equivalent to invoking
139     * <code>getContent(DataFormat.HTML)</code>. If no such entry exists,
140     * null is returned.
141     * @return The String on the clipboard associated with DataFormat.HTML,
142     * or null if there is not one.
143     */
144    public final String getHtml() {
145        return (String) get(DataFormat.HTML);
146    }
147
148    /**
149     * Gets whether an RTF String (DataFormat.RTF) has been registered
150     * on this Clipboard.
151     * @return true if hasContent(DataFormat.RTF) returns true, false otherwise
152     */
153    public final boolean hasRtf() {
154        return containsKey(DataFormat.RTF);
155    }
156
157    /**
158     * Puts an RTF text String onto the Clipboard. This is equivalent to
159     * invoking <code>setContent(DataFormat.RTF, s)</code>. Setting this value
160     * to null effectively clears it from the clipboard.
161     * @param rtf The string to place. This may be null.
162     * @return True if the string was successfully placed on the clipboard.
163     * @throws NullPointerException if null reference is passed
164     */
165    public final boolean putRtf(String rtf) {
166        if (rtf == null) {
167            throw new NullPointerException("Null RTF put on ClipboardContent");
168        }
169        return put(DataFormat.RTF, rtf) == rtf;
170    }
171
172    /**
173     * Gets the RTF text String from the clipboard which had previously
174     * been registered. This is equivalent to invoking
175     * <code>getContent(DataFormat.RTF)</code>. If no such entry exists,
176     * null is returned.
177     * @return The String on the clipboard associated with DataFormat.RTF,
178     * or null if there is not one.
179     */
180    public final String getRtf() {
181        return (String) get(DataFormat.RTF);
182    }
183
184    /**
185     * Gets whether an Image (DataFormat.IMAGE) has been registered
186     * on this Clipboard.
187     * @return true if hasContent(DataFormat.IMAGE) returns true, false otherwise
188     */
189    public final boolean hasImage() {
190        return containsKey(DataFormat.IMAGE);
191    };
192
193    /**
194     * Puts an Image onto the Clipboard. This is equivalent to
195     * invoking <code>setContent(DataFormat.IMAGE, image)</code>. Setting this value
196     * to null effectively clears it from the clipboard. When an image is placed
197     * on the clipboard in this manner, an operating system dependent image
198     * is loaded onto the clipboard (such as TIFF on mac or DIB on Windows).
199     *
200     * @param i The image to place. This may be null.
201     * @return True if the image was successfully placed on the clipboard.
202     * @throws NullPointerException if null reference is passed
203     */
204    public final boolean putImage(Image i) {
205        if (i == null) {
206            throw new NullPointerException("Null image put on ClipboardContent");
207        }
208        return put(DataFormat.IMAGE, i) == i;
209    }
210
211    /**
212     * Gets the Image from the clipboard which had previously
213     * been registered. This is equivalent to invoking
214     * <code>getContent(DataFormat.IMAGE)</code>. If no such entry exists,
215     * null is returned.
216     * @return The Image on the clipboard associated with DataFormat.IMAGE,
217     * or null if there is not one.
218     */
219    public final Image getImage() {
220        return (Image) get(DataFormat.IMAGE);
221    }
222
223    /**
224     * Gets whether an List of Files (DataFormat.FILES) has been registered
225     * on this Clipboard.
226     * @return true if hasContent(DataFormat.FILES) returns true, false otherwise
227     */
228    public final boolean hasFiles() {
229        return containsKey(DataFormat.FILES);
230    }
231
232    /**
233     * Puts an List of Files onto the Clipboard. This is equivalent to
234     * invoking <code>setContent(DataFormat.FILES, files)</code>. Setting this value
235     * to null effectively clears it from the clipboard.
236     * @param files The files to place. This may be null.
237     * @return True if the files were successfully placed on the clipboard.
238     * @throws NullPointerException if null reference is passed
239     */
240    public final boolean putFiles(List<File> files) {
241        if (files == null) {
242            throw new NullPointerException("Null reference to files put "
243                    + "on ClipboardContent");
244        }
245        return put(DataFormat.FILES, files) == files;
246    }
247
248    /**
249     * Puts an List of Files onto the Clipboard, based on the file path. This is
250     * simply a convenience method which constructs a List of Files and invokes
251     * the {@link #putFiles} method.
252     * @param filePaths The files to place. This may be null.
253     * @return True if the files were successfully placed on the clipboard.
254     * @throws NullPointerException if null reference is passed
255     */
256    public final boolean putFilesByPath(List<String> filePaths) {
257        /* No need to throw NPE manually here, the code throws it anyway */
258        final List<File> files = new ArrayList<File>(filePaths.size());
259        for (String path : filePaths) {
260            files.add(new File(path));
261        }
262        return putFiles(files);
263    }
264
265    /**
266     * Gets the List of Files from the clipboard which had previously
267     * been registered. This is equivalent to invoking
268     * <code>getContent(DataFormat.FILES)</code>. If no such entry exists,
269     * null is returned.
270     * @return The List of Files on the clipboard associated with DataFormat.FILES,
271     * or null if there is not one.
272     */
273    public final List<File> getFiles() {
274        return (List<File>) get(DataFormat.FILES);
275    }
276}