在使用Eclipse Forms Editor對(duì)EMF model進(jìn)行編輯的時(shí)候,由于editor的contributor是繼承EditingDomainActionBarContributor,因此Cut, Copy,Paste都是使用EMF的Cut,Copy和Paste,這樣在Editor中進(jìn)行的所有操作都將轉(zhuǎn)換為對(duì)EMF model的操作,對(duì)于Viewer上的節(jié)點(diǎn)來說當(dāng)然沒有什么問題,但是如果是使用Text來進(jìn)行文本編輯的時(shí)候,我們希望執(zhí)行的是Text中文本的Cut,Copy和Paste,這里就需要將EMF的Action重定向到系統(tǒng)的Global Action,開始試了各種辦法,都沒有成功,后來google了一下,發(fā)現(xiàn)不僅我碰到了這個(gè)問題,還有很多人也碰到了同樣的問題:http://dev.eclipse.org/newslists/news.eclipse.platform/msg53441.html, 同時(shí)也找到了相關(guān)的解決辦法:http://www.eclipsezone.com/eclipse/forums/t62058.rhtml
不過我按照這個(gè)上面的做法稍微調(diào)整了一下圓滿解決了我的問題。
從上面的解決方法中我們發(fā)現(xiàn)了TextActionHandler這樣一個(gè)類,它就是用來將ActionBarContributor定義的各種action在遇到指定的text時(shí)重定向到系統(tǒng)的global action。用法分四部分:
第一部分就是在初始化ActionBarContributor的時(shí)候創(chuàng)建該對(duì)象:
- textActionHandler = new TextActionHandler(actionBars);
緊接著就是將ActionBarContributor中創(chuàng)建的各種action先保存在textActionHandler,這樣在由TextActionHandler的global action撤回的時(shí)候能找到對(duì)應(yīng)的各種action:
- textActionHandler.setCopyAction(copyAction);
- textActionHandler.setCutAction(cutAction);
- textActionHandler.setDeleteAction(deleteAction);
- textActionHandler.setPasteAction(pasteAction);
告訴TextActionHandler哪個(gè)Text的action需要進(jìn)行重定向,即那個(gè)Text Widget獲得焦點(diǎn)的時(shí)候:
- textActionHandler.addText(text);
告訴TextActionHandler哪個(gè)Text的action需要撤回,即那個(gè)Text Widget失去焦點(diǎn)的時(shí)候:
- textActionHandler.removeText(text);
安徽新華電腦學(xué)校專業(yè)職業(yè)規(guī)劃師為你提供更多幫助【在線咨詢】