Created
February 1, 2017 22:18
-
-
Save Andrauss/06e46832e07c292ba20f95ec3afa1d08 to your computer and use it in GitHub Desktop.
WindowController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import javafx.beans.property.SimpleStringProperty; | |
import javafx.beans.property.StringProperty; | |
import javafx.fxml.FXMLLoader; | |
import javafx.fxml.Initializable; | |
import javafx.scene.control.Dialog; | |
import javafx.scene.control.DialogPane; | |
import javafx.scene.effect.ColorAdjust; | |
import javafx.scene.effect.Effect; | |
import javafx.scene.effect.GaussianBlur; | |
import javafx.scene.effect.Light; | |
import javafx.scene.effect.Lighting; | |
import javafx.scene.image.Image; | |
import javafx.scene.layout.Region; | |
import javafx.scene.paint.Color; | |
import javafx.stage.Modality; | |
import javafx.stage.Stage; | |
import javafx.stage.StageStyle; | |
import javafx.stage.Window; | |
import javafx.util.Duration; | |
/** | |
* | |
* @author Fernando Andrauss | |
*/ | |
public abstract class WindowController implements Initializable { | |
private final StringProperty titulo = new SimpleStringProperty(); | |
private Region rootWindowPane; | |
private Window pai; | |
private Image icon; | |
private Stage janela; | |
private WindowController controller; | |
private Modality modality; | |
private boolean wait = false; | |
private boolean blur = false; | |
private Effect oldEffectPai; | |
public WindowController setIcone(Image icone) { | |
this.icon = icone; | |
return this; | |
} | |
public WindowController setModality(Modality modality) { | |
this.modality = modality; | |
return this; | |
} | |
public WindowController setWait() { | |
this.wait = true; | |
return this; | |
} | |
public WindowController setBlur(boolean blur) { | |
this.blur = blur; | |
return this; | |
} | |
public WindowController maximize() { | |
if (janela == null) { | |
throw new RuntimeException("Esse método só pode ser usado após a criação da janela!"); | |
} | |
janela.setMaximized(true); | |
return this; | |
} | |
public WindowController altura(double altura) { | |
if (janela == null) { | |
throw new RuntimeException("Esse método só pode ser usado após a criação da janela!"); | |
} | |
janela.setHeight(altura); | |
return this; | |
} | |
public WindowController largura(double altura) { | |
if (janela == null) { | |
throw new RuntimeException("Esse método só pode ser usado após a criação da janela!"); | |
} | |
janela.setWidth(altura); | |
return this; | |
} | |
public WindowController minimize() { | |
if (janela == null) { | |
throw new RuntimeException("Esse método só pode ser usado após a criação da janela!"); | |
} | |
janela.setMaximized(false); | |
return this; | |
} | |
public WindowController telaCheia() { | |
if (janela == null) { | |
throw new RuntimeException("Esse método só pode ser usado após a criação da janela!"); | |
} | |
janela.setFullScreen(true); | |
return this; | |
} | |
public WindowController sairTelaCheia() { | |
if (janela == null) { | |
throw new RuntimeException("Esse método só pode ser usado após a criação da janela!"); | |
} | |
janela.setFullScreen(false); | |
return this; | |
} | |
public String getTitulo() { | |
return titulo.get(); | |
} | |
public WindowController setTitulo(String value) { | |
titulo.set(value); | |
return this; | |
} | |
public StringProperty tituloProperty() { | |
return titulo; | |
} | |
public String getFXML() { | |
return null; | |
} | |
public Region getRootPane() { | |
return null; | |
} | |
private void loadView() throws Exception { | |
System.out.println("LOAD VIEW"); | |
if (getFXML() != null && getRootPane() == null) { | |
FXMLLoader loader = new FXMLLoader(); | |
loader.setLocation(getClass().getResource(getFXML())); | |
try { | |
rootWindowPane = loader.load(); | |
controller = loader.getController(); | |
System.out.println("CONTROLLER " + controller); | |
} catch (Exception e) { | |
controller = null; | |
System.out.println(e.getMessage() + " - " + e.getMessage().contains("specified")); | |
if (!e.getMessage().contains("specified")) { | |
throw e; | |
} | |
} | |
if (controller == null) { | |
System.out.println("controller == null"); | |
loader = new FXMLLoader(); | |
loader.setLocation(getClass().getResource(getFXML())); | |
loader.setController(this); | |
controller = this; | |
rootWindowPane = loader.load(); | |
} | |
} else if (getRootPane() != null) { | |
rootWindowPane = getRootPane(); | |
} else { | |
throw new RuntimeException("O painel principal não foi definido, sobreescreva o método getFXML ou getRootPane!"); | |
} | |
} | |
public WindowController show(Window pai) { | |
return createShow(pai, wait); | |
} | |
private WindowController createShow(Window pai, boolean wait) { | |
try { | |
this.pai = pai; | |
loadView(); | |
janela = TopFxUtilidades.createStageFromContent(pai, rootWindowPane, titulo.get()); | |
janela.titleProperty().bind(titulo); | |
janela.initModality(Modality.NONE); | |
janela.initOwner(pai); | |
janela.initStyle(StageStyle.DECORATED); | |
if (icon != null) { | |
janela.getIcons().add(icon); | |
} | |
if (controller != null) { | |
controller.janela = janela; | |
} | |
if (blur) { | |
blurWindow(); | |
} | |
if (wait) { | |
janela.showAndWait(); | |
} else { | |
janela.showAndWait(); | |
} | |
onShow(); | |
return this; | |
} catch (Exception ex) { | |
TopMsg.showErrorDialog(pai, ex, "Falha ao criar a jenela", ex.getMessage()); | |
return null; | |
} | |
} | |
public WindowController showModal(Window pai) { | |
return createshowModal(pai, wait); | |
} | |
private WindowController createshowModal(Window pai, boolean wait) { | |
try { | |
this.pai = pai; | |
modality = modality != null ? modality : Modality.WINDOW_MODAL; | |
System.out.println(modality); | |
loadView(); | |
janela = TopFxUtilidades.createStageFromContent(pai, rootWindowPane, titulo.get()); | |
janela.titleProperty().bind(titulo); | |
janela.initModality(modality); | |
janela.initOwner(pai); | |
janela.initStyle(StageStyle.DECORATED); | |
if (icon != null) { | |
janela.getIcons().add(icon); | |
} | |
if (controller != null) { | |
controller.janela = janela; | |
} | |
if (blur) { | |
blurWindow(); | |
} | |
if (wait) { | |
janela.showAndWait(); | |
} else { | |
janela.show(); | |
} | |
onShow(); | |
return this; | |
} catch (Exception ex) { | |
TopMsg.showErrorDialog(pai, ex, "Falha ao criar a jenela", ex.getMessage()); | |
return null; | |
} | |
} | |
public WindowController showUndecorated(Window pai, boolean transparent) { | |
return createshowUndecorated(pai, transparent, wait); | |
} | |
private WindowController createshowUndecorated(Window pai, boolean transparent, boolean wait) { | |
try { | |
this.pai = pai; | |
modality = modality != null ? modality : Modality.WINDOW_MODAL; | |
loadView(); | |
janela = TopFxUtilidades.createStageFromContent(pai, rootWindowPane, titulo.get()); | |
janela.titleProperty().bind(titulo); | |
janela.initModality(modality); | |
janela.initOwner(pai); | |
janela.initStyle(StageStyle.UNDECORATED); | |
if (transparent) { | |
janela.initStyle(StageStyle.TRANSPARENT); | |
janela.getScene().setFill(Color.TRANSPARENT); | |
rootWindowPane.setStyle("-fx-background-color: transparent;"); | |
} | |
if (icon != null) { | |
janela.getIcons().add(icon); | |
} | |
if (controller != null) { | |
controller.janela = janela; | |
} | |
if (blur) { | |
blurWindow(); | |
} | |
if (wait) { | |
janela.showAndWait(); | |
} else { | |
janela.show(); | |
} | |
onShow(); | |
return this; | |
} catch (Exception ex) { | |
TopMsg.showErrorDialog(pai, ex, "Falha ao criar a jenela", ex.getMessage()); | |
return null; | |
} | |
} | |
public WindowController showAsDialg(Window pai) { | |
try { | |
this.pai = pai; | |
modality = modality != null ? modality : Modality.WINDOW_MODAL; | |
loadView(); | |
janela = TopFxUtilidades.createStageFromContent(pai, rootWindowPane, titulo.get()); | |
janela.titleProperty().bind(titulo); | |
janela.initModality(modality); | |
janela.initOwner(pai); | |
janela.initStyle(StageStyle.UTILITY); | |
if (icon != null) { | |
janela.getIcons().add(icon); | |
} | |
if (controller != null) { | |
controller.janela = janela; | |
} | |
if (blur) { | |
blurWindow(); | |
} | |
if (wait) { | |
janela.showAndWait(); | |
} else { | |
janela.show(); | |
} | |
onShow(); | |
return this; | |
} catch (Exception ex) { | |
TopMsg.showErrorDialog(pai, ex, "Falha ao criar a jenela", ex.getMessage()); | |
return null; | |
} | |
} | |
public Stage createStage(Window pai) { | |
try { | |
this.pai = pai; | |
modality = modality != null ? modality : Modality.NONE; | |
loadView(); | |
janela = TopFxUtilidades.createStageFromContent(pai, rootWindowPane, titulo.get()); | |
janela.titleProperty().bind(titulo); | |
janela.initModality(modality); | |
janela.initOwner(pai); | |
janela.initStyle(StageStyle.DECORATED); | |
if (blur) { | |
blurWindow(); | |
} | |
if (icon != null) { | |
janela.getIcons().add(icon); | |
} | |
return janela; | |
} catch (Exception ex) { | |
TopMsg.showErrorDialog(pai, ex, "Falha ao criar a jenela", ex.getMessage()); | |
return null; | |
} | |
} | |
public Stage getWindow() { | |
return janela; | |
} | |
public void onShow() { | |
} | |
private void blurWindow() { | |
final Effect EFFECT = new ColorAdjust(0, 0, -0.52, 0); | |
getWindow().setOnShown((evt) -> { | |
if (pai != null) { | |
oldEffectPai = pai.getScene().getRoot().getEffect(); | |
pai.getScene().getRoot().setEffect(EFFECT); | |
} | |
}); | |
getWindow().setOnHiding((event) -> { | |
if (pai != null) { | |
pai.getScene().getRoot().setEffect(oldEffectPai); | |
} | |
}); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment