summaryrefslogtreecommitdiff
path: root/src/plugins/preflighting.ui/src/com/motorolamobility/preflighting/ui/CommandLinePreferencePage.java
blob: af54d1d1a77ad55f1543de3fdca6ae466659b3e8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
/*
* Copyright (C) 2012 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
*      http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.motorolamobility.preflighting.ui;

import java.util.ArrayList;
import java.util.List;

import org.eclipse.core.runtime.IStatus;
import org.eclipse.jface.dialogs.IMessageProvider;
import org.eclipse.jface.preference.IPreferenceStore;
import org.eclipse.jface.preference.PreferencePage;
import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.TabFolder;
import org.eclipse.swt.widgets.TabItem;
import org.eclipse.ui.IWorkbench;
import org.eclipse.ui.IWorkbenchPreferencePage;
import org.eclipse.ui.PlatformUI;

import com.motorolamobility.preflighting.core.logging.PreflightingLogger;
import com.motorolamobility.preflighting.ui.i18n.PreflightingUiNLS;
import com.motorolamobility.preflighting.ui.tabs.AbstractAppValidatorTabComposite;
import com.motorolamobility.preflighting.ui.tabs.CheckersTabComposite;
import com.motorolamobility.preflighting.ui.tabs.DevicesTabComposite;
import com.motorolamobility.preflighting.ui.tabs.GeneralSettingsComposite;
import com.motorolamobility.preflighting.ui.tabs.UIChangedListener;

public class CommandLinePreferencePage extends PreferencePage implements IWorkbenchPreferencePage
{
    private final String PREFERENCE_PAGE_HELP = PreflightingUIPlugin.PREFLIGHTING_UI_PLUGIN_ID
            + ".preference-appvalidator-commandline"; //$NON-NLS-1$

    private List<AbstractAppValidatorTabComposite> pagesComposite;

    public CommandLinePreferencePage()
    {
        setPreferenceStore(PreflightingUIPlugin.getDefault().getPreferenceStore());
    }

    public void init(IWorkbench workbench)
    {
        //do nothing
    }

    /* (non-Javadoc)
     * @see org.eclipse.jface.preference.PreferencePage#performOk()
     */
    @Override
    public boolean performOk()
    {
        /*
         *  Workaround to make it work on MacOS
         *  
         *  On MacOS, the TableViewer cell doesn't lose the focus when the
         *  user clicks "Apply" or "OK". This way, the editor's setValue method
         *  is not called, and the cell value is not updated in the model. 
         *  Given that, the last modification would not be persisted.
         *  
         *  This forces the focus change and consequently updates the model 
         *  before continuing.
         */
        getShell().setFocus();
        getControl().setFocus();

        StringBuilder commandLine = new StringBuilder();
        for (AbstractAppValidatorTabComposite composite : pagesComposite)
        {
            composite.performOk(getPreferenceStore());
            commandLine.append(composite.commandLineBuilder() + " "); //$NON-NLS-1$

        }
        getPreferenceStore().setValue(PreflightingUIPlugin.COMMAND_LINE_PREFERENCE_KEY,
                commandLine.toString().trim());
        getPreferenceStore().setValue(PreflightingUIPlugin.ERRORS_TO_WARNINGS_PREFERENCE_KEY,
                Boolean.getBoolean(PreflightingUIPlugin.ECLIPSE_PROBLEM_TO_WARNING_VALUE));

        PreflightingLogger.debug("App Validator command line: " + commandLine);

        return super.performOk();
    }

    /**
     * 
     */

    /* (non-Javadoc)
     * @see org.eclipse.jface.preference.PreferencePage#performDefaults()
     */
    @Override
    protected void performDefaults()
    {
        for (AbstractAppValidatorTabComposite composite : pagesComposite)
        {
            composite.performDefaults();
        }
        super.performDefaults();
    }

    /* (non-Javadoc)
     * @see org.eclipse.jface.preference.PreferencePage#isValid()
     */

    @Override
    protected Control createContents(Composite parent)
    {
        PlatformUI.getWorkbench().getHelpSystem().setHelp(parent, PREFERENCE_PAGE_HELP);

        pagesComposite = new ArrayList<AbstractAppValidatorTabComposite>();

        IPreferenceStore prefStore = PreflightingUIPlugin.getDefault().getPreferenceStore();
        if ((!prefStore.contains(PreflightingUIPlugin.OUTPUT_LIMIT_VALUE))
                && prefStore.contains(PreflightingUIPlugin.COMMAND_LINE_PREFERENCE_KEY)
                && (!(prefStore.getString(PreflightingUIPlugin.COMMAND_LINE_PREFERENCE_KEY))
                        .equals(PreflightingUIPlugin.DEFAULT_BACKWARD_COMMANDLINE)))
        {

            Label backLabel = new Label(parent, SWT.WRAP);
            GridData layoutData = new GridData(SWT.FILL, SWT.TOP, true, false);
            layoutData.widthHint = 450;
            backLabel.setLayoutData(layoutData);
            backLabel.setText("You have previously set the following App Validator parameters:\n"
                    + prefStore.getString(PreflightingUIPlugin.COMMAND_LINE_PREFERENCE_KEY));

        }

        Composite mainComposite = new Composite(parent, SWT.NONE);
        mainComposite.setLayout(new GridLayout(1, false));
        GridData mainData = new GridData(SWT.FILL, SWT.TOP, true, false);
        mainComposite.setLayoutData(mainData);

        TabFolder tabFolder = new TabFolder(mainComposite, SWT.TOP);

        tabFolder.setLayoutData(new GridData(GridData.FILL_BOTH));

        TabItem generalSettingsTab = new TabItem(tabFolder, SWT.NONE);
        generalSettingsTab
                .setText(PreflightingUiNLS.CommandLinePreferencePage_GeneralSettingTabName);
        AbstractAppValidatorTabComposite genSettingsComposite =
                new GeneralSettingsComposite(tabFolder, SWT.NONE);
        generalSettingsTab.setControl(genSettingsComposite);
        genSettingsComposite.addUIChangedListener(new UIChangedListener()
        {

            public void uiChanged(AbstractAppValidatorTabComposite composite)
            {
                validateUI(composite);
            }
        });
        pagesComposite.add(genSettingsComposite);

        TabItem checkersSettingsTab = new TabItem(tabFolder, SWT.NONE);
        checkersSettingsTab.setText(PreflightingUiNLS.CommandLinePreferencePage_Checkers_Tab);
        AbstractAppValidatorTabComposite checkersTabComposite =
                new CheckersTabComposite(tabFolder, SWT.NONE, getPreferenceStore());
        checkersSettingsTab.setControl(checkersTabComposite);
        checkersTabComposite.addUIChangedListener(new UIChangedListener()
        {

            public void uiChanged(AbstractAppValidatorTabComposite composite)
            {
                validateUI(composite);
            }
        });
        pagesComposite.add(checkersTabComposite);

        TabItem devicesSettingTab = new TabItem(tabFolder, SWT.NONE);
        devicesSettingTab.setText(PreflightingUiNLS.CommandLinePreferencePage_Devices_Tab);
        AbstractAppValidatorTabComposite devicesTabComposite =
                new DevicesTabComposite(tabFolder, SWT.NONE, getPreferenceStore());
        devicesSettingTab.setControl(devicesTabComposite);
        pagesComposite.add(devicesTabComposite);

        setValid(((GeneralSettingsComposite) genSettingsComposite).canFinish());

        return mainComposite;
    }

    public void validateUI(AbstractAppValidatorTabComposite composite)
    {
        IStatus status = composite.isValid();

        if (status.getSeverity() == IStatus.ERROR)
        {
            setValid(false);
            setMessage(status.getMessage(), IMessageProvider.ERROR);
        }
        else if (status.getSeverity() == IStatus.WARNING)
        {
            setMessage(status.getMessage(), IMessageProvider.WARNING);
        }
        else
        {
            setValid(true);
            setMessage(null);
        }

    }
}