KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > help > ui > RootScopePage


1 /*******************************************************************************
2  * Copyright (c) 2000, 2005 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.help.ui;
12
13 import java.util.Hashtable JavaDoc;
14
15 import org.eclipse.help.ui.internal.Messages;
16 import org.eclipse.help.ui.internal.views.EngineDescriptor;
17 import org.eclipse.help.ui.internal.views.ScopeSet;
18 import org.eclipse.jface.preference.IPreferenceStore;
19 import org.eclipse.jface.preference.PreferencePage;
20 import org.eclipse.swt.SWT;
21 import org.eclipse.swt.events.SelectionAdapter;
22 import org.eclipse.swt.events.SelectionEvent;
23 import org.eclipse.swt.layout.GridData;
24 import org.eclipse.swt.layout.GridLayout;
25 import org.eclipse.swt.widgets.Button;
26 import org.eclipse.swt.widgets.Composite;
27 import org.eclipse.swt.widgets.Control;
28 import org.eclipse.swt.widgets.Label;
29 import org.eclipse.swt.widgets.Text;
30
31 /**
32  * Clients that contribute search scope root page to the search engine
33  * definition must extend this class and implement
34  * <code>createScopeContents</code> method. The page will come preset with the
35  * engine name, image and description, as well as the master switch that turns
36  * the engine on or off. When the engine master switch is set to false, all the
37  * children in the client composite will be disabled.
38  *
39  * @since 3.1
40  */

41 public abstract class RootScopePage extends PreferencePage implements
42         ISearchScopePage {
43     private IEngineDescriptor ed;
44
45     private String JavaDoc scopeSetName;
46
47     private Button masterButton;
48
49     private Text labelText;
50
51     private Text descText;
52
53     private Hashtable JavaDoc disabledStates = new Hashtable JavaDoc();
54
55     /**
56      * The default constructor.
57      */

58     public RootScopePage() {
59     }
60
61     /*
62      * (non-Javadoc)
63      *
64      * @see ISearchScopePage#init(IEngineDescriptor, String)
65      */

66     public void init(IEngineDescriptor ed, String JavaDoc scopeSetName) {
67         this.ed = ed;
68         this.scopeSetName = scopeSetName;
69     }
70
71     /**
72      * Creates the initial contents of the page and allocates the area for the
73      * clients. Classes that extend this class should implement
74      * <code>createScopeContents(Composite)</code> instead.
75      *
76      * @param parent
77      * the page parent
78      * @return the page client control
79      */

80     protected final Control createContents(Composite parent) {
81         initializeDefaults(getPreferenceStore());
82         Composite container = new Composite(parent, SWT.NULL);
83         GridLayout layout = new GridLayout();
84         //if (ed.isUserDefined())
85
layout.numColumns = 2;
86         container.setLayout(layout);
87         masterButton = new Button(container, SWT.CHECK);
88         masterButton.setText(Messages.RootScopePage_masterButton);
89         GridData gd = new GridData();
90         gd.horizontalSpan = 2;//ed.isUserDefined() ? 2 : 1;
91
masterButton.setLayoutData(gd);
92         Label spacer = new Label(container, SWT.NULL);
93         gd = new GridData();
94         gd.horizontalSpan = 2;//ed.isUserDefined() ? 2 : 1;
95
spacer.setLayoutData(gd);
96         boolean masterValue = getPreferenceStore().getBoolean(
97                 ScopeSet.getMasterKey(ed.getId()));
98         masterButton.setSelection(masterValue);
99         masterButton.addSelectionListener(new SelectionAdapter() {
100             public void widgetSelected(SelectionEvent e) {
101                 masterValueChanged(masterButton.getSelection());
102             }
103         });
104         //if (ed.isUserDefined()) {
105
Label label = new Label(container, SWT.NULL);
106             label.setText(Messages.RootScopePage_name);
107             labelText = new Text(container, SWT.BORDER);
108             gd = new GridData(GridData.FILL_HORIZONTAL);
109             gd.widthHint = 200;
110             labelText.setLayoutData(gd);
111             labelText.setEditable(ed.isUserDefined());
112             label = new Label(container, SWT.NULL);
113             label.setText(Messages.RootScopePage_desc);
114             gd = new GridData(GridData.VERTICAL_ALIGN_BEGINNING);
115             label.setLayoutData(gd);
116             descText = new Text(container, SWT.BORDER | SWT.MULTI | SWT.WRAP);
117             gd = new GridData(GridData.FILL_HORIZONTAL);
118             descText.setEditable(ed.isUserDefined());
119             gd.widthHint = 200;
120             gd.heightHint = 48;
121             descText.setLayoutData(gd);
122         //}
123
int ccol = createScopeContents(container);
124         // adjust number of columns if needed
125
if (ccol > layout.numColumns) {
126             layout.numColumns = ccol;
127             gd = (GridData) masterButton.getLayoutData();
128             gd.horizontalSpan = layout.numColumns;
129             gd = (GridData) spacer.getLayoutData();
130             gd.horizontalSpan = layout.numColumns;
131             //if (ed.isUserDefined()) {
132
gd = (GridData) labelText.getLayoutData();
133                 gd.horizontalSpan = layout.numColumns - 1;
134                 gd = (GridData) descText.getLayoutData();
135                 gd.horizontalSpan = layout.numColumns - 1;
136             //}
137
}
138         updateControls(true);
139         return container;
140     }
141
142     /**
143      * Called when the value of the master switch has changed. The default
144      * implementation disables the scope contents control when the master switch
145      * is off. Subclass can override this behaviour.
146      *
147      * @param value
148      * <code>true</code> if the master switch is on,
149      * <code>false</code> otherwise.
150      */

151
152     protected void masterValueChanged(boolean value) {
153         updateEnableState(value);
154     }
155
156     private void updateEnableState(boolean enabled) {
157         Composite container = masterButton.getParent();
158         Control[] children = container.getChildren();
159
160         boolean first = disabledStates.isEmpty();
161         for (int i = 0; i < children.length; i++) {
162             Control child = children[i];
163             if (child == masterButton)
164                 continue;
165             if (!enabled) {
166                 disabledStates.put(child, new Boolean JavaDoc(child.isEnabled()));
167                 child.setEnabled(false);
168             } else {
169                 Boolean JavaDoc savedState = (Boolean JavaDoc) disabledStates.get(child);
170                 if (!first)
171                     child.setEnabled(savedState != null ? savedState
172                             .booleanValue() : true);
173             }
174         }
175     }
176
177     /**
178      * Returns the scope set name passed to the page during initialization.
179      *
180      * @return the name of the current scope set
181      */

182
183     protected String JavaDoc getScopeSetName() {
184         return scopeSetName;
185     }
186
187     /**
188      * Returns the descriptor of the engine associated with this page.
189      *
190      * @return the engine descriptor
191      */

192
193     protected IEngineDescriptor getEngineDescriptor() {
194         return ed;
195     }
196
197     /**
198      * Tests whether the search engine has been selected to participate in the
199      * search.
200      *
201      * @return <code>true</code> if the search engine is enabled, </code>false</code>
202      * otherwise.
203      */

204
205     protected boolean isEngineEnabled() {
206         return masterButton.getSelection();
207     }
208
209     /**
210      * Stores the value of the master switch in the preference store. Subclasses
211      * may override but must call 'super'.
212      *
213      * @return <code>true</code> if the wizard can be closed,
214      * <code>false</code> otherwise.
215      */

216
217     public boolean performOk() {
218         getPreferenceStore().setValue(ScopeSet.getMasterKey(ed.getId()),
219                 masterButton.getSelection());
220         if (labelText != null) {
221             ed.setLabel(labelText.getText());
222             ed.setDescription(descText.getText());
223         }
224         return true;
225     }
226
227     /**
228      * Sets the value of the master switch to the initial value from the
229      * extension. Subclasses may override but must call 'super'.
230      */

231     protected void performDefaults() {
232         getPreferenceStore().setToDefault(ScopeSet.getMasterKey(ed.getId()));
233         updateControls(false);
234         super.performDefaults();
235     }
236
237     private void updateControls(boolean first) {
238         boolean value = getPreferenceStore().getBoolean(
239                 ScopeSet.getMasterKey(ed.getId()));
240         boolean cvalue = masterButton.getSelection();
241         if (value != cvalue) {
242             masterButton.setSelection(value);
243             masterValueChanged(value);
244         } else if (first)
245             masterValueChanged(value);
246         labelText.setText(ed.getLabel());
247         descText.setText(ed.getDescription());
248     }
249
250     /**
251      * Initializes default values of the store to be used when the user presses
252      * 'Defaults' button. Subclasses may override but must call 'super'.
253      *
254      * @param store
255      * the preference store
256      */

257
258     protected void initializeDefaults(IPreferenceStore store) {
259         Boolean JavaDoc value = (Boolean JavaDoc) ed.getParameters().get(
260                 EngineDescriptor.P_MASTER);
261         store.setDefault(ScopeSet.getMasterKey(ed.getId()), value
262                 .booleanValue());
263     }
264
265     /**
266      * Abstract method that subclasses must implement in order to provide root
267      * page content. The parent uses <code>GridLayout</code> to position and
268      * size the widgets. Widgets created in this method should use
269      * <code>GridData</code> to configure the way they fit in the overall
270      * page.
271      * <p>
272      * The common widgets created by this page will set number of columns they
273      * need for themselves only. Clients that implement this method should
274      * return the required number of columns so that the root page widgets can
275      * be adjusted if more columns are needed than initially set.
276      *
277      * @param parent
278      * the page parent
279      * @return number of columns required by the client content
280      */

281     protected abstract int createScopeContents(Composite parent);
282 }
283
Popular Tags