KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > help > ui > internal > preferences > HelpContentPreferencePage


1 /*******************************************************************************
2  * Copyright (c) 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.help.ui.internal.preferences;
12
13 import org.eclipse.core.runtime.Preferences;
14 import org.eclipse.help.internal.base.HelpBasePlugin;
15 import org.eclipse.help.internal.base.IHelpBaseConstants;
16 import org.eclipse.help.internal.base.remote.RemoteHelp;
17 import org.eclipse.help.ui.internal.IHelpUIConstants;
18 import org.eclipse.help.ui.internal.Messages;
19 import org.eclipse.jface.preference.PreferencePage;
20 import org.eclipse.swt.SWT;
21 import org.eclipse.swt.layout.GridData;
22 import org.eclipse.swt.layout.GridLayout;
23 import org.eclipse.swt.widgets.Button;
24 import org.eclipse.swt.widgets.Composite;
25 import org.eclipse.swt.widgets.Control;
26 import org.eclipse.swt.widgets.Event;
27 import org.eclipse.swt.widgets.Group;
28 import org.eclipse.swt.widgets.Label;
29 import org.eclipse.swt.widgets.Listener;
30 import org.eclipse.swt.widgets.Text;
31 import org.eclipse.ui.IWorkbench;
32 import org.eclipse.ui.IWorkbenchPreferencePage;
33 import org.eclipse.ui.PlatformUI;
34
35 /*
36  * The preference page implementation of Help -> Content.
37  */

38 public class HelpContentPreferencePage extends PreferencePage
39         implements IWorkbenchPreferencePage {
40
41     private Button checkbox;
42     private Group group;
43     private Label hostLabel;
44     private Text hostText;
45     private Label pathLabel;
46     private Text pathText;
47     private Button radio1;
48     private Button radio2;
49     private Text portText;
50     
51     /*
52      * Listens for any change in the UI and checks for valid
53      * input and correct enablement.
54      */

55     private Listener changeListener = new Listener() {
56         public void handleEvent(Event event) {
57             updateEnablement();
58             updateValidity();
59         }
60     };
61     
62     /* (non-Javadoc)
63      * @see org.eclipse.jface.preference.PreferencePage#createContents(org.eclipse.swt.widgets.Composite)
64      */

65     protected Control createContents(Composite parent) {
66         PlatformUI.getWorkbench().getHelpSystem().setHelp(parent,
67                 IHelpUIConstants.PREF_PAGE_HELP_CONTENT);
68
69         Composite composite = createComposite(parent);
70         createCheckbox(composite);
71         createGroup(composite);
72         
73         applyDialogFont(composite);
74
75         // initialize the UI
76
Preferences prefs = HelpBasePlugin.getDefault().getPluginPreferences();
77         boolean isOn = prefs.getBoolean(IHelpBaseConstants.P_KEY_REMOTE_HELP_ON);
78         String JavaDoc host = prefs.getString(IHelpBaseConstants.P_KEY_REMOTE_HELP_HOST);
79         String JavaDoc path = prefs.getString(IHelpBaseConstants.P_KEY_REMOTE_HELP_PATH);
80         boolean useDefaultPort = prefs.getBoolean(IHelpBaseConstants.P_KEY_REMOTE_HELP_DEFAULT_PORT);
81         int port = prefs.getInt(IHelpBaseConstants.P_KEY_REMOTE_HELP_PORT);
82         setValues(isOn, host, path, useDefaultPort, port);
83         
84         return composite;
85     }
86
87     /* (non-Javadoc)
88      * @see org.eclipse.ui.IWorkbenchPreferencePage#init(org.eclipse.ui.IWorkbench)
89      */

90     public void init(IWorkbench workbench) {
91         // nothing to do here
92
}
93
94     /* (non-Javadoc)
95      * @see org.eclipse.jface.preference.PreferencePage#performDefaults()
96      */

97     protected void performDefaults() {
98         Preferences prefs = HelpBasePlugin.getDefault().getPluginPreferences();
99         boolean isOn = prefs.getDefaultBoolean(IHelpBaseConstants.P_KEY_REMOTE_HELP_ON);
100         String JavaDoc host = prefs.getDefaultString(IHelpBaseConstants.P_KEY_REMOTE_HELP_HOST);
101         String JavaDoc path = prefs.getDefaultString(IHelpBaseConstants.P_KEY_REMOTE_HELP_PATH);
102         boolean useDefaultPort = prefs.getDefaultBoolean(IHelpBaseConstants.P_KEY_REMOTE_HELP_DEFAULT_PORT);
103         int port = prefs.getDefaultInt(IHelpBaseConstants.P_KEY_REMOTE_HELP_PORT);
104         setValues(isOn, host, path, useDefaultPort, port);
105     }
106
107     /* (non-Javadoc)
108      * @see org.eclipse.jface.preference.PreferencePage#performOk()
109      */

110     public boolean performOk() {
111         Preferences prefs = HelpBasePlugin.getDefault().getPluginPreferences();
112         prefs.setValue(IHelpBaseConstants.P_KEY_REMOTE_HELP_ON, checkbox.getSelection());
113         prefs.setValue(IHelpBaseConstants.P_KEY_REMOTE_HELP_HOST, hostText.getText().trim());
114         prefs.setValue(IHelpBaseConstants.P_KEY_REMOTE_HELP_PATH, pathText.getText().trim());
115         prefs.setValue(IHelpBaseConstants.P_KEY_REMOTE_HELP_DEFAULT_PORT, radio1.getSelection());
116         prefs.setValue(IHelpBaseConstants.P_KEY_REMOTE_HELP_PORT, portText.getText().trim());
117         
118         HelpBasePlugin.getDefault().savePluginPreferences();
119         RemoteHelp.notifyPreferenceChange();
120         return true;
121     }
122     
123     /*
124      * Create the Composite that will hold the entire preference page.
125      */

126     private Composite createComposite(Composite parent) {
127         Composite composite = new Composite(parent, SWT.NONE);
128         GridLayout layout = new GridLayout();
129         layout.marginWidth = 0;
130         layout.marginHeight = 0;
131         composite.setLayout(layout);
132         composite.setLayoutData(new GridData(SWT.FILL, SWT.FILL, false, false));
133         return composite;
134     }
135     
136     /*
137      * Create the top checkbox.
138      */

139     private void createCheckbox(Composite parent) {
140         checkbox = new Button(parent, SWT.CHECK);
141         checkbox.setText(Messages.HelpContentPreferencePage_remote);
142         checkbox.addListener(SWT.Selection, changeListener);
143     }
144     
145     /*
146      * Create the "Location" group.
147      */

148     private void createGroup(Composite parent) {
149         group = new Group(parent, SWT.NONE);
150         group.setText(Messages.HelpContentPreferencePage_location);
151         group.setLayout(new GridLayout(2, false));
152         group.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false));
153         
154         createHostSection(group);
155         createPathSection(group);
156         createPortSection(group);
157     }
158     
159     /*
160      * Create the "Host:" label and text field.
161      */

162     private void createHostSection(Composite parent) {
163         hostLabel = new Label(parent, SWT.NONE);
164         hostLabel.setText(Messages.HelpContentPreferencePage_host);
165         hostText = new Text(parent, SWT.BORDER);
166         hostText.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false));
167         if (hostText.getOrientation() == SWT.RIGHT_TO_LEFT)
168             hostText.setOrientation(SWT.LEFT_TO_RIGHT);
169         hostText.addListener(SWT.Modify, changeListener);
170     }
171
172     /*
173      * Create the "Path:" label and text field.
174      */

175     private void createPathSection(Composite parent) {
176         pathLabel = new Label(parent, SWT.NONE);
177         pathLabel.setText(Messages.HelpContentPreferencePage_path);
178         pathText = new Text(parent, SWT.BORDER);
179         pathText.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false));
180         if (pathText.getOrientation() == SWT.RIGHT_TO_LEFT)
181             pathText.setOrientation(SWT.LEFT_TO_RIGHT);
182         pathText.addListener(SWT.Modify, changeListener);
183     }
184
185     /*
186      * Create the port radio buttons, and text field.
187      */

188     private void createPortSection(Composite parent) {
189         Composite portComposite = new Composite(parent, SWT.NONE);
190         GridLayout layout = new GridLayout(2, false);
191         layout.marginWidth = 0;
192         layout.marginHeight = 0;
193         portComposite.setLayout(layout);
194         portComposite.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, false, false, 2, 1));
195         
196         radio1 = new Button(portComposite, SWT.RADIO);
197         radio1.setText(Messages.HelpContentPreferencePage_portDefault);
198         radio1.setLayoutData(new GridData(SWT.BEGINNING, SWT.CENTER, false, false, 2, 1));
199         radio1.addListener(SWT.Selection, changeListener);
200
201         radio2 = new Button(portComposite, SWT.RADIO);
202         radio2.setText(Messages.HelpContentPreferencePage_port);
203         radio2.addListener(SWT.Selection, changeListener);
204         
205         portText = new Text(portComposite, SWT.BORDER);
206         portText.setLayoutData(new GridData(50, SWT.DEFAULT));
207         portText.addListener(SWT.Modify, changeListener);
208     }
209     
210     /*
211      * Sets the given values for the UI.
212      */

213     private void setValues(boolean isOn, String JavaDoc host, String JavaDoc path, boolean useDefaultPort, int port) {
214         checkbox.setSelection(isOn);
215         hostText.setText(host);
216         pathText.setText(path);
217         radio1.setSelection(useDefaultPort);
218         radio2.setSelection(!useDefaultPort);
219         portText.setText(String.valueOf(port));
220
221         updateEnablement();
222         updateValidity();
223     }
224
225     /*
226      * Ensures that the correct controls are grayed out and disabled, and the
227      * rest are enabled.
228      */

229     private void updateEnablement() {
230         boolean isChecked = checkbox.getSelection();
231         group.setEnabled(isChecked);
232         hostLabel.setEnabled(isChecked);
233         hostText.setEnabled(isChecked);
234         pathLabel.setEnabled(isChecked);
235         pathText.setEnabled(isChecked);
236         radio1.setEnabled(isChecked);
237         radio2.setEnabled(isChecked);
238         portText.setEnabled(isChecked && radio2.getSelection());
239     }
240     
241     /*
242      * Checks for errors in the user input and shows/clears the error message
243      * as appropriate.
244      */

245     private void updateValidity() {
246         // no checking needed if remote not selected
247
if (checkbox.getSelection() == true) {
248             // check for empty hostname
249
if (hostText.getText().trim().length() == 0) {
250                 setErrorMessage(Messages.HelpContentPreferencePage_error_host);
251                 setValid(false);
252                 return;
253             }
254             // check for invalid port
255
if (radio2.getSelection() == true) {
256                 try {
257                     // check port range
258
int port = Integer.parseInt(portText.getText());
259                     if (port < 0 || port > 65535) {
260                         setErrorMessage(Messages.HelpContentPreferencePage_error_port);
261                         setValid(false);
262                         return;
263                     }
264                 }
265                 catch (NumberFormatException JavaDoc e) {
266                     // not a number
267
setErrorMessage(Messages.HelpContentPreferencePage_error_port);
268                     setValid(false);
269                     return;
270                 }
271             }
272         }
273         // no errors
274
setErrorMessage(null);
275         setValid(true);
276     }
277 }
278
Popular Tags