随笔 - 6, 文章 - 3, 评论 - 3, 引用 - 0
数据加载中……

使用workbenchkeyboard

IWorkbenchWindowConfigurer configurer = getWindowConfigurer();
Workbench workbench = (Workbench)configurer.getWindow().getWorkbench();
 
//        
WorkbenchKeyboard workbenchkeyboard = new WorkbenchKeyboard(workbench);        
workbenchkeyboard.openMultiKeyAssistShell();

当然workbench必需是被创建以后才可以

这样就可以show key assist出来啦~

<extension
         point="org.eclipse.ui.commands">
      <command
            categoryId="org.eclipse.ui.category.file"
            id="KeyAssist.test.save"
            name="Test Key Assist "/>
   </extension>
   <extension
         point="org.eclipse.ui.bindings">
      <key
            commandId="KeyAssist.test.save"
            contextId="org.eclipse.ui.contexts.dialogAndWindow"
            schemeId="org.eclipse.ui.defaultAcceleratorConfiguration"
            sequence="M1+Q"/>
   </extension>

定义多以上东东就可以显示自己的keyassit item啦。


终于研究出如何runtime modify keyassist了,记下这个util class

  1 package  com.hactl.eaf.ui.rich.composite.widget.keyassist;
  2
  3 import  java.io.IOException;
  4 import  java.util.HashMap;
  5 import  java.util.Iterator;
  6 import  java.util.Set;
  7
  8 import  org.eclipse.core.commands.Command;
  9 import  org.eclipse.core.commands.ParameterizedCommand;
 10 import  org.eclipse.jface.bindings.Binding;
 11 import  org.eclipse.jface.bindings.keys.KeyBinding;
 12 import  org.eclipse.jface.bindings.keys.KeySequence;
 13 import  org.eclipse.jface.bindings.keys.ParseException;
 14 import  org.eclipse.ui.IWorkbench;
 15 import  org.eclipse.ui.commands.ICommandService;
 16 import  org.eclipse.ui.internal.Workbench;
 17 import  org.eclipse.ui.internal.keys.WorkbenchKeyboard;
 18 import  org.eclipse.ui.keys.IBindingService;
 19
 20 /**
 21  * ShortCutKeyAssist Util
 22  * <p>
 23  * this class can modify KeyAssist in runtime
 24  * 
 25  *  @author  baal
 26  * 2006.05.17
 27   */

 28
 29 public   class  ShortCutKeyAssist  {
 30     
 31      // schemeid mapping plugin.xml
 32      private   final   static  String SCHEME_NAME  =   " com.hactl.eaf.ui.rich.application.defaultAcceleratorConfiguration " ;
 33
 34      // contextid mapping plugin.xml
 35      private   final   static  String CONTEXT_NAME  =   " org.eclipse.ui.contexts.dialogAndWindow " ;
 36     
 37      // categroyid mapping plugin.xml
 38      private   final   static  String CATEGORY_NAME  =   " RichClient.category " ;
 39     
 40      private  Workbench workbench;
 41
 42      private  ICommandService commandService;
 43
 44      private  IBindingService bindingService;
 45
 46      private  Binding[] oldbindings;
 47
 48      private  HashMap newbindings;
 49
 50      public  ShortCutKeyAssist(IWorkbench workbench)  {
 51
 52          this .workbench  =  (Workbench) workbench;
 53
 54         bindingService  =  (IBindingService) workbench
 55                 .getAdapter(IBindingService. class );
 56         commandService  =  (ICommandService) workbench
 57                 .getAdapter(ICommandService. class );
 58
 59         oldbindings  =  bindingService.getBindings();
 60
 61         newbindings  =   new  HashMap();
 62
 63     }

 64
 65      public  KeyBinding addShortKey(String commandId, String commandName,
 66             String keySequence, String description)  {
 67         KeySequence seq  =   null ;
 68         KeyBinding binding;
 69          final  Command command  =  commandService.getCommand(commandId);
 70
 71          if  ( ! command.isDefined())
 72             command.define(commandName, description, commandService
 73                     .getCategory(CATEGORY_NAME),  null );
 74
 75          try   {
 76             seq  =  KeySequence.getInstance(keySequence);
 77         }
  catch  (ParseException e1)  {
 78              //  TODO Auto-generated catch block
 79             e1.printStackTrace();
 80         }

 81
 82         binding  =   new  KeyBinding(seq,  new  ParameterizedCommand(command,  null ),
 83                 SCHEME_NAME, CONTEXT_NAME,  "" "" null , Binding.USER);
 84
 85         newbindings.put(keySequence,binding);
 86         System.out.println(newbindings.get(keySequence));
 87          return  binding;
 88     }

 89
 90      public   void  removeAllShortKey()  {
 91         newbindings.clear();
 92     }

 93
 94      public   void  removeShortKey(String keySequence)  {
 95         newbindings.remove(keySequence);
 96     }

 97
 98      public   void  OpenKeyAssist()  {
 99         recomputeBindings();
100         WorkbenchKeyboard workbenchkeyboard  =   new  WorkbenchKeyboard(
101                  this .workbench);
102         workbenchkeyboard.openMultiKeyAssistShell();
103     }

104
105      private   void  recomputeBindings()  {
106
107         Binding[] bindings  =   new  Binding[oldbindings.length
108                  +  newbindings.size()];
109         System.arraycopy(oldbindings,  0 , bindings,  0 , oldbindings.length);
110         
111         Set set  =  newbindings.keySet();
112         Iterator iter  =  set.iterator();
113                 
114          for  ( int  i  =  oldbindings.length; i  <  bindings.length; i ++ {
115              if  (iter.hasNext())
116             bindings[i]  =  (Binding) newbindings.get(iter.next());
117         }

118
119          try   {
120             bindingService.savePreferences(bindingService
121                     .getScheme(SCHEME_NAME), bindings);
122
123         }
  catch  (IOException e)  {
124              //  TODO Auto-generated catch block
125             e.printStackTrace();
126         }

127     }

128 }

129


关于command这个东西,也有点bt,自定义的command会记录在runtime data里,so 第一次run可以看到的item第二次就看不到。需要在run的选项里选中clear workspace data

posted on 2006-05-12 15:34 马甲丁 阅读(803) 评论(2)  编辑  收藏 所属分类: Eclipse

评论

# re: 使用workbenchkeyboard   回复  更多评论   

你的pda开发很牛吧
2006-05-18 11:53 | colin

# re: 使用workbenchkeyboard   回复  更多评论   

@colin
一般一般,才玩没几天。
colin? 是不是我认识哪个?
2006-05-19 09:07 | 马甲丁

只有注册用户登录后才能发表评论。


网站导航: