Back to index

wims  3.65+svn20090927
Keyboard.java
Go to the documentation of this file.
00001 package rene.gui;
00002 
00003 /*
00004 This file contains a keyboard translater. It translates key strokes
00005 into text strings. The strings are menu descriptions and key
00006 descriptions. Menu descriptions are used to call menu entries in
00007 EditorFrame, and key descriptions are used in TextDisplay.
00008 <p>
00009 JE supports up to 5 command keys, which may be prepended to other
00010 keys. Those keys are mapped to command.X, where X is from 1 to 5.
00011 There is also a special escape command key, mapped to command.escape.
00012 <p>
00013 Some strings are contained in the properties, others may be defined by
00014 the user, and are contained in the parameter file "je.cfg". There is
00015 also an editor for the keystrokes, which uses the ItemEditor dialog.
00016 */
00017 
00018 import java.awt.Frame;
00019 import java.awt.event.KeyEvent;
00020 import java.io.FileInputStream;
00021 import java.io.FileOutputStream;
00022 import java.util.Enumeration;
00023 import java.util.Hashtable;
00024 import java.util.Properties;
00025 import java.util.Vector;
00026 
00027 import rene.dialogs.ItemEditor;
00028 import rene.dialogs.MyFileDialog;
00029 import rene.util.sort.Sorter;
00030 
00039 public class Keyboard
00040 {      static Vector V;
00041        static Hashtable Hmenu,Hcharkey;
00042 
00043        static
00044        {      makeKeys();
00045        }
00046        
00051        public static void makeKeys ()
00052        {      V=new Vector();
00053               Hmenu=new Hashtable(); Hcharkey=new Hashtable();
00054               // collect all predefined keys
00055               Enumeration e=Global.names();
00056               if (e==null) return;
00057               while (e.hasMoreElements())
00058               {      String key=(String)e.nextElement();
00059                      if (key.startsWith("key."))
00060                      {      String menu=key.substring(4);
00061                             String charkey=Global.getParameter(key,"default");
00062                             String normalcharkey=Global.name(key);
00063                             if (charkey.equals("default")) charkey=normalcharkey;
00064                             KeyboardItem k=new KeyboardItem(menu,charkey);
00065                             V.addElement(k);
00066                             Hmenu.put(menu,k); Hcharkey.put(charkey,k);
00067                      }
00068               }
00069               // collect all user defined (double defined) keys
00070               e=Global.properties();
00071               while (e.hasMoreElements())
00072               {      String key=(String)e.nextElement();
00073                      if (key.startsWith("key."))
00074                      {      String menu=key.substring(4);
00075                             if (findMenu(menu)!=null) continue;
00076                             String charkey=Global.getParameter(key,"default");
00077                             if (charkey.equals("default")) continue;
00078                             KeyboardItem k=new KeyboardItem(menu,charkey);
00079                             V.addElement(k);
00080                             Hmenu.put(menu,k); Hcharkey.put(charkey,k);
00081                      }
00082               }
00083        }
00084        
00088        public static KeyboardItem findMenu (String menu)
00089        {      Object o=Hmenu.get(menu);
00090               if (o==null) return null;
00091               else return (KeyboardItem)o;
00092        }
00093        
00097        public static String shortcut (String tag)
00098        {      Enumeration e=V.elements();
00099               while (e.hasMoreElements())
00100               {      KeyboardItem item=(KeyboardItem)e.nextElement();
00101                      if (item.getMenuString().equals(tag))
00102                      {      String shortcut=item.shortcut();
00103                             if (!shortcut.equals("")) shortcut=" ("+shortcut+")";
00104                             return shortcut;
00105                      }
00106               }
00107               return "";
00108        }
00109 
00114        public static String findKey (KeyEvent event, int type)
00115        {      Object o=Hcharkey.get(toCharKey(event,type));
00116               if (o==null) return "";
00117               String s=((KeyboardItem)o).getMenuString();
00118               while (s.endsWith("*")) s=s.substring(0,s.length()-1);
00119               return s;
00120        }
00121        
00125        public static String toCharKey (KeyEvent e, int type)
00126        {      String s="";
00127               if (type>0) s=s+"esc"+type+".";
00128               if (e.isShiftDown()) s=s+"shift.";
00129               if (e.isControlDown()) s=s+"control.";
00130               if (e.isAltDown()) s=s+"alt.";
00131               return s+KeyDictionary.translate(e.getKeyCode()).toLowerCase();
00132        }
00133        
00137        public static void edit (Frame f)
00138        {      KeyboardItem keys[]=new KeyboardItem[V.size()];
00139               V.copyInto(keys);
00140               Sorter.sort(keys);
00141               Vector v=new Vector();
00142               for (int i=0; i<keys.length; i++) v.addElement(keys[i]);
00143               KeyboardPanel p=new KeyboardPanel();
00144               ItemEditor d=new ItemEditor(f,p,v,"keyeditor",
00145                      Global.name("keyeditor.prompt"),true,false,true,
00146                      Global.name("keyeditor.clearall"));
00147               p.setItemEditor(d);
00148               p.makeCommandChoice();
00149               d.center(f);
00150               d.setVisible(true);
00151               if (d.isAborted()) return;
00152               Global.removeAllParameters("key.");
00153               V=d.getElements();
00154               Enumeration e=V.elements();
00155               while (e.hasMoreElements())
00156               {      KeyboardItem k=(KeyboardItem)e.nextElement();
00157                      if (!k.getCharKey().equals("default"))
00158                      {      String keytag="key."+k.getMenuString();
00159                             String description=k.keyDescription();
00160                             if (!Global.name(keytag).toLowerCase().equals(description))
00161                             {      Global.setParameter(keytag,description);
00162                             }
00163                      }
00164               }
00165               makeKeys();
00166               if (d.getAction()==ItemEditor.SAVE)
00167               {      Properties parameters=new Properties();
00168                      e=Global.properties();
00169                      while (e.hasMoreElements())
00170                      {      String key=(String)e.nextElement();
00171                             if (key.startsWith("key."))
00172                                    parameters.put(key,Global.getParameter(key,"default"));
00173                      }
00174                      MyFileDialog save=new MyFileDialog(f,Global.name("save"),
00175                             Global.name("save"),true);
00176                      save.setPattern("*.keys");
00177                      save.center(f);
00178                      save.update();
00179                      save.setVisible(true);
00180                      if (save.isAborted()) return;
00181                      String filename=save.getFilePath();
00182                      if (filename.equals("")) return; // aborted dialog!
00183                      try
00184                      {      FileOutputStream o=new FileOutputStream(filename);
00185                             parameters.save(o,"JE Keyboard Definition");
00186                      }
00187                      catch (Exception ex) {}
00188               }
00189               else if (d.getAction()==ItemEditor.LOAD)
00190               {      Properties parameters=new Properties();
00191                      MyFileDialog load=new MyFileDialog(f,Global.name("load"),
00192                             Global.name("load"),true);
00193                      load.setPattern("*.keys");
00194                      load.center(f);
00195                      load.update();
00196                      load.setVisible(true);
00197                      if (load.isAborted()) return;
00198                      String filename=load.getFilePath();
00199                      if (filename.equals("")) return; // aborted dialog!
00200                      try
00201                      {      FileInputStream in=new FileInputStream(filename);
00202                             parameters.load(in);
00203                      }
00204                      catch (Exception ex) {}
00205                      Global.removeAllParameters("key.");
00206                      e=parameters.keys();
00207                      while (e.hasMoreElements())
00208                      {      String key=(String)e.nextElement();
00209                             Global.setParameter(key,(String)parameters.get(key));
00210                      }
00211                      makeKeys();
00212               }
00213        }
00214        
00218        public static Vector getKeys ()
00219        {      Vector keys=new Vector();
00220               Sorter.sort(V);
00221               Enumeration e=V.elements();
00222               while (e.hasMoreElements())
00223               {      KeyboardItem k=(KeyboardItem)e.nextElement();
00224                      if (!k.getCharKey().equals("none"))
00225                      {      String shortcut=k.shortcut();
00226                             int n=shortcut.length();
00227                             for (int i=0; i<30-n; i++) shortcut=shortcut+" ";
00228                             keys.addElement(shortcut+" = "+k.getActionName());
00229                      }
00230               }
00231               return keys;
00232        }
00233        
00237        public static String commandShortcut (int type)
00238        {      Object o=Hmenu.get("command."+type);
00239               if (o==null) return "";
00240               return ((KeyboardItem)o).shortcut();
00241        }
00242 }