package rene.gui;
import java.awt.BorderLayout;
import java.awt.Checkbox;
import java.awt.Choice;
import java.awt.GridLayout;
import java.awt.Panel;
import java.awt.TextField;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.util.Enumeration;
import java.util.Vector;
import rene.dialogs.ItemEditor;
import rene.dialogs.ItemEditorElement;
import rene.dialogs.ItemPanel;
/**
This is used as the display panel for the keyboard editor. It displays
information about the selected keyboard item.
*/
public class KeyboardPanel extends ItemPanel
ItemEditor E;
public KeyboardPanel ()
// the menu item
center.add(MenuString=new MyTextField("",30));
MenuString.setEditable(false);
// the description
center.add(ActionName=new MyTextField());
ActionName.setEditable(false);
// the key
center.add(CharKey=new MyTextField());
CharKey.setEditable(false);
CharKey.addKeyListener(this);
// modifiers
center.
add(Shift=
new Checkbox(Global.
name("keyeditor.shift")));
center.
add(Alt=
new Checkbox(Global.
name("keyeditor.alt")));
add("Center",center);
// the choice of command keys
if (Global.NormalFont!=null) C.setFont(Global.NormalFont);
c.add(C);
C.add("-------------");
south.add("Center",c);
// default and undefine buttons
buttons.add(new ButtonAction(this,
Global.name("keyeditor.default"),"Default"));
buttons.add(new ButtonAction(this,
Global.name("keyeditor.none"),"None"));
south.add("South",buttons);
add("South",south);
}
public void setItemEditor (ItemEditor e)
{ E=e;
}
/**
Build a list of available command keys.
*/
public void makeCommandChoice ()
{ C.removeAll();
C.add("");
for (int i=1; i<=5; i++)
{ String s=commandShortcut
(i
);
C.add(i+": "+s);
}
C.select(0);
}
/**
The the command shortcut number i.
*/
public String commandShortcut
(int i
)
while (e.hasMoreElements())
{ KeyboardItem k=(KeyboardItem)e.nextElement();
if (k.getMenuString().equals(s))
{ return k.shortcut();
}
}
return "";
}
/**
Set the key, if one is pressed inside the CharKey textfield.
*/
{ Shift.setState(e.isShiftDown());
Control.
setState(e.
isControlDown());
Alt.setState(e.isAltDown());
CharKey.setText(
KeyDictionary.translate(e.getKeyCode()).toLowerCase());
C.select(0);
}
/*
Override methods of ItemPanel
*/
/**
Display this element on the panel.
*/
public void display (ItemEditorElement e)
{ KeyboardItem k=(KeyboardItem)e;
MenuString.setText(k.getMenuString());
ActionName.setText(k.getActionName());
CharKey.setText(k.getCharKey());
MenuString.setText(k.getMenuString());
Shift.setState(k.isShift());
Alt.setState(k.isAlt());
C.select(k.getCommandType());
}
/**
Create a new keyboard element from the panel entries.
*/
public ItemEditorElement getElement ()
{ int type=C.getSelectedIndex();
return new KeyboardItem(CharKey.getText(),
MenuString.getText(),ActionName.getText(),
Shift.
getState(),
Control.
getState(),Alt.
getState(),type
);
}
}
public void setName
(String s
)
MenuString.setText(s);
}
/**
Test on doublicate keys, and undefine them.
*/
public void notifyChange
(Vector v,
int item
)
{ KeyboardItem changed=(KeyboardItem)v.elementAt(item);
String descr=changed.
keyDescription();
for (int i=0; i<v.size(); i++)
{ if (i==item) continue;
KeyboardItem k=(KeyboardItem)v.elementAt(i);
if (k.keyDescription().equals(descr))
{ v.setElementAt(new KeyboardItem(k.getMenuString(),"none"),i);
}
}
if (changed.getMenuString().startsWith("command."))
{ makeCommandChoice();
}
}
/**
React on the Default and None buttons.
*/
public void doAction
(String o
)
{ if (o.equals("Default"))
{ String s=MenuString.
getText();
KeyboardItem k=new KeyboardItem(s,Global.name("key."+s));
CharKey.setText(k.getCharKey());
Shift.setState(k.isShift());
Alt.setState(k.isAlt());
}
else if (o.equals("None"))
{ CharKey.setText("none");
Shift.setState(false);
Alt.setState(false);
}
else super.doAction(o);
}
/**
User wishes to clear all keyboard definitions.
*/
public boolean extra
(Vector v
)
{ v.removeAllElements();
return true;
}
}