Subversion Repositories wimsdev

Rev

Blame | Last modification | View Log | RSS feed

  1. package rene.gui;
  2.  
  3. import java.awt.BorderLayout;
  4. import java.awt.Checkbox;
  5. import java.awt.Choice;
  6. import java.awt.GridLayout;
  7. import java.awt.Panel;
  8. import java.awt.TextField;
  9. import java.awt.event.KeyEvent;
  10. import java.awt.event.KeyListener;
  11. import java.util.Enumeration;
  12. import java.util.Vector;
  13.  
  14. import rene.dialogs.ItemEditor;
  15. import rene.dialogs.ItemEditorElement;
  16. import rene.dialogs.ItemPanel;
  17.  
  18. /**
  19. This is used as the display panel for the keyboard editor. It displays
  20. information about the selected keyboard item.
  21. */
  22.  
  23. public class KeyboardPanel extends ItemPanel
  24.         implements KeyListener
  25. {       TextField MenuString,ActionName,CharKey;
  26.         Checkbox Shift,Control,Alt;
  27.         String Name="";
  28.         Choice C;
  29.         ItemEditor E;
  30.  
  31.         public KeyboardPanel ()
  32.         {       setLayout(new BorderLayout());
  33.                
  34.                 Panel center=new Panel();
  35.                 center.setLayout(new GridLayout(0,1));
  36.                 // the menu item
  37.                 center.add(MenuString=new MyTextField("",30));
  38.                 MenuString.setEditable(false);
  39.                 // the description
  40.                 center.add(ActionName=new MyTextField());
  41.                 ActionName.setEditable(false);
  42.                 // the key
  43.                 center.add(CharKey=new MyTextField());
  44.                 CharKey.setEditable(false);
  45.                 CharKey.addKeyListener(this);
  46.                 // modifiers
  47.                 center.add(Shift=new Checkbox(Global.name("keyeditor.shift")));
  48.                 center.add(Control=new Checkbox(Global.name("keyeditor.control")));
  49.                 center.add(Alt=new Checkbox(Global.name("keyeditor.alt")));
  50.                 add("Center",center);
  51.  
  52.                 Panel south=new Panel();
  53.                 south.setLayout(new BorderLayout());
  54.                 Panel c=new Panel();
  55.                 // the choice of command keys
  56.                 C=new Choice();
  57.                 if (Global.NormalFont!=null) C.setFont(Global.NormalFont);
  58.                 c.add(C);
  59.                 C.add("-------------");
  60.                 south.add("Center",c);
  61.                 // default and undefine buttons
  62.                 Panel buttons=new Panel();
  63.                 buttons.add(new ButtonAction(this,
  64.                         Global.name("keyeditor.default"),"Default"));
  65.                 buttons.add(new ButtonAction(this,
  66.                         Global.name("keyeditor.none"),"None"));
  67.                 south.add("South",buttons);
  68.                 add("South",south);
  69.         }
  70.        
  71.         public void setItemEditor (ItemEditor e)
  72.         {       E=e;
  73.         }
  74.        
  75.         /**
  76.         Build a list of available command keys.
  77.         */
  78.         public void makeCommandChoice ()
  79.         {       C.removeAll();
  80.                 C.add("");
  81.                 for (int i=1; i<=5; i++)
  82.                 {       String s=commandShortcut(i);
  83.                         C.add(i+": "+s);
  84.                 }
  85.                 C.select(0);
  86.         }
  87.        
  88.         /**
  89.         The the command shortcut number i.
  90.         */
  91.         public String commandShortcut (int i)
  92.         {       String s="command."+i;
  93.                 Enumeration e=E.getElements().elements();
  94.                 while (e.hasMoreElements())
  95.                 {       KeyboardItem k=(KeyboardItem)e.nextElement();
  96.                         if (k.getMenuString().equals(s))
  97.                         {       return k.shortcut();
  98.                         }
  99.                 }
  100.                 return "";
  101.         }
  102.        
  103.         /**
  104.         Set the key, if one is pressed inside the CharKey textfield.
  105.         */
  106.         public void keyPressed (KeyEvent e)
  107.         {       Shift.setState(e.isShiftDown());
  108.                 Control.setState(e.isControlDown());
  109.                 Alt.setState(e.isAltDown());
  110.                 CharKey.setText(
  111.                         KeyDictionary.translate(e.getKeyCode()).toLowerCase());
  112.                 C.select(0);
  113.         }
  114.         public void keyTyped (KeyEvent e) {}
  115.         public void keyReleased (KeyEvent e) {}
  116.        
  117.         /*
  118.         Override methods of ItemPanel
  119.         */
  120.        
  121.         /**
  122.         Display this element on the panel.
  123.         */
  124.         public void display (ItemEditorElement e)
  125.         {       KeyboardItem k=(KeyboardItem)e;
  126.                 Name=k.getName();
  127.                 MenuString.setText(k.getMenuString());
  128.                 ActionName.setText(k.getActionName());
  129.                 CharKey.setText(k.getCharKey());
  130.                 MenuString.setText(k.getMenuString());
  131.                 Shift.setState(k.isShift());
  132.                 Control.setState(k.isControl());
  133.                 Alt.setState(k.isAlt());
  134.                 C.select(k.getCommandType());
  135.         }
  136.        
  137.         /**
  138.         Create a new keyboard element from the panel entries.
  139.         */
  140.         public ItemEditorElement getElement ()
  141.         {       int type=C.getSelectedIndex();
  142.                 return new KeyboardItem(CharKey.getText(),
  143.                         MenuString.getText(),ActionName.getText(),
  144.                         Shift.getState(),Control.getState(),Alt.getState(),type);
  145.         }
  146.        
  147.         public String getName ()
  148.         {       return Name;
  149.         }
  150.        
  151.         public void setName (String s)
  152.         {       Name=s;
  153.                 MenuString.setText(s);
  154.         }
  155.        
  156.         /**
  157.         Test on doublicate keys, and undefine them.
  158.         */
  159.         public void notifyChange (Vector v, int item)
  160.         {       KeyboardItem changed=(KeyboardItem)v.elementAt(item);
  161.                 String descr=changed.keyDescription();
  162.                 for (int i=0; i<v.size(); i++)
  163.                 {       if (i==item) continue;
  164.                         KeyboardItem k=(KeyboardItem)v.elementAt(i);
  165.                         if (k.keyDescription().equals(descr))
  166.                         {       v.setElementAt(new KeyboardItem(k.getMenuString(),"none"),i);
  167.                         }
  168.                 }
  169.                 if (changed.getMenuString().startsWith("command."))
  170.                 {       makeCommandChoice();
  171.                 }
  172.         }
  173.        
  174.         /**
  175.         React on the Default and None buttons.
  176.         */
  177.         public void doAction (String o)
  178.         {       if (o.equals("Default"))
  179.                 {       String s=MenuString.getText();
  180.                         KeyboardItem k=new KeyboardItem(s,Global.name("key."+s));
  181.                         CharKey.setText(k.getCharKey());
  182.                         Shift.setState(k.isShift());
  183.                         Control.setState(k.isControl());
  184.                         Alt.setState(k.isAlt());
  185.                 }
  186.                 else if (o.equals("None"))
  187.                 {       CharKey.setText("none");
  188.                         Shift.setState(false);
  189.                         Control.setState(false);
  190.                         Alt.setState(false);
  191.                 }
  192.                 else super.doAction(o);
  193.         }
  194.        
  195.         /**
  196.         User wishes to clear all keyboard definitions.
  197.         */
  198.         public boolean extra (Vector v)
  199.         {       v.removeAllElements();
  200.                 return true;
  201.         }
  202. }
  203.