⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 tree.java

📁 源码为Eclipse开源开发平台桌面开发工具SWT的源代码,
💻 JAVA
📖 第 1 页 / 共 5 页
字号:
/******************************************************************************* * Copyright (c) 2000, 2004 IBM Corporation and others. * All rights reserved. This program and the accompanying materials * are made available under the terms of the Common Public License v1.0 * which accompanies this distribution, and is available at * http://www.eclipse.org/legal/cpl-v10.html *  * Contributors: *     IBM Corporation - initial API and implementation *******************************************************************************/package org.eclipse.swt.widgets;import org.eclipse.swt.internal.win32.*;import org.eclipse.swt.*;import org.eclipse.swt.graphics.*;import org.eclipse.swt.events.*;/** * Instances of this class provide a selectable user interface object * that displays a hierarchy of items and issue notificiation when an * item in the hierarchy is selected. * <p> * The item children that may be added to instances of this class * must be of type <code>TreeItem</code>. * </p><p> * Note that although this class is a subclass of <code>Composite</code>, * it does not make sense to add <code>Control</code> children to it, * or set a layout on it. * </p><p> * <dl> * <dt><b>Styles:</b></dt> * <dd>SINGLE, MULTI, CHECK</dd> * <dt><b>Events:</b></dt> * <dd>Selection, DefaultSelection, Collapse, Expand</dd> * </dl> * <p> * Note: Only one of the styles SINGLE and MULTI may be specified. * </p><p> * IMPORTANT: This class is <em>not</em> intended to be subclassed. * </p> */public class Tree extends Composite {	int hAnchor;	TreeItem [] items;	ImageList imageList;	boolean dragStarted, gestureCompleted;	boolean ignoreSelect, ignoreExpand, ignoreDeselect;	boolean customDraw;	static final int TreeProc;	static final TCHAR TreeClass = new TCHAR (0, OS.WC_TREEVIEW, true);	static {		WNDCLASS lpWndClass = new WNDCLASS ();		OS.GetClassInfo (0, TreeClass, lpWndClass);		TreeProc = lpWndClass.lpfnWndProc;	}/** * Constructs a new instance of this class given its parent * and a style value describing its behavior and appearance. * <p> * The style value is either one of the style constants defined in * class <code>SWT</code> which is applicable to instances of this * class, or must be built by <em>bitwise OR</em>'ing together  * (that is, using the <code>int</code> "|" operator) two or more * of those <code>SWT</code> style constants. The class description * lists the style constants that are applicable to the class. * Style bits are also inherited from superclasses. * </p> * * @param parent a composite control which will be the parent of the new instance (cannot be null) * @param style the style of control to construct * * @exception IllegalArgumentException <ul> *    <li>ERROR_NULL_ARGUMENT - if the parent is null</li> * </ul> * @exception SWTException <ul> *    <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the parent</li> *    <li>ERROR_INVALID_SUBCLASS - if this class is not an allowed subclass</li> * </ul> * * @see SWT#SINGLE * @see SWT#MULTI * @see SWT#CHECK * @see Widget#checkSubclass * @see Widget#getStyle */public Tree (Composite parent, int style) {	super (parent, checkStyle (style));}static int checkStyle (int style) {	/*	* Feature in Windows.  It is not possible to create	* a tree that scrolls and does not have scroll bars.	* The TVS_NOSCROLL style will remove the scroll bars	* but the tree will never scroll.  Therefore, no matter	* what style bits are specified, set the H_SCROLL and	* V_SCROLL bits so that the SWT style will match the	* widget that Windows creates.	*/	style |= SWT.H_SCROLL | SWT.V_SCROLL;	return checkBits (style, SWT.SINGLE, SWT.MULTI, 0, 0, 0, 0);}/** * Adds the listener to the collection of listeners who will * be notified when the receiver's selection changes, by sending * it one of the messages defined in the <code>SelectionListener</code> * interface. * <p> * When <code>widgetSelected</code> is called, the item field of the event object is valid. * If the reciever has <code>SWT.CHECK</code> style set and the check selection changes, * the event object detail field contains the value <code>SWT.CHECK</code>. * <code>widgetDefaultSelected</code> is typically called when an item is double-clicked. * The item field of the event object is valid for default selection, but the detail field is not used. * </p> * * @param listener the listener which should be notified * * @exception IllegalArgumentException <ul> *    <li>ERROR_NULL_ARGUMENT - if the listener is null</li> * </ul> * @exception SWTException <ul> *    <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li> *    <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li> * </ul> * * @see SelectionListener * @see #removeSelectionListener * @see SelectionEvent */public void addSelectionListener(SelectionListener listener) {	checkWidget ();	if (listener == null) error (SWT.ERROR_NULL_ARGUMENT);	TypedListener typedListener = new TypedListener (listener);	addListener (SWT.Selection, typedListener);	addListener (SWT.DefaultSelection, typedListener);}/** * Adds the listener to the collection of listeners who will * be notified when an item in the receiver is expanded or collapsed * by sending it one of the messages defined in the <code>TreeListener</code> * interface. * * @param listener the listener which should be notified * * @exception IllegalArgumentException <ul> *    <li>ERROR_NULL_ARGUMENT - if the listener is null</li> * </ul> * @exception SWTException <ul> *    <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li> *    <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li> * </ul> * * @see TreeListener * @see #removeTreeListener */public void addTreeListener(TreeListener listener) {	checkWidget ();	if (listener == null) error (SWT.ERROR_NULL_ARGUMENT);	TypedListener typedListener = new TypedListener (listener);	addListener (SWT.Expand, typedListener);	addListener (SWT.Collapse, typedListener);} int callWindowProc (int msg, int wParam, int lParam) {	if (handle == 0) return 0;	return OS.CallWindowProc (TreeProc, handle, msg, wParam, lParam);}boolean checkScroll (int hItem) {	/*	* Feature in Windows.  If redraw is turned off using WM_SETREDRAW 	* and a tree item that is not a child of the first root is selected or	* scrolled using TVM_SELECTITEM or TVM_ENSUREVISIBLE, then scrolling	* does not occur.  The fix is to detect this case, and make sure	* that redraw is temporarly enabled.  To avoid flashing, DefWindowProc()	* is called to disable redrawing.	* 	* NOTE:  The code that actually works around the problem is in the	* callers of this method.	*/	if (drawCount == 0) return false;	int hRoot = OS.SendMessage (handle, OS.TVM_GETNEXTITEM, OS.TVGN_ROOT, 0);	int hParent = OS.SendMessage (handle, OS.TVM_GETNEXTITEM, OS.TVGN_PARENT, hItem);	while (hParent != hRoot && hParent != 0) {		hParent = OS.SendMessage (handle, OS.TVM_GETNEXTITEM, OS.TVGN_PARENT, hParent);	}	return hParent == 0;}public Point computeSize (int wHint, int hHint, boolean changed) {	checkWidget ();	int width = 0, height = 0;	RECT rect = new RECT ();	int hItem = OS.SendMessage (handle, OS.TVM_GETNEXTITEM, OS.TVGN_ROOT, 0);	while (hItem != 0) {		rect.left = hItem;		if (OS.SendMessage (handle, OS.TVM_GETITEMRECT, 1, rect) != 0) {			width = Math.max (width, rect.right);			height += rect.bottom - rect.top;		}		hItem = OS.SendMessage (handle, OS.TVM_GETNEXTITEM, OS.TVGN_NEXT, hItem);	}	if (width == 0) width = DEFAULT_WIDTH;	if (height == 0) height = DEFAULT_HEIGHT;	if (wHint != SWT.DEFAULT) width = wHint;	if (hHint != SWT.DEFAULT) height = hHint;	int border = getBorderWidth ();	width += border * 2;  height += border * 2;	if ((style & SWT.V_SCROLL) != 0) {		width += OS.GetSystemMetrics (OS.SM_CXVSCROLL);	}	if ((style & SWT.H_SCROLL) != 0) {		height += OS.GetSystemMetrics (OS.SM_CYHSCROLL);	}	return new Point (width, height);}void createHandle () {	super.createHandle ();	state &= ~CANVAS;		/*	* Feature in Windows.  In version 5.8 of COMCTL32.DLL,	* if the font is changed for an item, the bounds for the	* item are not updated, causing the text to be clipped.	* The fix is to detect the version of COMCTL32.DLL, and	* if it is one of the versions with the problem, then	* use version 5.00 of the control (a version that does	* not have the problem).  This is the recomended work	* around from the MSDN.	*/	if (OS.COMCTL32_MAJOR < 6) {		OS.SendMessage (handle, OS.CCM_SETVERSION, 5, 0);	}	/* Set the checkbox image list */	if ((style & SWT.CHECK) != 0) setCheckboxImageList ();		/*	* Feature in Windows.  When the control is created,	* it does not use the default system font.  A new HFONT	* is created and destroyed when the control is destroyed.	* This means that a program that queries the font from	* this control, uses the font in another control and then	* destroys this control will have the font unexpectedly	* destroyed in the other control.  The fix is to assign	* the font ourselves each time the control is created.	* The control will not destroy a font that it did not	* create.	*/	int hFont = OS.GetStockObject (OS.SYSTEM_FONT);	OS.SendMessage (handle, OS.WM_SETFONT, hFont, 0);}void createItem (TreeItem item, int hParent, int hInsertAfter) {	item.foreground = item.background = item.font = -1;	int id = 0;	while (id < items.length && items [id] != null) id++;	if (id == items.length) {		TreeItem [] newItems = new TreeItem [items.length + 4];		System.arraycopy (items, 0, newItems, 0, items.length);		items = newItems;	}	TVINSERTSTRUCT tvInsert = new TVINSERTSTRUCT ();	tvInsert.hParent = hParent;	tvInsert.hInsertAfter = hInsertAfter;	tvInsert.lParam = id;	tvInsert.iImage = OS.I_IMAGENONE;	tvInsert.iSelectedImage = tvInsert.iImage;	tvInsert.mask = OS.TVIF_HANDLE | OS.TVIF_PARAM | OS.TVIF_IMAGE | OS.TVIF_SELECTEDIMAGE;		/* Set the initial unchecked state */	if ((style & SWT.CHECK) != 0) {		tvInsert.mask = tvInsert.mask | OS.TVIF_STATE;		tvInsert.state = 1 << 12;		tvInsert.stateMask = OS.TVIS_STATEIMAGEMASK;	}	/* Insert the item */	int hItem = OS.SendMessage (handle, OS.TVM_INSERTITEM, 0, tvInsert);	if (hItem == 0) error (SWT.ERROR_ITEM_NOT_ADDED);	item.handle = hItem;	items [id] = item;		/*	* This code is intentionally commented.	*///	if (hParent != 0) {//		int bits = OS.GetWindowLong (handle, OS.GWL_STYLE);//		bits |= OS.TVS_LINESATROOT;//		OS.SetWindowLong (handle, OS.GWL_STYLE, bits);//	}	/*	* Bug in Windows.  When a child item is added to a parent item	* that has no children outside of WM_NOTIFY with control code	* TVN_ITEMEXPANDED, the tree widget does not redraw the +/-	* indicator.  The fix is to detect the case when the first	* child is added to a visible parent item and redraw the parent.	*/	if (!OS.IsWindowVisible (handle) || drawCount > 0) return;	int hChild = OS.SendMessage (handle, OS.TVM_GETNEXTITEM, OS.TVGN_CHILD, hParent);	if (hChild != 0 && OS.SendMessage (handle, OS.TVM_GETNEXTITEM, OS.TVGN_NEXT, hChild) == 0) {		RECT rect = new RECT ();		rect.left = hParent;		if (OS.SendMessage (handle, OS.TVM_GETITEMRECT, 0, rect) != 0) {			OS.InvalidateRect (handle, rect, true);		}	}}void createWidget () {	super.createWidget ();	items = new TreeItem [4];}int defaultBackground () {	return OS.GetSysColor (OS.COLOR_WINDOW);}/** * Deselects all selected items in the receiver. * * @exception SWTException <ul> *    <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li> *    <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li> * </ul> */public void deselectAll () {	checkWidget ();	TVITEM tvItem = new TVITEM ();	tvItem.mask = OS.TVIF_STATE;	tvItem.stateMask = OS.TVIS_SELECTED;	if ((style & SWT.SINGLE) != 0) {		int hItem = OS.SendMessage (handle, OS.TVM_GETNEXTITEM, OS.TVGN_CARET, 0);		if (hItem != 0) {			tvItem.hItem = hItem;			OS.SendMessage (handle, OS.TVM_SETITEM, 0, tvItem);		}		return;	}	int oldProc = OS.GetWindowLong (handle, OS.GWL_WNDPROC);	OS.SetWindowLong (handle, OS.GWL_WNDPROC, TreeProc);		for (int i=0; i<items.length; i++) {		TreeItem item = items [i];		if (item != null) {			tvItem.hItem = item.handle;			OS.SendMessage (handle, OS.TVM_SETITEM, 0, tvItem);		}	}	OS.SetWindowLong (handle, OS.GWL_WNDPROC, oldProc);}void destroyItem (TreeItem item) {	/*	* Feature in Windows.  When an item is removed that is not	* visible in the tree because it belongs to a collapsed branch,	* Windows redraws the tree causing a flash for each item that	* is removed.  The fix is to detect whether the item is visible,	* force the widget to be fully painted, turn off redraw, remove	* the item and validate the damage caused by the removing of	* the item.	*/	int hItem = item.handle, hParent = 0;	boolean fixRedraw = false;	if (drawCount == 0 && OS.IsWindowVisible (handle)) {		RECT rect = new RECT ();		rect.left = hItem;		fixRedraw = OS.SendMessage (handle, OS.TVM_GETITEMRECT, 0, rect) == 0;	}	if (fixRedraw) {		hParent = OS.SendMessage (handle, OS.TVM_GETNEXTITEM, OS.TVGN_PARENT, hItem);		OS.UpdateWindow (handle);		OS.SendMessage (handle, OS.WM_SETREDRAW, 0, 0);	}	TVITEM tvItem = new TVITEM ();	tvItem.mask = OS.TVIF_HANDLE | OS.TVIF_PARAM;	releaseItems (item.getItems (), tvItem);

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -