OSDN Git Service

android-2.1_r1 snapshot
[android-x86/sdk.git] / sdkmanager / app / src / com / android / sdkmanager / internal / repository / SettingsPage.java
1 /*\r
2  * Copyright (C) 2009 The Android Open Source Project\r
3  *\r
4  * Licensed under the Apache License, Version 2.0 (the "License");\r
5  * you may not use this file except in compliance with the License.\r
6  * You may obtain a copy of the License at\r
7  *\r
8  *      http://www.apache.org/licenses/LICENSE-2.0\r
9  *\r
10  * Unless required by applicable law or agreed to in writing, software\r
11  * distributed under the License is distributed on an "AS IS" BASIS,\r
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
13  * See the License for the specific language governing permissions and\r
14  * limitations under the License.\r
15  */\r
16 \r
17 package com.android.sdkmanager.internal.repository;\r
18 \r
19 import com.android.sdkuilib.internal.repository.ISettingsPage;\r
20 \r
21 import org.eclipse.swt.SWT;\r
22 import org.eclipse.swt.events.ModifyEvent;\r
23 import org.eclipse.swt.events.ModifyListener;\r
24 import org.eclipse.swt.events.SelectionAdapter;\r
25 import org.eclipse.swt.events.SelectionEvent;\r
26 import org.eclipse.swt.layout.GridData;\r
27 import org.eclipse.swt.layout.GridLayout;\r
28 import org.eclipse.swt.widgets.Button;\r
29 import org.eclipse.swt.widgets.Composite;\r
30 import org.eclipse.swt.widgets.Group;\r
31 import org.eclipse.swt.widgets.Label;\r
32 import org.eclipse.swt.widgets.Text;\r
33 \r
34 import java.util.Properties;\r
35 \r
36 \r
37 public class SettingsPage extends Composite implements ISettingsPage {\r
38 \r
39     // data members\r
40     private SettingsChangedCallback mSettingsChangedCallback;\r
41 \r
42     // UI widgets\r
43     private Group mProxySettingsGroup;\r
44     private Group mMiscGroup;\r
45     private Button mApplyButton;\r
46     private Label mProxyServerLabel;\r
47     private Label mProxyPortLabel;\r
48     private Text mProxyServerText;\r
49     private Text mProxyPortText;\r
50     private Button mForceHttpCheck;\r
51     private Button mAskAdbRestartCheck;\r
52 \r
53     private ModifyListener mSetApplyDirty = new ModifyListener() {\r
54         public void modifyText(ModifyEvent e) {\r
55             mApplyButton.setEnabled(true);\r
56         }\r
57     };\r
58 \r
59 \r
60     /**\r
61      * Create the composite.\r
62      * @param parent The parent of the composite.\r
63      */\r
64     public SettingsPage(Composite parent) {\r
65         super(parent, SWT.BORDER);\r
66 \r
67         createContents(this);\r
68 \r
69         mProxySettingsGroup = new Group(this, SWT.NONE);\r
70         mProxySettingsGroup.setText("Proxy Settings");\r
71         mProxySettingsGroup.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 1, 1));\r
72         mProxySettingsGroup.setLayout(new GridLayout(2, false));\r
73 \r
74         mProxyServerLabel = new Label(mProxySettingsGroup, SWT.NONE);\r
75         mProxyServerLabel.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false, 1, 1));\r
76         mProxyServerLabel.setText("HTTP Proxy Server");\r
77         String tooltip = "The DNS name or IP of the HTTP proxy server to use. " +\r
78                          "When empty, no HTTP proxy is used.";\r
79         mProxyServerLabel.setToolTipText(tooltip);\r
80 \r
81         mProxyServerText = new Text(mProxySettingsGroup, SWT.BORDER);\r
82         mProxyServerText.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));\r
83         mProxyServerText.addModifyListener(mSetApplyDirty);\r
84         mProxyServerText.setToolTipText(tooltip);\r
85 \r
86         mProxyPortLabel = new Label(mProxySettingsGroup, SWT.NONE);\r
87         mProxyPortLabel.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false, 1, 1));\r
88         mProxyPortLabel.setText("HTTP Proxy Port");\r
89         tooltip = "The port of the HTTP proxy server to use. " +\r
90                   "When empty, the default for HTTP or HTTPS is used.";\r
91         mProxyPortLabel.setToolTipText(tooltip);\r
92 \r
93         mProxyPortText = new Text(mProxySettingsGroup, SWT.BORDER);\r
94         mProxyPortText.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));\r
95         mProxyPortText.addModifyListener(mSetApplyDirty);\r
96         mProxyPortText.setToolTipText(tooltip);\r
97 \r
98         mMiscGroup = new Group(this, SWT.NONE);\r
99         mMiscGroup.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 1, 1));\r
100         mMiscGroup.setText("Misc");\r
101         mMiscGroup.setLayout(new GridLayout(2, false));\r
102 \r
103         mForceHttpCheck = new Button(mMiscGroup, SWT.CHECK);\r
104         mForceHttpCheck.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 2, 1));\r
105         mForceHttpCheck.setText("Force https://... sources to be fetched using http://...");\r
106         mForceHttpCheck.setToolTipText("If you are not able to connect to the official Android repository " +\r
107                 "using HTTPS, enable this setting to force accessing it via HTTP.");\r
108         mForceHttpCheck.addSelectionListener(new SelectionAdapter() {\r
109             @Override\r
110             public void widgetSelected(SelectionEvent e) {\r
111                 onForceHttpSelected();  //$hide$\r
112             }\r
113         });\r
114 \r
115         mAskAdbRestartCheck = new Button(mMiscGroup, SWT.CHECK);\r
116         mAskAdbRestartCheck.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 2, 1));\r
117         mAskAdbRestartCheck.setText("Ask before restarting ADB");\r
118         mAskAdbRestartCheck.setToolTipText("When checked, the user will be asked for permission " +\r
119                 "to restart ADB after updating an addon-on package or a tool package.");\r
120         mAskAdbRestartCheck.addSelectionListener(new SelectionAdapter() {\r
121             @Override\r
122             public void widgetSelected(SelectionEvent e) {\r
123                 onForceHttpSelected();  //$hide$\r
124             }\r
125         });\r
126 \r
127         mApplyButton = new Button(this, SWT.NONE);\r
128         mApplyButton.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false, 1, 1));\r
129         mApplyButton.setText("Save && Apply");\r
130         mApplyButton.addSelectionListener(new SelectionAdapter() {\r
131             @Override\r
132             public void widgetSelected(SelectionEvent e) {\r
133                 onApplySelected(); //$hide$\r
134             }\r
135         });\r
136 \r
137         postCreate();  //$hide$\r
138     }\r
139 \r
140     private void createContents(Composite parent) {\r
141         parent.setLayout(new GridLayout(1, false));\r
142     }\r
143 \r
144     @Override\r
145     protected void checkSubclass() {\r
146         // Disable the check that prevents subclassing of SWT components\r
147     }\r
148 \r
149 \r
150 \r
151     // -- Start of internal part ----------\r
152     // Hide everything down-below from SWT designer\r
153     //$hide>>$\r
154 \r
155     /**\r
156      * Called by the constructor right after {@link #createContents(Composite)}.\r
157      */\r
158     private void postCreate() {\r
159     }\r
160 \r
161     /** Loads settings from the given {@link Properties} container and update the page UI. */\r
162     public void loadSettings(Properties in_settings) {\r
163         mProxyServerText.setText(in_settings.getProperty(KEY_HTTP_PROXY_HOST, ""));  //$NON-NLS-1$\r
164         mProxyPortText.setText(  in_settings.getProperty(KEY_HTTP_PROXY_PORT, ""));  //$NON-NLS-1$\r
165         mForceHttpCheck.setSelection(Boolean.parseBoolean(in_settings.getProperty(KEY_FORCE_HTTP)));\r
166         mAskAdbRestartCheck.setSelection(Boolean.parseBoolean(in_settings.getProperty(KEY_ASK_ADB_RESTART)));\r
167 \r
168         // We loaded fresh settings so there's nothing dirty to apply\r
169         mApplyButton.setEnabled(false);\r
170     }\r
171 \r
172     /** Called by the application to retrieve settings from the UI and store them in\r
173      * the given {@link Properties} container. */\r
174     public void retrieveSettings(Properties out_settings) {\r
175         out_settings.setProperty(KEY_HTTP_PROXY_HOST, mProxyServerText.getText());\r
176         out_settings.setProperty(KEY_HTTP_PROXY_PORT, mProxyPortText.getText());\r
177         out_settings.setProperty(KEY_FORCE_HTTP,\r
178                 Boolean.toString(mForceHttpCheck.getSelection()));\r
179         out_settings.setProperty(KEY_ASK_ADB_RESTART,\r
180                 Boolean.toString(mAskAdbRestartCheck.getSelection()));\r
181     }\r
182 \r
183     /**\r
184      * Called by the application to give a callback that the page should invoke when\r
185      * settings must be applied. The page does not apply the settings itself, instead\r
186      * it notifies the application.\r
187      */\r
188     public void setOnSettingsChanged(SettingsChangedCallback settingsChangedCallback) {\r
189         mSettingsChangedCallback = settingsChangedCallback;\r
190     }\r
191 \r
192     /**\r
193      * Callback invoked when user presses the "Save and Apply" button.\r
194      * Notify the application that settings have changed.\r
195      */\r
196     private void onApplySelected() {\r
197         if (mSettingsChangedCallback != null) {\r
198             mSettingsChangedCallback.onSettingsChanged(this);\r
199             mApplyButton.setEnabled(false);\r
200         }\r
201     }\r
202 \r
203     /**\r
204      * Callback invoked when the users presses the Force HTTPS checkbox.\r
205      */\r
206     private void onForceHttpSelected() {\r
207         mSetApplyDirty.modifyText(null);\r
208     }\r
209 \r
210     // End of hiding from SWT Designer\r
211     //$hide<<$\r
212 }\r