Skip to content

Commit

Permalink
Fixes settings menu, search text field is colored depending whether e…
Browse files Browse the repository at this point in the history
…ntries are found or not. White search bar.
  • Loading branch information
simonharrer committed Nov 17, 2015
1 parent a2b0588 commit e24a5a4
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 22 deletions.
8 changes: 4 additions & 4 deletions src/main/java/net/sf/jabref/SearchManagerNoGUI.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import java.util.Collection;
import java.util.Vector;

import net.sf.jabref.logic.search.SearchQuery;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

Expand Down Expand Up @@ -50,19 +51,18 @@ public BibtexDatabase getDBfromMatches() {
searchTerm = fieldYear();
}

SearchRule searchRule = SearchRules.getSearchRuleByQuery(searchTerm,
Globals.prefs.getBoolean(JabRefPreferences.SEARCH_CASE_SENSITIVE),
SearchQuery searchQuery = new SearchQuery(searchTerm, Globals.prefs.getBoolean(JabRefPreferences.SEARCH_CASE_SENSITIVE),
Globals.prefs.getBoolean(JabRefPreferences.SEARCH_REG_EXP));

if (!searchRule.validateSearchStrings(searchTerm)) {
if (!searchQuery.isValidQuery()) {
LOGGER.warn(Localization.lang("Search failed: illegal search expression"));
return base;
}

Collection<BibtexEntry> entries = database.getEntries();
Vector<BibtexEntry> matchEntries = new Vector<>();
for (BibtexEntry entry : entries) {
boolean hit = searchRule.applyRule(searchTerm, entry);
boolean hit = searchQuery.isMatch(entry);
entry.setSearchHit(hit);
if (hit) {
matchEntries.add(entry);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,9 @@
import net.sf.jabref.gui.JabRefFrame;
import net.sf.jabref.gui.worker.AbstractWorker;
import net.sf.jabref.logic.l10n.Localization;
import net.sf.jabref.logic.search.SearchQuery;
import net.sf.jabref.model.entry.BibtexEntry;

import java.util.LinkedList;
import java.util.List;
import java.util.Objects;

class GlobalSearchWorker extends AbstractWorker {
Expand All @@ -34,8 +33,7 @@ public void run() {
for (int i = 0; i < frame.getTabbedPane().getTabCount(); i++) {
BasePanel basePanel = frame.baseAt(i);
for (BibtexEntry entry : basePanel.getDatabase().getEntries()) {
boolean hit = searchQuery.rule.applyRule(searchQuery.query, entry);
if (hit) {
if (searchQuery.isMatch(entry)) {
dialog.addEntry(entry, basePanel);
}
}
Expand Down
31 changes: 21 additions & 10 deletions src/main/java/net/sf/jabref/gui/search/SearchBar.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
import net.sf.jabref.gui.IconTheme;
import net.sf.jabref.gui.autocompleter.AutoCompleteSupport;
import net.sf.jabref.gui.help.HelpAction;
import net.sf.jabref.logic.search.SearchQuery;
import net.sf.jabref.logic.search.rules.util.SentenceAnalyzer;

import net.sf.jabref.model.entry.BibtexEntry;
Expand All @@ -49,8 +50,10 @@ private SearchQuery getSearchQuery() {
public void updateResults(int matched, String description) {
if(matched == 0) {
this.currentResults.setText(Localization.lang("No results found."));
this.searchField.setBackground(Color.RED);
} else {
this.currentResults.setText(Localization.lang("Found %0 results.", String.valueOf(matched)));
this.searchField.setBackground(Color.GREEN);
}
this.searchField.setToolTipText("<html>" + description + "</html>");
}
Expand Down Expand Up @@ -93,14 +96,12 @@ public SearchBar(BasePanel basePanel) {

caseSensitive = new JToggleButton(IconTheme.JabRefIcon.CASE_SENSITIVE.getSmallIcon(), Globals.prefs.getBoolean(JabRefPreferences.SEARCH_CASE_SENSITIVE));
caseSensitive.setToolTipText(Localization.lang("Case sensitive"));
caseSensitive.addActionListener(ae -> updatePrefs());
caseSensitive.addActionListener(ae -> performSearch());
caseSensitive.addChangeListener(c -> performSearch());
caseSensitive.addItemListener(ae -> performSearch());
caseSensitive.addItemListener(ae -> updatePrefs());
regularExp = new JToggleButton(IconTheme.JabRefIcon.REGEX.getSmallIcon(), Globals.prefs.getBoolean(JabRefPreferences.SEARCH_REG_EXP));
regularExp.setToolTipText(Localization.lang("Use regular expressions"));
regularExp.addActionListener(ae -> updatePrefs());
regularExp.addActionListener(ae -> performSearch());
regularExp.addChangeListener(c -> performSearch());
regularExp.addItemListener(ae -> performSearch());
regularExp.addItemListener(ae -> updatePrefs());

openCurrentResultsInDialog = new JButton(IconTheme.JabRefIcon.OPEN_IN_NEW_WINDOW.getSmallIcon());
openCurrentResultsInDialog.setToolTipText(Localization.lang("Show search results in a window"));
Expand All @@ -118,30 +119,39 @@ public SearchBar(BasePanel basePanel) {

// Init controls
setLayout(new FlowLayout(FlowLayout.LEFT));
this.setBackground(Color.WHITE);
JLabel searchIcon = new JLabel(IconTheme.JabRefIcon.SEARCH.getSmallIcon());
searchIcon.setBackground(Color.WHITE);
this.add(searchIcon);
initSearchField();
this.add(searchField);
JButton button = new JButton(Localization.lang("Settings"));
button.setBackground(Color.WHITE);
JPopupMenu settingsMenu = createSettingsMenu();
button.addActionListener(l -> {
settingsMenu.setVisible(true);
settingsMenu.show(button, 0, button.getHeight());
});
this.add(button);

JToolBar toolBar = new JToolBar();
toolBar.setBackground(Color.WHITE);
toolBar.setFloatable(false);
toolBar.add(regularExp);
toolBar.add(caseSensitive);
toolBar.addSeparator();
toolBar.add(button);
toolBar.addSeparator();
toolBar.add(openCurrentResultsInDialog);
openCurrentResultsInDialog.setBackground(Color.WHITE);
JButton globalSearch = new JButton(Localization.lang("Search in all open databases"));
globalSearch.setBackground(Color.WHITE);
globalSearch.addActionListener(l -> {
AbstractWorker worker = new GlobalSearchWorker(basePanel.frame(), getSearchQuery());
worker.run();
worker.update();
});
toolBar.add(globalSearch);
toolBar.addSeparator();
toolBar.add(new HelpAction(basePanel.frame().helpDiag, GUIGlobals.searchHelp, Localization.lang("Help")));
toolBar.add(new HelpAction(basePanel.frame().helpDiag, GUIGlobals.searchHelp, Localization.lang("Help"))).setBackground(Color.WHITE);
this.add(toolBar);
this.add(currentResults);
}
Expand Down Expand Up @@ -379,6 +389,7 @@ private void clearSearch() {
worker.restart();

searchField.setText("");
searchField.setBackground(Color.WHITE);

fireSearchlistenerEvent(null);

Expand Down Expand Up @@ -407,7 +418,7 @@ private void performSearch() {

SearchQuery searchQuery = getSearchQuery();

if (!searchQuery.rule.validateSearchStrings(searchText)) {
if (!searchQuery.isValidQuery()) {
basePanel.output(Localization.lang("Search failed: illegal search expression"));
clearSearch();
return;
Expand Down
3 changes: 2 additions & 1 deletion src/main/java/net/sf/jabref/gui/search/SearchWorker.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import net.sf.jabref.gui.BasePanel;
import net.sf.jabref.gui.worker.AbstractWorker;
import net.sf.jabref.logic.search.SearchQuery;
import net.sf.jabref.logic.search.matchers.SearchMatcher;
import net.sf.jabref.model.entry.BibtexEntry;
import org.apache.commons.logging.LogFactory;
Expand Down Expand Up @@ -64,7 +65,7 @@ private void runNormal() {
// Search the current database
for (BibtexEntry entry : basePanel.getDatabase().getEntries()) {

boolean hit = searchQuery.rule.applyRule(searchQuery.query, entry);
boolean hit = searchQuery.isMatch(entry);
entry.setSearchHit(hit);
if (hit) {
hits++;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,20 +1,21 @@
package net.sf.jabref.gui.search;
package net.sf.jabref.logic.search;

import net.sf.jabref.logic.l10n.Localization;
import net.sf.jabref.logic.search.SearchRule;
import net.sf.jabref.logic.search.SearchRules;
import net.sf.jabref.logic.search.describer.SearchDescriber;
import net.sf.jabref.logic.search.describer.SearchDescribers;
import net.sf.jabref.model.entry.BibtexEntry;

public class SearchQuery {

public final String query;
public final boolean caseSensitive;
public final boolean regularExpression;
public final SearchRule rule;
private final SearchRule rule;
public final String description;

SearchQuery(String query, boolean caseSensitive, boolean regularExpression) {
public SearchQuery(String query, boolean caseSensitive, boolean regularExpression) {
this.query = query;
this.caseSensitive = caseSensitive;
this.regularExpression = regularExpression;
Expand All @@ -27,6 +28,14 @@ public String toString() {
return String.format("\"%s\" (%s, %s)", query, getCaseSensitiveDescription(), getRegularExpressionDescription());
}

public boolean isMatch(BibtexEntry entry) {
return this.rule.applyRule(query, entry);
}

public boolean isValidQuery() {
return this.rule.validateSearchStrings(query);
}

private SearchRule getSearchRule() {
return SearchRules.getSearchRuleByQuery(query, caseSensitive, regularExpression);
}
Expand Down

0 comments on commit e24a5a4

Please sign in to comment.