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