at.spardat.xma.guidesign.preferences.ProjectPreferencePage.java Source code

Java tutorial

Introduction

Here is the source code for at.spardat.xma.guidesign.preferences.ProjectPreferencePage.java

Source

/*******************************************************************************
 * Copyright (c) 2003, 2007 s IT Solutions AT Spardat GmbH .
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     s IT Solutions AT Spardat GmbH - initial API and implementation
 *******************************************************************************/
package at.spardat.xma.guidesign.preferences;

import org.eclipse.core.resources.IProject;
import org.eclipse.jface.preference.FieldEditor;
import org.eclipse.jface.preference.RadioGroupFieldEditor;
import org.eclipse.jface.preference.StringFieldEditor;
import org.eclipse.jface.util.PropertyChangeEvent;
import org.eclipse.swt.widgets.Composite;

import at.spardat.xma.gui.projectw.cmp.BuildToolEnumeration;
import at.spardat.xma.guidesign.plugin.GUIDesignerPlugin;

/**
 * This class represents a preference page that is contributed to the
 * Preferences dialog. 
 * By subclassing <samp>AbstractPreferenceAndPropertyPage</samp>, we can use the field support
 * built into JFace that allows us to create a page that is small and knows how
 * to save, restore and apply itself.
 * <p>
 * This page is used to modify xma related project-level settings. They are
 * either stored in the preference store that belongs to the current project or
 * the running workspace. That way, preferences can be accessed directly via the
 * (scoped) preference store.
 * 
 * @author <a href="mailto:michael.clay@s-itsolutions.at">Michael Clay</a>
 * @version $Revision: 9783 $ $Date: 2009-09-16 09:34:12 +0200 (Mi, 16 Sep 2009)$
 */

public class ProjectPreferencePage extends AbstractPreferenceAndPropertyPage {
    private static final String PAGE_ID_PREFERENCES_PROJECT_PREFERENCE_PAGE = "at.spardat.xma.guidesigner.preferences.ProjectPreferencePage";

    private StringFieldEditor applicationDescriptorEditor;
    private RadioGroupFieldEditor buildToolEditor;
    private BuildToolEnumeration selectedBuildTool = BuildToolEnumeration.ANT;

    public ProjectPreferencePage() {
        super(GRID);
    }

    /**
     * Creates the field editors. Field editors are abstractions of the common
     * GUI blocks needed to manipulate various types of preferences. Each field
     * editor knows how to save and restore itself.
     */
    public void createFieldEditors() {
        IProject project = (IProject) getElement();
        addField(new ProjectDirectorySelectionFieldEditor(PreferenceConstants.JAVA_SRC_ROOT_FOLDER,
                Messages.getString("ProjectPreferencesPage.Java_Source_Folder"), getFieldEditorParent(), project,
                true));
        addField(new ProjectDirectorySelectionFieldEditor(PreferenceConstants.WEBAPP_ROOT_FOLDER,
                Messages.getString("ProjectPreferencesPage.Webapp_Folder"), getFieldEditorParent(), project, true));
        addField(new ProjectDirectorySelectionFieldEditor(PreferenceConstants.RESOURCES_SRC_ROOT_FOLDER,
                Messages.getString("ProjectPreferencesPage.Resources_Source_Folder"), getFieldEditorParent(),
                project, true));
        addField(new ProjectDirectorySelectionFieldEditor(PreferenceConstants.JAVA_TEST_ROOT_FOLDER,
                Messages.getString("ProjectPreferencesPage.Test_Source_Folder"), getFieldEditorParent(), project,
                true));
        applicationDescriptorEditor = new ProjectDirectorySelectionFieldEditor(
                PreferenceConstants.APPLICATION_DESCRIPTOR_FOLDER,
                Messages.getString("ProjectPreferencesPage.App_Descriptor_Folder"), getFieldEditorParent(), project,
                true);
        applicationDescriptorEditor.setEnabled(false, getFieldEditorParent());
        addField(applicationDescriptorEditor);
        buildToolEditor = new RadioGroupFieldEditor(PreferenceConstants.USED_BUILD_TOOL,
                Messages.getString("ProjectPreferencesPage.Used_Build_Tool"), 2,
                new String[][] { { "Ant", BuildToolEnumeration.ANT.toString() },
                        { "Maven", BuildToolEnumeration.MAVEN.toString() } },
                getFieldEditorParent());
        addField(buildToolEditor);

        addField(new StringFieldEditor(PreferenceConstants.MODEL_ROOT_PACKAGE,
                Messages.getString("ProjectPreferencesPage.Model_Root_Package"), getFieldEditorParent()));
    }

    public void propertyChange(PropertyChangeEvent propertyChangeEvent) {
        if (propertyChangeEvent.getSource() == buildToolEditor) {
            selectedBuildTool = BuildToolEnumeration.valueOf((String) propertyChangeEvent.getNewValue());
            applicationDescriptorEditor.setEnabled(BuildToolEnumeration.MAVEN.equals(selectedBuildTool),
                    ProjectPreferencePage.this.getFieldEditorParent());
        }
    }

    @Override
    protected void initialize() {
        super.initialize();
        selectedBuildTool = BuildToolEnumeration
                .valueOf(getPreferenceStore().getString(PreferenceConstants.USED_BUILD_TOOL));
    }

    protected void doEnableFieldEditor(boolean enabled, Composite parent, FieldEditor editor) {
        if (enabled == true && applicationDescriptorEditor == editor
                && BuildToolEnumeration.ANT.equals(selectedBuildTool)) {
            // skip
        } else {
            super.doEnableFieldEditor(enabled, parent, editor);
        }
    }

    protected String getPageId() {
        return PAGE_ID_PREFERENCES_PROJECT_PREFERENCE_PAGE;
    }

    @Override
    protected String getNodeQualifier() {
        return GUIDesignerPlugin.getPlugin().getBundle().getSymbolicName();
    }

}