Skip to content

Commit

Permalink
refactor create group modal; clean up unused imports;
Browse files Browse the repository at this point in the history
  • Loading branch information
mattwright324 committed Feb 6, 2021
1 parent 8d65400 commit 46608f0
Show file tree
Hide file tree
Showing 20 changed files with 105 additions and 126 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
import org.apache.commons.lang3.StringUtils;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.apache.logging.log4j.util.Strings;

import java.io.Serializable;
import java.sql.ResultSet;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
import java.util.ArrayList;
import java.util.List;

import static io.mattw.youtube.commentsuite.db.SQLLoader.INSERT_IGNORE_COMMENTS;
import static io.mattw.youtube.commentsuite.db.SQLLoader.INSERT_IGNORE_MODERATED_COMMENTS;

public class ModeratedCommentsTable extends TableHelper<YouTubeComment> {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
package io.mattw.youtube.commentsuite.fxml;

import io.mattw.youtube.commentsuite.CommentSuite;
import io.mattw.youtube.commentsuite.db.CommentDatabase;
import io.mattw.youtube.commentsuite.db.Group;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.scene.control.Button;
Expand All @@ -10,6 +13,11 @@
import org.apache.logging.log4j.Logger;

import java.io.IOException;
import java.sql.SQLException;

import static javafx.application.Platform.runLater;
import static org.apache.commons.lang3.StringUtils.isBlank;
import static org.apache.commons.lang3.StringUtils.trimToEmpty;

/**
* This modal allows the user to create a new and empty Group with the name of their choosing. The name must be unique.
Expand All @@ -24,19 +32,61 @@ public class MGCreateGroupModal extends VBox implements Cleanable {
@FXML private TextField nameField;
@FXML private Button btnSubmit, btnClose;

private CommentDatabase database;

public MGCreateGroupModal() {
logger.debug("Initialize MGCreateGroupModal");

database = CommentSuite.getDatabase();

FXMLLoader loader = new FXMLLoader(getClass().getResource("MGCreateGroupModal.fxml"));
loader.setController(this);
loader.setRoot(this);
try {
loader.load();

errorMsg.managedProperty().bind(errorMsg.visibleProperty());

nameField.textProperty().addListener((ov, prevText, currText) ->
runLater(() -> btnSubmit.setDisable(isBlank(nameField.getText()))));

btnSubmit.setOnAction(ae -> tryCreate());
} catch (IOException e) {
logger.error(e);
}
}

public Label getErrorMsg() {
return errorMsg;
private void tryCreate() {
final String input = trimToEmpty(nameField.getText());

runLater(() -> nameField.setText(input));

if (isBlank(input)) {
runLater(() -> {
errorMsg.setVisible(true);
errorMsg.setText("Name cannot be blank");
});
return;
}

try {
final Group newGroup = database.groups().create(input);

logger.debug("Created new group [id={},name={}]", newGroup.getGroupId(), newGroup.getName());

runLater(() -> {
errorMsg.setText("");
errorMsg.setVisible(false);
btnClose.fire();
});
} catch (SQLException e) {
logger.error(e);

runLater(() -> {
errorMsg.setVisible(true);
errorMsg.setText("Name already exists, try another!");
});
}
}

public TextField getNameField() {
Expand All @@ -45,8 +95,9 @@ public TextField getNameField() {

@Override
public void cleanUp() {
getErrorMsg().setManaged(false);
getNameField().setText("");
errorMsg.setVisible(false);
nameField.setText("");
btnSubmit.setDisable(true);
}

public Button getBtnClose() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -94,9 +94,7 @@ public MGMVAddItemModal(final Group group) {
try {
database.groupItems().insertAll(this.group, list);
database.commit();
runLater(() -> {
btnClose.fire();
});
runLater(() -> btnClose.fire());
} catch (SQLException e1) {
runLater(() -> setError(e1.getClass().getSimpleName()));
}
Expand Down Expand Up @@ -139,9 +137,7 @@ public MGMVAddItemModal(final Group group) {
try {
database.groupItems().insertAll(this.group, list);
database.commit();
runLater(() -> {
btnClose.fire();
});
runLater(() -> btnClose.fire());
} catch (SQLException e1) {
failures++;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,9 +67,7 @@ public MGMVRemoveAllModal(final Group group, final ObservableList<MGMVGroupItemV

database.groupItems().deleteAssociations(group, items);
database.cleanUp();
runLater(() -> {
btnClose.fire();
});
runLater(() -> btnClose.fire());
} catch (SQLException e) {
runLater(() -> setError(e.getLocalizedMessage()));
logger.error(e);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,9 +67,7 @@ public MGMVRemoveSelectedModal(final Group group, final MultipleSelectionModel<M

database.groupItems().deleteAssociations(group, items);
database.cleanUp();
runLater(() -> {
btnClose.fire();
});
runLater(() -> btnClose.fire());
} catch (SQLException e) {
runLater(() -> setError(e.getLocalizedMessage()));
logger.error(e);
Expand Down
4 changes: 1 addition & 3 deletions src/main/java/io/mattw/youtube/commentsuite/fxml/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -61,9 +61,7 @@ public void initialize(URL location, ResourceBundle resources) {
});

quotaIcon.setImage(ImageLoader.QUOTA.getImage());
btnQuota.setOnAction(ae -> runLater(() -> {
logger.debug("Open Quota info");
}));
btnQuota.setOnAction(ae -> runLater(() -> logger.debug("Open Quota info")));

settingsIcon.setImage(ImageLoader.SETTINGS.getImage());
btnSettings.setOnAction(ae -> runLater(() -> {
Expand Down
47 changes: 10 additions & 37 deletions src/main/java/io/mattw/youtube/commentsuite/fxml/ManageGroups.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,12 @@

import java.io.IOException;
import java.net.URL;
import java.sql.SQLException;
import java.util.ResourceBundle;

import static javafx.application.Platform.runLater;

/**
* Manages group selection, creation, and content switching.
*
*/
public class ManageGroups implements Initializable {

Expand All @@ -42,12 +40,17 @@ public class ManageGroups implements Initializable {

private CommentDatabase database;

@FXML private OverlayModal<MGCreateGroupModal> overlayModal;
@FXML
private OverlayModal<MGCreateGroupModal> overlayModal;

@FXML private ImageView plusIcon;
@FXML private ComboBox<Group> comboGroupSelect;
@FXML private Button btnCreateGroup;
@FXML private Pane content;
@FXML
private ImageView plusIcon;
@FXML
private ComboBox<Group> comboGroupSelect;
@FXML
private Button btnCreateGroup;
@FXML
private Pane content;

public void initialize(URL location, ResourceBundle resources) {
logger.debug("Initialize ManageGroups");
Expand Down Expand Up @@ -108,36 +111,6 @@ public void initialize(URL location, ResourceBundle resources) {
modal.getBtnClose().setOnAction(ae -> runLater(() ->
overlayModal.setVisible(false))
);
modal.getBtnSubmit().setOnAction(ae -> new Thread(() -> {
logger.debug("Attempting to create group");
runLater(() -> overlayModal.setDisable(true));
String name = modal.getNameField().getText();
if (!name.isEmpty()) {
try {
Group g = database.groups().create(name);
logger.debug("Created new group [id={},name={}]", g.getGroupId(), g.getName());
runLater(() -> {
comboGroupSelect.getSelectionModel().select(g);
overlayModal.setDisable(false);
overlayModal.setVisible(false);
modal.getErrorMsg().setManaged(false);
});
} catch (SQLException e) {
logger.error(e);
runLater(() -> {
overlayModal.setDisable(false);
modal.getErrorMsg().setManaged(true);
modal.getErrorMsg().setText("Name already exists, try another!");
});
}
} else {
runLater(() -> {
overlayModal.setDisable(false);
modal.getErrorMsg().setManaged(true);
modal.getErrorMsg().setText("Name must not be empty.");
});
}
}).start());
}

public static Cache<String, ManageGroupsManager> getManagerCache() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@
import javafx.scene.image.ImageView;
import javafx.scene.input.KeyCode;
import javafx.scene.layout.GridPane;
import javafx.scene.layout.Priority;
import javafx.scene.layout.Region;
import javafx.scene.layout.StackPane;
import javafx.scene.text.Font;
Expand Down Expand Up @@ -166,12 +165,7 @@ public ManageGroupsManager(final Group group) throws IOException {
groupTitle.setMinWidth(Region.USE_PREF_SIZE);
groupTitle.setMaxWidth(Region.USE_PREF_SIZE);
groupTitle.textProperty().addListener((ov, prevText, currText) -> {
runLater(() -> {
int caretPosition = groupTitle.getCaretPosition();
groupTitle.setText(trimToEmpty(currText));
groupTitle.positionCaret(caretPosition);
rename.setDisable(isBlank(groupTitle.getText()));
});
runLater(() -> rename.setDisable(isBlank(groupTitle.getText())));

FXUtils.adjustTextFieldWidthByContent(groupTitle);
});
Expand All @@ -195,9 +189,11 @@ public ManageGroupsManager(final Group group) throws IOException {
rename.setOnAction(ae -> new Thread(() -> {
if (editIcon.getImage().equals(save)) {
try {
database.groups().rename(group, groupTitle.getText());
final String newTitle = trimToEmpty(groupTitle.getText());
database.groups().rename(group, newTitle);
runLater(() -> groupTitle.setText(newTitle));
} catch (SQLException e) {
groupTitle.setText(group.getName());
runLater(() -> groupTitle.setText(group.getName()));
logger.error(e);
}
}
Expand All @@ -224,22 +220,20 @@ public ManageGroupsManager(final Group group) throws IOException {
});
}).start());

renameCancel.setOnAction(ae -> new Thread(() -> {
runLater(() -> {
editIcon.setImage(edit);
groupTitle.getStyleClass().add("clearTextField");
groupTitle.setEditable(false);
rename.setTooltip(new Tooltip("Rename"));
renameCancel.setOnAction(ae -> new Thread(() -> runLater(() -> {
editIcon.setImage(edit);
groupTitle.getStyleClass().add("clearTextField");
groupTitle.setEditable(false);
rename.setTooltip(new Tooltip("Rename"));

renameCancel.setVisible(false);
renameCancel.setManaged(false);
renameCancel.setDisable(true);
renameCancel.setVisible(false);
renameCancel.setManaged(false);
renameCancel.setDisable(true);

groupTitle.setText(group.getName());
groupTitle.setText(group.getName());

FXUtils.adjustTextFieldWidthByContent(groupTitle);
});
}).start());
FXUtils.adjustTextFieldWidthByContent(groupTitle);
})).start());
renameCancel.setTooltip(new Tooltip("Cancel"));

SelectionModel selectionModel = groupItemList.getSelectionModel();
Expand Down Expand Up @@ -306,9 +300,7 @@ public ManageGroupsManager(final Group group) throws IOException {
MGMVDeleteGroupModal mgmvDelete = new MGMVDeleteGroupModal();
deleteModal.setContent(mgmvDelete);
deleteModal.setDividerClass("dividerDanger");
btnDelete.setOnAction(ae -> runLater(() -> {
deleteModal.setVisible(true);
}));
btnDelete.setOnAction(ae -> runLater(() -> deleteModal.setVisible(true)));
deleteModal.visibleProperty().addListener((cl) -> {
mgmvDelete.getBtnClose().setCancelButton(deleteModal.isVisible());
mgmvDelete.getBtnDelete().setDefaultButton(deleteModal.isVisible());
Expand Down Expand Up @@ -337,9 +329,7 @@ public ManageGroupsManager(final Group group) throws IOException {
btnReload.fire();
});
}).start());
mgmvDelete.getBtnClose().setOnAction(ae -> {
deleteModal.setVisible(false);
});
mgmvDelete.getBtnClose().setOnAction(ae -> deleteModal.setVisible(false));

/*
Add Item Modal
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
import java.io.*;
import java.nio.charset.StandardCharsets;
import java.sql.SQLException;
import java.time.format.DateTimeFormatter;
import java.util.concurrent.atomic.AtomicLong;

import static javafx.application.Platform.runLater;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import com.google.common.eventbus.Subscribe;
import io.mattw.youtube.commentsuite.CommentSuite;
import io.mattw.youtube.commentsuite.db.CommentDatabase;
import io.mattw.youtube.commentsuite.db.SQLLoader;
import io.mattw.youtube.commentsuite.db.YouTubeComment;
import io.mattw.youtube.commentsuite.events.TagsChangeEvent;
import javafx.fxml.FXML;
Expand Down Expand Up @@ -98,9 +97,7 @@ private void refreshAllTags() {
public void withComments(List<SearchCommentsListItem> selected) {
this.selected = selected;

runLater(() -> {
lblAbout.setText(String.format("%s comments(s) selected", selected.size()));
});
runLater(() -> lblAbout.setText(String.format("%s comments(s) selected", selected.size())));
}

public List<YouTubeComment> toComments() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import com.google.common.eventbus.Subscribe;
import io.mattw.youtube.commentsuite.CommentSuite;
import io.mattw.youtube.commentsuite.db.CommentDatabase;
import io.mattw.youtube.commentsuite.db.YouTubeComment;
import io.mattw.youtube.commentsuite.events.TagsChangeEvent;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
Expand All @@ -15,8 +14,6 @@
import java.io.IOException;
import java.sql.SQLException;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;

import static javafx.application.Platform.runLater;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@

import java.io.IOException;
import java.sql.SQLException;
import java.util.Collections;

import static javafx.application.Platform.runLater;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -132,9 +132,7 @@ private void submitItemsToGroup(final List<SearchYouTubeListItem> items, final G

logger.debug("GroupItems were successfully added to group");

runLater(() -> {
btnClose.fire();
});
runLater(() -> btnClose.fire());
} catch (SQLException e) {
logger.error("Failed to insert group items to group [id={}]", group.getGroupId());

Expand Down
Loading

0 comments on commit 46608f0

Please sign in to comment.