Implementation of SWT multi-choice tree

Source: Internet
Author: User

 

Package cn.com. test;

IMPORT org. Eclipse. SWT. Widgets. display;
IMPORT org. Eclipse. SWT. Widgets. shell;
IMPORT org. Eclipse. SWT. Widgets. composite;
IMPORT org. Eclipse. SWT. SWT;
IMPORT org. Eclipse. SWT. Widgets. tree;
IMPORT org. Eclipse. SWT. Widgets. treeitem;
IMPORT org. Eclipse. SWT. Events. selectionadapter;
IMPORT org. Eclipse. SWT. Events. selectionevent;

Public class mainapp {

Protected shell;

/**
* Launch the application.
* @ Param ARGs
*/
Public static void main (string [] ARGs ){
Try {
Mainapp window = new mainapp ();
Window. open ();
} Catch (exception e ){
E. printstacktrace ();
}
}

/**
* Open the window.
*/
Public void open (){
Display display = display. getdefault ();
Createcontents ();
Shell. open ();
Shell. layout ();
While (! Shell. isdisposed ()){
If (! Display. readanddispatch ()){
Display. Sleep ();
}
}
}

/**
* Create contents of the window.
*/
Protected void createcontents (){
Shell = new shell ();
Shell. setsize (450,300 );
Shell. settext ("SWT application ");
{
Composite composite = new composite (shell, SWT. None );
Composite. setbounds (0, 0,434,262 );
{
Final TREE tree = new tree (composite, SWT. Border | SWT. Check | SWT. full_selection | SWT. Virtual | SWT. multi );

Tree. setbounds (0, 0,189,252 );
Final treeitem = new treeitem (tree, SWT. None );
// Treeitem. setchecked (true );
Treeitem. settext ("\ u5168 \ u90e8 ");
Final treeitem treeitem_2 = new treeitem (tree, SWT. None );
Treeitem_2.setchecked (false );
Treeitem_2.settext ("\ u5168 \ u90e82 ");
Final treeitem treeitem1 = new treeitem (treeitem, SWT. None );

Treeitem1.settext ("New treeitem1 ");
Treeitem1.setdata ("value", "You are my Rose ");
Final treeitem treeitem2 = new treeitem (treeitem, SWT. None );

Treeitem2.settext ("New treeitem2 ");
Final treeitem treeitem3 = new treeitem (treeitem, SWT. None );
Treeitem3.settext ("New treeitem3 ");
Final treeitem treeitem4 = new treeitem (treeitem, SWT. None );
Treeitem4.settext ("New treeitem4 ");
Treeitem. setexpanded (true );
Final treeitem treeitem2_1 = new treeitem (treeitem_2, SWT. None );

Treeitem2_1.settext ("New TreeItem2-1 ");
Final treeitem treeitem2_2 = new treeitem (treeitem_2, SWT. None );

Treeitem2_2.settext ("New TreeItem2-2 ");
Final treeitem treeitem2_3 = new treeitem (treeitem_2, SWT. None );
Treeitem2_3.settext ("New TreeItem2-3 ");
Final treeitem treeitem2_4 = new treeitem (treeitem_2, SWT. None );
Treeitem2_4.settext ("New TreeItem2-4 ");
Treeitem_2.setexpanded (true );
Final treeitem treeitem1_2_1 = new treeitem (treeitem2, SWT. None );

Treeitem1_2_1.settext ("New TreeItem2-1 ");
Final treeitem treeitem1_2_2 = new treeitem (treeitem2, SWT. None );

Treeitem1_2_2.settext ("New TreeItem2-2 ");
Final treeitem treeitem1_2_3 = new treeitem (treeitem2, SWT. None );

Treeitem1_2_3.settext ("New TreeItem2-3 ");
Final treeitem treeitem1_2_4 = new treeitem (treeitem2, SWT. None );

Treeitem1_2_4.settext ("New TreeItem2-4 ");
Treeitem2.setexpanded (true );
Tree. addselectionlistener (New selectionadapter (){
// Multiple selection boxes of the parent directory for iterative Selection
Private void selectallsubitems (treeitem selectedtreeitem ){
Treeitem [] selection = selectedtreeitem. getitems ();
For (INT I = 0; I <selection. length; I ++ ){

Selection [I]. setchecked (selectedtreeitem. getchecked ());

Selectallsubitems (Selection [I]);
}
}
Private void selectallparentitems (treeitem selectedtreeitem ){

Treeitem parenttreeitem = selectedtreeitem. getparentitem ();

// When a parent node is not selected, cancel the selection of the parent node. When all are selected, the parent node is selected.
If (parenttreeitem! = NULL ){
Treeitem [] parentsubitems = parenttreeitem. getitems ();

Boolean allchecked = true;
For (INT I = 0; I <parentsubitems. length; I ++ ){
Allchecked = parentsubitems [I]. getchecked ();
If (! Allchecked ){
Break;
}
}
Parenttreeitem. setchecked (allchecked );
Selectallparentitems (parenttreeitem );
}
}
@ Override
Public void widgetselected (selectionevent e ){
If (E. Detail = SWT. Check ){
Treeitem selectedtreeitem = (treeitem) E. item;
String value = (string) selectedtreeitem. getdata ("value ");

System. Out. println (value );
// If a parent node exists, select the parent node
Selectallparentitems (selectedtreeitem );
// If a subnode exists, select all the subnodes.
Selectallsubitems (selectedtreeitem );
} Else {
System. Out. println ("Other events ");
}
}
});
}
}

}
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.