Skip to main content
summaryrefslogtreecommitdiffstats
blob: b00e3d77ffa9e31e672b003eef5cfe5bb55945da (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
package org.eclipse.swt.custom;

/*
 * Licensed Materials - Property of IBM,
 * (c) Copyright IBM Corp. 1998, 2001  All Rights Reserved
 */

import org.eclipse.swt.*;
import org.eclipse.swt.graphics.*;
import org.eclipse.swt.widgets.*;
import org.eclipse.swt.events.*;

/**
*
* A TreeEditor is a manager for a Control that appears above a cell in a Tree and tracks with the
* moving and resizing of that cell.  It can be used to display a text widget above a cell
* in a Tree so that the user can edit the contents of that cell.  It can also be used to display
* a button that can launch a dialog for modifying the contents of the associated cell.
*
* <p> Here is an example of using a TreeEditor:
* <code><pre>
* Tree tree = new Tree(parent, SWT.FULL_SELECTION);
* TreeEditor editor = new TreeEditor (tree);
* tree.addSelectionListener (new SelectionAdapter() {
*	public void widgetSelected(SelectionEvent e) {}
*
*		// Clean up any previous editor control
*		Control oldEditor = editor.getEditor();
*		if (oldEditor != null)
*			oldEditor.dispose();	
*
*		// Identify the selected row
*		int index = tree.getSelectionIndex ();
*		if (index == -1) return;
*		TreeItem item = tree.getItem (index);
*
*		// The control that will be the editor must be a child of the Tree
*		Text text = new Text(tree, SWT.NONE);
*
*		//The text editor must have the same size as the cell and must
*		//not be any smaller than 50 pixels.
*		editor.horizontalAlignment = SWT.LEFT;
*		editor.grabHorizontal = true;
*		editor.minimumWidth = 50;
*
*		// Open the text editor on the selected row.
*		editor.setEditor (text, item);
*
*		// Assign focus to the text control
*		text.setFocus ();
*	}
* });
* </pre></code>
*/
public class TreeEditor extends ControlEditor {	
	Tree tree;
	TreeItem item;
	TreeListener treeListener;
/**
* Creates a TreeEditor for the specified Tree.
*
* @param tree the Tree Control above which this editor will be displayed
*
*/
public TreeEditor (Tree tree) {
	super(tree);
	this.tree = tree;
	treeListener = new TreeAdapter () {
		final Runnable runnable = new Runnable() {
			public void run() {
				if (TreeEditor.this.tree.isDisposed() || editor == null) return;
				resize();
				editor.setVisible(true);
			}
		};
		public void treeCollapsed(TreeEvent e) {
			if (editor == null) return;
			editor.setVisible(false);
			Display display = TreeEditor.this.tree.getDisplay();
			display.asyncExec(runnable);
		}
		public void treeExpanded(TreeEvent e) {
			if (editor == null) return;
			editor.setVisible(false);
			Display display = TreeEditor.this.tree.getDisplay();
			display.asyncExec(runnable);
		}
	};
	tree.addTreeListener(treeListener);
}
Rectangle computeBounds () {
	if (item == null || item.isDisposed()) return new Rectangle(0, 0, 0, 0);
	
	Rectangle cell = item.getBounds();
	Rectangle area = tree.getClientArea();
	if (cell.x < area.x + area.width) {
		if (cell.x + cell.width > area.x + area.width) {
			cell.width = area.x + area.width - cell.x;
		}
	}
	Rectangle editorRect = new Rectangle(cell.x, cell.y, minimumWidth, cell.height);
	
	if (grabHorizontal) {
		editorRect.width = Math.max(area.x + area.width - cell.x, minimumWidth);
	}
	
	if (horizontalAlignment == SWT.RIGHT) {
		editorRect.x = Math.max(cell.x, cell.x + cell.width - editorRect.width);
	} else if (horizontalAlignment == SWT.LEFT) {
		// do nothing - cell.x is the right answer
	} else { // default is CENTER
		editorRect.x = Math.max(cell.x, cell.x + (cell.width - editorRect.width)/2);
	}
	
	return editorRect;
}
/**
 * Removes all associations between the TreeEditor and the cell in the tree.  The
 * tree and the editor Control are <b>not</b> disposed.
 */
public void dispose () {
	if (treeListener != null) 
		tree.removeTreeListener(treeListener);
	treeListener = null;
	tree = null;
	item = null;
	super.dispose();
}
/**
* Returns the TreeItem for the row of the cell being tracked by this editor.
*
* @return the TreeItem for the row of the cell being tracked by this editor
*/
public TreeItem getItem () {
	return item;
}
public void setItem (TreeItem item) {
	this.item = item;
}

/**
* Specify the Control that is to be displayed and the cell in the tree that it is to be positioned above.
*
* <p>Note: The Control provided as the editor <b>must</b> be created with its parent being the Tree control
* specified in the TreeEditor constructor.
* 
* @param editor the Control that is displayed above the cell being edited
* @param item the TreeItem for the row of the cell being tracked by this editor
* @param column the zero based index of the column of the cell being tracked by this editor
*/
public void setEditor (Control editor, TreeItem item) {
	setItem (item);
	super.setEditor (editor);
}
public void setEditor (Control editor) {
	TreeItem item = null;
	if (tree.getItemCount() != 0) {
		item = tree.getItems()[0];
	}
	this.setEditor(editor, item);
}

}

Back to the top