gembin

          OSGi, Eclipse Equinox, ECF, Virgo, Gemini, Apache Felix, Karaf, Aires, Camel, Eclipse RCP

          HBase, Hadoop, ZooKeeper, Cassandra

          Flex4, AS3, Swiz framework, GraniteDS, BlazeDS etc.

          There is nothing that software can't fix. Unfortunately, there is also nothing that software can't completely fuck up. That gap is called talent.

          About Me

           

          ViewerFilter與自定義選中Column的顏色

          ViewerFilter主要用于根據(jù)條件來顯示或隱藏Tree或Table中的數(shù)據(jù),它的實現(xiàn)就不贅述了,在程序中用了一個下拉框來做觸發(fā)條件:

           
          1. filterCombo.addSelectionListener(new SelectionAdapter() {  
          2.             @Override  
          3.             public void widgetSelected(SelectionEvent e) {  
          4.                 String text = filterCombo.getText();  
          5.                 if (!text.equals(EMPTY_FILLTER_STRING)) {  
          6.                     viewer.resetFilters();  
          7.                     viewerFilter.setFilterData(text);  
          8.                     viewer.addFilter(viewerFilter);  
          9.                 } else {  
          10.                     viewer.resetFilters();  
          11.                 }  
          12.             }  
          13.         });  

          從代碼中可以看到,當選中的條件為空字符串時——表明不過濾結(jié)果——就調(diào)用viewer.resetFilters()方法來去掉Filter,如果選中 條件不為空,就首先將現(xiàn)有的Filter清空,然后將把輸入值作為過濾條件賦給Filter,再將Filter添加給viewer.

          關(guān)于自定義選中Column的顏色則參照了Snippet229的代碼,監(jiān)聽了EraseItem的事件:


           
          1. protected void setSelectedRowColor() {  
          2.         table.addListener(SWT.EraseItem, colorListener);  
          3.     }  
          4.   
          5.     private class RowColorListener implements Listener {  
          6.         public void handleEvent(Event event) {  
          7.   
          8.             if ((event.detail & SWT.SELECTED) != 0) {  
          9.                 GC gc = event.gc;  
          10.                 Rectangle area = table.getClientArea();  
          11.                 /* 
          12.                  * If you wish to paint the selection beyond the end of last 
          13.                  * column, you must change the clipping region. 
          14.                  */  
          15.                 int columnCount = table.getColumnCount();  
          16.                 if (event.index == columnCount - 1 || columnCount == 0) {  
          17.                     int width = area.x + area.width - event.x;  
          18.                     if (width > 0) {  
          19.                         Region region = new Region();  
          20.                         gc.getClipping(region);  
          21.                         region.add(event.x, event.y, width, event.height);  
          22.                         gc.setClipping(region);  
          23.                         region.dispose();  
          24.                     }  
          25.                 }  
          26.                 gc.setAdvanced(true);  
          27.                 if (gc.getAdvanced())  
          28.                     gc.setAlpha(127);  
          29.                 Rectangle rect = event.getBounds();  
          30.                 Color foreground = gc.getForeground();  
          31.                 Color background = gc.getBackground();  
          32.                 gc.setForeground(tabComposite.getDisplay().getSystemColor(  
          33.                         SWT.COLOR_RED));  
          34.                 gc.setBackground(tabComposite.getDisplay().getSystemColor(  
          35.                         SWT.COLOR_LIST_BACKGROUND));  
          36.                 gc.fillGradientRectangle(0, rect.y, 1024, rect.height, false);  
          37.                 // restore colors for subsequent drawing  
          38.                 gc.setForeground(foreground);  
          39.                 gc.setBackground(background);  
          40.                 event.detail &= ~SWT.SELECTED;  
          41.             }  
          42.         }  
          43.     }  


          該Snippet的URL為:http://dev.eclipse.org/viewcvs/index.cgi /org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet229.java?view=co

          自定義顏色的目的是為了個性化,它與Filter看上來似乎是風馬牛不相及的,但是在應用中卻出現(xiàn)了問題,會出現(xiàn)一大堆空指針異常,而且Debug根本跟 蹤不過去,最開始以為是Filter實現(xiàn)的問題,但做了很長時間的調(diào)試都沒有發(fā)現(xiàn)問題所在,過了4、5個小時以后,才想到也許和EraseItem事件有 關(guān)系,然后把上面的代碼注釋掉,果然就一點問題沒有了.......

          又過了好久,才在同事的提示下,監(jiān)測一下在EraseItem事件被觸發(fā)的時候,F(xiàn)ilter是否完成了對數(shù)據(jù)的過濾,赫然發(fā)現(xiàn)在過濾以后 TableItem本來應該只有兩個的,但是在RowColorListener的handleEvent方法中table.getItemCount的 結(jié)果卻是三,也就是在Filter的過濾還未結(jié)束的時候,handleEvent已經(jīng)被觸發(fā)了......這樣子到最后自然會有異常產(chǎn)生。問題的來源找到 了,解法也隨即而生,在輸入條件發(fā)生改變的時候,先把RowColorListener remove掉,當Filter完成以后再把RowColorListener添加給Table,異常自然也就不會發(fā)生了。修改后的代碼如下:


           
          1. filterCombo.addSelectionListener(new SelectionAdapter() {  
          2.     @Override  
          3.     public void widgetSelected(SelectionEvent e) {  
          4.         String text = filterCombo.getText();  
          5.         if (!text.equals(EMPTY_FILLTER_STRING)) {  
          6.             table.removeListener(SWT.EraseItem, colorListener);  
          7.             viewer.resetFilters();  
          8.             viewerFilter.setFilterData(text);  
          9.             viewer.addFilter(viewerFilter);  
          10.             table.addListener(SWT.EraseItem, colorListener);  
          11.         } else {  
          12.             viewer.resetFilters();  
          13.         }  
          14.     }  
          15. }); 

          posted on 2008-04-15 14:05 gembin 閱讀(724) 評論(0)  編輯  收藏 所屬分類: Eclipse RCP

          導航

          統(tǒng)計

          常用鏈接

          留言簿(6)

          隨筆分類(440)

          隨筆檔案(378)

          文章檔案(6)

          新聞檔案(1)

          相冊

          收藏夾(9)

          Adobe

          Android

          AS3

          Blog-Links

          Build

          Design Pattern

          Eclipse

          Favorite Links

          Flickr

          Game Dev

          HBase

          Identity Management

          IT resources

          JEE

          Language

          OpenID

          OSGi

          SOA

          Version Control

          最新隨筆

          搜索

          積分與排名

          最新評論

          閱讀排行榜

          評論排行榜

          free counters
          主站蜘蛛池模板: 上饶县| 田阳县| 福清市| 安仁县| 洛阳市| 双桥区| 深圳市| 昭通市| 巫溪县| 江源县| 子洲县| 渭南市| 阳春市| 涿鹿县| 聂荣县| 德惠市| 绥阳县| 陇西县| 平顶山市| 集安市| 章丘市| 灵寿县| 桦川县| 浦县| 黔西| 黄骅市| 扬州市| 汉寿县| 康马县| 库车县| 本溪| 合阳县| 宾川县| 克拉玛依市| 夏邑县| 松江区| 闽清县| 恭城| 新野县| 东辽县| 德阳市|