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主要用于根據條件來顯示或隱藏Tree或Table中的數據,它的實現就不贅述了,在程序中用了一個下拉框來做觸發條件:

           
          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.         });  

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

          關于自定義選中Column的顏色則參照了Snippet229的代碼,監聽了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看上來似乎是風馬牛不相及的,但是在應用中卻出現了問題,會出現一大堆空指針異常,而且Debug根本跟 蹤不過去,最開始以為是Filter實現的問題,但做了很長時間的調試都沒有發現問題所在,過了4、5個小時以后,才想到也許和EraseItem事件有 關系,然后把上面的代碼注釋掉,果然就一點問題沒有了.......

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


           
          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 閱讀(735) 評論(0)  編輯  收藏 所屬分類: Eclipse RCP

          導航

          統計

          常用鏈接

          留言簿(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
          主站蜘蛛池模板: 杭锦后旗| 饶平县| 桐庐县| 师宗县| 昌邑市| 嘉黎县| 七台河市| 徐水县| 瑞丽市| 资阳市| 札达县| 洱源县| 汕头市| 溧阳市| 桐庐县| 新宾| 威信县| 临邑县| 讷河市| 阆中市| 龙江县| 墨玉县| 贵港市| 伊春市| 贵定县| 苏尼特右旗| 平阳县| 施秉县| 城市| 无棣县| 会理县| 南投县| 辽中县| 横峰县| 鄂州市| 阳山县| 黑水县| 陆河县| 随州市| 沅江市| 芜湖市|