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