/** {@inheritDoc} */
  @Override
  protected void createFieldEditors() {
    disableResolvers =
        new BooleanFieldEditor(
            EMFCompareUIPreferences.DISABLE_RESOLVERS_PREFERENCE,
            EMFCompareIDEUIMessages.getString(
                "ModelResolutionPreferencesPage.disableResolvers"), //$NON-NLS-1$
            getFieldEditorParent());
    addField(disableResolvers);
    useThreads =
        new BooleanFieldEditor(
            EMFCompareUIPreferences.DISABLE_THREADING_PREFERENCE,
            EMFCompareIDEUIMessages.getString(
                "ModelResolutionPreferencesPage.disableThreading"), //$NON-NLS-1$
            getFieldEditorParent());
    addField(useThreads);

    final Composite resolutionScopeComposite = new Composite(getFieldEditorParent(), SWT.BORDER);
    resolutionScopeComposite.setLayoutData(new GridData(SWT.FILL, SWT.TOP, true, false));
    resolutionScopeComposite.setLayout(new GridLayout(2, false));

    resolutionScopeMainDescription = new Label(resolutionScopeComposite, SWT.WRAP);
    resolutionScopeMainDescription.setText(
        EMFCompareIDEUIMessages.getString(
            "ModelResolutionPreferencesPage.resolutionScope.description")); //$NON-NLS-1$
    final GridData layoutData = new GridData(GridData.FILL_HORIZONTAL);
    layoutData.widthHint = 400;
    layoutData.horizontalSpan = 2;
    resolutionScopeMainDescription.setLayoutData(layoutData);

    // Use a composite for the combo editor field alone, to prevent it from grabbing all horizontal
    // space.
    resolutionScopeComboComposite = new Composite(resolutionScopeComposite, SWT.NONE);
    resolutionScopeComboComposite.setLayout(new GridLayout(2, false));
    resolutionScope =
        new ComboFieldEditor(
            EMFCompareUIPreferences.RESOLUTION_SCOPE_PREFERENCE,
            EMFCompareIDEUIMessages.getString(
                "ModelResolutionPreferencesPage.resolutionScope"), //$NON-NLS-1$
            scopeNamesAndValues,
            resolutionScopeComboComposite);
    addField(resolutionScope);
    resolutionScopeComboComposite.setLayoutData(new GridData(SWT.LEFT, SWT.CENTER, false, false));

    resolutionScopeDescriptionComposite = new Composite(resolutionScopeComposite, SWT.BORDER);
    resolutionScopeDescriptionComposite.setLayout(new GridLayout(1, true));
    GridData descriptionData = new GridData(SWT.FILL, SWT.FILL, true, false);
    descriptionData.widthHint = 200;
    resolutionScopeDescriptionComposite.setLayoutData(descriptionData);

    resolutionScopeDescriptionLabel = new Label(resolutionScopeDescriptionComposite, SWT.WRAP);
    GridData labelData = new GridData(SWT.FILL, SWT.FILL, true, true);
    resolutionScopeDescriptionLabel.setLayoutData(labelData);

    updateFieldEnablement(
        getPreferenceStore().getBoolean(EMFCompareUIPreferences.DISABLE_RESOLVERS_PREFERENCE));
    updateScopeDescription(
        getPreferenceStore().getString(EMFCompareUIPreferences.RESOLUTION_SCOPE_PREFERENCE));
  }
 private void updateScopeDescription(String scopeValue) {
   final CrossReferenceResolutionScope scope;
   if (scopeValue != null && scopeValue.length() > 0) {
     scope = CrossReferenceResolutionScope.valueOf(scopeValue);
   } else {
     scope = CrossReferenceResolutionScope.WORKSPACE;
   }
   switch (scope) {
     case WORKSPACE:
       resolutionScopeDescriptionLabel.setText(
           EMFCompareIDEUIMessages.getString(
               "ModelResolutionPreferencesPage.resolutionScope.workspace.description")); //$NON-NLS-1$
       break;
     case PROJECT:
       resolutionScopeDescriptionLabel.setText(
           EMFCompareIDEUIMessages.getString(
               "ModelResolutionPreferencesPage.resolutionScope.project.description")); //$NON-NLS-1$
       break;
     case CONTAINER:
       resolutionScopeDescriptionLabel.setText(
           EMFCompareIDEUIMessages.getString(
               "ModelResolutionPreferencesPage.resolutionScope.container.description")); //$NON-NLS-1$
       break;
     case OUTGOING:
       resolutionScopeDescriptionLabel.setText(
           EMFCompareIDEUIMessages.getString(
               "ModelResolutionPreferencesPage.resolutionScope.outgoing.description")); //$NON-NLS-1$
       break;
     default:
       // Shouldn't happen
       resolutionScopeDescriptionLabel.setText(
           EMFCompareIDEUIMessages.getString(
               "ModelResolutionPreferencesPage.resolutionScope.invalid")); //$NON-NLS-1$
       break;
   }
   getFieldEditorParent().layout();
 }
/**
 * Preference page for the model resolution, allowing users to disable the resolution altogether or
 * to configure how EMF Compare will handle it.
 *
 * @author <a href="mailto:[email protected]">Laurent Goubet</a>
 */
public class ModelResolutionPreferencePage extends FieldEditorPreferencePage
    implements IWorkbenchPreferencePage {
  private static final String[][] scopeNamesAndValues =
      new String[][] {
        {
          EMFCompareIDEUIMessages.getString(
              "ModelResolutionPreferencesPage.resolutionScope.workspace.label"), //$NON-NLS-1$
          CrossReferenceResolutionScope.WORKSPACE.name(),
        },
        {
          EMFCompareIDEUIMessages.getString(
              "ModelResolutionPreferencesPage.resolutionScope.project.label"), //$NON-NLS-1$
          CrossReferenceResolutionScope.PROJECT.name(),
        },
        {
          EMFCompareIDEUIMessages.getString(
              "ModelResolutionPreferencesPage.resolutionScope.container.label"), //$NON-NLS-1$
          CrossReferenceResolutionScope.CONTAINER.name(),
        },
        {
          EMFCompareIDEUIMessages.getString(
              "ModelResolutionPreferencesPage.resolutionScope.outgoing.label"), //$NON-NLS-1$
          CrossReferenceResolutionScope.OUTGOING.name(),
        },
      };

  private BooleanFieldEditor disableResolvers;

  private BooleanFieldEditor useThreads;

  private ComboFieldEditor resolutionScope;

  private Label resolutionScopeMainDescription;

  private Label resolutionScopeDescriptionLabel;

  private Composite resolutionScopeDescriptionComposite;

  private Composite resolutionScopeComboComposite;

  public ModelResolutionPreferencePage() {
    super(FieldEditorPreferencePage.GRID);
    setPreferenceStore(EMFCompareIDEUIPlugin.getDefault().getPreferenceStore());
  }

  /** {@inheritDoc} */
  @Override
  protected void createFieldEditors() {
    disableResolvers =
        new BooleanFieldEditor(
            EMFCompareUIPreferences.DISABLE_RESOLVERS_PREFERENCE,
            EMFCompareIDEUIMessages.getString(
                "ModelResolutionPreferencesPage.disableResolvers"), //$NON-NLS-1$
            getFieldEditorParent());
    addField(disableResolvers);
    useThreads =
        new BooleanFieldEditor(
            EMFCompareUIPreferences.DISABLE_THREADING_PREFERENCE,
            EMFCompareIDEUIMessages.getString(
                "ModelResolutionPreferencesPage.disableThreading"), //$NON-NLS-1$
            getFieldEditorParent());
    addField(useThreads);

    final Composite resolutionScopeComposite = new Composite(getFieldEditorParent(), SWT.BORDER);
    resolutionScopeComposite.setLayoutData(new GridData(SWT.FILL, SWT.TOP, true, false));
    resolutionScopeComposite.setLayout(new GridLayout(2, false));

    resolutionScopeMainDescription = new Label(resolutionScopeComposite, SWT.WRAP);
    resolutionScopeMainDescription.setText(
        EMFCompareIDEUIMessages.getString(
            "ModelResolutionPreferencesPage.resolutionScope.description")); //$NON-NLS-1$
    final GridData layoutData = new GridData(GridData.FILL_HORIZONTAL);
    layoutData.widthHint = 400;
    layoutData.horizontalSpan = 2;
    resolutionScopeMainDescription.setLayoutData(layoutData);

    // Use a composite for the combo editor field alone, to prevent it from grabbing all horizontal
    // space.
    resolutionScopeComboComposite = new Composite(resolutionScopeComposite, SWT.NONE);
    resolutionScopeComboComposite.setLayout(new GridLayout(2, false));
    resolutionScope =
        new ComboFieldEditor(
            EMFCompareUIPreferences.RESOLUTION_SCOPE_PREFERENCE,
            EMFCompareIDEUIMessages.getString(
                "ModelResolutionPreferencesPage.resolutionScope"), //$NON-NLS-1$
            scopeNamesAndValues,
            resolutionScopeComboComposite);
    addField(resolutionScope);
    resolutionScopeComboComposite.setLayoutData(new GridData(SWT.LEFT, SWT.CENTER, false, false));

    resolutionScopeDescriptionComposite = new Composite(resolutionScopeComposite, SWT.BORDER);
    resolutionScopeDescriptionComposite.setLayout(new GridLayout(1, true));
    GridData descriptionData = new GridData(SWT.FILL, SWT.FILL, true, false);
    descriptionData.widthHint = 200;
    resolutionScopeDescriptionComposite.setLayoutData(descriptionData);

    resolutionScopeDescriptionLabel = new Label(resolutionScopeDescriptionComposite, SWT.WRAP);
    GridData labelData = new GridData(SWT.FILL, SWT.FILL, true, true);
    resolutionScopeDescriptionLabel.setLayoutData(labelData);

    updateFieldEnablement(
        getPreferenceStore().getBoolean(EMFCompareUIPreferences.DISABLE_RESOLVERS_PREFERENCE));
    updateScopeDescription(
        getPreferenceStore().getString(EMFCompareUIPreferences.RESOLUTION_SCOPE_PREFERENCE));
  }

  @Override
  public void propertyChange(PropertyChangeEvent event) {
    if (event.getSource() == disableResolvers) {
      updateFieldEnablement(disableResolvers.getBooleanValue());
    } else if (event.getSource() == resolutionScope) {
      updateScopeDescription((String) event.getNewValue());
    }
    super.propertyChange(event);
  }

  private void updateFieldEnablement(boolean disabled) {
    useThreads.setEnabled(!disabled, getFieldEditorParent());
    resolutionScopeMainDescription.setEnabled(!disabled);
    resolutionScope.setEnabled(!disabled, resolutionScopeComboComposite);
    resolutionScopeDescriptionComposite.setEnabled(!disabled);
  }

  private void updateScopeDescription(String scopeValue) {
    final CrossReferenceResolutionScope scope;
    if (scopeValue != null && scopeValue.length() > 0) {
      scope = CrossReferenceResolutionScope.valueOf(scopeValue);
    } else {
      scope = CrossReferenceResolutionScope.WORKSPACE;
    }
    switch (scope) {
      case WORKSPACE:
        resolutionScopeDescriptionLabel.setText(
            EMFCompareIDEUIMessages.getString(
                "ModelResolutionPreferencesPage.resolutionScope.workspace.description")); //$NON-NLS-1$
        break;
      case PROJECT:
        resolutionScopeDescriptionLabel.setText(
            EMFCompareIDEUIMessages.getString(
                "ModelResolutionPreferencesPage.resolutionScope.project.description")); //$NON-NLS-1$
        break;
      case CONTAINER:
        resolutionScopeDescriptionLabel.setText(
            EMFCompareIDEUIMessages.getString(
                "ModelResolutionPreferencesPage.resolutionScope.container.description")); //$NON-NLS-1$
        break;
      case OUTGOING:
        resolutionScopeDescriptionLabel.setText(
            EMFCompareIDEUIMessages.getString(
                "ModelResolutionPreferencesPage.resolutionScope.outgoing.description")); //$NON-NLS-1$
        break;
      default:
        // Shouldn't happen
        resolutionScopeDescriptionLabel.setText(
            EMFCompareIDEUIMessages.getString(
                "ModelResolutionPreferencesPage.resolutionScope.invalid")); //$NON-NLS-1$
        break;
    }
    getFieldEditorParent().layout();
  }

  @Override
  protected void performDefaults() {
    super.performDefaults();
    updateFieldEnablement(
        getPreferenceStore().getBoolean(EMFCompareUIPreferences.DISABLE_RESOLVERS_PREFERENCE));
    updateScopeDescription(
        getPreferenceStore().getString(EMFCompareUIPreferences.RESOLUTION_SCOPE_PREFERENCE));
  }

  public void init(IWorkbench workbench) {
    // Empty implementation
  }
}