28
|
1 /*******************************************************************************
|
|
2 * Copyright (c) 2000, 2004 IBM Corporation and others.
|
|
3 * All rights reserved. This program and the accompanying materials
|
|
4 * are made available under the terms of the Eclipse Public License v1.0
|
|
5 * which accompanies this distribution, and is available at
|
|
6 * http://www.eclipse.org/legal/epl-v10.html
|
|
7 *
|
|
8 * Contributors:
|
|
9 * IBM Corporation - initial API and implementation
|
|
10 * D Port:
|
|
11 * Bill Baxter <wbaxter@gmail.com>
|
|
12 *******************************************************************************/
|
|
13 module org.eclipse.swt.snippets.Snippet111;
|
|
14
|
|
15 /*
|
|
16 * TreeEditor example snippet: edit the text of a tree item (in place, fancy)
|
|
17 *
|
|
18 * For a list of all SWT example snippets see
|
|
19 * http://www.eclipse.org/swt/snippets/
|
|
20 */
|
|
21 import org.eclipse.swt.SWT;
|
|
22 import org.eclipse.swt.graphics.Color;
|
|
23 import org.eclipse.swt.graphics.Rectangle;
|
|
24 import org.eclipse.swt.graphics.GC;
|
|
25 import org.eclipse.swt.widgets.Display;
|
|
26 import org.eclipse.swt.widgets.Shell;
|
|
27 import org.eclipse.swt.widgets.Widget;
|
|
28 import org.eclipse.swt.widgets.Composite;
|
|
29 import org.eclipse.swt.widgets.Tree;
|
|
30 import org.eclipse.swt.widgets.TreeItem;
|
|
31 import org.eclipse.swt.widgets.Text;
|
|
32 import org.eclipse.swt.widgets.Listener;
|
|
33 import org.eclipse.swt.widgets.Event;
|
|
34 import org.eclipse.swt.layout.FillLayout;
|
|
35 import org.eclipse.swt.custom.TreeEditor;
|
|
36
|
|
37 import java.lang.all : String, substring, Math;
|
|
38
|
|
39 import tango.util.Convert;
|
|
40 import tango.util.log.Trace;
|
|
41 version(JIVE){
|
|
42 import jive.stacktrace;
|
|
43 }
|
|
44
|
|
45 void main () {
|
|
46
|
|
47 Tree tree;
|
|
48 Color black;
|
|
49 void handleResize (Event e, Composite composite, Text text, int inset ) {
|
|
50 Rectangle rect = composite.getClientArea ();
|
|
51 text.setBounds (rect.x + inset, rect.y + inset, rect.width - inset * 2, rect.height - inset * 2);
|
|
52 }
|
|
53 void handleTextEvent (Event e, Composite composite, TreeItem item, TreeEditor editor,Text text, int inset ) {
|
|
54 switch (e.type) {
|
|
55 case SWT.FocusOut: {
|
|
56 item.setText (text.getText ());
|
|
57 composite.dispose ();
|
|
58 }
|
|
59 break;
|
|
60 case SWT.Verify: {
|
|
61 String newText = text.getText ();
|
|
62 String leftText = newText.substring (0, e.start);
|
|
63 String rightText = newText.substring (e.end, newText.length);
|
|
64 GC gc = new GC (text);
|
|
65 Point size = gc.textExtent (leftText ~ e.text ~ rightText);
|
|
66 gc.dispose ();
|
|
67 size = text.computeSize (size.x, SWT.DEFAULT);
|
|
68 editor.horizontalAlignment = SWT.LEFT;
|
|
69 Rectangle itemRect = item.getBounds (), rect = tree.getClientArea ();
|
|
70 editor.minimumWidth = Math.max (size.x, itemRect.width) + inset* 2;
|
|
71 int left = itemRect.x, right = rect.x + rect.width;
|
|
72 editor.minimumWidth = Math.min (editor.minimumWidth, right - left);
|
|
73 editor.minimumHeight = size.y + inset* 2;
|
|
74 editor.layout ();
|
|
75 }
|
|
76 break;
|
|
77 case SWT.Traverse: {
|
|
78 switch (e.detail) {
|
|
79 case SWT.TRAVERSE_RETURN:
|
|
80 item.setText (text.getText ());
|
|
81 //FALL THROUGH
|
|
82 case SWT.TRAVERSE_ESCAPE:
|
|
83 composite.dispose ();
|
|
84 e.doit = false;
|
|
85 default:
|
|
86 //no-op
|
|
87 }
|
|
88 break;
|
|
89 }
|
|
90 default:
|
|
91 // no-op
|
|
92 }
|
|
93 }
|
|
94 void handleSelection (Event event, TreeItem[] lastItem, TreeEditor editor ) {
|
|
95 TreeItem item = cast(TreeItem) event.item;
|
|
96 if (item !is null && item is lastItem [0]) {
|
|
97 bool showBorder = true;
|
|
98 Composite composite = new Composite (tree, SWT.NONE);
|
|
99 if (showBorder) composite.setBackground (black);
|
|
100 Text text = new Text (composite, SWT.NONE);
|
|
101 int inset = showBorder ? 1 : 0;
|
|
102 composite.addListener (SWT.Resize, dgListener( &handleResize, composite, text, inset ));
|
|
103 Listener textListener = dgListener( &handleTextEvent, composite, item, editor, text, inset);
|
|
104 text.addListener (SWT.FocusOut, textListener);
|
|
105 text.addListener (SWT.Traverse, textListener);
|
|
106 text.addListener (SWT.Verify, textListener);
|
|
107 editor.setEditor (composite, item);
|
|
108 text.setText (item.getText ());
|
|
109 text.selectAll ();
|
|
110 text.setFocus ();
|
|
111 }
|
|
112 lastItem [0] = item;
|
|
113 }
|
|
114
|
|
115 Display display = new Display ();
|
|
116 black = display.getSystemColor (SWT.COLOR_BLACK);
|
|
117 Shell shell = new Shell (display);
|
|
118 shell.setLayout (new FillLayout ());
|
|
119 tree = new Tree (shell, SWT.BORDER);
|
|
120 for (int i=0; i<16; i++) {
|
|
121 TreeItem itemI = new TreeItem (tree, SWT.NONE);
|
|
122 itemI.setText ("Item " ~ to!(char[])(i));
|
|
123 for (int j=0; j<16; j++) {
|
|
124 TreeItem itemJ = new TreeItem (itemI, SWT.NONE);
|
|
125 itemJ.setText ("Item " ~ to!(char[])(j) );
|
|
126 }
|
|
127 }
|
|
128 TreeItem [] lastItem = new TreeItem [1];
|
|
129 TreeEditor editor = new TreeEditor (tree);
|
|
130 tree.addListener (SWT.Selection, dgListener( &handleSelection, lastItem, editor ));
|
|
131 shell.pack ();
|
|
132 shell.open ();
|
|
133 while (!shell.isDisposed()) {
|
|
134 if (!display.readAndDispatch ()) display.sleep ();
|
|
135 }
|
|
136 display.dispose ();
|
|
137 }
|
|
138
|
|
139
|