com.mobilesorcery.sdk.update.internal.preferences.ProfileUpdatesPreferencePage.java Source code

Java tutorial

Introduction

Here is the source code for com.mobilesorcery.sdk.update.internal.preferences.ProfileUpdatesPreferencePage.java

Source

/*  Copyright (C) 2009 Mobile Sorcery AB
    
This program is free software; you can redistribute it and/or modify it
under the terms of the Eclipse Public License v1.0.
    
This program is distributed in the hope that it will be useful, but WITHOUT
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
FITNESS FOR A PARTICULAR PURPOSE. See the Eclipse Public License v1.0 for
more details.
    
You should have received a copy of the Eclipse Public License v1.0 along
with this program. It is also available at http://www.eclipse.org/legal/epl-v10.html
*/
package com.mobilesorcery.sdk.update.internal.preferences;

import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.jface.preference.BooleanFieldEditor;
import org.eclipse.jface.preference.FieldEditorPreferencePage;
import org.eclipse.jface.preference.IPreferenceStore;
import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.swt.widgets.Text;
import org.eclipse.ui.IWorkbench;
import org.eclipse.ui.IWorkbenchPreferencePage;

import com.mobilesorcery.sdk.core.CoreMoSyncPlugin;
import com.mobilesorcery.sdk.core.MoSyncTool;
import com.mobilesorcery.sdk.core.Util;
import com.mobilesorcery.sdk.update.MosyncUpdatePlugin;
import com.mobilesorcery.sdk.update.UpdateManager;

public class ProfileUpdatesPreferencePage extends FieldEditorPreferencePage implements IWorkbenchPreferencePage {

    private Text hhashText;
    private Button clearSettings;

    public ProfileUpdatesPreferencePage() {
        super(Messages.ProfileUpdatesPreferencePage_Title,
                CoreMoSyncPlugin.getImageDescriptor("/icons/mosyncproject.png"), GRID); //$NON-NLS-2$

        IPreferenceStore store = MosyncUpdatePlugin.getDefault().getPreferenceStore();
        setPreferenceStore(store);
    }

    @Override
    protected void createFieldEditors() {
        BooleanFieldEditor autoUpdate = new BooleanFieldEditor(MoSyncTool.AUTO_UPDATE_PREF,
                Messages.ProfileUpdatesPreferencePage_AutoUpdate, getFieldEditorParent());
        addField(autoUpdate);

        Composite registration = new Composite(getFieldEditorParent(), SWT.NONE);
        registration.setLayout(new GridLayout(2, false));
        registration.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));

        Label reg = new Label(registration, SWT.NONE);
        reg.setText(Messages.ProfileUpdatesPreferencePage_RegistrationDescription);
        reg.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));

        clearSettings = new Button(registration, SWT.PUSH);
        clearSettings.setText(Messages.ProfileUpdatesPreferencePage_ClearRegistrationInfo);
        clearSettings.setLayoutData(new GridData(SWT.RIGHT, SWT.TOP, false, false));

        hhashText = new Text(registration, SWT.READ_ONLY | SWT.SINGLE);
        GridData emailData = new GridData(GridData.FILL_HORIZONTAL);
        emailData.horizontalSpan = 2;
        hhashText.setLayoutData(emailData);

        updateUI();

        clearSettings.addListener(SWT.Selection, new Listener() {
            @Override
            public void handleEvent(Event event) {
                boolean result = MessageDialog.openQuestion(getShell(),
                        Messages.ProfileUpdatesPreferencePage_AreYouSureTitle,
                        Messages.ProfileUpdatesPreferencePage_AreYouSureMessage);

                if (result) {
                    UpdateManager.getDefault().clearRegistrationInfo();
                    updateUI();
                }
            }
        });
    }

    private void updateUI() {
        String hhash = MoSyncTool.getDefault().getProperty(MoSyncTool.USER_HASH_PROP_2);
        if (Util.isEmpty(hhash)) {
            hhashText.setText(Messages.ProfileUpdatesPreferencePage_NotRegistered);
        } else {
            hhashText.setText(hhash);
        }

        hhashText.setEnabled(!Util.isEmpty(hhash));
        clearSettings.setEnabled(!Util.isEmpty(hhash));
    }

    @Override
    public void init(IWorkbench window) {
    }

}