comparison org.eclipse.ui.forms/src/org/eclipse/ui/forms/widgets/ScrolledPageBook.d @ 12:bc29606a740c

Added dwt-addons in original directory structure of eclipse.org
author Frank Benoit <benoit@tionex.de>
date Sat, 14 Mar 2009 18:23:29 +0100
parents
children dbfb303e8fb0
comparison
equal deleted inserted replaced
11:43904fec5dca 12:bc29606a740c
1 /*******************************************************************************
2 * Copyright (c) 2000, 2007 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 * Port to the D programming language:
11 * Frank Benoit <benoit@tionex.de>
12 *******************************************************************************/
13 module org.eclipse.ui.forms.widgets.ScrolledPageBook;
14
15 import org.eclipse.ui.forms.widgets.SharedScrolledComposite;
16 import org.eclipse.ui.forms.widgets.LayoutComposite;
17
18 import org.eclipse.swt.SWT;
19 import org.eclipse.swt.graphics.Point;
20 import org.eclipse.swt.graphics.Rectangle;
21 import org.eclipse.swt.layout.GridLayout;
22 import org.eclipse.swt.widgets.Composite;
23 import org.eclipse.swt.widgets.Control;
24 import org.eclipse.swt.widgets.Event;
25 import org.eclipse.swt.widgets.Listener;
26 import org.eclipse.ui.internal.forms.widgets.WrappedPageBook;
27
28 import java.lang.all;
29 import java.util.Set;
30
31 /**
32 * ScrolledPageBook is a class that is capable of stacking several composites
33 * (pages), while showing one at a time. The content is scrolled if there is
34 * not enough space to fit it in the client area.
35 *
36 * @since 3.0
37 */
38 public class ScrolledPageBook : SharedScrolledComposite {
39 private WrappedPageBook pageBook;
40 private Hashtable pages;
41 private Composite emptyPage;
42 private Control currentPage;
43 /**
44 * Creates a new instance in the provided parent
45 *
46 * @param parent
47 */
48 public this(Composite parent) {
49 this(parent, SWT.H_SCROLL | SWT.V_SCROLL);
50 }
51 /**
52 * Creates a new instance in the provided parent and with the provided
53 * style.
54 *
55 * @param parent
56 * the control parent
57 * @param style
58 * the style to use
59 */
60 public this(Composite parent, int style) {
61 super(parent, style);
62 pageBook = new WrappedPageBook(this, SWT.NULL);
63 setContent(pageBook);
64 pages = new Hashtable();
65 setExpandHorizontal(true);
66 setExpandVertical(true);
67 this.addListener(SWT.Traverse, dgListener( (Event e) {
68 switch (e.detail) {
69 case SWT.TRAVERSE_ESCAPE :
70 case SWT.TRAVERSE_RETURN :
71 case SWT.TRAVERSE_TAB_NEXT :
72 case SWT.TRAVERSE_TAB_PREVIOUS :
73 e.doit = true;
74 break;
75 default:
76 }
77 }));
78 }
79 /**
80 * Removes the default size of the composite, allowing the control to
81 * shrink to the trim.
82 *
83 * @param wHint
84 * the width hint
85 * @param hHint
86 * the height hint
87 * @param changed
88 * if <code>true</code>, do not use cached values
89 */
90 public Point computeSize(int wHint, int hHint, bool changed) {
91 Rectangle trim = computeTrim(0, 0, 10, 10);
92 return new Point(trim.width, trim.height);
93 }
94 /**
95 * Tests if the page under the provided key is currently in the book.
96 *
97 * @param key
98 * the page key
99 * @return <code>true</code> if page exists, <code>false</code>
100 * otherwise.
101 */
102 public bool hasPage(Object key) {
103 return pages.containsKey(key);
104 }
105 /**
106 * Creates a new page for the provided key. Use the returned composite to
107 * create children in it.
108 *
109 * @param key
110 * the page key
111 * @return the newly created page composite
112 */
113 public Composite createPage(Object key) {
114 Composite page = createPage();
115 pages.put(key, page);
116 return page;
117 }
118 /**
119 * Returns the page book container.
120 *
121 * @return the page book container
122 */
123 public Composite getContainer() {
124 return pageBook;
125 }
126 /**
127 * Registers a page under the privided key to be managed by the page book.
128 * The page must be a direct child of the page book container.
129 *
130 * @param key
131 * the page key
132 * @param page
133 * the page composite to register
134 * @see #createPage(Object)
135 * @see #getContainer
136 */
137 public void registerPage(Object key, Control page) {
138 pages.put(key, page);
139 }
140 /**
141 * Removes the page under the provided key from the page book. Does nothing
142 * if page with that key does not exist.
143 *
144 * @param key
145 * the page key.
146 */
147 public void removePage(Object key) {
148 removePage(key, true);
149 }
150 /**
151 * Removes the page under the provided key from the page book. Does nothing
152 * if page with that key does not exist.
153 *
154 * @param key
155 * the page key.
156 * @param showEmptyPage
157 * if <code>true</code>, shows the empty page
158 * after page removal.
159 */
160 public void removePage(Object key, bool showEmptyPage_) {
161 Control page = cast(Control) pages.get(key);
162 if (page !is null) {
163 pages.remove(key);
164 page.dispose();
165 if (showEmptyPage_)
166 showEmptyPage();
167 }
168 }
169 /**
170 * Shows the page with the provided key and hides the page previously
171 * showing. Does nothing if the page with that key does not exist.
172 *
173 * @param key
174 * the page key
175 */
176 public void showPage(Object key) {
177 Control page = cast(Control) pages.get(key);
178 if (page !is null) {
179 pageBook.showPage(page);
180 if (currentPage !is null && currentPage !is page) {
181 // switching pages - force layout
182 if (null !is cast(Composite)page )
183 (cast(Composite) page).layout(false);
184 }
185 currentPage = page;
186 } else {
187 showEmptyPage();
188 }
189 reflow(true);
190 }
191 /**
192 * Shows a page with no children to be used if the desire is to not show
193 * any registered page.
194 */
195 public void showEmptyPage() {
196 if (emptyPage is null) {
197 emptyPage = createPage();
198 emptyPage.setLayout(new GridLayout());
199 }
200 pageBook.showPage(emptyPage);
201 currentPage = emptyPage;
202 reflow(true);
203 }
204 /**
205 * Sets focus on the current page if shown.
206 */
207 public bool setFocus() {
208 if (currentPage !is null)
209 return currentPage.setFocus();
210 return super.setFocus();
211 }
212 /**
213 * Returns the page currently showing.
214 *
215 * @return the current page
216 */
217 public Control getCurrentPage() {
218 return currentPage;
219 }
220 private Composite createPage() {
221 Composite page = new LayoutComposite(pageBook, SWT.NULL);
222 page.setBackground(getBackground());
223 page.setForeground(getForeground());
224 page.setMenu(pageBook.getMenu());
225 return page;
226 }
227 }