summaryrefslogtreecommitdiff
path: root/src/plugins/common/src/com/motorola/studio/android/common/preferences/DialogWithToggleUtils.java
blob: 114147b9203e690e96d5b98e98008cf4d29def59 (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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
/*
* 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.motorola.studio.android.common.preferences;

import org.eclipse.core.runtime.preferences.IEclipsePreferences;
import org.eclipse.core.runtime.preferences.IScopeContext;
import org.eclipse.core.runtime.preferences.InstanceScope;
import org.eclipse.jface.dialogs.IDialogConstants;
import org.eclipse.jface.dialogs.MessageDialogWithToggle;
import org.eclipse.jface.preference.IPreferenceStore;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.ui.IWorkbench;
import org.eclipse.ui.IWorkbenchWindow;
import org.eclipse.ui.PlatformUI;
import org.eclipse.ui.plugin.AbstractUIPlugin;
import org.osgi.service.prefs.BackingStoreException;

import com.motorola.studio.android.common.CommonPlugin;
import com.motorola.studio.android.common.utilities.i18n.UtilitiesNLS;

/**
 * DESCRIPTION: 
 * This class provides static methods to create toggle dialogs, storing the 
 * user action into preference file. It allows resetting the preference to 
 * their original status.
 * <br>
 * RESPONSIBILITY:
 * Provides the basic functionalities for questioning/informing user and 
 * persisting the choice made
 * <br>
 * COLABORATORS:
 * none
 * <br>
 * USAGE: 
 * - use showQuestion method to provide behavior for user choice between "yes"
 *  and "no" option.
 * - use showInformation method to provide behavior for showing informational 
 * dialog to user
 * - use resetAllDialogsConfiguration method to reset to initial dialog 
 * configuration.                        
 */
public class DialogWithToggleUtils
{

    // suffix of the preference key of the all toggle dialogs.
    private final static String TOGGLE_DIALOG = ".toggle.dialog";

    private static IEclipsePreferences getPreferences()
    {
        IScopeContext scope = InstanceScope.INSTANCE;
        return scope.getNode(CommonPlugin.PLUGIN_ID);
    }

    /**
     * Shows a dialog with "Yes" and "No" buttons.
     * The dialog is opened only if it is the first time that the dialog is 
     * shown or if the user did not check the option "Do not show this window 
     * again" when the dialog had been opened previously.
     *
     * @param preferenceKey the key to use when persisting the user's preference.
     * @param title the dialog's title, or <code>null</code> if none.
     * @param message the dialog's message.
     * @return if the dialog was opened: true, if the user pressed "Yes".
     *         if the dialog was not opened: true, if the option was set to "Always".
     */
    public static boolean showQuestion(final String preferenceKey, final String title,
            final String message)
    {
        final Boolean[] reply = new Boolean[1];

        final String prefKey = preferenceKey + TOGGLE_DIALOG;

        PlatformUI.getWorkbench().getDisplay().syncExec(new Runnable()
        {
            public void run()
            {
                AbstractUIPlugin plugin = CommonPlugin.getDefault();
                IPreferenceStore store = plugin.getPreferenceStore();

                String preferenceValue = store.getString(prefKey);

                if (MessageDialogWithToggle.PROMPT.equals(preferenceValue)
                        || (preferenceValue.length() == 0))
                {

                    IWorkbench workbench = PlatformUI.getWorkbench();
                    IWorkbenchWindow ww = workbench.getActiveWorkbenchWindow();
                    Shell shell = ww.getShell();

                    MessageDialogWithToggle dialog =
                            MessageDialogWithToggle.openYesNoQuestion(shell, title, message,
                                    UtilitiesNLS.UI_DoNotShowMEAgain, false, store, prefKey);
                    reply[0] = (dialog.getReturnCode() == IDialogConstants.YES_ID);
                }
                else
                {
                    reply[0] = preferenceValue.equals(MessageDialogWithToggle.ALWAYS);
                }
            }
        });

        return reply[0];
    }

    /**
     * Shows an information dialog to user. The dialog is opened
     * only if it is the first time that the dialog is shown or if the user did not checked
     * the option "Do not show this window again" when the dialog had been opened previously.  
     *
     * @param preferenceKey the key to use when persisting the user's preference
     * @param title the dialog's title, or <code>null</code> if none
     * @param message the dialog´s message
     *    
     */
    public static void showInformation(final String preferenceKey, final String title,
            final String message)
    {
        final String prefKey = preferenceKey + TOGGLE_DIALOG;

        PlatformUI.getWorkbench().getDisplay().syncExec(new Runnable()
        {
            public void run()
            {
                AbstractUIPlugin plugin = CommonPlugin.getDefault();
                IPreferenceStore store = plugin.getPreferenceStore();

                String preferenceValue = store.getString(prefKey);

                if (MessageDialogWithToggle.PROMPT.equals(preferenceValue)
                        || (preferenceValue.length() == 0))
                {
                    IWorkbench workbench = PlatformUI.getWorkbench();
                    IWorkbenchWindow ww = workbench.getActiveWorkbenchWindow();
                    Shell shell = ww.getShell();

                    MessageDialogWithToggle.openInformation(shell, title, message,
                            UtilitiesNLS.UI_DoNotShowMEAgain, false, store, prefKey);
                }
            }
        });
    }

    /**
     * Shown a warning dialog. The dialog is opened
     * only if it is the first time that the dialog is shown or if the user did not checked
     * the option "Do not show this window again" when the dialog had been opened previously.  
     *
     * @param preferenceKey the key to use when persisting the user's preference
     * @param title the dialog's title, or <code>null</code> if none
     * @param message the dialog´s message
     *    
     */
    public static void showWarning(final String preferenceKey, final String title,
            final String message)
    {
        final String prefKey = preferenceKey + TOGGLE_DIALOG;

        PlatformUI.getWorkbench().getDisplay().syncExec(new Runnable()
        {
            public void run()
            {
                AbstractUIPlugin plugin = CommonPlugin.getDefault();
                IPreferenceStore store = plugin.getPreferenceStore();

                String preferenceValue = store.getString(prefKey);

                if (MessageDialogWithToggle.PROMPT.equals(preferenceValue)
                        || (preferenceValue.length() == 0))
                {
                    IWorkbench workbench = PlatformUI.getWorkbench();
                    IWorkbenchWindow ww = workbench.getActiveWorkbenchWindow();
                    Shell shell = ww.getShell();

                    MessageDialogWithToggle.openWarning(shell, title, message,
                            UtilitiesNLS.UI_DoNotShowMEAgain, false, store, prefKey);
                }
            }
        });
    }

    /**
     * Show a confirmation message.
     *
     * @param preferenceKey the key to use when persisting the user's preference;
     *            <code>null</code> if you don't want it persisted.
     * @param title the dialog's title, or <code>null</code> if none
     * @param message the dialog´s message
     */
    public static boolean showConfirmation(final String preferenceKey, final String title,
            final String message)
    {
        final Boolean[] reply = new Boolean[1];

        final String prefKey = preferenceKey + TOGGLE_DIALOG;

        PlatformUI.getWorkbench().getDisplay().syncExec(new Runnable()
        {
            public void run()
            {
                AbstractUIPlugin plugin = CommonPlugin.getDefault();
                IPreferenceStore store = plugin.getPreferenceStore();

                String preferenceValue = store.getString(prefKey);

                if (MessageDialogWithToggle.PROMPT.equals(preferenceValue)
                        || (preferenceValue.length() == 0))
                {

                    IWorkbench workbench = PlatformUI.getWorkbench();
                    IWorkbenchWindow ww = workbench.getActiveWorkbenchWindow();
                    Shell shell = ww.getShell();

                    MessageDialogWithToggle dialog =
                            MessageDialogWithToggle.openOkCancelConfirm(shell, title, message,
                                    UtilitiesNLS.UI_AlwaysProceed, false, store, prefKey);
                    reply[0] = (dialog.getReturnCode() == IDialogConstants.OK_ID);
                }
                else
                {
                    reply[0] = preferenceValue.equals(MessageDialogWithToggle.ALWAYS);
                }
            }
        });

        return reply[0];
    }

    /**
     * Shows an error dialog. The dialog is opened
     * only if it is the first time that the dialog is shown or if the user did not checked
     * the option "Do not show this window again" when the dialog had been opened previously.  
     *
     * @param preferenceKey the key to use when persisting the user's preference
     * @param title the dialog's title, or <code>null</code> if none
     * @param message the dialog´s message
     *    
     */
    public static void showError(final String preferenceKey, final String title,
            final String message)
    {
        final String prefKey = preferenceKey + TOGGLE_DIALOG;

        PlatformUI.getWorkbench().getDisplay().syncExec(new Runnable()
        {
            public void run()
            {
                AbstractUIPlugin plugin = CommonPlugin.getDefault();
                IPreferenceStore store = plugin.getPreferenceStore();

                String preferenceValue = store.getString(prefKey);

                if (MessageDialogWithToggle.PROMPT.equals(preferenceValue)
                        || (preferenceValue.length() == 0))
                {
                    IWorkbench workbench = PlatformUI.getWorkbench();
                    IWorkbenchWindow ww = workbench.getActiveWorkbenchWindow();
                    Shell shell = ww.getShell();

                    MessageDialogWithToggle.openError(shell, title, message,
                            UtilitiesNLS.UI_DoNotShowMEAgain, false, store, prefKey);
                }
            }
        });
    }

    /**
     * Resets all dialog configuration clearing all 'Do not show me again' 
     * settings and showing all hidden dialogs again.
     */
    public static void resetAllDialogsConfiguration()
    {
        IEclipsePreferences preferences = getPreferences();
        String[] propertyNames;
        try
        {
            propertyNames = preferences.keys();
        }
        catch (BackingStoreException e)
        {
            propertyNames = new String[0];
        }

        for (String propertyName : propertyNames)
        {
            if (propertyName.contains(TOGGLE_DIALOG))
            {
                preferences.put(propertyName, MessageDialogWithToggle.PROMPT);
            }
        }
        try
        {
            preferences.flush();
        }
        catch (BackingStoreException e)
        {
            //do nothing
        }
    }

    /**
     * Set a preference key to a certain value
     * This key is used to toggle dialogs. Do not use it for general proposes
     * @param preferenceKey
     * @param value
     */
    public static void setToggleDialogPreferenceKey(final String preferenceKey, final String value)
    {
        final String prefKey = preferenceKey + TOGGLE_DIALOG;
        AbstractUIPlugin plugin = CommonPlugin.getDefault();
        IPreferenceStore store = plugin.getPreferenceStore();

        store.setValue(prefKey, value);
    }

    /**
     * Get a preference toggle dialog preference key
     * This key is used to toggle dialogs. Do not use it for general proposes
     * @param preferenceKey
     * @return the key, or null if it is default (not set)
     */
    public static String getToggleDialogPreferenceKey(final String preferenceKey)
    {
        final String prefKey = preferenceKey + TOGGLE_DIALOG;
        IPreferenceStore store = CommonPlugin.getDefault().getPreferenceStore();

        String prefValue = null;
        if (!store.isDefault(prefKey))
        {
            prefValue = store.getString(prefKey);
        }
        return prefValue;
    }
}