package core.commandinputframe;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JComboBox;
import javax.swing.JTextField;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;
import core.Command;
import core.Karen;
class TextField extends JTextField implements KeyListener, Runnable, DocumentListener
{
private static final long serialVersionUID = 1L;
private JComboBox fastSelectBox;
private CommandInputFrame commandInputFrame;
private boolean blockDocumentListener;
private boolean breakThread;
private Thread thread;
private Karen karen;
TextField(JComboBox fastSelectBox, CommandInputFrame commandInputFrame, Karen karen)
{
super();
this.fastSelectBox = fastSelectBox;
this.commandInputFrame = commandInputFrame;
this.karen = karen;
addKeyListener(this);
getDocument().addDocumentListener(this);
}
public void keyPressed(KeyEvent arg0)
{
int keyCode = arg0.getKeyCode();
if(arg0.getKeyCode() == KeyEvent.VK_ENTER)
commandInputFrame.runCommand(getText());
else if(arg0.getKeyCode() == KeyEvent.VK_ESCAPE)
commandInputFrame.setVisible(false);
else if(keyCode == KeyEvent.VK_DOWN)
{
if(fastSelectBox.getModel().getSize() - 1 > fastSelectBox.getSelectedIndex())
fastSelectBox.setSelectedIndex(fastSelectBox.getSelectedIndex() + 1);
String oldText = getText();
blockDocumentListener = true;
setText(fastSelectBox.getSelectedItem().toString());
blockDocumentListener = false;
select(getEqualCharCount(oldText, getText()), getText().length());
}
else if(keyCode == KeyEvent.VK_UP)
{
if(fastSelectBox.getSelectedIndex() > 0)
fastSelectBox.setSelectedIndex(fastSelectBox.getSelectedIndex() - 1);
String oldText = getText();
blockDocumentListener = true;
setText(fastSelectBox.getSelectedItem().toString());
blockDocumentListener = false;
select(getEqualCharCount(oldText, getText()), getText().length());
}
}
private int getEqualCharCount(String s1, String s2)
{
int result = 0;
char[] firstCharArray = s1.toCharArray();
char[] secondCharArray = s2.toCharArray();
for(int i = 0, size = Math.min(firstCharArray.length, secondCharArray.length); i < size; i++)
{
if(firstCharArray[i] == secondCharArray[i])
result++;
else
break;
}
return(result);
}
public void insertUpdate(DocumentEvent e)
{
if(!blockDocumentListener)
beginCollectionCommands();
}
private void beginCollectionCommands()
{
if(thread != null)
{
breakThread = true;
try
{
thread.join();
}
catch (InterruptedException e) {}
}
breakThread = false;
thread = new Thread(this);
thread.start();
}
public void run()
{
String text = getText();
int oldTextLenght = text.length();
DefaultComboBoxModel model = new DefaultComboBoxModel();
for(int i = 0, size = karen.getCommandCount(); i < size && !breakThread; i++)
{
Command command = karen.getCommandPerListId(i);
if(command.getData(Command.NAME).startsWith(text))
model.addElement(command);
}
if(!breakThread)
{
fastSelectBox.setModel(model);
if(model.getSize() > 0)
{
fastSelectBox.showPopup();
Object selectedItem = fastSelectBox.getSelectedItem();
blockDocumentListener = true;
setText(selectedItem.toString());
blockDocumentListener = false;
select(oldTextLenght, selectedItem.toString().length());
}
}
}
public void keyReleased(KeyEvent arg0) {}
public void keyTyped(KeyEvent arg0) {}
public void changedUpdate(DocumentEvent e) {}
public void removeUpdate(DocumentEvent e) {}
}