Skip to content

Commit 774da9f

Browse files
Siedlerchrtobiasdiez
authored andcommitted
Fix del/copy/paste key trigger main table action in search bar (#3070)
* Fix del/copy/paste key trigger main table action in search bar * Add comments what has to be done * checkstyle * Delete CHANGELOG.md.orig
1 parent ea29f56 commit 774da9f

File tree

3 files changed

+65
-51
lines changed

3 files changed

+65
-51
lines changed

CHANGELOG.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ If fetched article is already in database the ImportInspectionDialog is started
1919
- We fixed an issue where the fetcher for the Astrophysics Data System (ADS) added some non-bibtex data to the entry returned from the search [#3035](https://github.com/JabRef/jabref/issues/3035)
2020
- We fixed an issue where assigning an entry via drag and drop to a group caused JabRef to stop/freeze completely [#3036](https://github.com/JabRef/jabref/issues/3036)
2121
- We fixed an issue where the preferences could not be imported without a restart of JabRef [#3064](https://github.com/JabRef/jabref/issues/3064)
22-
22+
- We fixed an issue where <kbd>DEL</kbd>, <kbd>Ctrl</kbd>+<kbd>C</kbd>, <kbd>Ctrl</kbd>+<kbd>V</kbd> and <kbd>Ctrl</kbd>+<kbd>A</kbd> in the search field triggered corresponding actions in the main table [#3067](https://github.com/JabRef/jabref/issues/3067)
2323
### Removed
2424

2525

src/main/java/org/jabref/gui/entryeditor/EntryEditor.java

Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
import java.awt.Insets;
99
import java.awt.RenderingHints;
1010
import java.awt.event.ActionEvent;
11-
import java.awt.event.KeyListener;
11+
import java.awt.event.KeyAdapter;
1212
import java.awt.event.MouseAdapter;
1313
import java.awt.event.MouseEvent;
1414
import java.time.LocalDateTime;
@@ -157,18 +157,7 @@ public EntryEditor(JabRefFrame frame, BasePanel panel, BibEntry entry, String la
157157

158158
JFXPanel container = new JFXPanel();
159159

160-
container.addKeyListener(new KeyListener() {
161-
162-
@Override
163-
public void keyTyped(java.awt.event.KeyEvent e) {
164-
//empty
165-
}
166-
167-
@Override
168-
public void keyReleased(java.awt.event.KeyEvent e) {
169-
// empty
170-
171-
}
160+
container.addKeyListener(new KeyAdapter() {
172161

173162
@Override
174163
public void keyPressed(java.awt.event.KeyEvent e) {

src/main/java/org/jabref/gui/search/GlobalSearchBar.java

Lines changed: 62 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,11 @@
55
import java.awt.FlowLayout;
66
import java.awt.Font;
77
import java.awt.event.ActionEvent;
8+
import java.awt.event.KeyAdapter;
89
import java.io.File;
910
import java.util.List;
1011
import java.util.Objects;
12+
import java.util.Optional;
1113
import java.util.stream.Collectors;
1214

1315
import javax.swing.AbstractAction;
@@ -61,7 +63,6 @@ public class GlobalSearchBar extends JPanel {
6163
private static final PseudoClass CLASS_NO_RESULTS = PseudoClass.getPseudoClass("emptyResult");
6264
private static final PseudoClass CLASS_RESULTS_FOUND = PseudoClass.getPseudoClass("emptyResult");
6365

64-
6566
private final JabRefFrame frame;
6667

6768
private final TextField searchField = SearchTextField.create();
@@ -70,7 +71,7 @@ public class GlobalSearchBar extends JPanel {
7071
private final JButton searchModeButton = new JButton();
7172
private final JLabel currentResults = new JLabel("");
7273
private final SearchQueryHighlightObservable searchQueryHighlightObservable = new SearchQueryHighlightObservable();
73-
private JButton openCurrentResultsInDialog = new JButton(IconTheme.JabRefIcon.OPEN_IN_NEW_WINDOW.getSmallIcon());
74+
private final JButton openCurrentResultsInDialog = new JButton(IconTheme.JabRefIcon.OPEN_IN_NEW_WINDOW.getSmallIcon());
7475
private SearchWorker searchWorker;
7576
private GlobalSearchWorker globalSearchWorker;
7677

@@ -83,7 +84,6 @@ public class GlobalSearchBar extends JPanel {
8384
*/
8485
private boolean dontSelectSearchBar;
8586

86-
8787
public GlobalSearchBar(JabRefFrame frame) {
8888
super();
8989
this.frame = Objects.requireNonNull(frame);
@@ -99,6 +99,7 @@ public GlobalSearchBar(JabRefFrame frame) {
9999

100100
// default action to be performed for toggling globalSearch
101101
AbstractAction globalSearchStandardAction = new AbstractAction() {
102+
102103
@Override
103104
public void actionPerformed(ActionEvent e) {
104105
searchPreferences.setGlobalSearch(globalSearch.isSelected());
@@ -108,13 +109,43 @@ public void actionPerformed(ActionEvent e) {
108109

109110
// additional action for global search shortcut
110111
AbstractAction globalSearchShortCutAction = new AbstractAction() {
112+
111113
@Override
112114
public void actionPerformed(ActionEvent e) {
113115
globalSearch.setSelected(true);
114116
globalSearchStandardAction.actionPerformed(new ActionEvent(this, 0, "fire standard action"));
115117
focus();
116118
}
117119
};
120+
//TODO: These have to be somehow converted
121+
/*
122+
String endSearch = "endSearch";
123+
searchField.getInputMap().put(Globals.getKeyPrefs().getKey(KeyBinding.CLEAR_SEARCH), endSearch);
124+
searchField.getActionMap().put(endSearch, new AbstractAction() {
125+
@Override
126+
public void actionPerformed(ActionEvent event) {
127+
if (autoCompleteSupport.isVisible()) {
128+
autoCompleteSupport.setVisible(false);
129+
} else {
130+
endSearch();
131+
}
132+
}
133+
});
134+
*/
135+
136+
/*
137+
String acceptSearch = "acceptSearch";
138+
searchField.getInputMap().put(Globals.getKeyPrefs().getKey(KeyBinding.ACCEPT), acceptSearch);
139+
searchField.getActionMap().put(acceptSearch, new AbstractAction() {
140+
@Override
141+
public void actionPerformed(ActionEvent e) {
142+
autoCompleteSupport.setVisible(false);
143+
BasePanel currentBasePanel = frame.getCurrentBasePanel();
144+
Globals.getFocusListener().setFocused(currentBasePanel.getMainTable());
145+
currentBasePanel.getMainTable().requestFocus();
146+
}
147+
});
148+
*/
118149

119150
String searchGlobalByKey = "searchGlobalByKey";
120151
globalSearch.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(Globals.getKeyPrefs().getKey(KeyBinding.GLOBAL_SEARCH), searchGlobalByKey);
@@ -154,38 +185,31 @@ public void actionPerformed(ActionEvent e) {
154185

155186
EasyBind.subscribe(searchField.textProperty(), searchText -> performSearch());
156187

157-
/*
158-
String endSearch = "endSearch";
159-
searchField.getInputMap().put(Globals.getKeyPrefs().getKey(KeyBinding.CLEAR_SEARCH), endSearch);
160-
searchField.getActionMap().put(endSearch, new AbstractAction() {
161-
@Override
162-
public void actionPerformed(ActionEvent event) {
163-
if (autoCompleteSupport.isVisible()) {
164-
autoCompleteSupport.setVisible(false);
165-
} else {
166-
endSearch();
167-
}
168-
}
169-
});
170-
*/
171-
172-
/*
173-
String acceptSearch = "acceptSearch";
174-
searchField.getInputMap().put(Globals.getKeyPrefs().getKey(KeyBinding.ACCEPT), acceptSearch);
175-
searchField.getActionMap().put(acceptSearch, new AbstractAction() {
176-
@Override
177-
public void actionPerformed(ActionEvent e) {
178-
autoCompleteSupport.setVisible(false);
179-
BasePanel currentBasePanel = frame.getCurrentBasePanel();
180-
Globals.getFocusListener().setFocused(currentBasePanel.getMainTable());
181-
currentBasePanel.getMainTable().requestFocus();
182-
}
183-
});
184-
*/
185-
186188
JFXPanel container = new JFXPanel();
187189
DefaultTaskExecutor.runInJavaFXThread(() -> {
188190
container.setScene(new Scene(searchField));
191+
container.addKeyListener(new KeyAdapter() {
192+
193+
@Override
194+
public void keyPressed(java.awt.event.KeyEvent e) {
195+
//We need to consume this event here to prevent the propgation of keybinding events back to the JFrame
196+
Optional<KeyBinding> keyBinding = Globals.getKeyPrefs().mapToKeyBinding(e);
197+
if (keyBinding.isPresent()) {
198+
switch (keyBinding.get()) {
199+
case CUT:
200+
case COPY:
201+
case PASTE:
202+
case DELETE_ENTRY:
203+
case SELECT_ALL:
204+
e.consume();
205+
break;
206+
default:
207+
//do nothing
208+
}
209+
}
210+
}
211+
});
212+
189213
});
190214

191215
setLayout(new FlowLayout(FlowLayout.RIGHT));
@@ -207,7 +231,7 @@ public void actionPerformed(ActionEvent e) {
207231

208232
public void performGlobalSearch() {
209233
BasePanel currentBasePanel = frame.getCurrentBasePanel();
210-
if (currentBasePanel == null || validateSearchResultFrame(true)) {
234+
if ((currentBasePanel == null) || validateSearchResultFrame(true)) {
211235
return;
212236
}
213237

@@ -226,7 +250,7 @@ public void performGlobalSearch() {
226250

227251
private void openLocalFindingsInExternalPanel() {
228252
BasePanel currentBasePanel = frame.getCurrentBasePanel();
229-
if (currentBasePanel == null || validateSearchResultFrame(false)) {
253+
if ((currentBasePanel == null) || validateSearchResultFrame(false)) {
230254
return;
231255
}
232256

@@ -237,7 +261,7 @@ private void openLocalFindingsInExternalPanel() {
237261

238262
SearchResultFrame searchDialog = new SearchResultFrame(currentBasePanel.frame(),
239263
Localization.lang("Search results in library %0 for %1", currentBasePanel.getBibDatabaseContext()
240-
.getDatabaseFile().map(File::getName).orElse(GUIGlobals.UNTITLED_TITLE),
264+
.getDatabaseFile().map(File::getName).orElse(GUIGlobals.UNTITLED_TITLE),
241265
this.getSearchQuery().localize()),
242266
getSearchQuery(), false);
243267
List<BibEntry> entries = currentBasePanel.getDatabase().getEntries().stream()
@@ -250,7 +274,7 @@ private void openLocalFindingsInExternalPanel() {
250274

251275
private boolean validateSearchResultFrame(boolean globalSearch) {
252276
if (searchResultFrame != null) {
253-
if (searchResultFrame.isGlobalSearch() == globalSearch && isStillValidQuery(searchResultFrame.getSearchQuery())) {
277+
if ((searchResultFrame.isGlobalSearch() == globalSearch) && isStillValidQuery(searchResultFrame.getSearchQuery())) {
254278
searchResultFrame.focus();
255279
return true;
256280
} else {
@@ -358,6 +382,7 @@ public void setAutoCompleter(AutoCompleteSuggestionProvider<Author> searchComple
358382
searchCompleter,
359383
new PersonNameStringConverter(true, true, AutoCompleteFirstNameMode.BOTH),
360384
new AppendPersonNamesStrategy());
385+
361386
}
362387

363388
public SearchQueryHighlightObservable getSearchQueryHighlightObservable() {
@@ -384,7 +409,7 @@ public void updateResults(int matched, String description, boolean grammarBasedS
384409
currentResults.setText(Localization.lang("Found %0 results.", String.valueOf(matched)));
385410
searchField.pseudoClassStateChanged(CLASS_RESULTS_FOUND, true);
386411
}
387-
searchField.setTooltip(new Tooltip(description));
412+
DefaultTaskExecutor.runInJavaFXThread(() -> searchField.setTooltip(new Tooltip(description)));
388413
openCurrentResultsInDialog.setEnabled(true);
389414
}
390415

0 commit comments

Comments
 (0)