mirror of
https://github.com/Card-Forge/forge.git
synced 2025-11-17 19:28:01 +00:00
Make all text fields select all on focus
This commit is contained in:
@@ -3,9 +3,6 @@ package forge.gui.deckeditor.views;
|
||||
import java.awt.Container;
|
||||
import java.awt.Dimension;
|
||||
import java.awt.FlowLayout;
|
||||
import java.awt.event.FocusAdapter;
|
||||
import java.awt.event.FocusEvent;
|
||||
import java.awt.event.FocusListener;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
@@ -15,8 +12,6 @@ import javax.swing.JComponent;
|
||||
import javax.swing.JPanel;
|
||||
import javax.swing.JSpinner;
|
||||
import javax.swing.JTextField;
|
||||
import javax.swing.SwingUtilities;
|
||||
|
||||
import net.miginfocom.swing.MigLayout;
|
||||
|
||||
import org.apache.commons.lang3.tuple.Pair;
|
||||
@@ -146,7 +141,6 @@ public enum VCardCatalog implements IVDoc<CCardCatalog> {
|
||||
|
||||
pnlSearch.setOpaque(false);
|
||||
pnlSearch.add(btnAddRestriction, "center, w pref+8, h pref+8");
|
||||
txfSearch.addFocusListener(_selectAllOnFocus);
|
||||
pnlSearch.add(txfSearch, "pushx, growx");
|
||||
cbSearchMode.addItem("in");
|
||||
cbSearchMode.addItem("not in");
|
||||
@@ -172,16 +166,8 @@ public enum VCardCatalog implements IVDoc<CCardCatalog> {
|
||||
|
||||
private void _setupSpinner (JSpinner spinner) {
|
||||
spinner.setFocusable(false); // only the spinner text field should be focusable, not the up/down widget
|
||||
JTextField t = ((JSpinner.DefaultEditor)spinner.getEditor()).getTextField();
|
||||
t.addFocusListener(_selectAllOnFocus);
|
||||
}
|
||||
|
||||
private final FocusListener _selectAllOnFocus = new FocusAdapter() {
|
||||
@Override public void focusGained(final FocusEvent e) {
|
||||
SwingUtilities.invokeLater(new Runnable() { @Override public void run() { ((JTextField)e.getComponent()).selectAll(); } });
|
||||
}
|
||||
};
|
||||
|
||||
//========== Overridden from IVDoc
|
||||
|
||||
@Override
|
||||
|
||||
@@ -8,6 +8,7 @@ import java.awt.event.FocusAdapter;
|
||||
import java.awt.event.FocusEvent;
|
||||
|
||||
import javax.swing.JTextField;
|
||||
import javax.swing.SwingUtilities;
|
||||
import javax.swing.text.AttributeSet;
|
||||
import javax.swing.text.BadLocationException;
|
||||
import javax.swing.text.PlainDocument;
|
||||
@@ -59,8 +60,7 @@ public class FTextField extends JTextField {
|
||||
this.setMargin(new Insets(3, 3, 2, 3));
|
||||
this.setOpaque(true);
|
||||
|
||||
if (0 < builder.maxLength)
|
||||
{
|
||||
if (builder.maxLength > 0) {
|
||||
this.setDocument(new _LengthLimitedDocument(builder.maxLength));
|
||||
}
|
||||
|
||||
@@ -72,18 +72,26 @@ public class FTextField extends JTextField {
|
||||
addFocusListener(new FocusAdapter() {
|
||||
@Override
|
||||
public void focusGained(final FocusEvent e) {
|
||||
FTextField field = (FTextField)e.getSource();
|
||||
if (field.ghostText != null && field.isEmpty() && !field.showGhostTextWithFocus)
|
||||
{
|
||||
field.repaint();
|
||||
final FTextField field = (FTextField)e.getSource();
|
||||
if (field.isEmpty()) {
|
||||
if (field.ghostText != null && !field.showGhostTextWithFocus) {
|
||||
field.repaint();
|
||||
}
|
||||
}
|
||||
else { //if not empty, select all text when focused
|
||||
SwingUtilities.invokeLater(new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
field.selectAll();
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void focusLost(final FocusEvent e) {
|
||||
FTextField field = (FTextField)e.getSource();
|
||||
if (field.ghostText != null && field.isEmpty() && !field.showGhostTextWithFocus)
|
||||
{
|
||||
if (field.ghostText != null && field.isEmpty() && !field.showGhostTextWithFocus) {
|
||||
field.repaint();
|
||||
}
|
||||
}
|
||||
@@ -102,8 +110,7 @@ public class FTextField extends JTextField {
|
||||
@Override
|
||||
public void paint(Graphics g) {
|
||||
super.paint(g);
|
||||
if (this.ghostText != null && this.isEmpty() && (this.showGhostTextWithFocus || !this.hasFocus()))
|
||||
{
|
||||
if (this.ghostText != null && this.isEmpty() && (this.showGhostTextWithFocus || !this.hasFocus())) {
|
||||
//TODO: Make ghost text look more like regular text
|
||||
final Insets margin = this.getMargin();
|
||||
final Graphics2D g2d = (Graphics2D)g.create();
|
||||
@@ -125,8 +132,7 @@ public class FTextField extends JTextField {
|
||||
if (ghostText0 == "") { ghostText0 = null; } //don't allow empty string to make other logic easier
|
||||
if (this.ghostText == ghostText0) { return; }
|
||||
this.ghostText = ghostText0;
|
||||
if (this.isEmpty())
|
||||
{
|
||||
if (this.isEmpty()) {
|
||||
this.repaint();
|
||||
}
|
||||
}
|
||||
@@ -140,8 +146,7 @@ public class FTextField extends JTextField {
|
||||
{
|
||||
if (this.showGhostTextWithFocus == showGhostTextWithFocus0) { return; }
|
||||
this.showGhostTextWithFocus = showGhostTextWithFocus0;
|
||||
if (this.isEmpty() && this.hasFocus())
|
||||
{
|
||||
if (this.isEmpty() && this.hasFocus()) {
|
||||
this.repaint();
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user