Skip to content
Open
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
35 changes: 24 additions & 11 deletions src/main/java/edu/mills/cs180a/wordui/FXMLController.java
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ public class FXMLController implements Initializable {
private final ObservableList<WordRecord> wordRecordList = FXCollections.observableArrayList();

private WordRecord selectedWordRecord;
private final BooleanProperty wordExistsProperty = new SimpleBooleanProperty(false);
private final BooleanProperty modifiedProperty = new SimpleBooleanProperty(false);
private final BooleanProperty freqValidProperty = new SimpleBooleanProperty(false);
private ChangeListener<WordRecord> wordRecordChangeListener = new WordRecordChangeListener();
Expand All @@ -62,6 +63,7 @@ public void changed(ObservableValue<? extends WordRecord> observable, WordRecord
if (newValue != null) {
wordTextField.setText(selectedWordRecord.getWord());
frequencyTextField.setText(Integer.toString(selectedWordRecord.getFrequency()));
wordExistsProperty.set(isNewWord(newValue.getWord()));
freqValidProperty.set(isValidFrequency(frequencyTextField.textProperty()));
definitionTextArea.setText(selectedWordRecord.getDefinition());
} else {
Expand Down Expand Up @@ -107,8 +109,7 @@ public void initialize(URL url, ResourceBundle rb) {


private void populateChoiceBox() {
sortChoiceBox.setItems(FXCollections.observableArrayList(
WordRecord.SortOrder.values()));
sortChoiceBox.setItems(FXCollections.observableArrayList(WordRecord.SortOrder.values()));
sortChoiceBox.setValue(WordRecord.SortOrder.ALPHABETICALLY_FORWARD);
}

Expand All @@ -121,18 +122,32 @@ private void configureButtons() {
// been made, or any field is empty or invalid.
updateButton.disableProperty()
.bind(listView.getSelectionModel().selectedItemProperty().isNull()
.or(modifiedProperty.not())
.or(freqValidProperty.not())
.or(modifiedProperty.not()).or(freqValidProperty.not())
.or(wordTextField.textProperty().isEmpty())
.or(definitionTextArea.textProperty().isEmpty()));

// TODO: Disable the Create button if an existing entry is selected or any
// Disable the Create button if an existing entry is selected or any
// field is empty or invalid.
createButton.disableProperty()
.bind(listView.getSelectionModel().selectedItemProperty().isNull()
.or(wordExistsProperty.not()).or(modifiedProperty.not())

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK, but I'd put one .or clause per line.

.or(freqValidProperty.not()).or(wordTextField.textProperty().isEmpty())
.or(definitionTextArea.textProperty().isEmpty()));
}

// Does the word already exist in our list

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add question mark.

private boolean isNewWord(String w) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good.

for (WordRecord wr : wordRecordList) {
if (wr.getWord().equalsIgnoreCase(w.trim())) {
return false;
}
}
return true;
}


// A frequency is valid if it is an integer and is at least 0.
private boolean isValidFrequency(StringProperty sp) {
System.out.println("isLegalFrequency(" + sp.get() + ")");
try {
int value = Integer.parseInt(sp.get());
return value >= 0;
Expand All @@ -151,16 +166,14 @@ private void addListeners() {
private void handleKeyAction(KeyEvent keyEvent) {
modifiedProperty.set(true);
freqValidProperty.set(isValidFrequency(frequencyTextField.textProperty()));
wordExistsProperty.set(isNewWord(wordTextField.getText()));
}

@FXML
private void createButtonAction(ActionEvent actionEvent) {
System.out.println("Create");
WordRecord wordRecord =
new WordRecord(
wordTextField.getText(),
Integer.parseInt(frequencyTextField.getText()),
definitionTextArea.getText());
WordRecord wordRecord = new WordRecord(wordTextField.getText(),
Integer.parseInt(frequencyTextField.getText()), definitionTextArea.getText());
wordRecordList.add(wordRecord);
listView.getSelectionModel().select(wordRecord); // select the new item
}
Expand Down