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

Cleanup and format some files #166

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
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
641 changes: 319 additions & 322 deletions src/main/java/net/sf/jabref/JabRefPreferences.java

Large diffs are not rendered by default.

8 changes: 3 additions & 5 deletions src/main/java/net/sf/jabref/SearchManagerNoGUI.java
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,7 @@ public BibtexDatabase getDBfromMatches() {
searchTerm = fieldYear();
}

SearchRule searchRule = SearchRules.getSearchRuleByQuery(searchTerm,
Globals.prefs.getBoolean(JabRefPreferences.CASE_SENSITIVE_SEARCH),
Globals.prefs.getBoolean(JabRefPreferences.REG_EXP_SEARCH));
SearchRule searchRule = SearchRules.getSearchRuleByQuery(searchTerm, Globals.prefs.getBoolean(JabRefPreferences.CASE_SENSITIVE_SEARCH), Globals.prefs.getBoolean(JabRefPreferences.REG_EXP_SEARCH));

if (!searchRule.validateSearchStrings(searchTerm)) {
System.out.println(Localization.lang("Search failed: illegal search expression"));
Expand Down Expand Up @@ -88,7 +86,7 @@ private String fieldYear() {
int year1 = Integer.parseInt(years[0]);
int year2 = Integer.parseInt(years[1]);

if (year1 < 2000 && year2 >= 2000) { //for 199.
if ((year1 < 2000) && (year2 >= 2000)) { //for 199.
regPt1 = "199+[" + years[0].substring(3, 4) + "-9]";
reg1Set = true;
} else {
Expand All @@ -100,7 +98,7 @@ private String fieldYear() {
// @formatter:on
}
}
if (Integer.parseInt(years[1]) >= 2000 && year1 < 2000) { //for 200.
if ((Integer.parseInt(years[1]) >= 2000) && (year1 < 2000)) { //for 200.
regPt2 = "200+[0-" + years[1].substring(3, 4) + "]";
reg2Set = true;
} else {
Expand Down
96 changes: 41 additions & 55 deletions src/main/java/net/sf/jabref/gui/AutoCompleteListener.java
Original file line number Diff line number Diff line change
Expand Up @@ -59,9 +59,8 @@ public AutoCompleteListener(AutoCompleter completer) {
}

/**
* This method is used if the focus listener should call another focus listener
* after finishing. This is needed because the autocomplete listener must
* run before the focus listener responsible for storing the current edit.
* This method is used if the focus listener should call another focus listener after finishing. This is needed
* because the autocomplete listener must run before the focus listener responsible for storing the current edit.
*
* @param listener The listener to call.
*/
Expand All @@ -70,9 +69,10 @@ public void setNextFocusListener(FocusListener listener) {
}

/**
* This setting determines whether the autocomplete listener should consume the Enter key
* stroke when it leads to accepting a completion. If set to false, the JTextComponent will receive
* the Enter key press after the completion is done. The default value if true.
* This setting determines whether the autocomplete listener should consume the Enter key stroke when it leads to
* accepting a completion. If set to false, the JTextComponent will receive the Enter key press after the completion
* is done. The default value if true.
*
* @param t true to indicate that the Enter key should be consumed, false that it should be forwarded
*/
public void setConsumeEnterKey(boolean t) {
Expand All @@ -98,8 +98,7 @@ public void keyPressed(KeyEvent e) {
else if ((e.getKeyCode() == KeyEvent.VK_PAGE_DOWN) && (toSetIn != null)) {
cycle((JTextComponent) e.getSource(), 1);
e.consume();
}
else if ((e.getKeyCode() == KeyEvent.VK_PAGE_UP) && (toSetIn != null)) {
} else if ((e.getKeyCode() == KeyEvent.VK_PAGE_UP) && (toSetIn != null)) {
cycle((JTextComponent) e.getSource(), -1);
e.consume();
}
Expand All @@ -114,16 +113,16 @@ else if (e.getKeyChar() == KeyEvent.CHAR_UNDEFINED) {
// shift is OK, everyhting else leads to a reset
resetAutoCompletion();
} else {
LOGGER.debug("Special case: shift pressed. No action.");
AutoCompleteListener.LOGGER.debug("Special case: shift pressed. No action.");
}
} else {
LOGGER.debug("Special case: defined character, but not caught above");
AutoCompleteListener.LOGGER.debug("Special case: defined character, but not caught above");
}
}

private void cycle(JTextComponent comp, int increment) {
assert (lastCompletions != null);
assert (lastCompletions.length > 0);
assert(lastCompletions != null);
assert(lastCompletions.length > 0);
lastShownCompletion += increment;
if (lastShownCompletion >= lastCompletions.length) {
lastShownCompletion = 0;
Expand Down Expand Up @@ -157,37 +156,34 @@ private void cycle(JTextComponent comp, int increment) {
}

/**
* If user cancels autocompletion by
* a) entering another letter than the completed word (and there is no other auto completion)
* b) space
* the casing of the letters has to be kept
* If user cancels autocompletion by a) entering another letter than the completed word (and there is no other auto
* completion) b) space the casing of the letters has to be kept
*
* Global variable "lastBeginning" keeps track of typed letters.
* We rely on this variable to reconstruct the text
* Global variable "lastBeginning" keeps track of typed letters. We rely on this variable to reconstruct the text
*
* @param wordSeperatorTyped indicates whether the user has typed a white space character or a
*/
private void setUnmodifiedTypedLetters(JTextComponent comp, boolean lastBeginningContainsTypedCharacter, boolean wordSeperatorTyped) {
if (lastBeginning == null) {
LOGGER.debug("No last beginning found");
AutoCompleteListener.LOGGER.debug("No last beginning found");
// There was no previous input (if the user typed a word, where no autocompletion is available)
// Thus, there is nothing to replace
return;
}
LOGGER.debug("lastBeginning: >" + lastBeginning + '<');
AutoCompleteListener.LOGGER.debug("lastBeginning: >" + lastBeginning + '<');
if (comp.getSelectedText() == null) {
// if there is no selection
// the user has typed the complete word, but possibly with a different casing
// we need a replacement
if (wordSeperatorTyped) {
LOGGER.debug("Replacing complete word");
AutoCompleteListener.LOGGER.debug("Replacing complete word");
} else {
// if user did not press a white space character (space, ...),
// then we do not do anything
return;
}
} else {
LOGGER.debug("Selected text " + comp.getSelectedText() + " will be removed");
AutoCompleteListener.LOGGER.debug("Selected text " + comp.getSelectedText() + " will be removed");
// remove completion suggestion
comp.replaceSelection("");
}
Expand All @@ -201,9 +197,7 @@ private void setUnmodifiedTypedLetters(JTextComponent comp, boolean lastBeginnin
endIndex++;
}
String text = comp.getText();
comp.setText(text.substring(0, endIndex).
concat(lastBeginning).
concat(text.substring(lastCaretPosition)));
comp.setText(text.substring(0, endIndex).concat(lastBeginning).concat(text.substring(lastCaretPosition)));
if (lastBeginningContainsTypedCharacter) {
// the current letter is NOT contained in comp.getText()
// Thus, cursor position also did not get updated
Expand All @@ -214,18 +208,17 @@ private void setUnmodifiedTypedLetters(JTextComponent comp, boolean lastBeginnin
}

/**
* Start a new completion attempt
* (instead of treating a continuation of an existing word or an interrupt of the current word)
* Start a new completion attempt (instead of treating a continuation of an existing word or an interrupt of the
* current word)
*/
private void startCompletion(StringBuffer currentword, KeyEvent e) {
JTextComponent comp = (JTextComponent) e.getSource();

String[] completed = findCompletions(currentword.toString(), comp);
String prefix = completer.getPrefix();
String cWord = (prefix != null) && (!prefix.isEmpty()) ?
currentword.toString().substring(prefix.length()) : currentword.toString();
String cWord = (prefix != null) && (!prefix.isEmpty()) ? currentword.toString().substring(prefix.length()) : currentword.toString();

LOGGER.debug("StartCompletion currentword: >" + currentword + "'<' prefix: >" + prefix + "'<' cword: >" + cWord + '<');
AutoCompleteListener.LOGGER.debug("StartCompletion currentword: >" + currentword + "'<' prefix: >" + prefix + "'<' cword: >" + cWord + '<');

int no = 0; // We use the first word in the array of completions.
if ((completed != null) && (completed.length > 0)) {
Expand All @@ -239,7 +232,7 @@ private void startCompletion(StringBuffer currentword, KeyEvent e) {
// BUT we obey the completion
toSetIn = sno.substring(cWord.length() - 1);

LOGGER.debug("toSetIn: >" + toSetIn + '<');
AutoCompleteListener.LOGGER.debug("toSetIn: >" + toSetIn + '<');

StringBuilder alltext = new StringBuilder(comp.getText());
int cp = comp.getCaretPosition();
Expand All @@ -251,7 +244,7 @@ private void startCompletion(StringBuffer currentword, KeyEvent e) {
lastCaretPosition = comp.getCaretPosition();
char ch = e.getKeyChar();

LOGGER.debug("Appending >" + ch + '<');
AutoCompleteListener.LOGGER.debug("Appending >" + ch + '<');

if (cWord.length() <= 1) {
lastBeginning = Character.toString(ch);
Expand All @@ -264,7 +257,7 @@ private void startCompletion(StringBuffer currentword, KeyEvent e) {

@Override
public void keyTyped(KeyEvent e) {
LOGGER.debug("key typed event caught " + e.getKeyCode());
AutoCompleteListener.LOGGER.debug("key typed event caught " + e.getKeyCode());
char ch = e.getKeyChar();
if (ch == '\n') {
// this case is handled at keyPressed(e)
Expand All @@ -277,19 +270,18 @@ public void keyTyped(KeyEvent e) {
JTextComponent comp = (JTextComponent) e.getSource();

if (toSetIn == null) {
LOGGER.debug("toSetIn is null");
AutoCompleteListener.LOGGER.debug("toSetIn is null");
} else {
LOGGER.debug("toSetIn: >" + toSetIn + '<');
AutoCompleteListener.LOGGER.debug("toSetIn: >" + toSetIn + '<');
}

// The case-insensitive system is a bit tricky here
// If keyword is "TODO" and user types "tO", then this is treated as "continue" as the "O" matches the "O"
// If keyword is "TODO" and user types "To", then this is treated as "discont" as the "o" does NOT match the "O".

if ((toSetIn != null) && (toSetIn.length() > 1) &&
(ch == toSetIn.charAt(1))) {
if ((toSetIn != null) && (toSetIn.length() > 1) && (ch == toSetIn.charAt(1))) {
// User continues on the word that was suggested.
LOGGER.debug("cont");
AutoCompleteListener.LOGGER.debug("cont");

toSetIn = toSetIn.substring(1);
if (!toSetIn.isEmpty()) {
Expand Down Expand Up @@ -324,22 +316,19 @@ public void keyTyped(KeyEvent e) {
// "space" indicates that the user does NOT want the autocompletion,
// but the typed word
String text = comp.getText();
comp.setText(text.substring(0, lastCaretPosition - lastBeginning.length())
+ lastBeginning
+ text.substring(lastCaretPosition));
comp.setText(text.substring(0, lastCaretPosition - lastBeginning.length()) + lastBeginning + text.substring(lastCaretPosition));
// there is no selected text, therefore we are not updating the selection
toSetIn = null;
}
return;
}
}

if ((toSetIn != null) && ((toSetIn.length() <= 1) ||
(ch != toSetIn.charAt(1)))) {
if ((toSetIn != null) && ((toSetIn.length() <= 1) || (ch != toSetIn.charAt(1)))) {
// User discontinues the word that was suggested.
lastBeginning = lastBeginning + ch;

LOGGER.debug("discont toSetIn: >" + toSetIn + "'<' lastBeginning: >" + lastBeginning + '<');
AutoCompleteListener.LOGGER.debug("discont toSetIn: >" + toSetIn + "'<' lastBeginning: >" + lastBeginning + '<');

String[] completed = findCompletions(lastBeginning, comp);
if ((completed != null) && (completed.length > 0)) {
Expand All @@ -354,9 +343,7 @@ public void keyTyped(KeyEvent e) {
String text = comp.getText();
//Util.pr(""+lastLen);
//we do not use toSetIn as we want to obey the casing of "sno"
comp.setText(text.substring(0, (lastCaretPosition - lastLen - lastBeginning.length()) + 1)
+ sno
+ text.substring(lastCaretPosition));
comp.setText(text.substring(0, (lastCaretPosition - lastLen - lastBeginning.length()) + 1) + sno + text.substring(lastCaretPosition));
int startSelect = (lastCaretPosition + 1) - lastLen;
int endSelect = (lastCaretPosition + toSetIn.length()) - lastLen;
comp.select(startSelect, endSelect);
Expand All @@ -372,7 +359,7 @@ public void keyTyped(KeyEvent e) {
}
}

LOGGER.debug("case else");
AutoCompleteListener.LOGGER.debug("case else");

comp.replaceSelection("");

Expand All @@ -382,14 +369,14 @@ public void keyTyped(KeyEvent e) {
}

// only "real characters" end up here
assert (!Character.isISOControl(ch));
assert(!Character.isISOControl(ch));
currentword.append(ch);
startCompletion(currentword, e);
return;
} else {
if (Character.isWhitespace(ch)) {
assert (!completer.isSingleUnitField());
LOGGER.debug("whitespace && !singleUnitField");
assert(!completer.isSingleUnitField());
AutoCompleteListener.LOGGER.debug("whitespace && !singleUnitField");
// start a new search if end-of-field is reached

// replace displayed letters with typed letters
Expand All @@ -398,7 +385,7 @@ public void keyTyped(KeyEvent e) {
return;
}

LOGGER.debug("No letter/digit/whitespace or CHAR_UNDEFINED");
AutoCompleteListener.LOGGER.debug("No letter/digit/whitespace or CHAR_UNDEFINED");
// replace displayed letters with typed letters
setUnmodifiedTypedLetters((JTextComponent) e.getSource(), false, !Character.isISOControl(ch));
resetAutoCompletion();
Expand All @@ -412,7 +399,7 @@ public void keyTyped(KeyEvent e) {
* Resets the auto completion data in a way that no leftovers are there
*/
private void resetAutoCompletion() {
LOGGER.debug("Resetting autocompletion");
AutoCompleteListener.LOGGER.debug("Resetting autocompletion");
toSetIn = null;
lastBeginning = null;
}
Expand All @@ -431,8 +418,7 @@ private StringBuffer getCurrentWord(JTextComponent comp) {
// In most fields, we are only interested in the currently edited word, so we
// seek from the caret backward to the closest space:
if (!completer.isSingleUnitField()) {
if ((comp.getCaretPosition() < comp.getText().length())
&& Character.isWhitespace(comp.getText().charAt(comp.getCaretPosition()))) {
if ((comp.getCaretPosition() < comp.getText().length()) && Character.isWhitespace(comp.getText().charAt(comp.getCaretPosition()))) {
// caret is in the middle of the text AND current character is a whitespace
// that means: a new word is started and there is no current word
return null;
Expand Down
Loading