/**
     * Open the filter editor on a given selection of the list.
     *
     * @param selection the selection
     */
    private void openFilterEditor(ISelection selection) {

      if (selection instanceof IStructuredSelection) {
        Object selectedElement = ((IStructuredSelection) selection).getFirstElement();

        if (selectedElement instanceof IFilter) {

          try {

            IFilter aFilterDef = (IFilter) selectedElement;

            if (!PluginFilterEditors.hasEditor(aFilterDef)) {
              return;
            }

            IFilterEditor editableFilter = PluginFilterEditors.getNewEditor(aFilterDef);
            editableFilter.setInputProject(mProjectConfig.getProject());
            editableFilter.setFilterData(aFilterDef.getFilterData());

            if (Window.OK == editableFilter.openEditor(getShell())) {

              aFilterDef.setFilterData(editableFilter.getFilterData());
              mFilterList.refresh();
            }
          } catch (CheckstylePluginException ex) {
            CheckstyleUIPlugin.errorDialog(getShell(), ex, true);
          }
        }
      }
    }
  private void handleConfigFileError(Exception e, IProject project) {

    CheckstyleLog.log(e, Messages.errorOpeningPropertiesPage);
    CheckstyleUIPlugin.warningDialog(null, Messages.errorOpeningPropertiesPage, e);

    IProjectConfiguration projectConfig =
        ProjectConfigurationFactory.createDefaultProjectConfiguration(project);
    mProjectConfig = new ProjectConfigurationWorkingCopy(projectConfig);
    try {
      mCheckstyleInitiallyActivated = project.hasNature(CheckstyleNature.NATURE_ID);
    } catch (CoreException e1) {
      CheckstyleUIPlugin.errorDialog(null, e1.getMessage(), e1, true);
    }
  }
    /**
     * @see
     *     org.eclipse.swt.events.SelectionListener#widgetSelected(org.eclipse.swt.events.SelectionEvent)
     */
    public void widgetSelected(SelectionEvent e) {

      Object source = e.getSource();
      // edit filter
      if (source == mBtnEditFilter) {

        ISelection selection = mFilterList.getSelection();
        openFilterEditor(selection);
        getContainer().updateButtons();
      }
      if (source == mMainTab) {
        mFileSetsEditor.refresh();
        getContainer().updateButtons();

      } else if (source == mChkSyncFormatter) {
        mProjectConfig.setSyncFormatter(mChkSyncFormatter.getSelection());
      } else if (source == mChkSimpleConfig) {
        try {

          mProjectConfig.setUseSimpleConfig(mChkSimpleConfig.getSelection());

          boolean showWarning =
              CheckstyleUIPluginPrefs.getBoolean(CheckstyleUIPluginPrefs.PREF_FILESET_WARNING);
          if (mProjectConfig.isUseSimpleConfig() && showWarning) {
            MessageDialogWithToggle dialog =
                new MessageDialogWithToggle(
                    getShell(),
                    Messages.CheckstylePropertyPage_titleWarnFilesets,
                    null,
                    Messages.CheckstylePropertyPage_msgWarnFilesets,
                    MessageDialogWithToggle.WARNING,
                    new String[] {IDialogConstants.OK_LABEL},
                    0,
                    Messages.CheckstylePropertyPage_mgsWarnFileSetNagOption,
                    showWarning) {
                  /**
                   * Overwritten because we don't want to store which button the user pressed but
                   * the state of the toggle.
                   *
                   * @see MessageDialogWithToggle#buttonPressed(int)
                   */
                  protected void buttonPressed(int buttonId) {
                    getPrefStore().setValue(getPrefKey(), getToggleState());
                    setReturnCode(buttonId);
                    close();
                  }
                };
            dialog.setPrefStore(CheckstyleUIPlugin.getDefault().getPreferenceStore());
            dialog.setPrefKey(CheckstyleUIPluginPrefs.PREF_FILESET_WARNING);
            dialog.open();
          }

          createFileSetsArea(mFileSetsContainer);
          mFileSetsContainer.redraw();
          mFileSetsContainer.update();
          mFileSetsContainer.layout();
        } catch (CheckstylePluginException ex) {
          CheckstyleUIPlugin.errorDialog(getShell(), Messages.errorChangingFilesetEditor, ex, true);
        }
      }
    }
  /**
   * @return the result of the ok action
   * @see org.eclipse.jface.preference.IPreferencePage#performOk()
   */
  public boolean performOk() {

    try {

      IProject project = mProjectConfig.getProject();

      // save the edited project configuration
      if (mProjectConfig.isDirty()) {
        mProjectConfig.store();
      }

      boolean checkstyleEnabled = mChkEnable.getSelection();
      boolean needRebuild = mProjectConfig.isRebuildNeeded();

      // check if checkstyle nature has to be configured/deconfigured
      if (checkstyleEnabled != mCheckstyleInitiallyActivated) {

        ConfigureDeconfigureNatureJob configOperation =
            new ConfigureDeconfigureNatureJob(project, CheckstyleNature.NATURE_ID);
        configOperation.setRule(ResourcesPlugin.getWorkspace().getRoot());
        configOperation.schedule();

        needRebuild = needRebuild || !mCheckstyleInitiallyActivated;
      }

      if (checkstyleEnabled && mProjectConfig.isSyncFormatter()) {

        TransformCheckstyleRulesJob transFormJob = new TransformCheckstyleRulesJob(project);
        transFormJob.schedule();
      }

      // if a rebuild is advised, check/prompt if the rebuild should
      // really be done.
      if (checkstyleEnabled && needRebuild) {

        String promptRebuildPref =
            CheckstyleUIPluginPrefs.getString(CheckstyleUIPluginPrefs.PREF_ASK_BEFORE_REBUILD);

        boolean doRebuild = MessageDialogWithToggle.ALWAYS.equals(promptRebuildPref) && needRebuild;

        //
        // Prompt for rebuild
        //
        if (MessageDialogWithToggle.PROMPT.equals(promptRebuildPref) && needRebuild) {
          MessageDialogWithToggle dialog =
              MessageDialogWithToggle.openYesNoQuestion(
                  getShell(),
                  Messages.CheckstylePropertyPage_titleRebuild,
                  Messages.CheckstylePropertyPage_msgRebuild,
                  Messages.CheckstylePropertyPage_nagRebuild,
                  false,
                  CheckstyleUIPlugin.getDefault().getPreferenceStore(),
                  CheckstyleUIPluginPrefs.PREF_ASK_BEFORE_REBUILD);

          doRebuild = dialog.getReturnCode() == IDialogConstants.YES_ID;
        }

        // check if a rebuild is necessary
        if (checkstyleEnabled && doRebuild) {

          BuildProjectJob rebuildOperation =
              new BuildProjectJob(project, IncrementalProjectBuilder.FULL_BUILD);
          rebuildOperation.setRule(ResourcesPlugin.getWorkspace().getRoot());
          rebuildOperation.schedule();
        }
      }
    } catch (CheckstylePluginException e) {
      CheckstyleUIPlugin.errorDialog(getShell(), e, true);
    }
    return true;
  }
  /** {@inheritDoc} */
  public Control createContents(Composite parent) {

    Composite container = null;

    try {

      this.mPageController = new PageController();

      // suppress default- & apply-buttons
      noDefaultAndApplyButton();

      mMainTab = new TabFolder(parent, SWT.TOP);
      mMainTab.setLayoutData(new GridData(GridData.FILL_BOTH));
      mMainTab.addSelectionListener(mPageController);

      // create the main container
      container = new Composite(mMainTab, SWT.NULL);
      container.setLayout(new FormLayout());
      container.setLayoutData(new GridData(GridData.FILL_BOTH));

      // create the checkbox to enable/diable the simple configuration
      this.mChkSimpleConfig = new Button(container, SWT.CHECK);
      this.mChkSimpleConfig.setText(Messages.CheckstylePropertyPage_btnUseSimpleConfig);
      this.mChkSimpleConfig.addSelectionListener(this.mPageController);
      this.mChkSimpleConfig.setSelection(mProjectConfig.isUseSimpleConfig());

      FormData fd = new FormData();
      // fd.left = new FormAttachment(this.mChkEnable, 0, SWT.RIGHT);
      fd.top = new FormAttachment(0, 3);
      fd.right = new FormAttachment(100, -3);
      this.mChkSimpleConfig.setLayoutData(fd);

      // create the checkbox to enabel/disable checkstyle
      this.mChkEnable = new Button(container, SWT.CHECK);
      this.mChkEnable.setText(Messages.CheckstylePropertyPage_btnActivateCheckstyle);
      this.mChkEnable.addSelectionListener(this.mPageController);
      this.mChkEnable.setSelection(mCheckstyleInitiallyActivated);

      fd = new FormData();
      fd.left = new FormAttachment(0, 3);
      fd.top = new FormAttachment(0, 3);
      fd.right = new FormAttachment(this.mChkSimpleConfig, 3, SWT.LEFT);
      this.mChkEnable.setLayoutData(fd);

      // create the checkbox for formatter synching
      this.mChkSyncFormatter = new Button(container, SWT.CHECK);
      this.mChkSyncFormatter.setText(Messages.CheckstylePropertyPage_btnSyncFormatter);
      this.mChkSyncFormatter.addSelectionListener(this.mPageController);
      this.mChkSyncFormatter.setSelection(mProjectConfig.isSyncFormatter());

      fd = new FormData();
      fd.left = new FormAttachment(0, 3);
      fd.top = new FormAttachment(this.mChkEnable, 3, SWT.BOTTOM);
      this.mChkSyncFormatter.setLayoutData(fd);

      // create the configuration area
      mFileSetsContainer = new Composite(container, SWT.NULL);
      Control configArea = createFileSetsArea(mFileSetsContainer);
      fd = new FormData();
      fd.left = new FormAttachment(0, 3);
      fd.top = new FormAttachment(this.mChkSyncFormatter, 6, SWT.BOTTOM);
      fd.right = new FormAttachment(100, -3);
      fd.bottom = new FormAttachment(45);
      configArea.setLayoutData(fd);

      // create the filter area
      Control filterArea = createFilterArea(container);
      fd = new FormData();
      fd.left = new FormAttachment(0, 3);
      fd.top = new FormAttachment(configArea, 3, SWT.BOTTOM);
      fd.right = new FormAttachment(100, -3);
      fd.bottom = new FormAttachment(100, -3);
      fd.width = 500;
      filterArea.setLayoutData(fd);

      // create the local configurations area
      Control localConfigArea = createLocalConfigArea(mMainTab);

      TabItem mainItem = new TabItem(mMainTab, SWT.NULL);
      mainItem.setControl(container);
      mainItem.setText(Messages.CheckstylePropertyPage_tabMain);

      TabItem localItem = new TabItem(mMainTab, SWT.NULL);
      localItem.setControl(localConfigArea);
      localItem.setText(Messages.CheckstylePropertyPage_tabCheckConfigs);

    } catch (CheckstylePluginException e) {
      CheckstyleUIPlugin.errorDialog(getShell(), Messages.errorOpeningPropertiesPage, e, true);
    }

    return container;
  }