195 lines
5.1 KiB
Java
195 lines
5.1 KiB
Java
package nt.UI.control;
|
|
|
|
import java.awt.Font;
|
|
import java.awt.Window;
|
|
import java.awt.event.ActionEvent;
|
|
import java.awt.event.ComponentEvent;
|
|
import java.awt.event.ComponentListener;
|
|
import java.awt.event.FocusEvent;
|
|
import java.awt.event.FocusListener;
|
|
import java.awt.event.HierarchyBoundsListener;
|
|
import java.awt.event.HierarchyEvent;
|
|
import java.awt.event.KeyAdapter;
|
|
import java.awt.event.KeyEvent;
|
|
|
|
import javax.swing.AbstractAction;
|
|
import javax.swing.ActionMap;
|
|
import javax.swing.InputMap;
|
|
import javax.swing.JList;
|
|
import javax.swing.JTextField;
|
|
import javax.swing.KeyStroke;
|
|
import javax.swing.SwingUtilities;
|
|
import javax.swing.event.DocumentEvent;
|
|
import javax.swing.event.DocumentListener;
|
|
import javax.swing.event.ListSelectionEvent;
|
|
import javax.swing.event.ListSelectionListener;
|
|
|
|
|
|
public class JSearchBarController extends KeyAdapter implements DocumentListener, FocusListener, ComponentListener, HierarchyBoundsListener, ListSelectionListener{
|
|
|
|
private static final String COMPLETION_CANCEL_KEY = "comp.cancel";
|
|
|
|
private static final KeyStroke COMPLETION_CANCEL_STROKE = KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0);
|
|
|
|
private JSearchBar searchBar;
|
|
|
|
protected JSearchBarController(JSearchBar searchBar) {
|
|
this.searchBar = searchBar;
|
|
|
|
searchBar.txtSearch.addFocusListener(this);
|
|
searchBar.txtSearch.addKeyListener(this);
|
|
searchBar.txtSearch.getDocument().addDocumentListener(this);
|
|
InputMap inputMap = searchBar.txtSearch.getInputMap();
|
|
inputMap.put(COMPLETION_CANCEL_STROKE, COMPLETION_CANCEL_KEY);
|
|
|
|
ActionMap actionMap = searchBar.txtSearch.getActionMap();
|
|
actionMap.put(COMPLETION_CANCEL_KEY, new AbstractAction() {
|
|
private static final long serialVersionUID = 1L;
|
|
@Override
|
|
public void actionPerformed(ActionEvent e) {
|
|
setDefaultState();
|
|
}
|
|
});
|
|
|
|
searchBar.txtSearch.addComponentListener(this);
|
|
searchBar.txtSearch.addHierarchyBoundsListener(this);
|
|
searchBar.addActionListener(e -> setDefaultState());
|
|
|
|
searchBar.dropDown.list.addListSelectionListener(this);
|
|
|
|
}
|
|
|
|
protected void setDefaultState() {
|
|
JTextField txt = this.searchBar.txtSearch;
|
|
this.searchBar.showCancelButton(false);
|
|
txt.setText("search");
|
|
txt.setFont(txt.getFont().deriveFont(Font.ITALIC));
|
|
this.searchBar.repaint();
|
|
this.searchBar.stopCompletion();
|
|
Window win = SwingUtilities.getWindowAncestor(searchBar);
|
|
if(win != null) {
|
|
win.requestFocus();
|
|
}
|
|
}
|
|
protected void setEnteredState() {
|
|
JTextField txt = this.searchBar.txtSearch;
|
|
this.searchBar.showCancelButton(true);
|
|
txt.setFont(txt.getFont().deriveFont(Font.PLAIN));
|
|
}
|
|
|
|
@Override
|
|
public void focusGained(FocusEvent e) {
|
|
JTextField bar = this.searchBar.txtSearch;
|
|
if(bar.getText().toLowerCase().equals("search")) {
|
|
bar.setText("");
|
|
}
|
|
setEnteredState();
|
|
}
|
|
|
|
@Override
|
|
public void focusLost(FocusEvent e) {
|
|
JTextField bar = this.searchBar.txtSearch;
|
|
if(bar.getText().isEmpty() || bar.getText().toLowerCase().equals("search")) {
|
|
setDefaultState();
|
|
}else {
|
|
setEnteredState();
|
|
}
|
|
this.searchBar.stopCompletion();
|
|
}
|
|
|
|
@Override
|
|
public void keyPressed(KeyEvent e) {
|
|
if(this.searchBar.completionActive) {
|
|
JList<String> list = searchBar.dropDown.list;
|
|
int index = list.getSelectedIndex();
|
|
if(e.getKeyCode() == KeyEvent.VK_DOWN) {
|
|
index = index >= list.getModel().getSize()? list.getModel().getSize()-1:++index;
|
|
}else if(e.getKeyCode() == KeyEvent.VK_UP) {
|
|
index = index <= -1? -1 : --index;
|
|
}
|
|
list.setSelectedIndex(index);
|
|
list.ensureIndexIsVisible(index);
|
|
|
|
if(e.getKeyCode()==KeyEvent.VK_ENTER) {
|
|
searchBar.txtSearch.setText(list.getSelectedValue());
|
|
searchBar.finder.acceptedResult(list.getSelectedValue());
|
|
}
|
|
}
|
|
}
|
|
|
|
@Override
|
|
public void componentResized(ComponentEvent e) {
|
|
if(this.searchBar.completionActive) {
|
|
this.searchBar.dropDown.positionMenu();
|
|
}
|
|
}
|
|
|
|
@Override
|
|
public void componentMoved(ComponentEvent e) {
|
|
if(this.searchBar.completionActive) {
|
|
this.searchBar.dropDown.positionMenu();
|
|
}
|
|
}
|
|
|
|
@Override
|
|
public void componentShown(ComponentEvent e) {
|
|
|
|
}
|
|
|
|
@Override
|
|
public void componentHidden(ComponentEvent e) {
|
|
// TODO Auto-generated method stub
|
|
|
|
}
|
|
|
|
@Override
|
|
public void ancestorMoved(HierarchyEvent e) {
|
|
if(this.searchBar.completionActive) {
|
|
this.searchBar.stopCompletion();
|
|
}
|
|
}
|
|
|
|
@Override
|
|
public void ancestorResized(HierarchyEvent e) {
|
|
// TODO Auto-generated method stub
|
|
|
|
}
|
|
|
|
@Override
|
|
public void valueChanged(ListSelectionEvent e) {
|
|
// String val = searchBar.dropDown.list.getSelectedValue();
|
|
// if(val != null) {
|
|
// JTextField field = searchBar.txtSearch;
|
|
// field.setText(val);
|
|
// field.setCaretPosition(field.getText().length());
|
|
// }
|
|
|
|
}
|
|
|
|
@Override
|
|
public void insertUpdate(DocumentEvent e) {
|
|
if(searchBar.txtSearch.getDocument().getLength() > 0) {
|
|
if(!searchBar.completionActive) {
|
|
searchBar.startCompletion();
|
|
}
|
|
searchBar.doCompletion();
|
|
}else {
|
|
searchBar.stopCompletion();
|
|
}
|
|
}
|
|
|
|
@Override
|
|
public void removeUpdate(DocumentEvent e) {
|
|
searchBar.doCompletion();
|
|
|
|
}
|
|
|
|
@Override
|
|
public void changedUpdate(DocumentEvent e) {
|
|
// TODO Auto-generated method stub
|
|
|
|
}
|
|
|
|
|
|
}
|