1 /*
2  * Copyright (C) 2012 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.ide.eclipse.base.internal.preferences;
18 
19 import com.android.sdkstats.DdmsPreferenceStore;
20 import com.android.sdkstats.SdkStatsPermissionDialog;
21 
22 import org.eclipse.jface.preference.BooleanFieldEditor;
23 import org.eclipse.jface.preference.PreferencePage;
24 import org.eclipse.swt.SWT;
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.Composite;
30 import org.eclipse.swt.widgets.Control;
31 import org.eclipse.swt.widgets.Label;
32 import org.eclipse.swt.widgets.Link;
33 import org.eclipse.ui.IWorkbench;
34 import org.eclipse.ui.IWorkbenchPreferencePage;
35 
36 public class UsagePreferencePage extends PreferencePage implements IWorkbenchPreferencePage {
37     private static final int WRAP_WIDTH_PX = 200;
38 
39     private BooleanFieldEditor mOptInCheckBox;
40     private DdmsPreferenceStore mStore = new DdmsPreferenceStore();
41 
UsagePreferencePage()42     public UsagePreferencePage() {
43     }
44 
45     @Override
init(IWorkbench workbench)46     public void init(IWorkbench workbench) {
47         // pass
48     }
49 
50     @Override
createContents(Composite parent)51     protected Control createContents(Composite parent) {
52         Composite top = new Composite(parent, SWT.NONE);
53         top.setLayout(new GridLayout(1, false));
54         top.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
55 
56         Label l = new Label(top, SWT.WRAP);
57         l.setText(SdkStatsPermissionDialog.BODY_TEXT);
58         GridData gd = new GridData(GridData.FILL_HORIZONTAL);
59         gd.widthHint = WRAP_WIDTH_PX;
60         l.setLayoutData(gd);
61 
62         Link privacyPolicyLink = new Link(top, SWT.WRAP);
63         gd = new GridData(GridData.FILL_HORIZONTAL);
64         gd.widthHint = WRAP_WIDTH_PX;
65         privacyPolicyLink.setLayoutData(gd);
66         privacyPolicyLink.setText(SdkStatsPermissionDialog.PRIVACY_POLICY_LINK_TEXT);
67 
68         privacyPolicyLink.addSelectionListener(new SelectionAdapter() {
69             @Override
70             public void widgetSelected(SelectionEvent event) {
71                 SdkStatsPermissionDialog.openUrl(event.text);
72             }
73         });
74 
75         mOptInCheckBox = new BooleanFieldEditor(DdmsPreferenceStore.PING_OPT_IN,
76                 SdkStatsPermissionDialog.CHECKBOX_TEXT, top);
77         mOptInCheckBox.setPage(this);
78         mOptInCheckBox.setPreferenceStore(mStore.getPreferenceStore());
79         mOptInCheckBox.load();
80 
81         return top;
82     }
83 
84     /* (non-Javadoc)
85      * @see org.eclipse.jface.preference.PreferencePage#performCancel()
86      */
87     @Override
performCancel()88     public boolean performCancel() {
89         mOptInCheckBox.load();
90         return super.performCancel();
91     }
92 
93     /* (non-Javadoc)
94      * @see org.eclipse.jface.preference.PreferencePage#performDefaults()
95      */
96     @Override
performDefaults()97     protected void performDefaults() {
98         mOptInCheckBox.loadDefault();
99         super.performDefaults();
100     }
101 
102     /* (non-Javadoc)
103      * @see org.eclipse.jface.preference.PreferencePage#performOk()
104      */
105     @Override
performOk()106     public boolean performOk() {
107         save();
108         return super.performOk();
109     }
110 
111     /* (non-Javadoc)
112      * @see org.eclipse.jface.preference.PreferencePage#performApply()
113      */
114     @Override
performApply()115     protected void performApply() {
116         save();
117         super.performApply();
118     }
119 
save()120     private void save() {
121         mStore.setPingOptIn(mOptInCheckBox.getBooleanValue());
122     }
123 }
124