KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ui > internal > dialogs > PreferencesPageContainer


1 /*******************************************************************************
2  * Copyright (c) 2004, 2006 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  *******************************************************************************/

11 package org.eclipse.ui.internal.dialogs;
12
13 import org.eclipse.jface.dialogs.IDialogConstants;
14 import org.eclipse.jface.preference.IPreferenceNode;
15 import org.eclipse.jface.preference.IPreferencePageContainer;
16 import org.eclipse.jface.preference.IPreferenceStore;
17 import org.eclipse.jface.resource.JFaceResources;
18 import org.eclipse.swt.SWT;
19 import org.eclipse.swt.custom.ScrolledComposite;
20 import org.eclipse.swt.events.MouseAdapter;
21 import org.eclipse.swt.events.MouseEvent;
22 import org.eclipse.swt.graphics.Color;
23 import org.eclipse.swt.graphics.Font;
24 import org.eclipse.swt.graphics.Point;
25 import org.eclipse.swt.graphics.Rectangle;
26 import org.eclipse.swt.layout.FormAttachment;
27 import org.eclipse.swt.layout.FormData;
28 import org.eclipse.swt.layout.FormLayout;
29 import org.eclipse.swt.layout.GridData;
30 import org.eclipse.swt.layout.GridLayout;
31 import org.eclipse.swt.widgets.Composite;
32 import org.eclipse.swt.widgets.Control;
33 import org.eclipse.swt.widgets.Label;
34
35 /**
36  * The PreferencesPageContainer is the container object for the preference pages
37  * in a node.
38  */

39 public class PreferencesPageContainer implements IPreferencePageContainer {
40
41     private Composite control;
42
43     private ScrolledComposite scrolled;
44
45     private class PreferenceEntry {
46
47         Composite composite;
48
49         IPreferenceNode node;
50
51         String JavaDoc title;
52
53         int offset;
54
55         Label expandImage;
56
57         Label titleLabel;
58
59         Composite pageContainer;
60
61         /**
62          * Create a new instance of the receiver.
63          *
64          * @param displayedNode
65          * @param pageTitle
66          */

67         PreferenceEntry(IPreferenceNode displayedNode, String JavaDoc pageTitle) {
68             node = displayedNode;
69             title = pageTitle;
70         }
71
72         /**
73          * Add the subnodes of the receiver.
74          */

75         private void addSubNodes() {
76             IPreferenceNode[] subnodes = node.getSubNodes();
77             PreferenceEntry previous = null;
78             for (int i = 0; i < subnodes.length; i++) {
79                 PreferenceEntry entry = createEntry(subnodes[i], subnodes[i]
80                         .getLabelText(), offset + 1);
81                 if (previous == null) {
82                     entry.composite.moveBelow(this.composite);
83                 } else {
84                     entry.composite.moveBelow(previous.composite);
85                 }
86                 previous = entry;
87             }
88         }
89
90         /**
91          * Create the contents of the entry in parent. When laying this out
92          * indent the composite ident units.
93          *
94          * @param indent
95          */

96         void createContents(int indent) {
97
98             composite = new Composite(control, SWT.NULL);
99
100             // Create the title area which will contain
101
// a title, message, and image.
102

103             GridData gridData = new GridData(GridData.FILL_HORIZONTAL);
104             gridData.horizontalIndent = IDialogConstants.SMALL_INDENT * indent;
105             composite.setLayoutData(gridData);
106
107             FormLayout layout = new FormLayout();
108             layout.marginHeight = 0;
109             layout.marginWidth = 0;
110             composite.setLayout(layout);
111
112             Font titleFont = JFaceResources.getBannerFont();
113
114             expandImage = new Label(composite, SWT.RIGHT);
115             expandImage.setText("+");//$NON-NLS-1$
116
expandImage.setFont(titleFont);
117
118             FormData imageData = new FormData();
119             imageData.top = new FormAttachment(0);
120             imageData.left = new FormAttachment(0,
121                     IDialogConstants.HORIZONTAL_SPACING);
122             expandImage.setLayoutData(imageData);
123
124             // Title image
125
titleLabel = new Label(composite, SWT.LEFT);
126             titleLabel.setText(title);
127             titleLabel.setFont(titleFont);
128
129             FormData titleData = new FormData();
130             titleData.right = new FormAttachment(100);
131             titleData.top = new FormAttachment(0);
132             titleData.left = new FormAttachment(expandImage,
133                     IDialogConstants.HORIZONTAL_SPACING);
134             titleLabel.setLayoutData(titleData);
135
136             titleLabel.addMouseListener(new MouseAdapter() {
137                 /*
138                  * (non-Javadoc)
139                  *
140                  * @see org.eclipse.swt.events.MouseAdapter#mouseDown(org.eclipse.swt.events.MouseEvent)
141                  */

142                 public void mouseDown(MouseEvent e) {
143
144                     if (pageContainer == null) {
145                         boolean adjustScrollbars = false;
146
147                         pageContainer = new Composite(composite, SWT.BORDER);
148
149                         FormData containerData = new FormData();
150                         containerData.top = new FormAttachment(titleLabel, 0);
151                         containerData.left = new FormAttachment(0);
152                         containerData.right = new FormAttachment(100);
153                         pageContainer.setLayoutData(containerData);
154
155                         pageContainer.setLayout(new GridLayout());
156
157                         node.createPage();
158                         node.getPage().createControl(pageContainer);
159                         node.getPage().setContainer(
160                                 PreferencesPageContainer.this);
161                         node.getPage().getControl().setLayoutData(
162                                 new GridData(GridData.FILL_BOTH));
163                         adjustScrollbars = true;
164
165                         Point contentSize = node.getPage().computeSize();
166                         Rectangle totalArea = composite.getClientArea();
167
168                         if (contentSize.x < totalArea.width) {
169                             contentSize.x = totalArea.width;
170                         }
171
172                         node.getPage().setSize(contentSize);
173                         if (adjustScrollbars) {
174                             adjustScrollbars(contentSize);
175                         }
176
177                         expandImage.setText("-");//$NON-NLS-1$
178

179                         addSubNodes();
180
181                         setSelectionColors(composite.getDisplay()
182                                 .getSystemColor(SWT.COLOR_INFO_BACKGROUND));
183                     } else {
184                         setSelectionColors(null);
185                         pageContainer.dispose();
186                         pageContainer = null;
187                         expandImage.setText("+");//$NON-NLS-1$
188
}
189
190                     control.layout(true);
191                 }
192             });
193             offset = indent;
194         }
195
196         /**
197          * Set the background colors and the labels due to selection.
198          *
199          * @param highlight
200          * The highlight to set
201          */

202         private void setSelectionColors(Color highlight) {
203             composite.setBackground(highlight);
204             titleLabel.setBackground(highlight);
205             expandImage.setBackground(highlight);
206         }
207
208     }
209
210     /**
211      * Create a new instance of the receiver.
212      */

213     public PreferencesPageContainer() {
214         super();
215     }
216
217     /**
218      * Create the contents area of the composite.
219      *
220      * @param parent
221      * @param style
222      */

223     void createContents(Composite parent, int style) {
224         scrolled = new ScrolledComposite(parent, SWT.V_SCROLL | SWT.H_SCROLL);
225
226         GridData newPageData = new GridData(GridData.FILL_BOTH);
227         scrolled.setLayoutData(newPageData);
228
229         control = new Composite(scrolled, style);
230
231         scrolled.setContent(control);
232         scrolled.setExpandVertical(true);
233         scrolled.setExpandHorizontal(true);
234         GridData controlData = new GridData(GridData.FILL_BOTH);
235         control.setLayoutData(controlData);
236
237         GridLayout layout = new GridLayout();
238         layout.marginHeight = 0;
239         layout.marginWidth = 0;
240         layout.verticalSpacing = 1;
241         control.setLayout(layout);
242
243     }
244
245     /**
246      * Return the top level control
247      *
248      * @return Control
249      */

250     Control getControl() {
251         return control;
252     }
253
254     /**
255      * Show the selected node. Return whether or not this succeeded.
256      *
257      * @param node
258      * @return <code>true</code> if the page selection was sucessful
259      * <code>false</code> is unsuccessful
260      */

261     boolean show(IPreferenceNode node) {
262         createGeneralEntry(node);
263         control.layout(true);
264         return true;
265     }
266
267     /**
268      * Create an entry for the receiver with the general tag. Do not recurse
269      * through the children as this is the implied top node.
270      *
271      * @param node
272      */

273     private void createGeneralEntry(IPreferenceNode node) {
274         PreferenceEntry entry = createEntry(node, "General", 0); //$NON-NLS-1$
275
entry.addSubNodes();
276
277     }
278
279     /**
280      * Create an entry with the given title for the IPreferenceNode with an
281      * indent i.
282      *
283      * @param node
284      * @param name
285      * @param indent
286      * @return the entry
287      */

288     private PreferenceEntry createEntry(IPreferenceNode node, String JavaDoc name,
289             int indent) {
290         PreferenceEntry entry = new PreferenceEntry(node, name);
291         entry.createContents(indent);
292         return entry;
293     }
294
295     /*
296      * (non-Javadoc)
297      *
298      * @see org.eclipse.jface.preference.IPreferencePageContainer#getPreferenceStore()
299      */

300     public IPreferenceStore getPreferenceStore() {
301         // TODO Auto-generated method stub
302
return null;
303     }
304
305     /*
306      * (non-Javadoc)
307      *
308      * @see org.eclipse.jface.preference.IPreferencePageContainer#updateButtons()
309      */

310     public void updateButtons() {
311         // TODO Auto-generated method stub
312

313     }
314
315     /*
316      * (non-Javadoc)
317      *
318      * @see org.eclipse.jface.preference.IPreferencePageContainer#updateMessage()
319      */

320     public void updateMessage() {
321         // TODO Auto-generated method stub
322

323     }
324
325     /*
326      * (non-Javadoc)
327      *
328      * @see org.eclipse.jface.preference.IPreferencePageContainer#updateTitle()
329      */

330     public void updateTitle() {
331         // TODO Auto-generated method stub
332

333     }
334
335     /**
336      * Adjust the scrollbars for the content that just got added.
337      *
338      * @param contentSize
339      */

340     private void adjustScrollbars(Point contentSize) {
341
342         Point size = control.getSize();
343         scrolled.setMinHeight(size.y + contentSize.y);
344         scrolled.setMinWidth(Math.max(size.x, contentSize.x));
345     }
346
347 }
348
Popular Tags