Skip to content

Commit

Permalink
Added gray background text to authors field to assist newcomers (JabR…
Browse files Browse the repository at this point in the history
…ef#2147)

* Added gray background text to assist newcomers

* Display placeholder even when focused

* Change placeholder string and include editor field

* Update changelog

* Fix localization

* Refactor localization
  • Loading branch information
boceckts authored and zesaro committed Oct 27, 2016
1 parent 328407b commit f01b064
Show file tree
Hide file tree
Showing 26 changed files with 237 additions and 76 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ We refer to [GitHub issues](https://github.com/JabRef/jabref/issues) by using `#
- Change "Recent files" to "Recent databases" to keep the file menu consistent
- Customized importer files need to be slightly changed since the class `ImportFormat` was renamed to `Importer`
- [koppor#5](https://github.com/koppor/jabref/issues/5) When entries are found while dropping a pdf with xmp meta data the found entries will be displayed in the import dialog
- [koppor#61](https://github.com/koppor/jabref/issues/61) Display gray background text in "Author" and "Editor" field to assist newcomers

### Fixed
- Fixed [#2089](https://github.com/JabRef/jabref/issues/2089): Fixed faulty cite key generation
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,9 @@
import net.sf.jabref.gui.keyboard.KeyBinding;
import net.sf.jabref.gui.util.GUIUtil;
import net.sf.jabref.logic.autocompleter.AutoCompleter;
import net.sf.jabref.logic.l10n.Localization;
import net.sf.jabref.model.entry.BibEntry;
import net.sf.jabref.model.entry.FieldName;
import net.sf.jabref.model.entry.FieldProperty;
import net.sf.jabref.model.entry.InternalBibtexFields;

Expand Down Expand Up @@ -151,7 +153,12 @@ private void setupPanel(JabRefFrame frame, BasePanel bPanel, boolean addKeyField
false);
defaultHeight = 0;
} else {
fieldEditor = new TextArea(field, null);
String prompt = "";
if (field.equals(FieldName.AUTHOR) || field.equals(FieldName.EDITOR)) {
prompt = String.format("%1$s and %1$s and others", Localization.lang("Firstname Lastname"));
}

fieldEditor = new TextArea(field, null, prompt);
bPanel.frame().getGlobalSearchBar().getSearchQueryHighlightObservable().addSearchListener((TextArea) fieldEditor);
defaultHeight = fieldEditor.getPane().getPreferredSize().height;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
import java.util.regex.Pattern;

import javax.swing.AbstractAction;
import javax.swing.JTextArea;
import javax.swing.KeyStroke;
import javax.swing.text.BadLocationException;
import javax.swing.text.DefaultHighlighter;
Expand All @@ -20,13 +19,14 @@
import net.sf.jabref.Globals;
import net.sf.jabref.gui.actions.Actions;
import net.sf.jabref.gui.actions.PasteAction;
import net.sf.jabref.gui.util.component.JTextAreaWithPlaceholder;
import net.sf.jabref.logic.search.SearchQueryHighlightListener;
import net.sf.jabref.preferences.JabRefPreferences;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

public class JTextAreaWithHighlighting extends JTextArea implements SearchQueryHighlightListener {
public class JTextAreaWithHighlighting extends JTextAreaWithPlaceholder implements SearchQueryHighlightListener {

private static final Log LOGGER = LogFactory.getLog(JTextAreaWithHighlighting.class);

Expand All @@ -35,13 +35,22 @@ public class JTextAreaWithHighlighting extends JTextArea implements SearchQueryH
private UndoManager undo;

public JTextAreaWithHighlighting() {
super();
setupUndoRedo();
setupPasteListener();
this("");
}

public JTextAreaWithHighlighting(String text) {
this(text, "");
}

JTextAreaWithHighlighting(String text) {
super(text);
/**
* Creates a text area with the ability to highlight parts of the content.
* It also defines a placeholder which will be displayed the content is empty.
*
* @param text
* @param placeholder
*/
public JTextAreaWithHighlighting(String text, String placeholder) {
super(text, placeholder);
setupUndoRedo();
setupPasteListener();
}
Expand Down
6 changes: 5 additions & 1 deletion src/main/java/net/sf/jabref/gui/fieldeditors/TextArea.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,11 @@ public class TextArea extends JTextAreaWithHighlighting implements FieldEditor {


public TextArea(String fieldName, String content) {
super(content);
this(fieldName, content, "");
}

public TextArea(String fieldName, String content, String title) {
super(content, title);


updateFont();
Expand Down
10 changes: 7 additions & 3 deletions src/main/java/net/sf/jabref/gui/fieldeditors/TextField.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
import javax.swing.AbstractAction;
import javax.swing.JComponent;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;
import javax.swing.text.Document;
import javax.swing.undo.CannotRedoException;
Expand All @@ -20,6 +19,7 @@
import net.sf.jabref.gui.actions.PasteAction;
import net.sf.jabref.gui.autocompleter.AutoCompleteListener;
import net.sf.jabref.gui.fieldeditors.contextmenu.FieldTextMenu;
import net.sf.jabref.gui.util.component.JTextFieldWithPlaceholder;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
Expand All @@ -28,7 +28,7 @@
* An implementation of the FieldEditor backed by a JTextField. Used for single-line input, only BibTex key at the
* moment?!
*/
public class TextField extends JTextField implements FieldEditor {
public class TextField extends JTextFieldWithPlaceholder implements FieldEditor {

private static final Log LOGGER = LogFactory.getLog(TextField.class);

Expand All @@ -39,7 +39,11 @@ public class TextField extends JTextField implements FieldEditor {


public TextField(String fieldName, String content, boolean changeColorOnFocus) {
super(content);
this(fieldName, content, changeColorOnFocus, "");
}

public TextField(String fieldName, String content, boolean changeColorOnFocus, String title) {
super(content, title);

setupPasteListener();
setupUndoRedo();
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/net/sf/jabref/gui/search/GlobalSearchBar.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
import net.sf.jabref.gui.keyboard.KeyBinding;
import net.sf.jabref.gui.maintable.MainTable;
import net.sf.jabref.gui.maintable.MainTableDataModel;
import net.sf.jabref.gui.util.component.JTextFieldWithUnfocusedText;
import net.sf.jabref.gui.util.component.JTextFieldWithPlaceholder;
import net.sf.jabref.logic.autocompleter.AutoCompleter;
import net.sf.jabref.logic.help.HelpFile;
import net.sf.jabref.logic.l10n.Localization;
Expand All @@ -49,7 +49,7 @@ public class GlobalSearchBar extends JPanel {
private final JabRefFrame frame;

private final JLabel searchIcon = new JLabel(IconTheme.JabRefIcon.SEARCH.getSmallIcon());
private final JTextFieldWithUnfocusedText searchField = new JTextFieldWithUnfocusedText(Localization.lang("Search") + "...");
private final JTextFieldWithPlaceholder searchField = new JTextFieldWithPlaceholder(Localization.lang("Search") + "...");
private JButton openCurrentResultsInDialog = new JButton(IconTheme.JabRefIcon.OPEN_IN_NEW_WINDOW.getSmallIcon());

private final JToggleButton caseSensitive;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
package net.sf.jabref.gui.util.component;

import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;

import javax.swing.JTextArea;
import javax.swing.UIManager;

/**
* A text area which displays a predefined text the same way as {@link JTextFieldWithPlaceholder} does.
*/
public class JTextAreaWithPlaceholder extends JTextArea implements KeyListener {

private final String textWhenNotFocused;

public JTextAreaWithPlaceholder() {
this("");
}

/**
* Additionally to {@link JTextAreaWithPlaceholder#JTextAreaWithPlaceholder(String)}
* this also sets the initial text of the text field component.
*
* @param content as the text of the textfield
* @param placeholder as the placeholder of the textfield
*/
public JTextAreaWithPlaceholder(String content, String placeholder) {
this(placeholder);
setText(content);
}

/**
* This will create a {@link JTextArea} with a placeholder text. The placeholder
* will always be displayed if the text of the {@link JTextArea} is empty.
*
* @param placeholder as the placeholder of the textarea
*/
public JTextAreaWithPlaceholder(String placeholder) {
super();
this.setEditable(true);
this.setText("");
this.textWhenNotFocused = placeholder;
}

@Override
protected void paintComponent(Graphics graphics) {
super.paintComponent(graphics);

if (this.getText().isEmpty()) {
Font prev = graphics.getFont();
Color prevColor = graphics.getColor();
graphics.setColor(UIManager.getColor("textInactiveText"));
int textHeight = graphics.getFontMetrics().getHeight();
int x = this.getInsets().left;
Graphics2D g2d = (Graphics2D) graphics;
RenderingHints hints = g2d.getRenderingHints();
g2d.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
g2d.drawString(textWhenNotFocused, x, textHeight + this.getInsets().top);
g2d.setRenderingHints(hints);
graphics.setFont(prev);
graphics.setColor(prevColor);
}
}

@Override
public void keyTyped(KeyEvent e) {
if (this.getText().isEmpty()) {
this.repaint();
}
}

@Override
public void keyPressed(KeyEvent e) {
if (this.getText().isEmpty()) {
this.repaint();
}
}

@Override
public void keyReleased(KeyEvent e) {
if (this.getText().isEmpty()) {
this.repaint();
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
package net.sf.jabref.gui.util.component;

import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;

import javax.swing.JTextField;
import javax.swing.UIManager;

/**
* A text field which displays a predefined text (e.g. "Search") if the text field is empty.
* This is similar to a html5 input element with a defined placeholder attribute.
* Implementation based on https://gmigdos.wordpress.com/2010/03/30/java-a-custom-jtextfield-for-searching/
*/
public class JTextFieldWithPlaceholder extends JTextField implements KeyListener {

private final String textWhenNotFocused;

/**
* Additionally to {@link JTextFieldWithPlaceholder#JTextFieldWithPlaceholder(String)}
* this also sets the initial text of the text field component.
*
* @param content as the text of the textfield
* @param placeholder as the placeholder of the textfield
*/
public JTextFieldWithPlaceholder(String content, String placeholder) {
this(placeholder);
setText(content);
}

/**
* This will create a {@link JTextField} with a placeholder text. The placeholder
* will always be displayed if the text of the {@link JTextField} is empty.
*
* @param placeholder as the placeholder of the textfield
*/
public JTextFieldWithPlaceholder(String placeholder) {
super();
this.setEditable(true);
this.setText("");
this.textWhenNotFocused = placeholder;
}

@Override
protected void paintComponent(Graphics graphics) {
super.paintComponent(graphics);

if (this.getText().isEmpty()) {
int height = this.getHeight();
Font prev = graphics.getFont();
Color prevColor = graphics.getColor();
graphics.setColor(UIManager.getColor("textInactiveText"));
int textHeight = graphics.getFontMetrics().getHeight();
int textBottom = (((height - textHeight) / 2) + textHeight) - 4;
int x = this.getInsets().left;
Graphics2D g2d = (Graphics2D) graphics;
RenderingHints hints = g2d.getRenderingHints();
g2d.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
g2d.drawString(textWhenNotFocused, x, textBottom);
g2d.setRenderingHints(hints);
graphics.setFont(prev);
graphics.setColor(prevColor);
}
}

@Override
public void keyTyped(KeyEvent e) {
if (this.getText().isEmpty()) {
this.repaint();
}
}

@Override
public void keyPressed(KeyEvent e) {
if (this.getText().isEmpty()) {
this.repaint();
}
}

@Override
public void keyReleased(KeyEvent e) {
if (this.getText().isEmpty()) {
this.repaint();
}
}

}
Loading

0 comments on commit f01b064

Please sign in to comment.