filebot/source/net/sourceforge/tuned/ui/TextFieldWithSelect.java

96 lines
2.2 KiB
Java
Raw Normal View History

package net.sourceforge.tuned.ui;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Collection;
import javax.swing.AbstractAction;
import javax.swing.BorderFactory;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.KeyStroke;
import javax.swing.border.Border;
import net.sourceforge.tuned.ui.SelectButton.Entry;
public class TextFieldWithSelect<T> extends JPanel {
private SelectButton<T> selectButton;
private JTextField textfield = new JTextField();
private Color borderColor = new Color(0xA4A4A4);
public TextFieldWithSelect(Collection<Entry<T>> options) {
setLayout(new BorderLayout(0, 0));
selectButton = new SelectButton<T>(options);
selectButton.addActionListener(textFieldFocusOnClick);
Border lineBorder = BorderFactory.createLineBorder(borderColor, 1);
Border matteBorder = BorderFactory.createMatteBorder(1, 0, 1, 1, borderColor);
Border emptyBorder = BorderFactory.createEmptyBorder(0, 3, 0, 3);
selectButton.setBorder(lineBorder);
textfield.setBorder(BorderFactory.createCompoundBorder(matteBorder, emptyBorder));
textfield.setColumns(20);
add(textfield, BorderLayout.CENTER);
add(selectButton, BorderLayout.WEST);
TunedUtil.registerActionForKeystroke(this, KeyStroke.getKeyStroke("shift UP"), new SpinClientAction(-1));
TunedUtil.registerActionForKeystroke(this, KeyStroke.getKeyStroke("shift DOWN"), new SpinClientAction(1));
}
public JTextField getTextField() {
return textfield;
}
public SelectButton<T> getSelectButton() {
return selectButton;
}
public T getSelectedValue() {
return selectButton.getSelectedEntry();
}
public void clearTextSelection() {
int length = textfield.getText().length();
textfield.select(length, length);
}
private final ActionListener textFieldFocusOnClick = new ActionListener() {
public void actionPerformed(ActionEvent e) {
textfield.requestFocus();
}
};
private class SpinClientAction extends AbstractAction {
private int spin;
public SpinClientAction(int spin) {
this.spin = spin;
}
public void actionPerformed(ActionEvent e) {
selectButton.spinValue(spin);
}
}
}