JAVA. SWT/JFace: SWT advanced control tree

Source: Internet
Author: User

Package com. wjc. tree;
 
Import org. eclipse. swt. SWT;
Import org. eclipse. swt. custom. TreeEditor;
Import org. eclipse. swt. events. ModifyEvent;
Import org. eclipse. swt. events. ModifyListener;
Import org. eclipse. swt. events. SelectionAdapter;
Import org. eclipse. swt. events. SelectionEvent;
Import org. eclipse. swt. events. TreeEvent;
Import org. eclipse. swt. events. TreeListener;
Import org. eclipse. swt. layout. FillLayout;
Import org. eclipse. swt. widgets. Control;
Import org. eclipse. swt. widgets. Display;
Import org. eclipse. swt. widgets. Shell;
Import org. eclipse. swt. widgets. Text;
Import org. eclipse. swt. widgets. Tree;
Import org. eclipse. swt. widgets. TreeColumn;
Import org. eclipse. swt. widgets. TreeItem;
 
Public class TreeSample {
Public static void main (String [] args ){
 
Final Display display = new Display ();
Final Shell shell = new Shell (display );
Shell. setText ("Tree Sample ");
Shell. setLayout (new FillLayout ());
// Create a tree object
Final Tree tree = new Tree (shell, SWT. BORDER | SWT. SINGLE );
// Create a root node of the tree
TreeItem root = new TreeItem (tree, SWT. NULL );
Root. setText ("root Node ");
// Create a child node
TreeItem child1 = new TreeItem (root, SWT. NULL );
Child1.setText ("Descendant 1 ");
TreeItem child2 = new TreeItem (root, SWT. NULL );
Child2.setText ("Descendant 2 ");
TreeItem child3 = new TreeItem (root, SWT. NULL );
Child3.setText ("Descendant 3 ");
 
TreeItem child11 = new TreeItem (child1, SWT. NULL );
Child11.setText ("Descendant 11 ");
TreeItem child12 = new TreeItem (child1, SWT. NULL );
Child12.setText (" 12 ");
 
TreeItem child111 = new TreeItem (child11, SWT. NULL );
Child111.setText (" 111 ");
TreeItem child112 = new TreeItem (child11, SWT. NULL );
Child112.setText (" 112 ");
// Call the convertImage method to set the tree icon
ConvertImage (tree );
// Register the tree for the event
Tree. addTreeListener (new TreeListener (){
// When Tree nodes are folded
Public void treeCollapsed (TreeEvent e ){
// Obtain the trigger event's TreeItem
TreeItem item = (TreeItem) e. item;
// Set the icon of the node to disabled.
// Item. setImage (ImageFactory. loadImage (tree. getDisplay (),
// ImageFactory. TOC_CLOSED ));
}
 
// When the tree node is expanded
Public void treeExpanded (TreeEvent e ){
TreeItem item = (TreeItem) e. item;
// Item. setImage (ImageFactory. loadImage (tree. getDisplay (),
// ImageFactory. TOC_OPEN ));
}
 
});
///************************************* *******************
// Create an editable TreeEditor object
Final TreeEditor editor = new TreeEditor (tree );
Editor. horizontalAlignment = SWT. LEFT;
Editor. grabHorizontal = true;
Editor. minimumWidth = 30;
// Register selected events
Tree. addSelectionListener (new SelectionAdapter (){
// Edit the node when you double-click the node.
Public void widgetdefaselecselected (SelectionEvent e ){
// Release the previously edited Control
Control oldEditor = editor. getEditor ();
If (oldEditor! = Null)
OldEditor. dispose ();
// Obtain the trigger event's TreeItem. If it is null, return
TreeItem item = (TreeItem) e. item;
If (item = null)
Return;
// Create a text box as the text entered when editing the node
Text newEditor = new Text (tree, SWT. NONE );
// Assign the value of the Tree node to the text box
NewEditor. setText (item. getText ());
// When the value of the text box changes, the value of your tree node data should also be
NewEditor. addModifyListener (new ModifyListener (){
Public void modifyText (ModifyEvent e ){
Text text = (Text) editor. getEditor ();
Editor. getItem (). setText (text. getText ());
}
});
NewEditor. selectAll (); // select all text boxes
NewEditor. setFocus (); // and set the focus to the text box
// Bind the tree node to the text box Node
Editor. setEditor (newEditor, item );
}
 
});
//************************************** **************************/
///************************************* **************************
// Create 5 columns for the tree
For (int I = 0; I <5; I ++ ){
TreeColumn column = new TreeColumn (tree, SWT. NONE );
Column. setText ("column" + I );
}
For (int I = 0; I <tree. getColumnCount (); I ++)
Tree. getColumn (I). pack ();
// Set the gridline visibility
Tree. setLinesVisible (true );
// Set the visible Header
Tree. setHeaderVisible (true );
//************************************** *************************/
Shell. setSize (200,150 );
Shell. open ();
While (! Shell. isDisposed ()){
If (! Display. readAndDispatch ())
Display. sleep ();
}
// ImageFactory. dispose ();
Display. dispose ();
}
 
// Method for setting a tree chart
Public static void convertImage (Tree tree ){
// Assume that there is only one root node.
TreeItem [] items = tree. getItems ();
// First, set the icon Based on the root node status
// If (items [0]. getExpanded () // if the node is in the expanded State
// Items [0]. setImage (ImageFactory. loadImage (tree. getDisplay (),
// ImageFactory. TOC_OPEN ));
// Else
//// Otherwise, if it is in the folding state
// Items [0]. setImage (ImageFactory. loadImage (tree. getDisplay (),
// ImageFactory. TOC_CLOSED ));
// Set the icon of the Root Node
SetChildImage (items [0]);
}
 
// Set the method of a node. This method is very important. Understand the recursive usage of this method.
// The parameter item can be considered as a TreeItem in the tree.
Public static void setChildImage (TreeItem item ){
// Obtain all the sub-treeitems of the TreeItem
TreeItem [] items = item. getItems ();
// Loop every TreeItem
For (int I = 0; I <items. length; I ++ ){
// If there are no children under this TreeItem
// If (items [I]. getItems (). length = 0)
// Items [I]. setImage (ImageFactory. loadImage (item. getDisplay (),
// ImageFactory. TOPIC ));
// Else {// if this TreeItem has multiple children
//// If this TreeItem is in the expanded State, set the expanded Image
// If (items [I]. getExpanded ())
// Items [I]. setImage (ImageFactory. loadImage (item. getDisplay (),
// ImageFactory. TOC_OPEN ));
// Else
//// Otherwise, the collapsed image is set
// Items [I]. setImage (ImageFactory. loadImage (item. getDisplay (),
// ImageFactory. TOC_CLOSED ));
// Set the icon for the child of the TreeItem and call the setChildImage method recursively
SetChildImage (items [I]);
}
}
}

Author: TinyKing"

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.