Java tutorial
// Description: Java 8 JavaFX Picker of Obj Pane implementation for ClearDep. /* * CFBam * * Copyright (c) 2014-2016 Mark Sobkow * * This program is available as free software under the GNU GPL v3, or * under a commercial license from Mark Sobkow. For commercial licensing * details, please contact msobkow@sasktel.net. * * Under the terms of the GPL: * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program. If not, see <http://www.gnu.org/licenses/>. * */ package net.sourceforge.msscodefactory.cfbam.v2_7.CFBamJavaFX; import java.math.*; import java.sql.*; import java.text.*; import java.util.*; import java.util.List; import javafx.beans.property.ReadOnlyObjectWrapper; import javafx.beans.value.ChangeListener; import javafx.beans.value.ObservableValue; import javafx.collections.FXCollections; import javafx.collections.ObservableList; import javafx.event.ActionEvent; import javafx.event.EventHandler; import javafx.scene.Parent; import javafx.scene.control.ScrollPane; import javafx.scene.control.ScrollPane.ScrollBarPolicy; import javafx.scene.control.TableCell; import javafx.scene.control.TableColumn; import javafx.scene.control.TableColumn.CellDataFeatures; import javafx.scene.control.TableView; import javafx.util.Callback; import net.sourceforge.msscodefactory.cflib.v2_7.CFLib.*; import net.sourceforge.msscodefactory.cflib.v2_7.CFLib.JavaFX.*; import org.apache.commons.codec.binary.Base64; import net.sourceforge.msscodefactory.cfsecurity.v2_7.CFSecurity.*; import net.sourceforge.msscodefactory.cfinternet.v2_7.CFInternet.*; import net.sourceforge.msscodefactory.cfbam.v2_7.CFBam.*; import net.sourceforge.msscodefactory.cfsecurity.v2_7.CFSecurityObj.*; import net.sourceforge.msscodefactory.cfinternet.v2_7.CFInternetObj.*; import net.sourceforge.msscodefactory.cfbam.v2_7.CFBamObj.*; import net.sourceforge.msscodefactory.cfsecurity.v2_7.CFSecurityJavaFX.*; import net.sourceforge.msscodefactory.cfinternet.v2_7.CFInternetJavaFX.*; /** * CFBamJavaFXClearDepPickerPane JavaFX Pick Obj Pane implementation * for ClearDep. */ public class CFBamJavaFXClearDepPickerPane extends CFBorderPane implements ICFBamJavaFXClearDepPaneList { public static String S_FormName = "Choose ClearDep"; protected ICFBamJavaFXSchema javafxSchema = null; protected Collection<ICFBamClearDepObj> javafxDataCollection = null; protected ObservableList<ICFBamClearDepObj> observableListOfClearDep = null; protected TableColumn<ICFBamClearDepObj, String> tableColumnObjKind = null; protected TableColumn<ICFBamClearDepObj, Long> tableColumnId = null; protected TableColumn<ICFBamClearDepObj, ICFBamRelationObj> tableColumnLookupRelation = null; protected TableColumn<ICFBamClearDepObj, ICFBamSchemaDefObj> tableColumnLookupDefSchema = null; protected TableView<ICFBamClearDepObj> dataTable = null; protected CFHBox hboxMenu = null; public final String S_ColumnNames[] = { "Name" }; protected ICFFormManager cfFormManager = null; protected ICFBamJavaFXClearDepChosen invokeWhenChosen = null; protected ICFLibAnyObj javafxContainer = null; protected CFButton buttonCancel = null; protected CFButton buttonChooseNone = null; protected CFButton buttonChooseSelected = null; public CFBamJavaFXClearDepPickerPane(ICFFormManager formManager, ICFBamJavaFXSchema argSchema, ICFBamClearDepObj argFocus, ICFLibAnyObj argContainer, Collection<ICFBamClearDepObj> argDataCollection, ICFBamJavaFXClearDepChosen whenChosen) { super(); final String S_ProcName = "construct-schema-focus"; if (formManager == null) { throw CFLib.getDefaultExceptionFactory().newNullArgumentException(getClass(), S_ProcName, 1, "formManager"); } cfFormManager = formManager; if (argSchema == null) { throw CFLib.getDefaultExceptionFactory().newNullArgumentException(getClass(), S_ProcName, 2, "argSchema"); } if (whenChosen == null) { throw CFLib.getDefaultExceptionFactory().newNullArgumentException(getClass(), S_ProcName, 6, "whenChosen"); } invokeWhenChosen = whenChosen; // argFocus is optional; focus may be set later during execution as // conditions of the runtime change. javafxSchema = argSchema; javaFXFocus = argFocus; javafxContainer = argContainer; setJavaFXDataCollection(argDataCollection); dataTable = new TableView<ICFBamClearDepObj>(); tableColumnObjKind = new TableColumn<ICFBamClearDepObj, String>("Class Code"); tableColumnObjKind.setCellValueFactory( new Callback<CellDataFeatures<ICFBamClearDepObj, String>, ObservableValue<String>>() { @Override public ObservableValue<String> call(CellDataFeatures<ICFBamClearDepObj, String> p) { ICFBamClearDepObj obj = p.getValue(); if (obj == null) { return (null); } else { String classCode = obj.getClassCode(); ReadOnlyObjectWrapper<String> observable = new ReadOnlyObjectWrapper<String>(); observable.setValue(classCode); return (observable); } } }); tableColumnObjKind.setCellFactory( new Callback<TableColumn<ICFBamClearDepObj, String>, TableCell<ICFBamClearDepObj, String>>() { @Override public TableCell<ICFBamClearDepObj, String> call(TableColumn<ICFBamClearDepObj, String> arg) { return new CFStringTableCell<ICFBamClearDepObj>(); } }); dataTable.getColumns().add(tableColumnObjKind); tableColumnId = new TableColumn<ICFBamClearDepObj, Long>("Id"); tableColumnId.setCellValueFactory( new Callback<CellDataFeatures<ICFBamClearDepObj, Long>, ObservableValue<Long>>() { public ObservableValue<Long> call(CellDataFeatures<ICFBamClearDepObj, Long> p) { ICFBamScopeObj obj = p.getValue(); if (obj == null) { return (null); } else { long value = obj.getRequiredId(); Long wrapped = new Long(value); ReadOnlyObjectWrapper<Long> observable = new ReadOnlyObjectWrapper<Long>(); observable.setValue(wrapped); return (observable); } } }); tableColumnId.setCellFactory( new Callback<TableColumn<ICFBamClearDepObj, Long>, TableCell<ICFBamClearDepObj, Long>>() { @Override public TableCell<ICFBamClearDepObj, Long> call(TableColumn<ICFBamClearDepObj, Long> arg) { return new CFInt64TableCell<ICFBamClearDepObj>(); } }); dataTable.getColumns().add(tableColumnId); tableColumnLookupRelation = new TableColumn<ICFBamClearDepObj, ICFBamRelationObj>("Relation"); tableColumnLookupRelation.setCellValueFactory( new Callback<CellDataFeatures<ICFBamClearDepObj, ICFBamRelationObj>, ObservableValue<ICFBamRelationObj>>() { public ObservableValue<ICFBamRelationObj> call( CellDataFeatures<ICFBamClearDepObj, ICFBamRelationObj> p) { ICFBamClearDepObj obj = p.getValue(); if (obj == null) { return (null); } else { ICFBamRelationObj ref = obj.getRequiredLookupRelation(); ReadOnlyObjectWrapper<ICFBamRelationObj> observable = new ReadOnlyObjectWrapper<ICFBamRelationObj>(); observable.setValue(ref); return (observable); } } }); tableColumnLookupRelation.setCellFactory( new Callback<TableColumn<ICFBamClearDepObj, ICFBamRelationObj>, TableCell<ICFBamClearDepObj, ICFBamRelationObj>>() { @Override public TableCell<ICFBamClearDepObj, ICFBamRelationObj> call( TableColumn<ICFBamClearDepObj, ICFBamRelationObj> arg) { return new CFReferenceTableCell<ICFBamClearDepObj, ICFBamRelationObj>(); } }); dataTable.getColumns().add(tableColumnLookupRelation); tableColumnLookupDefSchema = new TableColumn<ICFBamClearDepObj, ICFBamSchemaDefObj>( "Defining Schema Definition"); tableColumnLookupDefSchema.setCellValueFactory( new Callback<CellDataFeatures<ICFBamClearDepObj, ICFBamSchemaDefObj>, ObservableValue<ICFBamSchemaDefObj>>() { public ObservableValue<ICFBamSchemaDefObj> call( CellDataFeatures<ICFBamClearDepObj, ICFBamSchemaDefObj> p) { ICFBamClearDepObj obj = p.getValue(); if (obj == null) { return (null); } else { ICFBamSchemaDefObj ref = obj.getOptionalLookupDefSchema(); ReadOnlyObjectWrapper<ICFBamSchemaDefObj> observable = new ReadOnlyObjectWrapper<ICFBamSchemaDefObj>(); observable.setValue(ref); return (observable); } } }); tableColumnLookupDefSchema.setCellFactory( new Callback<TableColumn<ICFBamClearDepObj, ICFBamSchemaDefObj>, TableCell<ICFBamClearDepObj, ICFBamSchemaDefObj>>() { @Override public TableCell<ICFBamClearDepObj, ICFBamSchemaDefObj> call( TableColumn<ICFBamClearDepObj, ICFBamSchemaDefObj> arg) { return new CFReferenceTableCell<ICFBamClearDepObj, ICFBamSchemaDefObj>(); } }); dataTable.getColumns().add(tableColumnLookupDefSchema); dataTable.getSelectionModel().selectedItemProperty().addListener(new ChangeListener<ICFBamClearDepObj>() { @Override public void changed(ObservableValue<? extends ICFBamClearDepObj> observable, ICFBamClearDepObj oldValue, ICFBamClearDepObj newValue) { setJavaFXFocus(newValue); if (buttonChooseSelected != null) { if (newValue != null) { buttonChooseSelected.setDisable(false); } else { buttonChooseSelected.setDisable(true); } } } }); hboxMenu = new CFHBox(10); buttonCancel = new CFButton(); buttonCancel.setMinWidth(200); buttonCancel.setText("Cancel"); buttonCancel.setOnAction(new EventHandler<ActionEvent>() { @Override public void handle(ActionEvent e) { final String S_ProcName = "handle"; try { cfFormManager.closeCurrentForm(); } catch (Throwable t) { CFConsole.formException(S_FormName, ((CFButton) e.getSource()).getText(), t); } } }); hboxMenu.getChildren().add(buttonCancel); buttonChooseNone = new CFButton(); buttonChooseNone.setMinWidth(200); buttonChooseNone.setText("ChooseNone"); buttonChooseNone.setOnAction(new EventHandler<ActionEvent>() { @Override public void handle(ActionEvent e) { final String S_ProcName = "handle"; try { ICFBamSchemaObj schemaObj = (ICFBamSchemaObj) javafxSchema.getSchema(); if (schemaObj == null) { throw CFLib.getDefaultExceptionFactory().newNullArgumentException(getClass(), S_ProcName, 0, "schemaObj"); } invokeWhenChosen.choseClearDep(null); cfFormManager.closeCurrentForm(); } catch (Throwable t) { CFConsole.formException(S_FormName, ((CFButton) e.getSource()).getText(), t); } } }); hboxMenu.getChildren().add(buttonChooseNone); buttonChooseSelected = new CFButton(); buttonChooseSelected.setMinWidth(200); buttonChooseSelected.setText("ChooseSelected"); buttonChooseSelected.setOnAction(new EventHandler<ActionEvent>() { @Override public void handle(ActionEvent e) { final String S_ProcName = "handle"; try { ICFBamSchemaObj schemaObj = (ICFBamSchemaObj) javafxSchema.getSchema(); if (schemaObj == null) { throw CFLib.getDefaultExceptionFactory().newNullArgumentException(getClass(), S_ProcName, 0, "schemaObj"); } ICFBamClearDepObj selectedInstance = getJavaFXFocusAsClearDep(); invokeWhenChosen.choseClearDep(selectedInstance); cfFormManager.closeCurrentForm(); } catch (Throwable t) { CFConsole.formException(S_FormName, ((CFButton) e.getSource()).getText(), t); } } }); hboxMenu.getChildren().add(buttonChooseSelected); if (argFocus != null) { dataTable.getSelectionModel().select(argFocus); } setTop(hboxMenu); setCenter(dataTable); } public ICFFormManager getCFFormManager() { return (cfFormManager); } public void setCFFormManager(ICFFormManager value) { final String S_ProcName = "setCFFormManager"; if (value == null) { throw CFLib.getDefaultExceptionFactory().newNullArgumentException(getClass(), S_ProcName, 1, "value"); } cfFormManager = value; } public ICFBamJavaFXSchema getJavaFXSchema() { return (javafxSchema); } public void setJavaFXFocus(ICFLibAnyObj value) { final String S_ProcName = "setJavaFXFocus"; if ((value == null) || (value instanceof ICFBamClearDepObj)) { super.setJavaFXFocus(value); } else { throw CFLib.getDefaultExceptionFactory().newUnsupportedClassException(getClass(), S_ProcName, "value", value, "ICFBamClearDepObj"); } if (dataTable == null) { return; } } public ICFBamClearDepObj getJavaFXFocusAsClearDep() { return ((ICFBamClearDepObj) getJavaFXFocus()); } public void setJavaFXFocusAsClearDep(ICFBamClearDepObj value) { setJavaFXFocus(value); } public class ClearDepByQualNameComparator implements Comparator<ICFBamClearDepObj> { public ClearDepByQualNameComparator() { } public int compare(ICFBamClearDepObj lhs, ICFBamClearDepObj rhs) { if (lhs == null) { if (rhs == null) { return (0); } else { return (-1); } } else if (rhs == null) { return (1); } else { String lhsValue = lhs.getObjQualifiedName(); String rhsValue = rhs.getObjQualifiedName(); if (lhsValue == null) { if (rhsValue == null) { return (0); } else { return (-1); } } else if (rhsValue == null) { return (1); } else { return (lhsValue.compareTo(rhsValue)); } } } } protected ClearDepByQualNameComparator compareClearDepByQualName = new ClearDepByQualNameComparator(); public Collection<ICFBamClearDepObj> getJavaFXDataCollection() { return (javafxDataCollection); } public void setJavaFXDataCollection(Collection<ICFBamClearDepObj> value) { final String S_ProcName = "setJavaFXDataCollection"; javafxDataCollection = value; observableListOfClearDep = null; if (javafxDataCollection != null) { observableListOfClearDep = FXCollections.observableArrayList(javafxDataCollection); observableListOfClearDep.sort(compareClearDepByQualName); } else { observableListOfClearDep = FXCollections.observableArrayList(); } if (dataTable != null) { dataTable.setItems(observableListOfClearDep); // Hack from stackoverflow to fix JavaFX TableView refresh issue ((TableColumn) dataTable.getColumns().get(0)).setVisible(false); ((TableColumn) dataTable.getColumns().get(0)).setVisible(true); } } public ICFLibAnyObj getJavaFXContainer() { return (javafxContainer); } public void setJavaFXContainer(ICFLibAnyObj value) { javafxContainer = value; } }