Label Provider的使用

來源:互聯網
上載者:User

一聲歎息!還是書接上文吧。

今天我們來討論一下TableViewer的Label Provider。

對於TableViewer的Label Provider來說,根據Eclipse源碼,只要是實現IBaseLabelProvider介面的類就可以,但是事實上TableViewer的Label Provider必須是實現ITableLabelProvider或者ILabelProvider介面的類,如果除了提供Label以外,我們還想定製表格的字型或者顏色的話,我們還需要實現ITableFontProvider和ITableColorProvider或者IColorProvider和IFontProvider;

那麼對於TreeViewer來說,根據Eclipse源碼,它和TableViewer是一樣的。這樣我們只要理解了TableViewer的Label Provider,那麼TreeViewer也就迎刃而解了,因為自訂的Label Provider必須是實現ITableLabelProvider或者ILabelProvider介面的類;

既然如此,這兩個介面到底有什麼區別呢?它們的區別就在於如果我們實現了ITableLabelProvider介面,那麼它將對每一列提供label text和映像而且在需要時要與ITableFontProvider和ITableColorProvider共同使用;如果我們實現了ILabelProvider介面,那麼它僅僅對第一列提供label text和映像,並且需要與IColorProvider和IFontProvider一起使用;

對於ListViewer來說,它就更簡單了,它的Label Provider只要實現ILabelProvider就可以了,當然為了處理字型和顏色,它還可以實現IColorProvider和IFontProvider;

 

那好,讓我們看看這幾個介面到底長的是什麼樣吧。

第一:ILabelProvider,這個介面提供了兩個函數:
1、public Image getImage(Object element);這個函數用來獲得第一列的映像;
2、public String getText(Object element);同理,這個函數用來獲得第一列的文字描述;

 

第二:ITableLabelProvider,這個介面也提供了兩個函數:
1、public Image getColumnImage(Object element, int columnIndex);這個函數用來獲得columnIndex所指的那一列,和element所指定的那一行所處位置的映像;
2、public String getColumnText(Object element, int columnIndex);這個不用說了吧;但是記住columnIndex是“zero-based”的!

 

第三:IColorProvider,這個介面也提供了兩個函數:
1、Color getForeground(Object element);這個不用說了吧;
2、Color getBackground(Object element);呵呵,還真一樣;

 

第四:ITableColorProvider,這個介面也提供了兩個函數:
1、Color getForeground(Object element, int columnIndex);對照上面的說明吧;
2、Color getBackground(Object element, int columnIndex);同上;

 

第五:IFontProvider,這個介面僅僅提供了一個函數:
1、public Font getFont(Object element);還用說嗎?

 

第六:ITableFontProvider,這個介面也提供了一個函數:
1、public Font getFont(Object element, int columnIndex);我就不說了吧;

 

再羅嗦一下:

Eclipse中三個比較重要的viewer在Label Provider方面事實上使用了相同的兩套東西,一套是帶Table的Label、Font和Color Provider,一套是不帶Table的;前者可以對不同的列做更細緻的處理,而後者只對第一列起作用;看個人愛好和需要使用吧!

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.