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

KeyAssist 整理

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

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

 57
 58    /**
 59     * 
 60     * @param commandId
 61     *         The map of command identifiers to commands must not be null
 62     * @param commandName
 63     *         Show name in the KeyAssist list must not be null
 64     * @param keySequence
 65     *         Short key String must not be null
 66     * @param description
 67     * @return
 68     */

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

 85
 86        binding = new KeyBinding(seq, new ParameterizedCommand(command, null),
 87                SCHEME_NAME, CONTEXT_NAME, """"null, Binding.USER);
 88
 89        newbindings.put(keySequence, binding);
 90        
 91        return binding;
 92    }

 93
 94    public void removeAllShortKey() {
 95        newbindings.clear();
 96    }

 97
 98    public void removeShortKey(String keySequence) {
 99        newbindings.remove(keySequence);
100    }

101
102    public void OpenKeyAssist() {
103        recomputeBindings();
104        WorkbenchKeyboard workbenchkeyboard = new WorkbenchKeyboard(
105                this.workbench);
106        workbenchkeyboard.openMultiKeyAssistShell();
107    }

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

122
123        try {
124            bindingService.savePreferences(bindingService
125                    .getScheme(SCHEME_NAME), bindings);
126
127        }
 catch (IOException e) {
128            // TODO Auto-generated catch block
129            e.printStackTrace();
130        }

131    }

132}

133

这里类就是核心部分了,用来操作keyassit,keyassit包括两部分的,一是在plugin.xml里定义的,和在runtime时动态加入的。
eclipse 已经提供了取得plugin.xml里keyassist item,的功能。需要实现的是runtime的功能。

典型的应用是在当前画面中搜索带快捷键的button,哇这个功能看上去有点bt,实际上也是很简单的。

  1package com.hactl.eaf.ui.rich.composite.widget.keyassist;
  2
  3import java.util.ArrayList;
  4import java.util.HashMap;
  5import java.util.Iterator;
  6import java.util.Set;
  7
  8import org.eclipse.swt.widgets.Button;
  9import org.eclipse.swt.widgets.Composite;
 10import org.eclipse.swt.widgets.Control;
 11import org.eclipse.swt.widgets.Group;
 12import org.eclipse.swt.widgets.TabFolder;
 13import org.eclipse.swt.widgets.TabItem;
 14
 15import com.hactl.eaf.ui.rich.composite.ReadOnlyListingS2Composite;
 16import com.hactl.eaf.ui.rich.composite.widget.HButton;
 17
 18public class WidgetParser {
 19    private HashMap widgets;
 20
 21    public WidgetParser() {
 22        widgets = new HashMap();
 23    }

 24
 25    public void parseWidget(Control control) {
 26        if (control == null{
 27            return;
 28        }

 29        if (control instanceof Composite) {
 30            Composite composite = (Composite) control;
 31            Control[] controls = composite.getChildren();
 32            for (int i = 0; i < controls.length; i++)
 33                parseWidget(controls[i]);
 34        }

 35        if (control instanceof ReadOnlyListingS2Composite) {
 36            ReadOnlyListingS2Composite s2composite = (ReadOnlyListingS2Composite) control;
 37            Control[] controls = s2composite.getChildren();
 38            for (int i = 0; i < controls.length; i++)
 39                parseWidget(controls[i]);
 40        }

 41        if (control instanceof Group) {
 42            Group group = (Group) control;
 43            Control[] controls = group.getChildren();
 44            for (int i = 0; i < controls.length; i++)
 45                parseWidget(controls[i]);
 46        }

 47        if (control instanceof TabFolder) {
 48            TabFolder tab = (TabFolder) control;
 49            TabItem[] controls = tab.getItems();
 50            for (int i = 0; i < controls.length; i++{
 51                widgets.put("folder." + controls[i].getText(), controls[i]
 52                        .getText());
 53            }

 54        }

 55        if (control instanceof HButton) {
 56            HButton button = (HButton) control;
 57            widgets.put("button." + button.getText(), button.getText());
 58        }

 59        if (control instanceof Button) {
 60            Button button = (Button) control;
 61            widgets.put("button." + button.getText(), button.getText());
 62        }

 63
 64    }

 65
 66    /**
 67     * 
 68     * @return shortkey in all of widget ,maybe has null value
 69     */

 70    public ArrayList getWidgetInfo() {
 71        if (widgets.size() == 0)
 72            return null;
 73        ArrayList list = new ArrayList();
 74        Set keyset = widgets.keySet();
 75        Iterator iter = keyset.iterator();
 76        String[] strings = new String[widgets.size()];
 77        while (iter.hasNext()) {
 78            WidgetInfo info = new WidgetInfo();
 79            String widgetkey = (String)iter.next();
 80            info.setCommandId(widgetkey);
 81            
 82            final String temp = (String)widgets.get(widgetkey);
 83            
 84            info.setText(getWidgetText(temp));
 85            
 86            int index = temp.indexOf("&");
 87            if (index >= 0{
 88                info.setShortkey( "M3+"
 89                        + temp.substring(index + 1, index + 2)
 90                                .toUpperCase());
 91            }
            
 92            list.add(info);
 93            
 94        }

 95        return list;
 96    }

 97
 98    /**
 99     * 
100     * @return delete '&' in the String
101     */

102    public String getWidgetText(String oldtext) {
103        if (oldtext == null)
104            return null;
105        String temp = oldtext;
106        int index = temp.indexOf("&");
107        if (index >= 0{
108            final String temp1 = temp.substring(0, index);
109            temp = temp1 + temp.substring(index + 1);
110        }

111        return temp;
112    }

113
114    public HashMap getWidgets() {
115        return widgets;
116    }

117}

118

这个类就是为了parser 介面而写的。利用递归可以很简单的遍历所以界面中的控件


 1package com.hactl.eaf.ui.rich.composite.widget.keyassist;
 2
 3public class WidgetInfo {
 4    private String commandId;
 5
 6    private String text;
 7
 8    private String shortkey;
 9
10    public String getCommandId() {
11        return commandId;
12    }

13
14    public void setCommandId(String commandId) {
15        this.commandId = commandId;
16    }

17
18    public String getShortkey() {
19        return shortkey;
20    }

21
22    public void setShortkey(String shortkey) {
23        this.shortkey = shortkey;
24    }

25
26    public String getText() {
27        return text;
28    }

29
30    public void setText(String text) {
31        this.text = text;
32    }

33
34}

35


最后的这个就是parser后返回的结果的类型。

posted on 2006-05-19 14:24 马甲丁 阅读(382) 评论(0)  编辑  收藏 所属分类: Eclipse JFace/SWT


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


网站导航: