Skip to content

Commit

Permalink
include a "#" column on candidate and CVR list (#810)
Browse files Browse the repository at this point in the history
* include a "#" column on candidate and CVR list

* add comment

---------

Co-authored-by: Armin Samii <[email protected]>
  • Loading branch information
artoonie and artoonie authored Mar 25, 2024
1 parent a7bc987 commit 2ac2291
Showing 1 changed file with 46 additions and 0 deletions.
46 changes: 46 additions & 0 deletions src/main/java/network/brightspots/rcv/GuiConfigController.java
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
import java.util.Set;
import java.util.stream.Collectors;
import javafx.application.Platform;
import javafx.beans.NamedArg;
import javafx.beans.property.SimpleStringProperty;
import javafx.concurrent.Service;
import javafx.concurrent.Task;
Expand All @@ -66,6 +67,7 @@
import javafx.scene.control.SelectionMode;
import javafx.scene.control.Tab;
import javafx.scene.control.TabPane;
import javafx.scene.control.TableCell;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.control.TextArea;
Expand All @@ -80,6 +82,7 @@
import javafx.stage.DirectoryChooser;
import javafx.stage.FileChooser;
import javafx.stage.FileChooser.ExtensionFilter;
import javafx.util.Callback;
import javafx.util.Pair;
import javafx.util.StringConverter;
import network.brightspots.rcv.ContestConfig.Provider;
Expand Down Expand Up @@ -1200,6 +1203,7 @@ public LocalDate fromString(String string) {
.setCellValueFactory(new PropertyValueFactory<>("treatBlankAsUndeclaredWriteIn"));
tableViewCvrFiles.getSelectionModel().setSelectionMode(SelectionMode.MULTIPLE);
tableViewCvrFiles.setEditable(true);
tableViewCvrFiles.getColumns().add(0, NumberTableCellFactory.createNumberColumn("#", 1));

EditableColumn[] candidateStringColumnsAndProperties = new EditableColumn[]{
new EditableColumnString(tableColumnCandidateName, "name"),
Expand All @@ -1223,6 +1227,7 @@ public LocalDate fromString(String string) {

tableViewCandidates.getSelectionModel().setSelectionMode(SelectionMode.MULTIPLE);
tableViewCandidates.setEditable(true);
tableViewCandidates.getColumns().add(0, NumberTableCellFactory.createNumberColumn("#", 1));

// Let's also catch programming errors -- all of these properties must have corresponding
// functions in order to have the PropertyValueFactory work correctly.
Expand Down Expand Up @@ -1783,4 +1788,45 @@ public void setCellFactory() {
column.setCellFactory(tc -> new EditableTableCellPopup<Candidate>());
}
}

/**
* Adapted from https://stackoverflow.com/a/41282740/1057105
*/
private static class NumberTableCellFactory<S, T>
implements Callback<TableColumn<S, T>, TableCell<S, T>> {
private final int startNumber;

public NumberTableCellFactory(@NamedArg("startNumber") int startNumber) {
this.startNumber = startNumber;
}

public static class NumberTableCell<S, T> extends TableCell<S, T> {
private final int startNumber;

public NumberTableCell(int startNumber) {
this.startNumber = startNumber;
}

@Override
public void updateItem(T item, boolean empty) {
super.updateItem(item, empty);

setText(empty ? "" : Integer.toString(startNumber + getIndex()));
}
}

@Override
public TableCell<S, T> call(TableColumn<S, T> param) {
return new NumberTableCell<>(startNumber);
}

public static <T> TableColumn<T, Void> createNumberColumn(String text, int startNumber) {
TableColumn<T, Void> column = new TableColumn<>(text);
column.setSortable(false);
column.setEditable(false);
column.setPrefWidth(25);
column.setCellFactory(new NumberTableCellFactory<>(startNumber));
return column;
}
}
}

0 comments on commit 2ac2291

Please sign in to comment.