Skip to content

Commit

Permalink
Some code refactoring/cleanup and styling of the ability score table
Browse files Browse the repository at this point in the history
  • Loading branch information
Lichtblitz committed Nov 5, 2023
1 parent 4db9747 commit 9a8c028
Show file tree
Hide file tree
Showing 11 changed files with 156 additions and 136 deletions.
24 changes: 5 additions & 19 deletions code/src/java/pcgen/gui2/tabs/SummaryInfoTab.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,8 @@
import java.awt.Font;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.GridLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.io.IOException;
import java.net.URL;
import java.text.NumberFormat;
import java.util.Arrays;
import java.util.Collection;
Expand Down Expand Up @@ -107,12 +104,12 @@
import pcgen.gui3.tabs.summary.AbilityScores;
import pcgen.gui3.tabs.summary.AbilityScoresModel;
import pcgen.gui3.utilty.ColorUtilty;
import pcgen.gui3.utilty.JavaFXLoader;
import pcgen.system.LanguageBundle;
import pcgen.util.enumeration.Tab;

import javafx.application.Platform;
import javafx.embed.swing.JFXPanel;
import javafx.fxml.FXMLLoader;
import javafx.scene.Scene;

/**
Expand Down Expand Up @@ -296,26 +293,15 @@ private void initTodoPanel(JPanel panel)

private void initMiddlePanel(JFXPanel middlePanel)
{
URL resource = AbilityScores.class.getResource(AbilityScores.class.getSimpleName() + ".fxml");
FXMLLoader loader = new FXMLLoader(resource, LanguageBundle.getBundle());
JavaFXLoader.Components<Scene, AbilityScores> components = JavaFXLoader.load(AbilityScores.class);

Scene scene = null;
try
{
scene = loader.load();
abilityScores = loader.getController();
}
catch (IOException e)
{
throw new RuntimeException(e);
}

middlePanel.setScene(scene);
abilityScores = components.controller();
middlePanel.setScene(components.fxml());

StatTableModel.initializeTable(statsTable);


middlePanel.setLayout(new GridLayout(2, 1));
//middlePanel.setLayout(new GridLayout(2, 1));

JPanel statsPanel = new JPanel();
setPanelTitle(statsPanel, LanguageBundle.getString("in_sumAbilityScores")); //$NON-NLS-1$
Expand Down
20 changes: 5 additions & 15 deletions code/src/java/pcgen/gui3/tabs/summary/AbilityScores.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@
import javafx.beans.property.ObjectProperty;
import javafx.beans.property.SimpleObjectProperty;
import javafx.beans.value.ObservableIntegerValue;
import javafx.beans.value.ObservableStringValue;
import javafx.beans.value.ObservableValue;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.fxml.FXML;
import javafx.scene.control.TableCell;
import javafx.scene.control.TableColumn;
import javafx.scene.control.cell.TextFieldTableCell;
Expand All @@ -23,17 +23,7 @@ public class AbilityScores
private final ObjectProperty<AbilityScoresModel> abilityScoresModelProperty = new SimpleObjectProperty<>();

private final ObservableList<AbilityScoresModel.StatValues> pcStatList =
FXCollections.observableArrayList();/*statValues-> new Observable[]{
statValues.getLabel(),
statValues.getTotal(),
statValues.getMod(),
statValues.getBase(),
statValues.getRaceBonus(),
statValues.getOtherBonus()
});*/

@FXML
public TableColumn<AbilityScoresModel.StatValues, Integer> editableColumn;
FXCollections.observableArrayList();

public AbilityScores()
{
Expand Down Expand Up @@ -62,7 +52,7 @@ public Callback<TableColumn.CellDataFeatures<AbilityScoresModel.StatValues, Stri
return cellDataFeatures -> cellDataFeatures.getValue().getTotal();
}

public Callback<TableColumn.CellDataFeatures<AbilityScoresModel.StatValues, Integer>, ObservableIntegerValue> getModFactory()
public Callback<TableColumn.CellDataFeatures<AbilityScoresModel.StatValues, String>, ObservableStringValue> getModFactory()
{
return cellDataFeatures -> cellDataFeatures.getValue().getMod();
}
Expand All @@ -82,12 +72,12 @@ public Callback<TableColumn<AbilityScoresModel.StatValues, Integer>, TableCell<A
return CustomTableCellFactory.forTableColumn(EditableStatCell.class);
}

public Callback<TableColumn.CellDataFeatures<AbilityScoresModel.StatValues, Integer>, ObservableIntegerValue> getRaceBonusFactory()
public Callback<TableColumn.CellDataFeatures<AbilityScoresModel.StatValues, String>, ObservableStringValue> getRaceBonusFactory()
{
return cellDataFeatures -> cellDataFeatures.getValue().getRaceBonus();
}

public Callback<TableColumn.CellDataFeatures<AbilityScoresModel.StatValues, Integer>, ObservableIntegerValue> getOtherBonusFactory()
public Callback<TableColumn.CellDataFeatures<AbilityScoresModel.StatValues, String>, ObservableStringValue> getOtherBonusFactory()
{
return cellDataFeatures -> cellDataFeatures.getValue().getOtherBonus();
}
Expand Down
42 changes: 27 additions & 15 deletions code/src/java/pcgen/gui3/tabs/summary/AbilityScoresModel.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import pcgen.facade.core.CharacterFacade;
import pcgen.facade.util.event.ReferenceEvent;
import pcgen.facade.util.event.ReferenceListener;
import pcgen.gui2.util.PrettyIntegerFormat;

import javafx.application.Platform;
import javafx.beans.property.IntegerProperty;
Expand All @@ -36,11 +37,11 @@ public static class StatValues
{
@Builder
private StatValues(@NonNull String label,
int mod,
@NonNull String mod,
int base,
int raceBonus,
@NonNull String raceBonus,
@NonNull String total,
int otherBonus,
@NonNull String otherBonus,
@NonNull ChangeListener<Number> changeListener)
{
this.label.set(label);
Expand All @@ -54,11 +55,11 @@ private StatValues(@NonNull String label,
}

StringProperty label = new SimpleStringProperty();
IntegerProperty mod = new SimpleIntegerProperty();
StringProperty mod = new SimpleStringProperty();
IntegerProperty base = new SimpleIntegerProperty();
IntegerProperty raceBonus = new SimpleIntegerProperty();
StringProperty raceBonus = new SimpleStringProperty();
StringProperty total = new SimpleStringProperty();
IntegerProperty otherBonus = new SimpleIntegerProperty();
StringProperty otherBonus = new SimpleStringProperty();

@EqualsAndHashCode.Include
private String getLabelValue()
Expand All @@ -67,7 +68,7 @@ private String getLabelValue()
}

@EqualsAndHashCode.Include
private int getModValue()
private String getModValue()
{
return mod.getValue();
}
Expand All @@ -79,7 +80,7 @@ private int getBaseValue()
}

@EqualsAndHashCode.Include
private int getRaceBonusValue()
private String getRaceBonusValue()
{
return raceBonus.getValue();
}
Expand All @@ -91,7 +92,7 @@ private String getTotalValue()
}

@EqualsAndHashCode.Include
private int getOtherBonusValue()
private String getOtherBonusValue()
{
return otherBonus.getValue();
}
Expand Down Expand Up @@ -162,11 +163,11 @@ private static StatValues createObjectProperty(PCStat stat, CharacterFacade char
{
return StatValues.builder()
.label(stat.getDisplayName())
.mod(character.getModTotal(stat))
.mod(formatModValue(character.getModTotal(stat)))
.base(character.getScoreBase(stat))
.raceBonus(character.getScoreRaceBonus(stat))
.raceBonus(formatModValue(character.getScoreRaceBonus(stat)))
.total(character.getScoreTotalString(stat))
.otherBonus(character.getScoreOtherBonus(stat))
.otherBonus(formatModValue(character.getScoreOtherBonus(stat)))
.changeListener((observable, oldValue, newValue) ->
character.setScoreBase(stat, newValue.intValue()))
.build();
Expand All @@ -176,10 +177,21 @@ private static StatValues createObjectProperty(PCStat stat, CharacterFacade char
private static void overrideObjectProperty(PCStat stat, CharacterFacade character, StatValues values)
{
values.getLabel().setValue(stat.getDisplayName());
values.getMod().setValue(character.getModTotal(stat));
values.getMod().setValue(formatModValue(character.getModTotal(stat)));
values.getBase().setValue(character.getScoreBase(stat));
values.getRaceBonus().setValue(character.getScoreRaceBonus(stat));
values.getRaceBonus().setValue(formatModValue(character.getScoreRaceBonus(stat)));
values.getTotal().setValue(character.getScoreTotalString(stat));
values.getOtherBonus().setValue(character.getScoreOtherBonus(stat));
values.getOtherBonus().setValue(formatModValue(character.getScoreOtherBonus(stat)));
}


private static String formatModValue(int value)
{
if (value == 0)
{
// let's use a pretty em dash instead of hyphen/minus.
return "\u2014";
}
return PrettyIntegerFormat.getFormat().format(value);
}
}
12 changes: 12 additions & 0 deletions code/src/java/pcgen/gui3/tabs/summary/EditableStatCell.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,11 @@
import lombok.Getter;
import pcgen.gui3.utilty.BoundController;

import javafx.beans.property.BooleanProperty;
import javafx.beans.property.Property;
import javafx.beans.property.ReadOnlyBooleanProperty;
import javafx.beans.property.ReadOnlyBooleanWrapper;
import javafx.beans.property.SimpleBooleanProperty;
import javafx.beans.property.SimpleObjectProperty;
import javafx.fxml.FXML;
import javafx.scene.control.Spinner;
Expand All @@ -14,6 +18,7 @@
public class EditableStatCell implements BoundController<Property<Integer>>
{
private final Property<Integer> externalProperty = new SimpleObjectProperty<>();
private final BooleanProperty isEditingProperty = new SimpleBooleanProperty(false);

@FXML
public Spinner<Integer> spinner;
Expand All @@ -23,6 +28,7 @@ public void initialize(){
if (oldValue && ! newValue){
externalProperty.setValue(spinner.getValueFactory().getValue());
}
isEditingProperty.set(newValue);
});

externalProperty.subscribe(value -> spinner.getValueFactory().setValue(value));
Expand All @@ -34,6 +40,12 @@ public void bind(Property<Integer> property)
externalProperty.bindBidirectional(property); // don't bind the spinner directly; only update when we lose focus
}

@Override
public ReadOnlyBooleanProperty isEditing()
{
return ReadOnlyBooleanWrapper.readOnlyBooleanProperty(isEditingProperty);
}

public void onKeyReleased(KeyEvent keyEvent)
{
if (KeyCode.ENTER.equals(keyEvent.getCode())){
Expand Down
3 changes: 3 additions & 0 deletions code/src/java/pcgen/gui3/utilty/BoundController.java
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
package pcgen.gui3.utilty;

import javafx.beans.property.Property;
import javafx.beans.property.ReadOnlyBooleanProperty;

public interface BoundController<T extends Property<?>>
{
void bind(T property);

ReadOnlyBooleanProperty isEditing();
}
35 changes: 17 additions & 18 deletions code/src/java/pcgen/gui3/utilty/CustomTableCellFactory.java
Original file line number Diff line number Diff line change
@@ -1,17 +1,13 @@
package pcgen.gui3.utilty;

import java.io.IOException;
import java.net.URL;
import java.util.Objects;
import lombok.AccessLevel;
import lombok.Builder;
import lombok.NonNull;
import pcgen.system.LanguageBundle;

import javafx.application.Platform;
import javafx.beans.property.Property;
import javafx.beans.property.SimpleObjectProperty;
import javafx.fxml.FXMLLoader;
import javafx.scene.Node;
import javafx.scene.control.TableCell;
import javafx.scene.control.TableColumn;
Expand All @@ -20,15 +16,15 @@
@Builder(access = AccessLevel.PROTECTED)
public class CustomTableCellFactory<S, T> extends TableCell<S, T>
{
private final Class<?> fxmlControllerClass;
private final Class<? extends BoundController<Property<T>>> fxmlControllerClass;
private final Property<T> valueProperty = new SimpleObjectProperty<>();
private Node graphic;
private boolean isExternalUpdate;

public static <S, T> Callback<TableColumn<S, T>, TableCell<S, T>>
forTableColumn(@NonNull Class<? extends BoundController<? extends Property<T>>> fxmlControllerClass)
forTableColumn(@NonNull Class<? extends BoundController<Property<T>>> fxmlControllerClass)
{
return list -> CustomTableCellFactory.<S, T>builder()
return column -> CustomTableCellFactory.<S, T>builder()
.fxmlControllerClass(fxmlControllerClass)
.build();
}
Expand All @@ -39,11 +35,9 @@ protected void initialize()
{
return;
}
try
{
URL resource = fxmlControllerClass.getResource(fxmlControllerClass.getSimpleName() + ".fxml");
FXMLLoader loader = new FXMLLoader(resource, LanguageBundle.getBundle());
graphic = loader.load();
JavaFXLoader.Components<Node, ? extends BoundController<Property<T>>> loaded = JavaFXLoader.load(fxmlControllerClass);
graphic = loaded.fxml();

valueProperty.addListener((observable, oldValue, newValue) -> {
if (isExternalUpdate || Objects.equals(oldValue, newValue))
{
Expand All @@ -55,12 +49,17 @@ protected void initialize()
requestFocus();
});
});
loader.<BoundController<Property<T>>>getController().bind(valueProperty);
}
catch (IOException e)
{
throw new RuntimeException(e);
}
loaded.controller().bind(valueProperty);
loaded.controller().isEditing().subscribe(isEditing -> {
if (isEditing)
{
getTableView().getSelectionModel().clearAndSelect(getTableRow().getIndex(), getTableColumn());
}
else
{
getTableView().getSelectionModel().clearSelection();
}
});
}

@Override
Expand Down
32 changes: 32 additions & 0 deletions code/src/java/pcgen/gui3/utilty/JavaFXLoader.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package pcgen.gui3.utilty;

import java.io.IOException;
import java.net.URL;
import lombok.NonNull;
import pcgen.system.LanguageBundle;

import javafx.fxml.FXMLLoader;

public class JavaFXLoader
{

public record Components<T, U>(T fxml, U controller)
{
}

public static <T, U> Components<T, U> load(@NonNull Class<U> controllerClass)
{
URL resource = controllerClass.getResource(controllerClass.getSimpleName() + ".fxml");

FXMLLoader loader = new FXMLLoader(resource, LanguageBundle.getBundle());

try
{
return new Components<>(loader.load(), loader.getController());
}
catch (IOException e)
{
throw new RuntimeException(e);
}
}
}
7 changes: 7 additions & 0 deletions code/src/resources/pcgen/gui3/tabs/summary/AbilityScores.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
.titled-pane {
-fx-alignment: center;
}

.table-column {
-fx-alignment: center;
}
Loading

0 comments on commit 9a8c028

Please sign in to comment.