Skip to content

Commit

Permalink
Display placeholder even when focused
Browse files Browse the repository at this point in the history
  • Loading branch information
boceckts committed Oct 11, 2016
1 parent 789bf72 commit 6f35ff1
Show file tree
Hide file tree
Showing 7 changed files with 197 additions and 144 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,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 JTextAreaWithUnfocusedText implements SearchQueryHighlightListener {
public class JTextAreaWithHighlighting extends JTextAreaWithPlaceholder implements SearchQueryHighlightListener {

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

Expand All @@ -34,15 +35,22 @@ public class JTextAreaWithHighlighting extends JTextAreaWithUnfocusedText implem
private UndoManager undo;

public JTextAreaWithHighlighting() {
this(null);
this("");
}

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

public JTextAreaWithHighlighting(String text, String title) {
super(text, title);
/**
* 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

This file was deleted.

4 changes: 2 additions & 2 deletions src/main/java/net/sf/jabref/gui/fieldeditors/TextField.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +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.JTextFieldWithUnfocusedText;
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 JTextFieldWithUnfocusedText implements FieldEditor {
public class TextField extends JTextFieldWithPlaceholder implements FieldEditor {

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

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 @@ -27,7 +27,7 @@
import net.sf.jabref.gui.help.HelpAction;
import net.sf.jabref.gui.keyboard.KeyBinding;
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 @@ -47,7 +47,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 6f35ff1

Please sign in to comment.