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

Improved buttons #1

Merged
merged 3 commits into from
Nov 14, 2021
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
102 changes: 52 additions & 50 deletions src/main/java/org/jabref/gui/search/DropDownMenu.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ public class DropDownMenu {
public Button leftBracketButton;
public Button rightBracketButton;
public RecentSearch recentSearch;
private boolean isPrevAttribute;
// private final Button articleButton;
// private final Button bookButton;
// private final Button citationKeyButton;
Expand Down Expand Up @@ -60,82 +61,65 @@ public DropDownMenu(CustomTextField searchField, GlobalSearchBar globalSearchBar
Node buttonBox = mainBox;

searchField.addEventFilter(MouseEvent.MOUSE_CLICKED, event -> {
if (searchbarDropDown == null) {
if (searchbarDropDown == null || !searchbarDropDown.isShowing()) {
searchbarDropDown = new PopOver(buttonBox);
searchbarDropDown.setWidth(searchField.getWidth());
searchbarDropDown.setArrowLocation(PopOver.ArrowLocation.TOP_CENTER);
searchbarDropDown.setContentNode(buttonBox);
searchbarDropDown.setDetachable(false); // not detachable
searchbarDropDown.show(searchField);
} else if (searchbarDropDown.isShowing()) {
searchbarDropDown.hide();
// } else if (searchbarDropDown.isShowing()) {
// searchbarDropDown.hide(); // this makes the dropdown disappear if you re-click on searchbar
} else {
searchbarDropDown.setContentNode(buttonBox);
searchbarDropDown.show(searchField);
// searchbarDropDown.setContentNode(buttonBox);
// this makes the drop down reappear every time you click on search bar, even if its shown already
// searchbarDropDown.show(searchField);
}
});

// authorButton action
authorButton.setOnAction(event -> {
if (searchField.getCaretPosition() != 0) {
if (!searchField.getText(searchField.getCaretPosition() - 1, searchField.getCaretPosition()).equals(" ")) {
searchField.insertText(searchField.getCaretPosition(), " ");
searchField.positionCaret(searchField.getText().length());
}
authorButton.addEventFilter(MouseEvent.MOUSE_CLICKED, event -> {
if (!isPrevAttribute(searchField)) { // checks if the search term prior is an attribute and wont queue another if so
checkAndAddSpace(searchField); // checks if there is a space prior and if not adds it
searchField.insertText(searchField.getCaretPosition(), "author:");
searchField.positionCaret(searchField.getText().length());
}
searchField.insertText(searchField.getCaretPosition(), "author:");
searchField.positionCaret(searchField.getText().length());
});

// journalButton action
journalButton.setOnAction(event -> {
if (searchField.getCaretPosition() != 0) {
if (!searchField.getText(searchField.getCaretPosition() - 1, searchField.getCaretPosition()).equals(" ")) {
searchField.insertText(searchField.getCaretPosition(), " ");
searchField.positionCaret(searchField.getText().length());
}
journalButton.addEventFilter(MouseEvent.MOUSE_CLICKED, event ->{
if (!isPrevAttribute(searchField)) {
checkAndAddSpace(searchField);
searchField.insertText(searchField.getCaretPosition(), "journal:");
searchField.positionCaret(searchField.getText().length());
}
searchField.insertText(searchField.getCaretPosition(), "journal:");
searchField.positionCaret(searchField.getText().length());
});

// titleButton action
titleButton.setOnAction(event -> {
if (searchField.getCaretPosition() != 0) {
if (!searchField.getText(searchField.getCaretPosition() - 1, searchField.getCaretPosition()).equals(" ")) {
searchField.insertText(searchField.getCaretPosition(), " ");
searchField.positionCaret(searchField.getText().length());
}
titleButton.addEventFilter(MouseEvent.MOUSE_CLICKED, event ->{
if (!isPrevAttribute(searchField)) {
checkAndAddSpace(searchField);
searchField.insertText(searchField.getCaretPosition(), "title:");
searchField.positionCaret(searchField.getText().length());
}
searchField.insertText(searchField.getCaretPosition(), "title:");
searchField.positionCaret(searchField.getText().length());
});

// yearButton action
yearButton.setOnAction(event -> {
if (searchField.getCaretPosition() != 0) {
if (!searchField.getText(searchField.getCaretPosition() - 1, searchField.getCaretPosition()).equals(" ")) {
searchField.insertText(searchField.getCaretPosition(), " ");
searchField.positionCaret(searchField.getText().length());
}
yearButton.addEventFilter(MouseEvent.MOUSE_CLICKED, event ->{
if (!isPrevAttribute(searchField)) {
checkAndAddSpace(searchField);
searchField.insertText(searchField.getCaretPosition(), "year:");
searchField.positionCaret(searchField.getText().length());
}
searchField.insertText(searchField.getCaretPosition(), "year:");
searchField.positionCaret(searchField.getText().length());
});

// yearRangeButton action
yearRangeButton.setOnAction(event -> {
if (searchField.getCaretPosition() != 0) {
if (!searchField.getText(searchField.getCaretPosition() - 1, searchField.getCaretPosition()).equals(" ")) {
searchField.insertText(searchField.getCaretPosition(), " ");
searchField.positionCaret(searchField.getText().length());
}
}
searchField.insertText(searchField.getCaretPosition(), "year:");
searchField.positionCaret(searchField.getText().length());
yearRangeButton.addEventFilter(MouseEvent.MOUSE_CLICKED, event ->{

});

// andButton action
andButton.setOnAction(event -> {
andButton.addEventFilter(MouseEvent.MOUSE_CLICKED, event ->{
if (searchField.getCaretPosition() != 0) {
if (!searchField.getText(searchField.getCaretPosition() - 1, searchField.getCaretPosition()).equals(" ")) {
searchField.insertText(searchField.getCaretPosition(), " ");
Expand All @@ -147,7 +131,7 @@ public DropDownMenu(CustomTextField searchField, GlobalSearchBar globalSearchBar
});

// orButton action
orButton.setOnAction(event -> {
orButton.addEventFilter(MouseEvent.MOUSE_CLICKED, event ->{
if (searchField.getCaretPosition() != 0) {
if (!searchField.getText(searchField.getCaretPosition() - 1, searchField.getCaretPosition()).equals(" ")) {
searchField.insertText(searchField.getCaretPosition(), " ");
Expand All @@ -159,7 +143,7 @@ public DropDownMenu(CustomTextField searchField, GlobalSearchBar globalSearchBar
});

// leftBracketButton action
leftBracketButton.setOnAction(event -> {
leftBracketButton.addEventFilter(MouseEvent.MOUSE_CLICKED, event ->{
if (searchField.getCaretPosition() != 0) {
if (!searchField.getText(searchField.getCaretPosition() - 1, searchField.getCaretPosition()).equals(" ")) {
searchField.insertText(searchField.getCaretPosition(), " ");
Expand All @@ -171,7 +155,7 @@ public DropDownMenu(CustomTextField searchField, GlobalSearchBar globalSearchBar
});

// orButton action
rightBracketButton.setOnAction(event -> {
rightBracketButton.addEventFilter(MouseEvent.MOUSE_CLICKED, event ->{
if (searchField.getCaretPosition() != 0) {
if (!searchField.getText(searchField.getCaretPosition() - 1, searchField.getCaretPosition()).equals(" ")) {
searchField.insertText(searchField.getCaretPosition(), " ");
Expand All @@ -183,4 +167,22 @@ public DropDownMenu(CustomTextField searchField, GlobalSearchBar globalSearchBar
});
}

private void checkAndAddSpace(CustomTextField searchField) {
if (searchField.getCaretPosition() != 0) {
if (!searchField.getText(searchField.getCaretPosition() - 1, searchField.getCaretPosition()).equals(" ")) {
searchField.insertText(searchField.getCaretPosition(), " ");
searchField.positionCaret(searchField.getText().length());
}
}
}

private boolean isPrevAttribute(CustomTextField searchField) {
isPrevAttribute = false;
if (searchField.getCaretPosition() != 0) {
if (searchField.getText(searchField.getCaretPosition() - 1, searchField.getCaretPosition()).equals(":")) {
isPrevAttribute = true;
}
}
return isPrevAttribute;
}
}