Skip to content

Instantly share code, notes, and snippets.

@rbmethu
Last active June 25, 2017 09:22
Show Gist options
  • Save rbmethu/edc16c307184968db877948a25f8868a to your computer and use it in GitHub Desktop.
Save rbmethu/edc16c307184968db877948a25f8868a to your computer and use it in GitHub Desktop.
/**
* Staff feature title
*/
public class FeatureObject {
private String name;
private String title;
private boolean hidden;
private ModuleObject module;
public FeatureObject(){
this.hidden= false;
this.module= new ModuleObject();
}
/**
* @param module ModuleObject owner
* @param name String module unique name
* @param title String module title
*/
public FeatureObject(ModuleObject module, String name, String title){
this.hidden= false;
this.title= title;
this.module = module;
this.name= name;
}
/**
* get feature name
* @return String name
*/
public String getName(){
return this.name;
}
/**
* get feature title
* @return String title
*/
public String getTitle(){
return this.title;
}
/**
* get module
* @return ModuleObject module
*/
public ModuleObject getModule() {
return this.module;
}
/**
* check if feature is hidden
* @return boolean status
*/
public boolean isHidden(){
return this.hidden;
}
/**
* set feature name
* @param name String name
*/
public void setName(String name){
this.name= name;
}
/**
* set feature title
* @param title String title
*/
public void setTitle(String title){
this.title= title;
}
/**
* hide feature
* @param hidden boolean status
*/
public void setHidden(boolean hidden){
this.hidden= hidden;
}
/**
* set module
* @param module ModuleObject
*/
public void setModule(ModuleObject module) {
this.module= module;
}
}
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.geometry.*?>
<?import java.lang.*?>
<?import javafx.collections.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>
<?import javafx.scene.layout.AnchorPane?>
<VBox prefWidth="300.0" spacing="5.0" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1">
<children>
<Label styleClass="title-normal" text="Module:" />
<AnchorPane>
<children>
<ComboBox fx:id="moduleField" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0" />
</children>
</AnchorPane>
<Label styleClass="title-normal" text="Feature:" />
<AnchorPane>
<children>
<ComboBox fx:id="featureField" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0" />
</children>
</AnchorPane>
</children>
<padding>
<Insets bottom="5.0" left="5.0" right="5.0" />
</padding>
</VBox>
import java.io.IOException;
import java.io.InputStream;
import java.util.List;
import javafx.beans.value.ObservableValue;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.collections.transformation.FilteredList;
import javafx.fxml.FXMLLoader;
import javafx.fxml.JavaFXBuilderFactory;
import javafx.scene.Node;
import javafx.scene.control.ComboBox;
import javafx.scene.control.ListCell;
import javafx.scene.control.ListView;
import javafx.scene.layout.VBox;
import javafx.util.Callback;
import javafx.util.StringConverter;
/**
* module form layout
*/
public class ModuleFormLayout {
private final VBox container;
private ComboBox<ModuleObject> moduleField;
private ComboBox<FeatureObject> featureField;
private final ObservableList<ModuleObject> modules;
private final ObservableList<FeatureObject> features;
/**
* @param modules List of modules
* @param features List of features
*/
public ModuleFormLayout(List<ModuleObject> modules,
List<FeatureObject> features){
this.modules= FXCollections.observableArrayList(modules);
this.features= FXCollections.observableArrayList(features);
this.container= this.loadFormLayout();
}
/**
* get layout container
* @return VBox container
*/
public VBox getContainer(){
return this.container;
}
/**
* fill form fields with values from current object
*/
public void intFormValues(){
this.moduleField.setItems(this.modules);
this.featureField.setItems(this.features);
}
/**
* initialize form and form fields
* @return VBox form container
*/
private VBox loadFormLayout() {
VBox form= (VBox) ModuleFormLayout.readFxml("form.fxml");
this.featureField= (ComboBox<FeatureObject>) form.lookup("#featureField");
this.moduleField= (ComboBox<ModuleObject>) form.lookup("#moduleField");
this.actionHandler();
this.valueFactory();
this.intFormValues();
return form;
}
/**
* read layout from FXML
* this method is reused in other layouts
* @param fxml String file name
* @return Node read layout
*/
public static Node readFxml(String fxml) {
FXMLLoader loader = new FXMLLoader();
InputStream in = ModuleFormLayout.class.getResourceAsStream(fxml);
loader.setBuilderFactory(new JavaFXBuilderFactory());
loader.setLocation(ModuleFormLayout.class.getResource(fxml));
Node node = null;
try {
node = loader.load(in);
} catch (IOException ex) {
System.out.println(ex.getMessage());
}
return node;
}
/**
* format variables to fit layout
*/
private void valueFactory(){
this.moduleValueFactory();
this.featureValueFactory();
}
/**
* format module variables to fit layout
* this works fine
*/
private void moduleValueFactory() {
Callback<ListView<ModuleObject>, ListCell<ModuleObject>> module=
(ListView<ModuleObject> param) -> {
ListCell<ModuleObject> cell = new ListCell<ModuleObject>() {
@Override
protected void updateItem(ModuleObject item, boolean empty) {
super.updateItem(item, empty);
if (item != null) {
this.setText(item.getTitle());
} else {
this.setText(null);
}
}
};
return cell;
};
this.moduleField.setConverter(new StringConverter<ModuleObject>() {
@Override
public String toString(ModuleObject object) {
return object.getTitle();
}
@Override
public ModuleObject fromString(String string) {
return null;
}
});
this.moduleField.setCellFactory(module);
this.moduleField.setButtonCell(module.call(null));
}
/**
* format feature variables to fit layout
* this works fine
*/
private void featureValueFactory() {
Callback<ListView<FeatureObject>, ListCell<FeatureObject>> feature=
(ListView<FeatureObject> param) -> {
ListCell<FeatureObject> cell = new ListCell<FeatureObject>() {
@Override
protected void updateItem(FeatureObject item, boolean empty) {
super.updateItem(item, empty);
if (item != null) {
this.setText(item.getTitle());
} else {
this.setText(null);
}
}
};
return cell;
};
this.featureField.setConverter(new StringConverter<FeatureObject>() {
@Override
public String toString(FeatureObject object) {
return object.getTitle();
}
@Override
public FeatureObject fromString(String string) {
return null;
}
});
this.featureField.setCellFactory(feature);
this.featureField.setButtonCell(feature.call(null));
}
/**
* handle actions
* this is where error occurs
*/
private void actionHandler(){
//makesure module field is uptodate with selected feature
//works fine
this.featureField.valueProperty().addListener(
(ObservableValue<? extends FeatureObject> observable,
FeatureObject oldValue, FeatureObject newValue) -> {
if (newValue!=null) {
this.moduleField.setValue(newValue.getModule());
}
});
//exception thrown here
this.moduleField.valueProperty().addListener(
(ObservableValue<? extends ModuleObject> observable,
ModuleObject oldValue, ModuleObject newValue) -> {
FilteredList<FeatureObject> feature = new FilteredList<>(this.features);
feature.setPredicate((FeatureObject t) -> {
if (newValue==null) {
return true;
} else if (newValue.getName()==null || newValue.getName().equals("*")) {
return true;
} else {
return t.getModule().getName().equals(newValue.getName());
}
});
//setting this causes the exception
this.featureField.setItems(feature);
});
}
}
/**
* Staff module title
*/
public class ModuleObject {
private String name;
private String title;
private boolean hidden;
public ModuleObject(){
this.hidden= false;
}
/**
* @param name String module unique name
* @param title String module title
*/
public ModuleObject(String name, String title){
this();
this.title= title;
this.name= name;
}
/**
* get module name
* @return String name
*/
public String getName(){
return this.name;
}
/**
* get module title
* @return String title
*/
public String getTitle(){
return this.title;
}
/**
* check if module is hidden
* @return boolean status
*/
public boolean isHidden(){
return this.hidden;
}
/**
* set module name
* @param name String name
*/
public void setName(String name){
this.name= name;
}
/**
* set module title
* @param title String title
*/
public void setTitle(String title){
this.title= title;
}
/**
* hide module
* @param hidden boolean status
*/
public void setHidden(boolean hidden){
this.hidden= hidden;
}
}
import java.util.Arrays;
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.stage.Stage;
/**
* testing layout
*/
public class Test extends Application {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
launch(args);
}
@Override
public void start(Stage stage) throws Exception {
stage.setTitle("Module Form");
ModuleFormLayout form= this.testData();
Scene scene= new Scene(form.getContainer());
stage.setScene(scene);
stage.show();
}
/**
* add some testing data
* @return ModuleFormLayout
*/
private ModuleFormLayout testData(){
ModuleObject all= new ModuleObject("*", "All");
ModuleObject one= new ModuleObject("one", "Module One");
ModuleObject two= new ModuleObject("two", "Module Two");
FeatureObject all1= new FeatureObject(all, "all_1", "All Feature 1");
FeatureObject one1= new FeatureObject(one, "one_1", "One Feature 1");
FeatureObject one2= new FeatureObject(one, "one_2", "One Feature 2");
FeatureObject one3= new FeatureObject(one, "one_3", "One Feature 3");
FeatureObject two1= new FeatureObject(two, "two_1", "Two Feature 1");
FeatureObject two2= new FeatureObject(two, "two_2", "Two Feature 2");
FeatureObject two3= new FeatureObject(two, "two_3", "Two Feature 3");
return new ModuleFormLayout(Arrays.asList(all, one, two),
Arrays.asList(all1, one1, one2, one3, two1, two2,two3));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment