
com.dooapp.fxform.Demo Maven / Gradle / Ivy
/*
* Copyright (c) 2012, dooApp
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
*
* Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
* Neither the name of dooApp nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
package com.dooapp.fxform;
import com.dooapp.fxform.builder.FXFormBuilder;
import com.dooapp.fxform.view.FXFormSkin;
import com.dooapp.fxform.view.FXFormSkinFactory;
import com.dooapp.fxform.view.skin.FXMLSkin;
import javafx.application.Application;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.embed.swing.SwingFXUtils;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.Node;
import javafx.scene.Scene;
import javafx.scene.SceneBuilder;
import javafx.scene.control.*;
import javafx.scene.image.WritableImage;
import javafx.scene.layout.StackPane;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
import javafx.util.Callback;
import org.apache.log4j.BasicConfigurator;
import javax.imageio.ImageIO;
import javax.validation.ConstraintViolation;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.ResourceBundle;
/**
* User: Antoine Mischler
* Date: 09/04/11
* Time: 21:44
* FXForm demo
*/
public class Demo extends Application {
private FXForm fxForm;
private StackPane root = new StackPane();
private final String css = Demo.class.getResource("style.css").toExternalForm();
private final String material = Demo.class.getResource("material-fx-v0.3.css").toExternalForm();
protected void setup() {
MyBean joe = new MyBean("Joe", "contact@", "How does this crazy form works?", true, MyBean.Subject.QUESTION);
new ObjectPropertyObserver(joe);
fxForm = new FXFormBuilder()
.source(joe)
.categorize("-USER-", "name", "welcome", "email", "-DATA-", "subject", "message")
.resourceBundle(ResourceBundle.getBundle("com.dooapp.fxform.Demo"))
.build();
fxForm.setTitle("Dude, where is my form?");
root.getChildren().add(createNode());
}
private Node createNode() {
VBox vBox = new VBox();
vBox.getChildren().addAll(createSkinSelector(), createCSSNode(), createSnapshotButton(), fxForm, createConstraintNode());
return ScrollPaneBuilder.create().content(vBox).fitToWidth(true).build();
}
private Button createSnapshotButton() {
Button button = new Button("Snapshot");
button.setOnAction(event -> {
WritableImage writableImage = new WritableImage((int) fxForm.getWidth(), (int) fxForm.getHeight());
fxForm.snapshot(null, writableImage);
BufferedImage bImage = SwingFXUtils.fromFXImage(writableImage, null);
try {
ImageIO.write(bImage, "png", new File("fxform.png"));
} catch (IOException e) {
e.printStackTrace();
}
});
return button;
}
private Node createConstraintNode() {
return ButtonBuilder.create().text("Validate")
.defaultButton(true)
.onAction(new EventHandler() {
@Override
public void handle(ActionEvent actionEvent) {
ListView listView = new ListView();
listView.setCellFactory(new Callback, ListCell>() {
@Override
public ListCell call(ListView constraintViolation) {
return new ListCell() {
@Override
protected void updateItem(ConstraintViolation constraintViolation, boolean b) {
super.updateItem(constraintViolation, b);
if (constraintViolation != null) {
setText(constraintViolation.getPropertyPath().toString() + " - " + constraintViolation.getMessage());
} else {
setText("");
}
}
};
}
});
listView.getItems().setAll(fxForm.getConstraintViolations());
Scene scene = new Scene(listView);
Stage stage = new Stage();
stage.setScene(scene);
stage.show();
}
}).build();
}
private Node createCSSNode() {
CheckBox checkBox = new CheckBox("Use css");
checkBox.selectedProperty().addListener(new ChangeListener() {
public void changed(ObservableValue extends Boolean> observableValue, Boolean aBoolean, Boolean aBoolean1) {
if (aBoolean1) {
root.getScene().getStylesheets().add(css);
root.getScene().getStylesheets().add(material);
} else {
root.getScene().getStylesheets().remove(css);
root.getScene().getStylesheets().remove(material);
}
}
});
checkBox.setSelected(true);
return checkBox;
}
/**
* Create a selector that changes the skin of the form.
*
* @return
*/
private Node createSkinSelector() {
ChoiceBox choiceBox = new ChoiceBox();
choiceBox.getItems().addAll(FXFormSkinFactory.DEFAULT_FACTORY, FXFormSkinFactory.INLINE_FACTORY, new FXFormSkinFactory() {
@Override
public FXFormSkin createSkin(FXForm form) {
return new FXMLSkin(form, Demo.class.getResource("Demo_form.fxml"));
}
@Override
public String toString() {
return "FXML Skin (Demo_form.fxml)";
}
});
choiceBox.getSelectionModel().selectedItemProperty().addListener(new ChangeListener() {
public void changed(ObservableValue extends FXFormSkinFactory> observableValue, FXFormSkinFactory fxFormSkinFactory, FXFormSkinFactory fxFormSkinFactory1) {
fxForm.setSkin(fxFormSkinFactory1.createSkin(fxForm));
}
});
choiceBox.getSelectionModel().selectFirst();
return choiceBox;
}
public static void main(String[] args) {
BasicConfigurator.configure();
Application.launch(Demo.class, args);
}
@Override
public void start(Stage stage) throws Exception {
stage.setTitle("FXForm Demo");
stage.setScene(SceneBuilder.create().root(root).build());
stage.setHeight(600);
stage.setWidth(490);
setup();
stage.show();
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy