Skip to content

Commit

Permalink
Merge pull request #1 from josphstar/DropDownProto_Mark
Browse files Browse the repository at this point in the history
Improved buttons
  • Loading branch information
josphstar authored Nov 14, 2021
2 parents 41b4f38 + c6f8066 commit 98c958b
Showing 1 changed file with 52 additions and 50 deletions.
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;
}
}

0 comments on commit 98c958b

Please sign in to comment.