2021년 11월 21일 일요일

Java FX API

1. Java FX API

package com.home.fx;

import javafx.beans.value.ChangeListener;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.geometry.Insets;
import javafx.geometry.Orientation;
import javafx.geometry.Pos;
import javafx.scene.Node;
import javafx.scene.control.*;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.input.Clipboard;
import javafx.scene.input.Dragboard;
import javafx.scene.input.TransferMode;
import javafx.scene.layout.HBox;
import javafx.scene.layout.Priority;
import javafx.scene.layout.VBox;

import java.io.File;
import java.util.List;


public class FX {
	
	private static int fontSize = 12;
	private static int gap = 2;
	private static Insets padding = new Insets(0,0,0,0); //TRBL

	public static void setFontSize(Node node) {
		node.setStyle("-fx-font: " + fontSize + " arial;");
	}
	public static void setBackground(Node node) { node.setStyle("-fx-background-color: #D8BFD8;"); }
	public static void enable(Node node) { node.setDisable(false); }
	public static void diable(Node node) { node.setDisable(false); }
	public static void show(Node node) { node.setVisible(true); }
	public static void hide(Node node) { node.setVisible(false); }

	public static void setDropHandler(Node node, FxRunnable runnable) {
		node.setOnDragOver( event -> {
			if( event.getDragboard().hasFiles() ) {
				event.acceptTransferModes(TransferMode.COPY_OR_MOVE);
			}
			event.consume();
		});
		node.setOnDragDropped(event -> {
			Dragboard db = event.getDragboard();
			boolean success = false;
			if( db.hasFiles() ) {
				success = true;
				List<File> files = db.getFiles();
				runnable.run(files);
			}
			event.setDropCompleted(success);
			event.consume();
		});
	}

	public static MenuBar getMenuBar() {
		MenuBar menubar = new MenuBar();
		Menu menu = new Menu("File");
		MenuItem item = new MenuItem("Exit");
		setFontSize(menubar);
		menu.setStyle("-fx-font: " + fontSize + " arial;");
		item.setStyle("-fx-font: " + fontSize + " arial;");
		item.setOnAction((e) -> System.exit(0));
		menu.getItems().add(item);
		menubar.getMenus().add(menu);
		return menubar;
	}

	public static SplitPane getHSplitPane(Double first) {
		SplitPane pane = new SplitPane();
		pane.setDividerPositions(first, 1 - first);
		pane.setOrientation(Orientation.HORIZONTAL);
		return pane;
	}
	public static SplitPane getHSplitPane() {
		return getHSplitPane(0.5);
	}
	public static SplitPane getVSplitPane(Double first) {
		SplitPane pane = new SplitPane();
		pane.setDividerPositions(first, 1 - first);
		pane.setOrientation(Orientation.VERTICAL);
		return pane;
	}
	public static SplitPane getVSplitPane() {
		return getVSplitPane(0.5);
	}
	public static void addSplitPane(SplitPane split, Node node) {
		split.getItems().add(node);
	}
	public static void addSplitPane(SplitPane split, Node node, Double size) {
		node.minWidth(size);
		node.maxWidth(size);
		split.getItems().add(node);
	}
	public static void addSplitPaneTwo(SplitPane split, Node left, Node right, Double size) {
		left.minWidth(size);
		left.maxWidth(size);
		split.getItems().addAll(left, right);
		//split.setDividerPositions(size / stage.getScene().getWidth());
	}
	public static void addSplitPaneAll(SplitPane split, Node...node) {
		for( int i = 0; i < node.length; i++ ) {
			split.getItems().add(node[i]);
		}
	}

	public static ScrollPane getScrollPane(Node node) {
		ScrollPane scroll = new ScrollPane(node);
		scroll.setVbarPolicy( ScrollPane.ScrollBarPolicy.ALWAYS );
		scroll.setHbarPolicy( ScrollPane.ScrollBarPolicy.ALWAYS );
		scroll.setPannable( true );
		scroll.setFitToWidth( true );
		scroll.setFitToHeight( true );
		return scroll;
	}

	public static Button getButton(String title, EventHandler<ActionEvent> action) {
		Button button = new Button();
		button.setText(title);
		setFontSize(button);
		button.setOnAction( action );
		return button;
	}

	public static ComboBox<String> getComboBox(Runnable runnable, String[] items) {
		ComboBox<String> combo = new ComboBox<>();
		setFontSize( combo );
		if (items != null) {
			combo.getItems().addAll(items);
			combo.getSelectionModel().select(0);
		}
		if (runnable != null) {
			combo.getSelectionModel().selectedItemProperty().addListener(
					(ChangeListener<String>) (observable, oldValue, newValue) -> runnable.run());
		}
		return combo;
	}
	public String getSelectedComboItem(ComboBox<String> combo) {
		return combo.getSelectionModel().getSelectedItem();
	}
	public int getSelectedComboIndex(ComboBox<String> combo) {
		return combo.getSelectionModel().getSelectedIndex();
	}

	public static TextArea getTextArea() {
		TextArea textArea = new TextArea();
		setFontSize(textArea);
		return textArea;
	}
	
	public static VBox getVBox() {
		VBox box = new VBox(gap); 
		box.setAlignment(Pos.CENTER);
		box.setSpacing(gap);
		box.setPadding(padding);		
		return box;
	}
	public static void addVbox(VBox box, Node node, boolean expand) {
		if( expand ) { VBox.setVgrow(node, Priority.ALWAYS);} 
		else         { VBox.setVgrow(node, Priority.NEVER); }
		box.getChildren().add(node);
	}
	public static HBox getHBox() {
		HBox box = new HBox(gap); 
		box.setAlignment(Pos.BASELINE_LEFT);
		box.setSpacing(gap);
		box.setPadding(padding);		
		return box;
	}
	public static void addHbox(HBox box, Node node, boolean expand) {
		if( expand ) { HBox.setHgrow(node, Priority.ALWAYS);} 
		else         { HBox.setHgrow(node, Priority.NEVER); }
		box.getChildren().add(node);
	}

	public static Node getToolTextArea() {
		TextArea textArea = new TextArea();
		VBox vbox = new VBox(gap); 
		HBox hbox = new HBox(gap);
		Button button = new Button("Paste Html");
		button.setOnAction((e) -> {
			if( Clipboard.getSystemClipboard().hasHtml() ) {
				textArea.clear();
				textArea.setText( Clipboard.getSystemClipboard().getHtml() );
			}
		});
		hbox.getChildren().add(button);
		textArea.setStyle("-fx-font: " + fontSize + " arial;");
		VBox.setVgrow(hbox, Priority.NEVER);
		VBox.setVgrow(textArea, Priority.ALWAYS);
		vbox.setAlignment(Pos.CENTER);
		vbox.setSpacing(gap);
		vbox.setPadding(padding);
		vbox.getChildren().add(hbox);
		vbox.getChildren().add(textArea);
		return vbox;
	}

	public static ImageView getImageView(Image image, Double width, Double height) {
		ImageView iv;
		if( image == null ) { iv = new ImageView(); }
		else { iv = new ImageView(image); }
		if( width != null ) iv.setFitWidth(width);
		if( height != null ) iv.setFitHeight(height);
		iv.setPreserveRatio(true);
		return iv;
	}
	public static ImageView getImageView() {
		return getImageView( null, null, null );
	}
	public static ImageView getImageView(Image image) {
		return getImageView( image, null, null );
	}
}

댓글 없음:

댓글 쓰기