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

com.appslandia.javafx.base.DialogImpl Maven / Gradle / Ivy

The newest version!
// The MIT License (MIT)
// Copyright © 2015 AppsLandia. All rights reserved.

// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:

// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.

// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

package com.appslandia.javafx.base;

import java.util.function.Consumer;

import com.appslandia.javafx.utils.ButtonUtils;
import com.appslandia.javafx.utils.ControlUtils;
import com.appslandia.javafx.utils.Resources;

import javafx.event.ActionEvent;
import javafx.scene.Node;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.ButtonType;
import javafx.scene.control.Dialog;
import javafx.scene.control.DialogEvent;
import javafx.stage.Modality;
import javafx.stage.Window;

/**
 *
 * @author Loc Ha
 *
 */
public class DialogImpl extends Dialog {

	public DialogImpl() {
		titleRes(Resources.APP_TITLE);
		setResultConverter(b -> getResult());

		setOnShowing(this::onDialogShowing);
		setOnShown(this::onDialogShown);
	}

	protected void onDialogShowing(DialogEvent e) {
		for (ButtonType type : getDialogPane().getButtonTypes()) {
			Button btn = (Button) getDialogPane().lookupButton(type);
			btn.setDefaultButton(false);
		}
	}

	protected void onDialogShown(DialogEvent e) {
	}

	public DialogImpl title(String value) {
		setTitle(value);
		return this;
	}

	public DialogImpl titleRes(String value) {
		setTitle(Resources.getString(value));
		return this;
	}

	public DialogImpl titleRes(String value, Object... params) {
		setTitle(Resources.getString(value, params));
		return this;
	}

	public DialogImpl headerText(String value) {
		setHeaderText(value);
		return this;
	}

	public DialogImpl headerTextRes(String value) {
		setHeaderText(Resources.getString(value));
		return this;
	}

	public DialogImpl headerTextRes(String value, Object... params) {
		setHeaderText(Resources.getString(value, params));
		return this;
	}

	public DialogImpl contentText(String value) {
		setContentText(value);
		return this;
	}

	public DialogImpl contentTextRes(String value) {
		setContentText(Resources.getString(value));
		return this;
	}

	public DialogImpl contentTextRes(String value, Object... params) {
		setContentText(Resources.getString(value, params));
		return this;
	}

	public DialogImpl modality(Modality value) {
		initModality(value);
		return this;
	}

	public DialogImpl owner(Window owner) {
		initOwner(owner);
		return this;
	}

	public DialogImpl headerNode(Node value) {
		getDialogPane().setHeader(value);
		return this;
	}

	public DialogImpl contentNode(Node value) {
		getDialogPane().setContent(value);
		return this;
	}

	public DialogImpl expandableContentNode(Node value) {
		getDialogPane().setExpandableContent(value);
		return this;
	}

	public Button initButton(ButtonType type, boolean enable, Runnable handler) {
		getDialogPane().getButtonTypes().add(type);
		Button btn = (Button) getDialogPane().lookupButton(type);

		btn.setDisable(!enable);
		ButtonUtils.localize(btn, type);
		ControlUtils.bindEnter(btn);

		btn.addEventFilter(ActionEvent.ACTION, e -> {
			e.consume();
			handler.run();
		});
		return btn;
	}

	public Button cancelButton() {
		getDialogPane().getButtonTypes().add(ButtonType.CANCEL);
		Button btn = (Button) getDialogPane().lookupButton(ButtonType.CANCEL);

		ButtonUtils.localize(btn, ButtonType.CANCEL);
		ControlUtils.bindEnter(btn);
		return btn;
	}

	public DialogImpl ifResult(Consumer handler) {
		resultProperty().addListener((observable, oldValue, newValue) -> {
			if (newValue != null) {
				handler.accept(newValue);
			}
		});
		return this;
	}

	public DialogImpl showAsync() {
		show();
		return this;
	}

	public Window getWindow() {
		Scene s = getDialogPane().getScene();
		return (s != null) ? s.getWindow() : null;
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy