root/branches/0.2/controlexample/tabletreetab.d

Revision 46, 13.0 kB (checked in by JJR, 4 years ago)

* 0.2 Branch added: New working DWT version submitted by Shawn Liu.

Line 
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.tabletreetab;
12
13 private {
14 import swt.all;
15 import controlexample.scrollabletab;
16 import controlexample.controlexample;
17 }
18
19
20 class TableTreeTab : ScrollableTab {
21     /* Example widgets and groups that contain them */
22     TableTree tree1;
23     TableTreeItem node1;
24     Group treeGroup, itemGroup;
25    
26     /* Style widgets added to the "Style" group */
27     Button checkButton, fullSelectionButton;
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     /* Other widgets added to the "Other" group */
36     Button headerVisibleButton, linesVisibleButton;
37
38     /**
39      * Creates the Tab within a given instance_ren of ControlExample.
40      */
41     this(ControlExample instance_ren) {
42         super(instance_ren);
43     }
44    
45     /**
46      * Creates the "Colors" group.
47      */
48     void createColorGroup () {
49         super.createColorGroup();
50        
51         itemGroup = new Group (colorGroup, SWT.NONE);
52         itemGroup.setText (ControlExample.getResourceString ("Tree_Item_Colors"));
53         GridData data = new GridData ();
54         data.horizontalSpan = 2;
55         itemGroup.setLayoutData (data);
56         itemGroup.setLayout (new GridLayout (2, false));
57         Label label = new Label (colorGroup, SWT.NONE);
58         label.setText (ControlExample.getResourceString ("Foreground_Color"));
59         itemForegroundButton = new Button (itemGroup, SWT.PUSH);
60         label = new Label (colorGroup, SWT.NONE);
61         label.setText (ControlExample.getResourceString ("Background_Color"));
62         itemBackgroundButton = new Button (itemGroup, SWT.PUSH);
63         itemFontButton = new Button (itemGroup, SWT.PUSH);
64         itemFontButton.setText(ControlExample.getResourceString("Font"));
65         itemFontButton.setLayoutData(new GridData (GridData.HORIZONTAL_ALIGN_FILL));
66        
67         Shell shell = colorGroup.getShell ();
68        
69         int imageSize = 12;
70         Display display = shell.getDisplay ();
71         itemForegroundImage = new Image(display, imageSize, imageSize);
72         itemBackgroundImage = new Image(display, imageSize, imageSize);
73
74         /* Add listeners to set the colors and font */
75         itemForegroundButton.setImage(itemForegroundImage);
76         itemForegroundButton.handleSelection(this, delegate(SelectionEvent event) {
77             TableTreeTab c = cast(TableTreeTab)event.cData;
78             Color oldColor = c.itemForegroundColor;
79             if (oldColor is null) oldColor = c.node1.getForeground ();
80             ColorDialog foregroundDialog = new ColorDialog (c.colorGroup.getShell());
81             foregroundDialog.setRGB(oldColor.getRGB());
82             RGB rgb = foregroundDialog.open();
83             if (rgb is null) return;
84             oldColor = c.itemForegroundColor;
85             c.itemForegroundColor = new Color (event.display, rgb);
86             c.setItemForeground ();
87             if (oldColor !== null) oldColor.dispose ();
88         });
89         itemBackgroundButton.setImage(itemBackgroundImage);
90         itemBackgroundButton.handleSelection(this, delegate(SelectionEvent event) {
91             TableTreeTab c = cast(TableTreeTab)event.cData;
92             Color oldColor = c.itemBackgroundColor;
93             if (oldColor is null) oldColor = c.node1.getBackground ();
94             ColorDialog backgroundDialog = new ColorDialog (c.colorGroup.getShell());
95             backgroundDialog.setRGB(oldColor.getRGB());
96             RGB rgb = backgroundDialog.open();
97             if (rgb is null) return;
98             oldColor = c.itemBackgroundColor;
99             c.itemBackgroundColor = new Color (event.display, rgb);
100             c.setItemBackground ();
101             if (oldColor !== null) oldColor.dispose ();
102         });
103         itemFontButton.handleSelection(this, delegate(SelectionEvent event) {
104             TableTreeTab c = cast(TableTreeTab)event.cData;
105             Font oldFont = c.itemFont;
106             if (oldFont is null) oldFont = c.node1.getFont ();
107             FontDialog fontDialog = new FontDialog (c.colorGroup.getShell());
108             fontDialog.setFontList(oldFont.getFontData());
109             FontData fontData = fontDialog.open ();
110             if (fontData is null) return;
111             oldFont = c.itemFont;
112             c.itemFont = new Font (event.display, fontData);
113             c.setItemFont ();
114             c.setExampleWidgetSize ();
115             if (oldFont !== null) oldFont.dispose ();
116         });
117         shell.handleDispose(this, delegate(DisposeEvent event) {
118             TableTreeTab c = cast(TableTreeTab)event.cData;
119             if (c.itemBackgroundImage !== null) c.itemBackgroundImage.dispose();
120             if (c.itemForegroundImage !== null) c.itemForegroundImage.dispose();
121             if (c.itemBackgroundColor !== null) c.itemBackgroundColor.dispose();
122             if (c.itemForegroundColor !== null) c.itemForegroundColor.dispose();
123             if (c.itemFont !== null) c.itemFont.dispose();
124             c.itemBackgroundColor = null;
125             c.itemForegroundColor = null;           
126             c.itemFont = null;
127         });
128     }
129
130     /**
131      * Creates the "Other" group.
132      */
133     void createOtherGroup () {
134         super.createOtherGroup ();
135    
136         /* Create display controls specific to this example */
137         headerVisibleButton = new Button (otherGroup, SWT.CHECK);
138         headerVisibleButton.setText (ControlExample.getResourceString("Header_Visible"));
139         linesVisibleButton = new Button (otherGroup, SWT.CHECK);
140         linesVisibleButton.setText (ControlExample.getResourceString("Lines_Visible"));
141    
142         /* Add the listeners */
143         headerVisibleButton.handleEvent(this, SWT.Selection, &setWidgetHeaderVisible);
144         linesVisibleButton.handleEvent(this, SWT.Selection, &setWidgetLinesVisible);
145     }
146
147     /**
148      * Creates the "Example" group.
149      */
150     void createExampleGroup () {
151         super.createExampleGroup ();
152        
153         /* Create a group for the text tree */
154         treeGroup = new Group (exampleGroup, SWT.NONE);
155         treeGroup.setLayout (new GridLayout ());
156         treeGroup.setLayoutData (new GridData (GridData.GRAB_HORIZONTAL | GridData.HORIZONTAL_ALIGN_FILL | GridData.VERTICAL_ALIGN_FILL));
157         treeGroup.setText ("TableTree");
158     }
159    
160     /**
161      * Creates the "Example" widgets.
162      */
163     void createExampleWidgets () {
164         /* Compute the widget style */
165         int style = getDefaultStyle();
166         if (singleButton.getSelection()) style |= SWT.SINGLE;
167         if (multiButton.getSelection()) style |= SWT.MULTI;
168         if (checkButton.getSelection()) style |= SWT.CHECK;
169         if (borderButton.getSelection()) style |= SWT.BORDER;
170         if (fullSelectionButton.getSelection()) style |= SWT.FULL_SELECTION;
171    
172         /* Create the text tree */
173         tree1 = new TableTree (treeGroup, style);
174         Table table = tree1.getTable();
175         for (int i = 0; i < 3; i++) {
176             TableColumn column = new TableColumn (table,SWT.NONE);
177             column.setWidth(100);
178             column.setText(ControlExample.getResourceString("TableTree_column") ~ ": " ~ std.string.toString(i));
179         }
180         node1 = new TableTreeItem (tree1, SWT.NONE);
181         for (int i = 0; i < 3; i++) {
182             node1.setText (i, ControlExample.getResourceString("Node_1") ~ "-" ~ std.string.toString(i));
183         }
184         node1.setImage (instance_ren.images[ControlExample.ciOpenFolder]);
185         TableTreeItem node2 = new TableTreeItem (tree1, SWT.NONE);
186         for (int i = 0; i < 3; i++) {
187             node2.setText (i, ControlExample.getResourceString("Node_2") ~ "-" ~ std.string.toString(i));
188         }
189         node2.setImage (instance_ren.images[ControlExample.ciClosedFolder]);
190         TableTreeItem node3 = new TableTreeItem (tree1, SWT.NONE);
191         for (int i = 0; i < 3; i++) {
192             node3.setText (i, ControlExample.getResourceString("Node_3") ~ "-" ~ std.string.toString(i));
193         }
194         node3.setImage (instance_ren.images[ControlExample.ciTarget]);
195         TableTreeItem node4 = new TableTreeItem (tree1, SWT.NONE);
196         for (int i = 0; i < 3; i++) {
197             node4.setText (i, ControlExample.getResourceString("Node_4") ~ "-" ~ std.string.toString(i));
198         }
199         node4.setImage (instance_ren.images[ControlExample.ciOpenFolder]);
200         TableTreeItem node1_1 = new TableTreeItem (node1, SWT.NONE);
201         for (int i = 0; i < 3; i++) {
202             node1_1.setText (i, ControlExample.getResourceString("Node_1_1") ~ "-" ~ std.string.toString(i));
203         }
204         node1_1.setImage (instance_ren.images[ControlExample.ciClosedFolder]);
205         TableTreeItem node2_1 = new TableTreeItem (node2, SWT.NONE);
206         for (int i = 0; i < 3; i++) {
207             node2_1.setText (i, ControlExample.getResourceString("Node_2_1") ~ "-" ~ std.string.toString(i));
208         }
209         node2_1.setImage (instance_ren.images[ControlExample.ciTarget]);
210         TableTreeItem node3_1 = new TableTreeItem (node3, SWT.NONE);
211         for (int i = 0; i < 3; i++) {
212             node3_1.setText (i, ControlExample.getResourceString("Node_3_1") ~ "-" ~ std.string.toString(i));
213         }
214         node3_1.setImage (instance_ren.images[ControlExample.ciOpenFolder]);
215         TableTreeItem node2_2 = new TableTreeItem (node2, SWT.NONE);
216         for (int i = 0; i < 3; i++) {
217             node2_2.setText (i, ControlExample.getResourceString("Node_2_2") ~ "-" ~ std.string.toString(i));
218         }
219         node2_2.setImage (instance_ren.images[ControlExample.ciClosedFolder]);
220         TableTreeItem node2_2_1 = new TableTreeItem (node2_2, SWT.NONE);
221         for (int i = 0; i < 3; i++) {
222             node2_2_1.setText (i, ControlExample.getResourceString("Node_2_2_1") ~ "-" ~ std.string.toString(i));
223         }
224         node2_2_1.setImage (instance_ren.images[ControlExample.ciTarget]);
225     }
226    
227     /**
228      * Creates the "Style" group.
229      */
230     void createStyleGroup() {
231         super.createStyleGroup();
232        
233         /* Create the extra widgets */
234         checkButton = new Button (styleGroup, SWT.CHECK);
235         checkButton.setText ("SWT.CHECK");
236         fullSelectionButton = new Button (styleGroup, SWT.CHECK);
237         fullSelectionButton.setText ("SWT.FULL_SELECTION");
238     }
239    
240     /**
241      * Creates the tab folder page.
242      *
243      * @param tabFolder org.eclipse.swt.widgets.TabFolder
244      * @return the new page for the tab folder
245      */
246     Composite createTabFolderPage (TabFolder tabFolder) {
247         super.createTabFolderPage (tabFolder);
248
249         /*
250          * Add a resize listener to the tabFolderPage so that
251          * if the user types into the example widget to change
252          * its preferred size, and then resizes the shell, we
253          * recalculate the preferred size correctly.
254          */
255         tabFolderPage.handleEvent(this, SWT.Resize, &setExampleWidgetSize);
256        
257         return tabFolderPage;
258     }
259
260     /**
261      * Gets the "Example" widget children's items, if any.
262      *
263      * @return an array containing the example widget children's items
264      */
265     Item [] getExampleWidgetItems () {
266         return tree1.getItems();
267     }
268    
269     /**
270      * Gets the "Example" widget children.
271      */
272     Control [] getExampleWidgets () {
273         Control [] controls;
274         controls ~= tree1;
275         return controls;
276     }
277    
278     /**
279      * Gets the text for the tab folder item.
280      */
281     String getTabText () {
282         return "TableTree";
283     }
284
285     /**
286      * Sets the foreground color, background color, and font
287      * of the "Example" widgets to their default settings.
288      * Also sets foreground and background color of the Node 1
289      * TreeItems to default settings.
290      */
291     void resetColorsAndFonts () {
292         super.resetColorsAndFonts ();
293         Color oldColor = itemForegroundColor;
294         itemForegroundColor = null;
295         setItemForeground ();
296         if (oldColor !== null) oldColor.dispose();
297         oldColor = itemBackgroundColor;
298         itemBackgroundColor = null;
299         setItemBackground ();
300         if (oldColor !== null) oldColor.dispose();
301         Font oldFont = font;
302         itemFont = null;
303         setItemFont ();
304         setExampleWidgetSize ();
305         if (oldFont !== null) oldFont.dispose();
306     }
307    
308     /**
309      * Sets the state of the "Example" widgets.
310      */
311     void setExampleWidgetState () {
312         super.setExampleWidgetState ();
313         setItemBackground ();
314         setItemForeground ();
315         setItemFont ();
316         setExampleWidgetSize ();
317         setWidgetHeaderVisible ();
318         setWidgetLinesVisible ();
319     }
320    
321     /**
322      * Sets the background color of the Node 1 TreeItems.
323      */
324     void setItemBackground () {
325         node1.setBackground (itemBackgroundColor);
326         /* Set the background button's color to match the color just set. */
327         Color color = itemBackgroundColor;
328         if (color is null) color = node1.getBackground ();
329         drawImage (itemBackgroundImage, color);
330         itemBackgroundButton.setImage (itemBackgroundImage);
331     }
332    
333     /**
334      * Sets the foreground color of the Node 1 TreeItems.
335      */
336     void setItemForeground () {
337         node1.setForeground (itemForegroundColor);
338         /* Set the foreground button's color to match the color just set. */
339         Color color = itemForegroundColor;
340         if (color is null) color = node1.getForeground ();
341         drawImage (itemForegroundImage, color);
342         itemForegroundButton.setImage (itemForegroundImage);
343     }
344    
345     /**
346      * Sets the font of the Node 1 TreeItems.
347      */
348     void setItemFont () {
349         if (instance_ren.startup) return;
350         node1.setFont (itemFont);
351     }
352
353     /**
354      * Sets the header visible state of the "Example" widgets.
355      */
356     void setWidgetHeaderVisible () {
357         tree1.getTable().setHeaderVisible (headerVisibleButton.getSelection ());
358     }
359    
360     /**
361      * Sets the lines visible state of the "Example" widgets.
362      */
363     void setWidgetLinesVisible () {
364         tree1.getTable().setLinesVisible (linesVisibleButton.getSelection ());
365     }
366 }
Note: See TracBrowser for help on using the browser.