邋遢居

          我的Java天堂

            BlogJava :: 首頁(yè) :: 聯(lián)系 :: 聚合  :: 管理
            61 Posts :: 2 Stories :: 90 Comments :: 0 Trackbacks

          大家肯定都知道在Eclipse中獲得當(dāng)前活動(dòng)的workbenchWindow可以采用如下的方式來(lái)完成。

          PlatformUI.getWorkbench().getActiveWorkbenchWindow()

          Plugin類中) getDefault().getWorkbench().getActiveWorkbenchWindow() 。一般情況下這兩個(gè)方式能夠很好的工作。但是突然有一天發(fā)現(xiàn)他不能工作了。郁悶我都不行了。察看了一下變量如下圖。display.jpg

          這個(gè)activatedWindow明明有啊,怎么就取不到呢。

          打開代碼一看,看到 org.eclipse.ui.internal.Workbench 類中的 getActiveWorkbenchWindow 方法。

          public ?IWorkbenchWindow?getActiveWorkbenchWindow()?{
          ????????
          // ?Return?null?if?called?from?a?non-UI?thread.
          ????????
          // ?This?is?not?spec'ed?behaviour?and?is?misleading,?however?this?is?how
          ????????
          // ?it
          ????????
          // ?worked?in?2.1?and?we?cannot?change?it?now.
          ????????
          // ?For?more?details,?see?[Bug?57384]?[RCP]?Main?window?not?active?on
          ????????
          // ?startup
          ???????? if ?(Display.getCurrent()? == ? null )?{
          ????????????
          return ? null ;
          ????????}

          ????????
          // ?Look?at?the?current?shell?and?up?its?parent
          ????????
          // ?hierarchy?for?a?workbench?window.
          ????????Control?shell? = ?display.getActiveShell();
          ????????
          while ?(shell? != ? null )?{
          ????????????Object?data?
          = ?shell.getData();
          ????????????
          if ?(data? instanceof ?IWorkbenchWindow)?{
          ????????????????
          return ?(IWorkbenchWindow)?data;
          ????????????}
          ????????????shell?
          = ?shell.getParent();
          ????????}

          ????????
          // ?Look?for?the?window?that?was?last?known?being
          ????????
          // ?the?active?one
          ????????WorkbenchWindow?win? = ?getActivatedWindow();
          ????????
          if ?(win? != ? null )?{
          ????????????
          return ?win;
          ????????}

          ????????
          // ?Look?at?all?the?shells?and?pick?the?first?one
          ????????
          // ?that?is?a?workbench?window.
          ????????Shell?shells[]? = ?display.getShells();
          ????????
          for ?( int ?i? = ? 0 ;?i? < ?shells.length;?i ++ )?{
          ????????????Object?data?
          = ?shells[i].getData();
          ????????????
          if ?(data? instanceof ?IWorkbenchWindow)?{
          ????????????????
          return ?(IWorkbenchWindow)?data;
          ????????????}
          ????????}

          ????????
          // ?Can't?find?anything!
          ???????? return ? null ;
          ????}

          程序跑到 Display.getCurrent() 這一絕句就給我掉鏈子(返回了一個(gè) null )。就是他,就是他了!到網(wǎng)上 google 了一下。看看在什么狀況下他會(huì)返回一個(gè) null. http://wiki.eclipse.org/index.php/FAQ_How_do_I_get_a_Display_instance%3F 找到了。

          他說(shuō), Display 永遠(yuǎn)和創(chuàng)建他的線程聯(lián)系在一起,并且一個(gè)線程擁有一個(gè)活動(dòng)的 Display 。在創(chuàng)立 Display 的線程中可可以通過(guò) Display.getCurrent() 獲得當(dāng)前活動(dòng)的 Display 。如果出了這個(gè)線程就看不到這個(gè) Display 了。如果有的線程不擁有 Display ,那它的 Display.getCurrent() 就只能得到 null 了。

          回頭看了一下我的代碼,我是在新建的線程中啟動(dòng)了調(diào)用了這個(gè)方法。難怪他不給面子呢!


          參考文獻(xiàn):

          http://wiki.eclipse.org/index.php/FAQ_How_do_I_get_a_Display_instance%3F

          http://help.eclipse.org/help31/index.jsp?topic=/org.eclipse.platform.doc.isv/reference/api/org/eclipse/swt/widgets/Display.html

          posted on 2006-12-04 15:01 Jet Geng 閱讀(2684) 評(píng)論(0)  編輯  收藏 所屬分類: Eclipse
          主站蜘蛛池模板: 罗城| 临城县| 南乐县| 喀喇沁旗| 澄江县| 兴安盟| 息烽县| 囊谦县| 公安县| 北京市| 商南县| 仪征市| 嘉鱼县| 贞丰县| 玉田县| 正定县| 托克托县| 辉南县| 永安市| 武川县| 上虞市| 库尔勒市| 阿克陶县| 东兰县| 清徐县| 雅安市| 岢岚县| 贵州省| 乌拉特中旗| 绥江县| 湖州市| 三穗县| 双城市| 梅州市| 鄂伦春自治旗| 栾川县| 宣化县| 民和| 曲沃县| 佛学| 南投市|