Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

include a "#" column on candidate and CVR list #810

Merged
merged 2 commits into from
Mar 25, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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;
}
}
}
Loading