Back to index

wims  3.65+svn20090927
KeyboardItem.java
Go to the documentation of this file.
00001 package rene.gui;
00002 
00003 import java.util.StringTokenizer;
00004 
00005 import rene.dialogs.ItemEditorElement;
00006 import rene.util.sort.SortObject;
00007 
00014 public class KeyboardItem
00015        implements ItemEditorElement, SortObject
00016 {      boolean Shift,Control,Alt;
00017        String CharKey;
00018        String MenuString,ActionName;
00019        int CommandType=0;
00020 
00024        public KeyboardItem (KeyboardItem item)
00025        {      Shift=item.Shift; Control=item.Control; Alt=item.Alt;
00026               CharKey=item.CharKey; MenuString=item.MenuString;
00027               ActionName=item.ActionName; CommandType=item.CommandType;
00028        }
00029 
00037        public KeyboardItem (String charkey, String menustring, String actionname,
00038               boolean shift, boolean control, boolean alt, int commandtype)
00039        {      Shift=shift; Control=control; Alt=alt;
00040               CharKey=charkey.toLowerCase();
00041               MenuString=menustring; ActionName=actionname;
00042               CommandType=commandtype;
00043        }
00044 
00049        public KeyboardItem (String menu, String key)
00050        {      MenuString=menu;
00051               Shift=Control=Alt=false;
00052               CommandType=0;
00053               CharKey="";
00054               StringTokenizer t=new StringTokenizer(key,".");
00055               while (t.hasMoreTokens())
00056               {      String token=(String)t.nextToken();
00057                      if (t.hasMoreTokens())
00058                      {      if (token.equals("control")) Control=true;
00059                             else if (token.equals("shift")) Shift=true;
00060                             else if (token.equals("alt")) Alt=true;
00061                             else if (token.startsWith("esc"))
00062                                    // esc should be followed by a number
00063                             {      try
00064                                    {      CommandType=Integer.parseInt(token.substring(3));
00065                                    }
00066                                    catch (Exception e) {}
00067                             }
00068                             else return;
00069                      }
00070                      else
00071                      {      if (key.equals("")) return;
00072                             CharKey=token.toLowerCase();
00073                      }
00074               }
00075               ActionName=Global.name(getStrippedMenuString());
00076        }
00077 
00078        public String getMenuString () { return MenuString; }
00079        public String getActionName () { return ActionName; }
00080        public String getCharKey () { return CharKey; }
00081        public boolean isShift () { return Shift; }
00082        public boolean isControl () { return Control; }
00083        public boolean isAlt () { return Alt; }
00084        public int getCommandType () { return CommandType; }
00085 
00089        public String getStrippedMenuString ()
00090        {      String s=MenuString;
00091               while (s.endsWith("*")) s=s.substring(0,s.length()-1);
00092               return s;
00093        }
00094 
00099        public String shortcut ()
00100        {      if (CharKey.equals("none")) return "";
00101               String s=CharKey.toUpperCase();
00102               if (Alt) s=Global.name("shortcut.alt")+" "+s;
00103               if (Control) s=Global.name("shortcut.control")+" "+s;
00104               if (Shift) s=Global.name("shortcut.shift")+" "+s;
00105               if (CommandType>0)
00106                      s=Keyboard.commandShortcut(CommandType)+" "+s;
00107               return s;
00108        }
00109 
00113        public String getName ()
00114        {      return MenuString;
00115        }
00116        
00120        public int compare (SortObject o)
00121        {      return getName().compareTo(((KeyboardItem)o).getName());
00122        }
00123        
00128        public String keyDescription ()
00129        {      String s=CharKey.toLowerCase();
00130               if (s.equals("none") || s.equals("default")) return s;
00131               if (Alt) s="alt."+s;
00132               if (Control) s="control."+s;
00133               if (Shift) s="shift."+s;
00134               if (CommandType>0) s="esc"+CommandType+"."+s;
00135               return s;
00136        }
00137 }