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 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272
|
import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.CCombo;
import org.eclipse.swt.custom.TreeEditor;
import org.eclipse.swt.graphics.GC;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.graphics.Rectangle;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Listener;
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 TableTreeTest {
// Rajout Laurent
static Point lastMouseDown;
public TableTreeTest() {
final Display display = new Display();
final Shell shell = new Shell(display);
shell.setText("Elements");
shell.setLayout(new FillLayout());
final Tree myTree = new Tree(shell, SWT.FULL_SELECTION);
final TreeItem iItem = new TreeItem(myTree, SWT.NONE);
TreeColumn column = new TreeColumn(myTree, SWT.NONE);
column.setText("Property");
column.setResizable(true);
column.setMoveable(false);
column.setWidth(100);
column = new TreeColumn(myTree, SWT.NONE);
column.setText("Attributes");
column.setResizable(true);
column.setWidth(100);
column = new TreeColumn(myTree, SWT.NONE);
column.setText("Value");
column.setResizable(true);
column.setWidth(100);
iItem.setText("Prompt");
for (int j = 1; j < 2; j++) {
TreeItem jItem = new TreeItem(iItem, SWT.NONE);
jItem.setText(1, "bargein");
jItem.setText(2, "True");
/***/
}
for (int j = 1; j < 2; j++) {
TreeItem jItem2 = new TreeItem(iItem, SWT.NONE);
jItem2.setText(1, "Timeout");
jItem2.setText(2, "Saisissez le temps");
/***/
}
for (int j = 1; j < 2; j++) {
TreeItem jItem3 = new TreeItem(iItem, SWT.NONE);
jItem3.setText(1, "cond");
jItem3.setText(2, "Définir la condition");
/***/
}
for (int j = 1; j < 2; j++) {
TreeItem jItem4 = new TreeItem(iItem, SWT.NONE);
jItem4.setText(1, "bargeintype");
jItem4.setText(2, "sppech ou hotword");
/***/
}
for (int j = 1; j < 2; j++) {
final TreeItem jItem5 = new TreeItem(iItem, SWT.NONE);
jItem5.setText(1, "count");
jItem5.setText(2, "False");
/***/
}
myTree.setHeaderVisible(true);
myTree.setLinesVisible(true);
myTree.getItem(0).setExpanded(true);
// Rajout Laurent
final TreeEditor editor = new TreeEditor(myTree);
myTree.addListener(SWT.MouseDown, new Listener() {
public void handleEvent(Event event) {
lastMouseDown = new Point(event.x, event.y);
}
});
myTree.addListener(SWT.KeyDown, new Listener() {
public void handleEvent(Event event) {
lastMouseDown = null;
}
});
myTree.addListener(SWT.Selection, new Listener() {
public void handleEvent(Event event) {
if (lastMouseDown == null)
return; // Selection faite au clavier
TreeItem item = myTree.getItem(lastMouseDown);
for (int i = 0; i < myTree.getColumnCount(); i++) {
Rectangle rect = item.getBounds(i);
if (rect.contains(lastMouseDown)) {
if (i == 1) {
buildText(editor, item, i);
}
if (i == 2) {
// Attention, on est sur la troisième colonne, on affiche :
// une combo pour bargein
// une combo différente pour bargeintype
// Pour les autres on affiche du texte
if (item.getText(1).equalsIgnoreCase("bargein")) {
buildCombo(editor, item, i, new String[] {"True","False"});
} else if (item.getText(1).equalsIgnoreCase("bargeintype")) {
buildCombo(editor, item, i, new String[] {"Speech","Hotword"});
} else {
buildText(editor, item, i);
}
}
}
}
lastMouseDown = null;
}
});
shell.setSize(350, 350);
shell.open();
while (!shell.isDisposed()) {
if (!display.readAndDispatch())
display.sleep();
}
display.dispose();
}
/**
* @param editor
* @param item
* @param i
*/
protected void buildCombo(final TreeEditor editor, final TreeItem item, final int i, final String[] values) {
boolean showBorder = true;
final Composite composite = new Composite(item.getParent(), SWT.NONE);
if (showBorder)
composite.setBackground(item.getDisplay().getSystemColor(SWT.COLOR_BLACK));
final CCombo combo = new CCombo(composite, SWT.READ_ONLY);
for (String current : values) {
combo.add(current);
}
combo.pack();
editor.horizontalAlignment = SWT.LEFT;
editor.minimumWidth = combo.getSize().x;
editor.minimumHeight = combo.getSize().y;
editor.layout();
final int inset = showBorder ? 1 : 0;
composite.addListener(SWT.Resize, new Listener() {
public void handleEvent(Event e) {
Rectangle rect = composite.getClientArea();
combo.setBounds(rect.x + inset, rect.y + inset, rect.width - inset * 2, rect.height - inset * 2);
}
});
Listener listener = new Listener() {
public void handleEvent(final Event e) {
switch (e.type) {
case SWT.FocusOut:
item.setText(i, combo.getText());
composite.dispose();
break;
case SWT.Traverse:
switch (e.detail) {
case SWT.TRAVERSE_RETURN:
item.setText(i, combo.getText());
// FALL THROUGH
case SWT.TRAVERSE_ESCAPE:
composite.dispose();
e.doit = false;
}
break;
}
}
};
combo.addListener(SWT.FocusOut, listener);
combo.addListener(SWT.Traverse, listener);
editor.setEditor(composite, item, i);
combo.setText(item.getText(i));
combo.setFocus();
}
/**
* @param editor
* @param item
* @param i
*/
protected void buildText(final TreeEditor editor, final TreeItem item, final int i) {
boolean showBorder = true;
final Composite composite = new Composite(item.getParent(), SWT.NONE);
if (showBorder)
composite.setBackground(item.getDisplay().getSystemColor(SWT.COLOR_BLACK));
final Text text = new Text(composite, SWT.NONE);
final int inset = showBorder ? 1 : 0;
composite.addListener(SWT.Resize, new Listener() {
public void handleEvent(Event e) {
Rectangle rect = composite.getClientArea();
text.setBounds(rect.x + inset, rect.y + inset, rect.width - inset * 2, rect.height - inset * 2);
}
});
Listener textListener = new Listener() {
public void handleEvent(final Event e) {
switch (e.type) {
case SWT.FocusOut:
item.setText(i, text.getText());
composite.dispose();
break;
case SWT.Verify:
String newText = text.getText();
String leftText = newText.substring(0, e.start);
String rightText = newText.substring(e.end, newText.length());
GC gc = new GC(text);
Point size = gc.textExtent(leftText + e.text + rightText);
gc.dispose();
size = text.computeSize(size.x, SWT.DEFAULT);
editor.horizontalAlignment = SWT.LEFT;
Rectangle itemRect = item.getBounds(),
rect = item.getParent().getClientArea();
editor.minimumWidth = Math.max(size.x, itemRect.width) + inset * 2;
int left = itemRect.x,
right = rect.x + rect.width;
editor.minimumWidth = Math.min(editor.minimumWidth, right - left);
editor.minimumHeight = size.y + inset * 2;
editor.layout();
break;
case SWT.Traverse:
switch (e.detail) {
case SWT.TRAVERSE_RETURN:
item.setText(i, text.getText());
// FALL THROUGH
case SWT.TRAVERSE_ESCAPE:
composite.dispose();
e.doit = false;
}
break;
}
}
};
text.addListener(SWT.FocusOut, textListener);
text.addListener(SWT.Traverse, textListener);
text.addListener(SWT.Verify, textListener);
editor.setEditor(composite, item, i);
text.setText(item.getText(i));
text.selectAll();
text.setFocus();
}
public static void main(String[] argv) {
new TableTreeTest();
}
} |