Skip to content

Instantly share code, notes, and snippets.

@y4nnick
Created August 21, 2016 14:23
Show Gist options
  • Save y4nnick/ca976e58be23aab20dfbc8d81ea46816 to your computer and use it in GitHub Desktop.
Save y4nnick/ca976e58be23aab20dfbc8d81ea46816 to your computer and use it in GitHub Desktop.
Extension for the JavaFX TableView. Automated storage of the Columns order and width.
package at.beachcrew.union.service;
import at.beachcrew.union.exceptions.EinstellungNotFoundException;
import at.beachcrew.union.exceptions.EinstellungPersistenceException;
import at.beachcrew.union.model.Einstellung;
import java.util.List;
/**
* Service für {@link at.beachcrew.union.model.Einstellung}
*/
public interface EinstellungService {
/**
* Speichert eine neue Einstellung
* @param einstellung die zu speichernde Eisntellung
* @return die gespeicherte Einstellung mit gesetzter ID
* @throws EinstellungPersistenceException falls die Einstellung nicht gespeichert werden kann
*/
Einstellung save(Einstellung einstellung) throws EinstellungPersistenceException;
/**
* Liefert die Einstellung zur gegebenen Bezeichnung
* @param bezeichnung die Bezeichnung nach welcher gesucht werden soll
* @return die gefundene Einstellung
* @throws EinstellungNotFoundException falls zur Bezeichnung keine Einstellung gefunden werden konnte
*/
Einstellung getByBezeichnung(String bezeichnung) throws EinstellungNotFoundException;
}
package at.beachcrew.union.gui.customControls;
import at.beachcrew.union.exceptions.EinstellungNotFoundException;
import at.beachcrew.union.exceptions.EinstellungPersistenceException;
import at.beachcrew.union.model.Einstellung;
import at.beachcrew.union.service.EinstellungService;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.collections.ListChangeListener;
import javafx.collections.ObservableList;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
public class PropertySaveTableView<T> extends TableView {
private static final Logger LOGGER = LoggerFactory.getLogger(PropertySaveTableView.class);
List<TableColumn<T, ?>> unchangedColumns;
private EinstellungService propertyService;
private String tableName;
/**
* Initialises the table with the given tableName
* @param tableName the table Name
* @param einstellungService the propertyService for storing the properties
*/
public void init(String tableName, EinstellungService einstellungService){
//Table name and propertyStore service
this.tableName = tableName;
this.propertyService = einstellungService;
//
// Column order
//
unchangedColumns = Collections.unmodifiableList(new ArrayList<TableColumn<T, ?>>(getColumns()));
getColumns().addListener(new ListChangeListener<TableColumn<T, ?>>() {
@Override
public void onChanged(ListChangeListener.Change<? extends TableColumn<T, ?>> change) {
while (change.next()) {
if (change.wasRemoved()) {
ObservableList<TableColumn<T, ?>> columns = getColumns();
String order = "";
for (int i = 0; i < columns.size(); ++i) {
order+= unchangedColumns.indexOf(columns.get(i)) + ((i==columns.size()-1)?"":",");
}
storeProperty("COLUMN_ORDER",order);
}
}
}
});
restoreOrder();
//
// Column width
//
for (final TableColumn<T, ?> column: unchangedColumns) {
column.widthProperty().addListener(new ChangeListener<Number>() {
@Override
public void changed(ObservableValue<? extends Number> observableValue, Number oldWidth, Number newWidth) {
String columnLabel = column.getText().toUpperCase();
storeProperty("COLUMN_WIDTH_"+columnLabel,newWidth+"");
// LOGGER.info(columnLabel + ": Width: " + oldWidth + " -> " + newWidth);
}
});
}
restoreWidth();
}
/**
* Restores the order of the columns or leave it, if no propterty is saved yet.
*/
private void restoreOrder(){
List<TableColumn<T, ?>> currentColumns = Collections.unmodifiableList(new ArrayList<TableColumn<T, ?>>(getColumns()));
List<TableColumn<T, ?>> orderedColumns = new ArrayList<TableColumn<T, ?>>();
Einstellung property = getProperty("COLUMN_ORDER");
if(property != null){
String[] values = property.getStringValue().split(",");
for(int i = 0 ; i < values.length; i++){
Integer index = Integer.valueOf(values[i]);
orderedColumns.add(currentColumns.get(index));
}
getColumns().setAll(orderedColumns);
}
}
/**
* Restores the width of the columns or leave it, if no property is saved yet
*/
private void restoreWidth(){
List<TableColumn<T, ?>> currentColumns = Collections.unmodifiableList(new ArrayList<TableColumn<T, ?>>(getColumns()));
for(int i = 0; i < currentColumns.size(); i++){
TableColumn<T,?> column = currentColumns.get(i);
String label = "COLUMN_WIDTH_"+column.getText().toUpperCase();
Einstellung property = getProperty(label);
if (property != null) {
Double width = Double.valueOf(property.getStringValue());
column.setPrefWidth(width);
}
}
}
/**
* Stores the property value under the given label
* @param label the property label
* @param value the String value
*/
private void storeProperty(String label,String value){
String propertyLabel = tableName + "_" + label;
Einstellung property;
try{
property = propertyService.getByBezeichnung(propertyLabel);
}catch (EinstellungNotFoundException ex){
//Property doesn't exist --> create it
property = new Einstellung();
property.setBezeichnung(propertyLabel);
}
property.setStringValue(value);
try{
propertyService.save(property);
}catch (EinstellungPersistenceException pex){
LOGGER.error(propertyLabel + " could not be saved: " + pex.getMessage());
}
}
/**
* Gets the property with the given label
* @param label the label of the searched property
* @return the property or null if not found
*/
private Einstellung getProperty(String label){
try{
return propertyService.getByBezeichnung(tableName + "_" + label);
}catch (EinstellungNotFoundException ex){
return null;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment