All Downloads are FREE. Search and download functionalities are using the official Maven repository.

se.europeanspallationsource.xaos.ui.util.ClipboardUtils Maven / Gradle / Ivy

Go to download

JavaFX-based portion of the XAOS framework, containing the JavaFX-based controls and tools suitable for other projects too.

The newest version!
/*
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
 * Copyright (C) 2018-2019 by European Spallation Source ERIC.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package se.europeanspallationsource.xaos.ui.util;


import java.util.Objects;
import javafx.scene.Node;
import javafx.scene.SnapshotParameters;
import javafx.scene.input.Clipboard;
import javafx.scene.input.ClipboardContent;


/**
 * Various utilities for clipboards.
 *
 * @author [email protected]
 */
@SuppressWarnings( "ClassWithoutLogger" )
public class ClipboardUtils {

	/**
	 * Captures a snapshot image of the given {@link Node} and copies it into
	 * the system clipboard.
	 *
	 * @param node The node whose snapshot image must be copied in the system
	 *             clipboard.
	 * @throws NullPointerException If {@code node} is {@code null}.
	 */
	public static void captureSnapshotAndCopyToSystemClipboard( Node node ) {

		Objects.requireNonNull(node, "Null 'node' parameter.");

		node.snapshot(p -> {

				final ClipboardContent content = new ClipboardContent();

				content.putImage(p.getImage());
				Clipboard.getSystemClipboard().setContent(content);
				return null;

			},
			new SnapshotParameters(),
			null
		);

	}

	/**
	 * Clear the system clipboard.
	 */
	public static void clearSystemClipboard() {
		Clipboard.getSystemClipboard().clear();
	}

	/**
	 * Copy the given string into the system clipboard. The clipboard is cleared
	 * before adding the new content.
	 *
	 * @param text The string to be copied into the system clipboard.
	 */
	public static void copyToSystemClipboard( String text ) {

		final ClipboardContent content = new ClipboardContent();

		content.putString(text);
		Clipboard.getSystemClipboard().setContent(content);

	}

	private ClipboardUtils() {
		//	Nothing to do.
	}

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy