annotate snippets/treeeditor/Snippet111.d @ 84:398aa64a7243

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