| 1 |
/******************************************************************************* |
|---|
| 2 |
* Copyright (c) 2000, 2003 IBM Corporation and others. |
|---|
| 3 |
* All rights reserved. This program and the accompanying materials |
|---|
| 4 |
* are made available under the terms of the Common Public License v1.0 |
|---|
| 5 |
* which accompanies this distribution, and is available at |
|---|
| 6 |
* http://www.eclipse.org/legal/cpl-v10.html |
|---|
| 7 |
* |
|---|
| 8 |
* Contributors: |
|---|
| 9 |
* IBM Corporation - initial API and implementation |
|---|
| 10 |
*******************************************************************************/ |
|---|
| 11 |
module controlexample.treetab; |
|---|
| 12 |
|
|---|
| 13 |
private { |
|---|
| 14 |
import swt.all; |
|---|
| 15 |
import controlexample.scrollabletab; |
|---|
| 16 |
import controlexample.controlexample; |
|---|
| 17 |
} |
|---|
| 18 |
|
|---|
| 19 |
|
|---|
| 20 |
class TreeTab : ScrollableTab { |
|---|
| 21 |
/* Example widgets and groups that contain them */ |
|---|
| 22 |
Tree tree1, tree2; |
|---|
| 23 |
TreeItem textNode1, imageNode1; |
|---|
| 24 |
Group treeGroup, imageTreeGroup, itemGroup; |
|---|
| 25 |
|
|---|
| 26 |
/* Style widgets added to the "Style" group */ |
|---|
| 27 |
Button checkButton; |
|---|
| 28 |
|
|---|
| 29 |
/* Controls and resources added to the "Colors" group */ |
|---|
| 30 |
Button itemForegroundButton, itemBackgroundButton, itemFontButton; |
|---|
| 31 |
Color itemForegroundColor, itemBackgroundColor; |
|---|
| 32 |
Image itemForegroundImage, itemBackgroundImage; |
|---|
| 33 |
Font itemFont; |
|---|
| 34 |
|
|---|
| 35 |
/** |
|---|
| 36 |
* Creates the Tab within a given instance_ren of ControlExample. |
|---|
| 37 |
*/ |
|---|
| 38 |
this(ControlExample instance_ren) { |
|---|
| 39 |
super(instance_ren); |
|---|
| 40 |
} |
|---|
| 41 |
|
|---|
| 42 |
/** |
|---|
| 43 |
* Creates the "Colors" group. |
|---|
| 44 |
*/ |
|---|
| 45 |
void createColorGroup () { |
|---|
| 46 |
super.createColorGroup(); |
|---|
| 47 |
|
|---|
| 48 |
itemGroup = new Group (colorGroup, SWT.NONE); |
|---|
| 49 |
itemGroup.setText (ControlExample.getResourceString ("Tree_Item_Colors")); |
|---|
| 50 |
GridData data = new GridData (); |
|---|
| 51 |
data.horizontalSpan = 2; |
|---|
| 52 |
itemGroup.setLayoutData (data); |
|---|
| 53 |
itemGroup.setLayout (new GridLayout (2, false)); |
|---|
| 54 |
Label label = new Label (colorGroup, SWT.NONE); |
|---|
| 55 |
label.setText (ControlExample.getResourceString ("Foreground_Color")); |
|---|
| 56 |
itemForegroundButton = new Button (itemGroup, SWT.PUSH); |
|---|
| 57 |
label = new Label (colorGroup, SWT.NONE); |
|---|
| 58 |
label.setText (ControlExample.getResourceString ("Background_Color")); |
|---|
| 59 |
itemBackgroundButton = new Button (itemGroup, SWT.PUSH); |
|---|
| 60 |
itemFontButton = new Button (itemGroup, SWT.PUSH); |
|---|
| 61 |
itemFontButton.setText(ControlExample.getResourceString("Font")); |
|---|
| 62 |
itemFontButton.setLayoutData(new GridData (GridData.HORIZONTAL_ALIGN_FILL)); |
|---|
| 63 |
|
|---|
| 64 |
Shell shell = colorGroup.getShell (); |
|---|
| 65 |
int imageSize = 12; |
|---|
| 66 |
Display display = shell.getDisplay (); |
|---|
| 67 |
itemForegroundImage = new Image(display, imageSize, imageSize); |
|---|
| 68 |
itemBackgroundImage = new Image(display, imageSize, imageSize); |
|---|
| 69 |
|
|---|
| 70 |
/* Add listeners to set the colors and font */ |
|---|
| 71 |
itemForegroundButton.setImage(itemForegroundImage); |
|---|
| 72 |
itemForegroundButton.handleSelection(this, delegate(SelectionEvent event) { |
|---|
| 73 |
TreeTab c = cast(TreeTab)event.cData; |
|---|
| 74 |
ColorDialog foregroundDialog = new ColorDialog (c.colorGroup.getShell()); |
|---|
| 75 |
Color oldColor = c.itemForegroundColor; |
|---|
| 76 |
if (oldColor is null) oldColor = c.textNode1.getForeground (); |
|---|
| 77 |
foregroundDialog.setRGB(oldColor.getRGB()); |
|---|
| 78 |
RGB rgb = foregroundDialog.open(); |
|---|
| 79 |
if (rgb is null) return; |
|---|
| 80 |
oldColor = c.itemForegroundColor; |
|---|
| 81 |
c.itemForegroundColor = new Color (event.display, rgb); |
|---|
| 82 |
c.setItemForeground (); |
|---|
| 83 |
if (oldColor !== null) oldColor.dispose (); |
|---|
| 84 |
}); |
|---|
| 85 |
|
|---|
| 86 |
itemBackgroundButton.setImage(itemBackgroundImage); |
|---|
| 87 |
itemBackgroundButton.handleSelection(this, delegate(SelectionEvent event) { |
|---|
| 88 |
TreeTab c = cast(TreeTab)event.cData; |
|---|
| 89 |
ColorDialog backgroundDialog = new ColorDialog (c.colorGroup.getShell()); |
|---|
| 90 |
Color oldColor = c.itemBackgroundColor; |
|---|
| 91 |
if (oldColor is null) oldColor = c.textNode1.getBackground (); |
|---|
| 92 |
backgroundDialog.setRGB(oldColor.getRGB()); |
|---|
| 93 |
RGB rgb = backgroundDialog.open(); |
|---|
| 94 |
if (rgb is null) return; |
|---|
| 95 |
oldColor = c.itemBackgroundColor; |
|---|
| 96 |
c.itemBackgroundColor = new Color (event.display, rgb); |
|---|
| 97 |
c.setItemBackground (); |
|---|
| 98 |
if (oldColor !== null) oldColor.dispose (); |
|---|
| 99 |
}); |
|---|
| 100 |
itemFontButton.handleSelection(this, delegate(SelectionEvent event) { |
|---|
| 101 |
TreeTab c = cast(TreeTab)event.cData; |
|---|
| 102 |
FontDialog fontDialog = new FontDialog (c.colorGroup.getShell()); |
|---|
| 103 |
Font oldFont = c.itemFont; |
|---|
| 104 |
if (oldFont is null) oldFont = c.textNode1.getFont (); |
|---|
| 105 |
fontDialog.setFontList(oldFont.getFontData()); |
|---|
| 106 |
FontData fontData = fontDialog.open (); |
|---|
| 107 |
if (fontData is null) return; |
|---|
| 108 |
oldFont = c.itemFont; |
|---|
| 109 |
c.itemFont = new Font (event.display, fontData); |
|---|
| 110 |
c.setItemFont (); |
|---|
| 111 |
c.setExampleWidgetSize (); |
|---|
| 112 |
if (oldFont !== null) oldFont.dispose (); |
|---|
| 113 |
}); |
|---|
| 114 |
shell.handleDispose(this, delegate(DisposeEvent event) { |
|---|
| 115 |
TreeTab c = cast(TreeTab)event.cData; |
|---|
| 116 |
if (c.itemBackgroundImage !== null) c.itemBackgroundImage.dispose(); |
|---|
| 117 |
if (c.itemForegroundImage !== null) c.itemForegroundImage.dispose(); |
|---|
| 118 |
if (c.itemBackgroundColor !== null) c.itemBackgroundColor.dispose(); |
|---|
| 119 |
if (c.itemForegroundColor !== null) c.itemForegroundColor.dispose(); |
|---|
| 120 |
if (c.itemFont !== null) c.itemFont.dispose(); |
|---|
| 121 |
c.itemBackgroundColor = null; |
|---|
| 122 |
c.itemForegroundColor = null; |
|---|
| 123 |
c.itemFont = null; |
|---|
| 124 |
}); |
|---|
| 125 |
} |
|---|
| 126 |
|
|---|
| 127 |
/** |
|---|
| 128 |
* Creates the "Example" group. |
|---|
| 129 |
*/ |
|---|
| 130 |
void createExampleGroup () { |
|---|
| 131 |
super.createExampleGroup (); |
|---|
| 132 |
|
|---|
| 133 |
/* Create a group for the text tree */ |
|---|
| 134 |
treeGroup = new Group (exampleGroup, SWT.NONE); |
|---|
| 135 |
treeGroup.setLayout (new GridLayout ()); |
|---|
| 136 |
treeGroup.setLayoutData (new GridData (GridData.GRAB_HORIZONTAL | GridData.HORIZONTAL_ALIGN_FILL | GridData.VERTICAL_ALIGN_FILL)); |
|---|
| 137 |
treeGroup.setText ("Tree"); |
|---|
| 138 |
|
|---|
| 139 |
/* Create a group for the image tree */ |
|---|
| 140 |
imageTreeGroup = new Group (exampleGroup, SWT.NONE); |
|---|
| 141 |
imageTreeGroup.setLayout (new GridLayout ()); |
|---|
| 142 |
imageTreeGroup.setLayoutData (new GridData (GridData.GRAB_HORIZONTAL | GridData.HORIZONTAL_ALIGN_FILL | GridData.VERTICAL_ALIGN_FILL)); |
|---|
| 143 |
imageTreeGroup.setText (ControlExample.getResourceString("Tree_With_Images")); |
|---|
| 144 |
} |
|---|
| 145 |
|
|---|
| 146 |
/** |
|---|
| 147 |
* Creates the "Example" widgets. |
|---|
| 148 |
*/ |
|---|
| 149 |
void createExampleWidgets () { |
|---|
| 150 |
/* Compute the widget style */ |
|---|
| 151 |
int style = getDefaultStyle(); |
|---|
| 152 |
if (singleButton.getSelection()) style |= SWT.SINGLE; |
|---|
| 153 |
if (multiButton.getSelection()) style |= SWT.MULTI; |
|---|
| 154 |
if (checkButton.getSelection()) style |= SWT.CHECK; |
|---|
| 155 |
if (borderButton.getSelection()) style |= SWT.BORDER; |
|---|
| 156 |
|
|---|
| 157 |
/* Create the text tree */ |
|---|
| 158 |
tree1 = new Tree (treeGroup, style); |
|---|
| 159 |
textNode1 = new TreeItem (tree1, SWT.NONE); |
|---|
| 160 |
textNode1.setText (ControlExample.getResourceString("Node_1")); |
|---|
| 161 |
TreeItem node2 = new TreeItem (tree1, SWT.NONE); |
|---|
| 162 |
node2.setText (ControlExample.getResourceString("Node_2")); |
|---|
| 163 |
TreeItem node3 = new TreeItem (tree1, SWT.NONE); |
|---|
| 164 |
node3.setText (ControlExample.getResourceString("Node_3")); |
|---|
| 165 |
TreeItem node4 = new TreeItem (tree1, SWT.NONE); |
|---|
| 166 |
node4.setText (ControlExample.getResourceString("Node_4")); |
|---|
| 167 |
TreeItem node1_1 = new TreeItem (textNode1, SWT.NONE); |
|---|
| 168 |
node1_1.setText (ControlExample.getResourceString("Node_1_1")); |
|---|
| 169 |
TreeItem node2_1 = new TreeItem (node2, SWT.NONE); |
|---|
| 170 |
node2_1.setText (ControlExample.getResourceString("Node_2_1")); |
|---|
| 171 |
TreeItem node3_1 = new TreeItem (node3, SWT.NONE); |
|---|
| 172 |
node3_1.setText (ControlExample.getResourceString("Node_3_1")); |
|---|
| 173 |
TreeItem node2_2 = new TreeItem (node2, SWT.NONE); |
|---|
| 174 |
node2_2.setText (ControlExample.getResourceString("Node_2_2")); |
|---|
| 175 |
TreeItem node2_2_1 = new TreeItem (node2_2, SWT.NONE); |
|---|
| 176 |
node2_2_1.setText (ControlExample.getResourceString("Node_2_2_1")); |
|---|
| 177 |
|
|---|
| 178 |
/* Create the image tree */ |
|---|
| 179 |
tree2 = new Tree (imageTreeGroup, style); |
|---|
| 180 |
imageNode1 = new TreeItem (tree2, SWT.NONE); |
|---|
| 181 |
imageNode1.setText (ControlExample.getResourceString("Node_1")); |
|---|
| 182 |
imageNode1.setImage (instance_ren.images[ControlExample.ciClosedFolder]); |
|---|
| 183 |
node2 = new TreeItem (tree2, SWT.NONE); |
|---|
| 184 |
node2.setText (ControlExample.getResourceString("Node_2")); |
|---|
| 185 |
node2.setImage (instance_ren.images[ControlExample.ciClosedFolder]); |
|---|
| 186 |
node3 = new TreeItem (tree2, SWT.NONE); |
|---|
| 187 |
node3.setText (ControlExample.getResourceString("Node_3")); |
|---|
| 188 |
node3.setImage (instance_ren.images[ControlExample.ciClosedFolder]); |
|---|
| 189 |
node4 = new TreeItem (tree2, SWT.NONE); |
|---|
| 190 |
node4.setText (ControlExample.getResourceString("Node_4")); |
|---|
| 191 |
node4.setImage (instance_ren.images[ControlExample.ciClosedFolder]); |
|---|
| 192 |
node1_1 = new TreeItem (imageNode1, SWT.NONE); |
|---|
| 193 |
node1_1.setText (ControlExample.getResourceString("Node_1_1")); |
|---|
| 194 |
node1_1.setImage (instance_ren.images[ControlExample.ciClosedFolder]); |
|---|
| 195 |
node2_1 = new TreeItem (node2, SWT.NONE); |
|---|
| 196 |
node2_1.setText (ControlExample.getResourceString("Node_2_1")); |
|---|
| 197 |
node2_1.setImage (instance_ren.images[ControlExample.ciClosedFolder]); |
|---|
| 198 |
node3_1 = new TreeItem (node3, SWT.NONE); |
|---|
| 199 |
node3_1.setText (ControlExample.getResourceString("Node_3_1")); |
|---|
| 200 |
node3_1.setImage (instance_ren.images[ControlExample.ciClosedFolder]); |
|---|
| 201 |
node2_2 = new TreeItem(node2, SWT.NONE); |
|---|
| 202 |
node2_2.setText (ControlExample.getResourceString("Node_2_2")); |
|---|
| 203 |
node2_2.setImage (instance_ren.images[ControlExample.ciClosedFolder]); |
|---|
| 204 |
node2_2_1 = new TreeItem (node2_2, SWT.NONE); |
|---|
| 205 |
node2_2_1.setText (ControlExample.getResourceString("Node_2_2_1")); |
|---|
| 206 |
node2_2_1.setImage (instance_ren.images[ControlExample.ciClosedFolder]); |
|---|
| 207 |
} |
|---|
| 208 |
|
|---|
| 209 |
/** |
|---|
| 210 |
* Creates the "Style" group. |
|---|
| 211 |
*/ |
|---|
| 212 |
void createStyleGroup() { |
|---|
| 213 |
super.createStyleGroup(); |
|---|
| 214 |
|
|---|
| 215 |
/* Create the extra widgets */ |
|---|
| 216 |
checkButton = new Button (styleGroup, SWT.CHECK); |
|---|
| 217 |
checkButton.setText ("SWT.CHECK"); |
|---|
| 218 |
} |
|---|
| 219 |
|
|---|
| 220 |
/** |
|---|
| 221 |
* Gets the "Example" widget children. |
|---|
| 222 |
*/ |
|---|
| 223 |
Control [] getExampleWidgets () { |
|---|
| 224 |
Control[] controls; |
|---|
| 225 |
controls ~= tree1; |
|---|
| 226 |
controls ~= tree2; |
|---|
| 227 |
return controls; |
|---|
| 228 |
} |
|---|
| 229 |
|
|---|
| 230 |
/** |
|---|
| 231 |
* Gets the text for the tab folder item. |
|---|
| 232 |
*/ |
|---|
| 233 |
String getTabText () { |
|---|
| 234 |
return "Tree"; |
|---|
| 235 |
} |
|---|
| 236 |
|
|---|
| 237 |
/** |
|---|
| 238 |
* Sets the foreground color, background color, and font |
|---|
| 239 |
* of the "Example" widgets to their default settings. |
|---|
| 240 |
* Also sets foreground and background color of the Node 1 |
|---|
| 241 |
* TreeItems to default settings. |
|---|
| 242 |
*/ |
|---|
| 243 |
void resetColorsAndFonts () { |
|---|
| 244 |
super.resetColorsAndFonts (); |
|---|
| 245 |
Color oldColor = itemForegroundColor; |
|---|
| 246 |
itemForegroundColor = null; |
|---|
| 247 |
setItemForeground (); |
|---|
| 248 |
if (oldColor !== null) oldColor.dispose(); |
|---|
| 249 |
oldColor = itemBackgroundColor; |
|---|
| 250 |
itemBackgroundColor = null; |
|---|
| 251 |
setItemBackground (); |
|---|
| 252 |
if (oldColor !== null) oldColor.dispose(); |
|---|
| 253 |
Font oldFont = font; |
|---|
| 254 |
itemFont = null; |
|---|
| 255 |
setItemFont (); |
|---|
| 256 |
setExampleWidgetSize (); |
|---|
| 257 |
if (oldFont !== null) oldFont.dispose(); |
|---|
| 258 |
} |
|---|
| 259 |
|
|---|
| 260 |
/** |
|---|
| 261 |
* Sets the state of the "Example" widgets. |
|---|
| 262 |
*/ |
|---|
| 263 |
void setExampleWidgetState () { |
|---|
| 264 |
super.setExampleWidgetState (); |
|---|
| 265 |
setItemBackground (); |
|---|
| 266 |
setItemForeground (); |
|---|
| 267 |
setItemFont (); |
|---|
| 268 |
setExampleWidgetSize (); |
|---|
| 269 |
checkButton.setSelection ((tree1.getStyle () & SWT.CHECK) != 0); |
|---|
| 270 |
} |
|---|
| 271 |
|
|---|
| 272 |
/** |
|---|
| 273 |
* Sets the background color of the Node 1 TreeItems. |
|---|
| 274 |
*/ |
|---|
| 275 |
void setItemBackground () { |
|---|
| 276 |
textNode1.setBackground (itemBackgroundColor); |
|---|
| 277 |
imageNode1.setBackground (itemBackgroundColor); |
|---|
| 278 |
/* Set the background button's color to match the color just set. */ |
|---|
| 279 |
Color color = itemBackgroundColor; |
|---|
| 280 |
if (color is null) color = textNode1.getBackground (); |
|---|
| 281 |
drawImage (itemBackgroundImage, color); |
|---|
| 282 |
itemBackgroundButton.setImage (itemBackgroundImage); |
|---|
| 283 |
} |
|---|
| 284 |
|
|---|
| 285 |
/** |
|---|
| 286 |
* Sets the foreground color of the Node 1 TreeItems. |
|---|
| 287 |
*/ |
|---|
| 288 |
void setItemForeground () { |
|---|
| 289 |
textNode1.setForeground (itemForegroundColor); |
|---|
| 290 |
imageNode1.setForeground (itemForegroundColor); |
|---|
| 291 |
/* Set the foreground button's color to match the color just set. */ |
|---|
| 292 |
Color color = itemForegroundColor; |
|---|
| 293 |
if (color is null) color = textNode1.getForeground (); |
|---|
| 294 |
drawImage (itemForegroundImage, color); |
|---|
| 295 |
itemForegroundButton.setImage (itemForegroundImage); |
|---|
| 296 |
} |
|---|
| 297 |
|
|---|
| 298 |
/** |
|---|
| 299 |
* Sets the font of the Node 1 TreeItems. |
|---|
| 300 |
*/ |
|---|
| 301 |
void setItemFont () { |
|---|
| 302 |
if (instance_ren.startup) return; |
|---|
| 303 |
textNode1.setFont (itemFont); |
|---|
| 304 |
imageNode1.setFont (itemFont); |
|---|
| 305 |
} |
|---|
| 306 |
} |
|---|