在GEF中實現帶content assist的Directedit

          GEF中自帶有Directeditrequest,所以實現Directedit還是比較容易的,八進制的gef例子里面就有實現.但我在給directedit加上content assist的時候卻發現由一個小bug不太好弄,費了兩天才搞定,現在先記下來,以供參考
            directedit是通過一個text celleditor來實現編輯功能的,所以可以在directeditmanager類里面的initCellEditor方法里面加上ContentAssistHandler來實現auto complete.但是加上去之后卻發現有一個問題:不支持用鼠標來選擇proposal.只能用鍵盤上的上下箭頭來選擇.雖然也可以用,但是終究不是那么的人性化.
            為了修復這個bug,走了不少的彎路,一開始以為是contentassist的問題,因為它是deprecated,所以換了3.3里面的assist api,發現還是不行.后來才知道是因為celleditor有一個focus listener,當用戶點擊proposals 來選擇一行的時候,celleditor的focus就lost了,就會調用focusLost方法,導致directedit編輯失敗.所以我重寫了celleditor的focusLost方法,把它設成當focus在contentassist的popup dialog就什么都不干,否則調用父類的focusLost方法.理論上是一個好的解決方法,但是contentassist的hasPopupFocus居然一直都返回false,這個方法也失敗了.
             最后,在bug.eclipse.org上面有人提到GMF里面的TextDirectEditManager是可以做到用鼠標選擇proposal的,于是又去看gmf的這個類,它也是繼承自DirectEditManager,不過它消除這個bug不是在listener上作文章,而是在commit方法里面,在這個方法里面判斷popup dialog是否是active的,如果是的話則給celleditor加上deactive lock,不允許它deactive,這樣來實現用鼠標選擇proposal.
          下面是TextDirectEditManager的方法commit里面的部分代碼:

          Shell activeShell = Display.getCurrent().getActiveShell();
                  if (activeShell != null
                      && getCellEditor().getControl().getShell().equals(
                          activeShell.getParent())) {
                      Control[] children = activeShell.getChildren();
                      if (children.length == 1 && children[0] instanceof Table) {
                          /*
                           * CONTENT ASSIST: focus is lost to the content assist pop up -
                           * stay in focus
                           */
                          getCellEditor().getControl().setVisible(true);
                          ((MyTextCellEditor) getCellEditor()).setDeactivationLock(true);
                          return;
                      }
                  }

            下面是MyTextCellEditor里面對于deactive lock的應用,MyTextCellEditor的deactive之前會判斷一下deactive lock是否為true:
          public boolean isDeactivationLocked() {
            return deactivationLock;
           }
           public void deactivate() {
            if (! isDeactivationLocked())
             super.deactivate();
            setDeactivationLock(false);
           }
           

           public void setDeactivationLock(boolean deactivationLock) {
            this.deactivationLock = deactivationLock;
           }

          posted on 2008-08-05 17:09 小牛小蝦 閱讀(1120) 評論(1)  編輯  收藏

          評論

          # re: 在GEF中實現帶content assist的Directedit[未登錄] 2008-08-06 08:56 寒武紀

          不錯!  回復  更多評論   


          只有注冊用戶登錄后才能發表評論。


          網站導航:
           
          <2008年8月>
          272829303112
          3456789
          10111213141516
          17181920212223
          24252627282930
          31123456

          導航

          統計

          常用鏈接

          留言簿(6)

          隨筆檔案

          文章檔案

          eclipse

          搜索

          最新評論

          閱讀排行榜

          評論排行榜

          主站蜘蛛池模板: 南澳县| 邓州市| 治县。| 高雄市| 开鲁县| 博白县| 陈巴尔虎旗| 阿巴嘎旗| 涪陵区| 灵武市| 星座| 灯塔市| 香河县| 土默特左旗| 威信县| 衡东县| 诸城市| 东阳市| 固始县| 侯马市| 定州市| 仙桃市| 松江区| 峨边| 青阳县| 伊宁县| 枣强县| 龙门县| 隆德县| 辽中县| 新民市| 如皋市| 莆田市| 衡东县| 绥宁县| 黎川县| 穆棱市| 睢宁县| 克拉玛依市| 乌拉特前旗| 平顺县|