KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > help > ui > internal > AppserverPreferencePage


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.internal;
12 import org.eclipse.core.runtime.Preferences;
13 import org.eclipse.help.internal.appserver.AppserverPlugin;
14 import org.eclipse.jface.preference.*;
15 import org.eclipse.swt.SWT;
16 import org.eclipse.swt.events.*;
17 import org.eclipse.swt.graphics.*;
18 import org.eclipse.swt.layout.*;
19 import org.eclipse.swt.widgets.*;
20 import org.eclipse.ui.*;
21 import org.eclipse.ui.forms.HyperlinkSettings;
22 import org.eclipse.ui.forms.widgets.FormText;
23
24 /**
25  * Preference page for Tomcat network interface and port.
26  */

27 public class AppserverPreferencePage extends PreferencePage
28         implements
29             IWorkbenchPreferencePage {
30     protected Text textServerAddr;
31     protected Text textServerPort;
32     /**
33      * Creates preference page controls on demand.
34      *
35      * @param parent
36      * the parent for the preference page
37      */

38     protected Control createContents(Composite parent) {
39         Font font = parent.getFont();
40
41         PlatformUI.getWorkbench().getHelpSystem().setHelp(parent, IHelpUIConstants.PREF_PAGE_APPSERVER);
42
43         final Composite mainComposite = new Composite(parent, SWT.NULL);
44         mainComposite.setLayoutData(new GridData(GridData.FILL_BOTH));
45         final GridLayout layout = new GridLayout();
46         layout.marginHeight = 0;
47         layout.marginWidth = 0;
48         layout.numColumns = 2;
49         mainComposite.setLayout(layout);
50
51         final Label descLabel = new Label(mainComposite, SWT.WRAP);
52         descLabel.setText(Messages.AppserverPreferencePage_description);
53         GridData data = new GridData();
54         data.horizontalSpan = 2;
55         data.widthHint = 100;
56         data.heightHint = 20;
57         descLabel.setLayoutData(data);
58         descLabel.setFont(font);
59
60         // Spacer
61
Label label = new Label(mainComposite, SWT.NONE);
62         data = new GridData();
63         data.horizontalSpan = 2;
64         label.setLayoutData(data);
65         label.setFont(font);
66
67         label = new Label(mainComposite, SWT.NONE);
68         label.setFont(font);
69         label.setText(Messages.AppserverPreferencePage_hostDescription);
70
71         textServerAddr = new Text(mainComposite, SWT.SINGLE | SWT.BORDER);
72         //text.addListener(SWT.Modify, this);
73
data = new GridData(GridData.FILL_HORIZONTAL);
74         data.widthHint = convertWidthInCharsToPixels(8);
75         textServerAddr.setLayoutData(data);
76         textServerAddr.setFont(font);
77
78         label = new Label(mainComposite, SWT.NONE);
79         label.setFont(font);
80         label.setText(Messages.AppserverPreferencePage_portDescription);
81
82         textServerPort = new Text(mainComposite, SWT.SINGLE | SWT.BORDER);
83         textServerPort.setTextLimit(5);
84         data = new GridData(GridData.FILL_HORIZONTAL);
85         data.widthHint = convertWidthInCharsToPixels(8);
86         textServerPort.setLayoutData(data);
87         textServerPort.setFont(font);
88
89         // Validation of port field
90
textServerPort.addModifyListener(new ModifyListener() {
91             public void modifyText(ModifyEvent e) {
92                 if (textServerPort.getText().length() == 0) {
93                     AppserverPreferencePage.this.setValid(true);
94                     setErrorMessage(null);
95                     return;
96                 }
97                 try {
98                     int num = Integer.valueOf(textServerPort.getText())
99                             .intValue();
100                     if (0 <= num && num <= 0xFFFF) {
101                         // port is valid
102
AppserverPreferencePage.this.setValid(true);
103                         setErrorMessage(null);
104                         return;
105                     }
106
107                     // port is invalid
108
} catch (NumberFormatException JavaDoc nfe) {
109                 }
110                 AppserverPreferencePage.this.setValid(false);
111                 setErrorMessage(Messages.AppserverPreferencePage_invalidPort);
112             }
113         });
114
115         // Spacer
116
label = new Label(mainComposite, SWT.NONE);
117         data = new GridData();
118         data.horizontalSpan = 2;
119         label.setLayoutData(data);
120         label.setFont(font);
121     
122         FormText ftext = new FormText(mainComposite, SWT.WRAP);
123         ftext.setText(Messages.AppserverPreferencePage_requireRestart, true, false);
124         data = new GridData(GridData.GRAB_HORIZONTAL);
125         ftext.setHyperlinkSettings(new HyperlinkSettings(mainComposite.getDisplay()));
126         data.horizontalSpan = 2;
127         data.widthHint = 100;
128         data.heightHint = 20;
129         ftext.setLayoutData(data);
130         ftext.setFont(font);
131         final GridData fdata = data;
132         mainComposite.addControlListener(new ControlAdapter() {
133             public void controlResized(ControlEvent e) {
134                 GridData ddata = (GridData)descLabel.getLayoutData();
135                 int width = mainComposite.getSize().x-layout.marginWidth*2;
136                 fdata.widthHint = width;
137                 fdata.heightHint = SWT.DEFAULT;
138                 ddata.widthHint = width;
139                 ddata.heightHint = SWT.DEFAULT;
140                 mainComposite.layout();
141             }
142         });
143
144         Preferences pref = AppserverPlugin.getDefault().getPluginPreferences();
145         textServerAddr.setText(pref.getString(AppserverPlugin.HOST_KEY));
146         textServerPort.setText(pref.getString(AppserverPlugin.PORT_KEY));
147
148         return mainComposite;
149     }
150     /**
151      * @see IWorkbenchPreferencePage
152      */

153     public void init(IWorkbench workbench) {
154     }
155     /**
156      * Performs special processing when this page's Defaults button has been
157      * pressed.
158      * <p>
159      * This is a framework hook method for sublcasses to do special things when
160      * the Defaults button has been pressed. Subclasses may override, but should
161      * call <code>super.performDefaults</code>.
162      * </p>
163      */

164     protected void performDefaults() {
165         Preferences pref = AppserverPlugin.getDefault().getPluginPreferences();
166         textServerAddr.setText(pref.getDefaultString(AppserverPlugin.HOST_KEY));
167         textServerPort.setText(pref.getDefaultString(AppserverPlugin.PORT_KEY));
168         super.performDefaults();
169     }
170     /**
171      * @see IPreferencePage
172      */

173     public boolean performOk() {
174         Preferences pref = AppserverPlugin.getDefault().getPluginPreferences();
175         pref.setValue(AppserverPlugin.HOST_KEY, textServerAddr.getText());
176         pref.setValue(AppserverPlugin.PORT_KEY, textServerPort.getText());
177         AppserverPlugin.getDefault().savePluginPreferences();
178         return true;
179     }
180
181 }
182
Popular Tags