Subversion Repositories wimsdev

Rev

Blame | Last modification | View Log | RSS feed

  1. package rene.gui;
  2.  
  3. /*
  4. This file contains a keyboard translater. It translates key strokes
  5. into text strings. The strings are menu descriptions and key
  6. descriptions. Menu descriptions are used to call menu entries in
  7. EditorFrame, and key descriptions are used in TextDisplay.
  8. <p>
  9. JE supports up to 5 command keys, which may be prepended to other
  10. keys. Those keys are mapped to command.X, where X is from 1 to 5.
  11. There is also a special escape command key, mapped to command.escape.
  12. <p>
  13. Some strings are contained in the properties, others may be defined by
  14. the user, and are contained in the parameter file "je.cfg". There is
  15. also an editor for the keystrokes, which uses the ItemEditor dialog.
  16. */
  17.  
  18. import java.awt.Frame;
  19. import java.awt.event.KeyEvent;
  20. import java.io.FileInputStream;
  21. import java.io.FileOutputStream;
  22. import java.util.Enumeration;
  23. import java.util.Hashtable;
  24. import java.util.Properties;
  25. import java.util.Vector;
  26.  
  27. import rene.dialogs.ItemEditor;
  28. import rene.dialogs.MyFileDialog;
  29. import rene.util.sort.Sorter;
  30.  
  31. /**
  32. A static class, which contains tranlations for key events. It keeps
  33. the translations in a vector of KeyboardItem.
  34. <p>
  35. The class will statically generate the list of translations from the
  36. default local properties and the JE configuration.
  37. */
  38.  
  39. public class Keyboard
  40. {       static Vector V;
  41.         static Hashtable Hmenu,Hcharkey;
  42.  
  43.         static
  44.         {       makeKeys();
  45.         }
  46.        
  47.         /**
  48.         Read the keys from the Global names and parameters, and put into a
  49.         vector and two hash tables for easy access.
  50.         */
  51.         public static void makeKeys ()
  52.         {       V=new Vector();
  53.                 Hmenu=new Hashtable(); Hcharkey=new Hashtable();
  54.                 // collect all predefined keys
  55.                 Enumeration e=Global.names();
  56.                 if (e==null) return;
  57.                 while (e.hasMoreElements())
  58.                 {       String key=(String)e.nextElement();
  59.                         if (key.startsWith("key."))
  60.                         {       String menu=key.substring(4);
  61.                                 String charkey=Global.getParameter(key,"default");
  62.                                 String normalcharkey=Global.name(key);
  63.                                 if (charkey.equals("default")) charkey=normalcharkey;
  64.                                 KeyboardItem k=new KeyboardItem(menu,charkey);
  65.                                 V.addElement(k);
  66.                                 Hmenu.put(menu,k); Hcharkey.put(charkey,k);
  67.                         }
  68.                 }
  69.                 // collect all user defined (double defined) keys
  70.                 e=Global.properties();
  71.                 while (e.hasMoreElements())
  72.                 {       String key=(String)e.nextElement();
  73.                         if (key.startsWith("key."))
  74.                         {       String menu=key.substring(4);
  75.                                 if (findMenu(menu)!=null) continue;
  76.                                 String charkey=Global.getParameter(key,"default");
  77.                                 if (charkey.equals("default")) continue;
  78.                                 KeyboardItem k=new KeyboardItem(menu,charkey);
  79.                                 V.addElement(k);
  80.                                 Hmenu.put(menu,k); Hcharkey.put(charkey,k);
  81.                         }
  82.                 }
  83.         }
  84.        
  85.         /**
  86.         Find a menu string in the key definitions
  87.         */
  88.         public static KeyboardItem findMenu (String menu)
  89.         {       Object o=Hmenu.get(menu);
  90.                 if (o==null) return null;
  91.                 else return (KeyboardItem)o;
  92.         }
  93.        
  94.         /**
  95.         Generate a shortcut for the menu item.
  96.         */
  97.         public static String shortcut (String tag)
  98.         {       Enumeration e=V.elements();
  99.                 while (e.hasMoreElements())
  100.                 {       KeyboardItem item=(KeyboardItem)e.nextElement();
  101.                         if (item.getMenuString().equals(tag))
  102.                         {       String shortcut=item.shortcut();
  103.                                 if (!shortcut.equals("")) shortcut=" ("+shortcut+")";
  104.                                 return shortcut;
  105.                         }
  106.                 }
  107.                 return "";
  108.         }
  109.  
  110.         /**
  111.         See, if the key event matches any of my translations, and get the
  112.         menu entry.
  113.         */     
  114.         public static String findKey (KeyEvent event, int type)
  115.         {       Object o=Hcharkey.get(toCharKey(event,type));
  116.                 if (o==null) return "";
  117.                 String s=((KeyboardItem)o).getMenuString();
  118.                 while (s.endsWith("*")) s=s.substring(0,s.length()-1);
  119.                 return s;
  120.         }
  121.        
  122.         /**
  123.         Make a keychar string from the event.
  124.         */
  125.         public static String toCharKey (KeyEvent e, int type)
  126.         {       String s="";
  127.                 if (type>0) s=s+"esc"+type+".";
  128.                 if (e.isShiftDown()) s=s+"shift.";
  129.                 if (e.isControlDown()) s=s+"control.";
  130.                 if (e.isAltDown()) s=s+"alt.";
  131.                 return s+KeyDictionary.translate(e.getKeyCode()).toLowerCase();
  132.         }
  133.        
  134.         /**
  135.         Edit the translations.
  136.         */
  137.         public static void edit (Frame f)
  138.         {       KeyboardItem keys[]=new KeyboardItem[V.size()];
  139.                 V.copyInto(keys);
  140.                 Sorter.sort(keys);
  141.                 Vector v=new Vector();
  142.                 for (int i=0; i<keys.length; i++) v.addElement(keys[i]);
  143.                 KeyboardPanel p=new KeyboardPanel();
  144.                 ItemEditor d=new ItemEditor(f,p,v,"keyeditor",
  145.                         Global.name("keyeditor.prompt"),true,false,true,
  146.                         Global.name("keyeditor.clearall"));
  147.                 p.setItemEditor(d);
  148.                 p.makeCommandChoice();
  149.                 d.center(f);
  150.                 d.setVisible(true);
  151.                 if (d.isAborted()) return;
  152.                 Global.removeAllParameters("key.");
  153.                 V=d.getElements();
  154.                 Enumeration e=V.elements();
  155.                 while (e.hasMoreElements())
  156.                 {       KeyboardItem k=(KeyboardItem)e.nextElement();
  157.                         if (!k.getCharKey().equals("default"))
  158.                         {       String keytag="key."+k.getMenuString();
  159.                                 String description=k.keyDescription();
  160.                                 if (!Global.name(keytag).toLowerCase().equals(description))
  161.                                 {       Global.setParameter(keytag,description);
  162.                                 }
  163.                         }
  164.                 }
  165.                 makeKeys();
  166.                 if (d.getAction()==ItemEditor.SAVE)
  167.                 {       Properties parameters=new Properties();
  168.                         e=Global.properties();
  169.                         while (e.hasMoreElements())
  170.                         {       String key=(String)e.nextElement();
  171.                                 if (key.startsWith("key."))
  172.                                         parameters.put(key,Global.getParameter(key,"default"));
  173.                         }
  174.                         MyFileDialog save=new MyFileDialog(f,Global.name("save"),
  175.                                 Global.name("save"),true);
  176.                         save.setPattern("*.keys");
  177.                         save.center(f);
  178.                         save.update();
  179.                         save.setVisible(true);
  180.                         if (save.isAborted()) return;
  181.                         String filename=save.getFilePath();
  182.                         if (filename.equals("")) return; // aborted dialog!
  183.                         try
  184.                         {       FileOutputStream o=new FileOutputStream(filename);
  185.                                 parameters.save(o,"JE Keyboard Definition");
  186.                         }
  187.                         catch (Exception ex) {}
  188.                 }
  189.                 else if (d.getAction()==ItemEditor.LOAD)
  190.                 {       Properties parameters=new Properties();
  191.                         MyFileDialog load=new MyFileDialog(f,Global.name("load"),
  192.                                 Global.name("load"),true);
  193.                         load.setPattern("*.keys");
  194.                         load.center(f);
  195.                         load.update();
  196.                         load.setVisible(true);
  197.                         if (load.isAborted()) return;
  198.                         String filename=load.getFilePath();
  199.                         if (filename.equals("")) return; // aborted dialog!
  200.                         try
  201.                         {       FileInputStream in=new FileInputStream(filename);
  202.                                 parameters.load(in);
  203.                         }
  204.                         catch (Exception ex) {}
  205.                         Global.removeAllParameters("key.");
  206.                         e=parameters.keys();
  207.                         while (e.hasMoreElements())
  208.                         {       String key=(String)e.nextElement();
  209.                                 Global.setParameter(key,(String)parameters.get(key));
  210.                         }
  211.                         makeKeys();
  212.                 }
  213.         }
  214.        
  215.         /**
  216.         Append a list of keyboard shortcuts to a text area.
  217.         */
  218.         public static Vector getKeys ()
  219.         {       Vector keys=new Vector();
  220.                 Sorter.sort(V);
  221.                 Enumeration e=V.elements();
  222.                 while (e.hasMoreElements())
  223.                 {       KeyboardItem k=(KeyboardItem)e.nextElement();
  224.                         if (!k.getCharKey().equals("none"))
  225.                         {       String shortcut=k.shortcut();
  226.                                 int n=shortcut.length();
  227.                                 for (int i=0; i<30-n; i++) shortcut=shortcut+" ";
  228.                                 keys.addElement(shortcut+" = "+k.getActionName());
  229.                         }
  230.                 }
  231.                 return keys;
  232.         }
  233.        
  234.         /**
  235.         Find a shortcut for the command.
  236.         */
  237.         public static String commandShortcut (int type)
  238.         {       Object o=Hmenu.get("command."+type);
  239.                 if (o==null) return "";
  240.                 return ((KeyboardItem)o).shortcut();
  241.         }
  242. }
  243.