Back to index

wims  3.65+svn20090927
KeyboardPanel.java
Go to the documentation of this file.
00001 package rene.gui;
00002 
00003 import java.awt.BorderLayout;
00004 import java.awt.Checkbox;
00005 import java.awt.Choice;
00006 import java.awt.GridLayout;
00007 import java.awt.Panel;
00008 import java.awt.TextField;
00009 import java.awt.event.KeyEvent;
00010 import java.awt.event.KeyListener;
00011 import java.util.Enumeration;
00012 import java.util.Vector;
00013 
00014 import rene.dialogs.ItemEditor;
00015 import rene.dialogs.ItemEditorElement;
00016 import rene.dialogs.ItemPanel;
00017 
00023 public class KeyboardPanel extends ItemPanel
00024        implements KeyListener
00025 {      TextField MenuString,ActionName,CharKey;
00026        Checkbox Shift,Control,Alt;
00027        String Name="";
00028        Choice C;
00029        ItemEditor E;
00030 
00031        public KeyboardPanel ()
00032        {      setLayout(new BorderLayout());
00033               
00034               Panel center=new Panel();
00035               center.setLayout(new GridLayout(0,1));
00036               // the menu item
00037               center.add(MenuString=new MyTextField("",30));
00038               MenuString.setEditable(false);
00039               // the description
00040               center.add(ActionName=new MyTextField());
00041               ActionName.setEditable(false);
00042               // the key
00043               center.add(CharKey=new MyTextField());
00044               CharKey.setEditable(false);
00045               CharKey.addKeyListener(this);
00046               // modifiers
00047               center.add(Shift=new Checkbox(Global.name("keyeditor.shift")));
00048               center.add(Control=new Checkbox(Global.name("keyeditor.control")));
00049               center.add(Alt=new Checkbox(Global.name("keyeditor.alt")));
00050               add("Center",center);
00051 
00052               Panel south=new Panel();
00053               south.setLayout(new BorderLayout());
00054               Panel c=new Panel();
00055               // the choice of command keys
00056               C=new Choice();
00057               if (Global.NormalFont!=null) C.setFont(Global.NormalFont);
00058               c.add(C);
00059               C.add("-------------");
00060               south.add("Center",c);
00061               // default and undefine buttons
00062               Panel buttons=new Panel();
00063               buttons.add(new ButtonAction(this,
00064                      Global.name("keyeditor.default"),"Default"));
00065               buttons.add(new ButtonAction(this,
00066                      Global.name("keyeditor.none"),"None"));
00067               south.add("South",buttons);
00068               add("South",south);
00069        }
00070        
00071        public void setItemEditor (ItemEditor e)
00072        {      E=e;
00073        }
00074        
00078        public void makeCommandChoice ()
00079        {      C.removeAll();
00080               C.add("");
00081               for (int i=1; i<=5; i++)
00082               {      String s=commandShortcut(i);
00083                      C.add(i+": "+s);
00084               }
00085               C.select(0);
00086        }
00087        
00091        public String commandShortcut (int i)
00092        {      String s="command."+i;
00093               Enumeration e=E.getElements().elements();
00094               while (e.hasMoreElements())
00095               {      KeyboardItem k=(KeyboardItem)e.nextElement();
00096                      if (k.getMenuString().equals(s))
00097                      {      return k.shortcut();
00098                      }
00099               }
00100               return "";
00101        }
00102        
00106        public void keyPressed (KeyEvent e)
00107        {      Shift.setState(e.isShiftDown());
00108               Control.setState(e.isControlDown());
00109               Alt.setState(e.isAltDown());
00110               CharKey.setText(
00111                      KeyDictionary.translate(e.getKeyCode()).toLowerCase());
00112               C.select(0);
00113        }
00114        public void keyTyped (KeyEvent e) {}
00115        public void keyReleased (KeyEvent e) {}
00116        
00117        /*
00118        Override methods of ItemPanel
00119        */
00120        
00124        public void display (ItemEditorElement e)
00125        {      KeyboardItem k=(KeyboardItem)e;
00126               Name=k.getName();
00127               MenuString.setText(k.getMenuString());
00128               ActionName.setText(k.getActionName());
00129               CharKey.setText(k.getCharKey());
00130               MenuString.setText(k.getMenuString());
00131               Shift.setState(k.isShift());
00132               Control.setState(k.isControl());
00133               Alt.setState(k.isAlt());
00134               C.select(k.getCommandType());
00135        }
00136        
00140        public ItemEditorElement getElement ()
00141        {      int type=C.getSelectedIndex();
00142               return new KeyboardItem(CharKey.getText(),
00143                      MenuString.getText(),ActionName.getText(),
00144                      Shift.getState(),Control.getState(),Alt.getState(),type);
00145        }
00146        
00147        public String getName ()
00148        {      return Name;
00149        }
00150        
00151        public void setName (String s)
00152        {      Name=s;
00153               MenuString.setText(s);
00154        }
00155        
00159        public void notifyChange (Vector v, int item)
00160        {      KeyboardItem changed=(KeyboardItem)v.elementAt(item);
00161               String descr=changed.keyDescription();
00162               for (int i=0; i<v.size(); i++)
00163               {      if (i==item) continue;
00164                      KeyboardItem k=(KeyboardItem)v.elementAt(i);
00165                      if (k.keyDescription().equals(descr))
00166                      {      v.setElementAt(new KeyboardItem(k.getMenuString(),"none"),i);
00167                      }
00168               }
00169               if (changed.getMenuString().startsWith("command."))
00170               {      makeCommandChoice();
00171               }
00172        }
00173        
00177        public void doAction (String o)
00178        {      if (o.equals("Default"))
00179               {      String s=MenuString.getText();
00180                      KeyboardItem k=new KeyboardItem(s,Global.name("key."+s));
00181                      CharKey.setText(k.getCharKey());
00182                      Shift.setState(k.isShift());
00183                      Control.setState(k.isControl());
00184                      Alt.setState(k.isAlt());
00185               }
00186               else if (o.equals("None"))
00187               {      CharKey.setText("none");
00188                      Shift.setState(false);
00189                      Control.setState(false);
00190                      Alt.setState(false);
00191               }
00192               else super.doAction(o);
00193        }
00194        
00198        public boolean extra (Vector v)
00199        {      v.removeAllElements();
00200               return true;
00201        }
00202 }