/**
   * Creates a new file resource as requested by the user. If everything is OK then answer true. If
   * not, false will cause the dialog to stay open and the appropriate error message is shown
   *
   * @return whether creation was successful
   * @see WOComponentCreationWizard#performFinish()
   */
  public boolean createComponent() {
    WOComponentCreator componentCreator;
    String componentName = getFileName();
    String packageName = _packageDialogField.getText();
    String superclassName = _superclassDialogField.getText();
    IProject actualProject =
        ResourcesPlugin.getWorkspace().getRoot().getProject(getContainerFullPath().segment(0));
    switch (getContainerFullPath().segmentCount()) {
      case 0:
        // not possible ( see validatePage() )
        setErrorMessage("unknown error");
        return false;
      default:
        // determine parent resource for component creator by removing
        // first element (workspace) from full path
        IFolder subprojectFolder =
            actualProject.getFolder(getContainerFullPath().removeFirstSegments(1));
        componentCreator =
            new WOComponentCreator(
                subprojectFolder,
                componentName,
                packageName,
                superclassName,
                _bodyCheckbox.getSelection(),
                _apiCheckbox.getSelection(),
                this);
        break;
    }
    this.getDialogSettings()
        .put(WOComponentCreationPage.SUPERCLASS_KEY, _superclassDialogField.getText());
    this.getDialogSettings()
        .put(WOComponentCreationPage.BODY_CHECKBOX_KEY, _bodyCheckbox.getSelection());
    this.getDialogSettings().put(WOComponentCreationPage.HTML_DOCTYPE_KEY, _htmlCombo.getText());
    this.getDialogSettings()
        .put(WOComponentCreationPage.NSSTRING_ENCODING_KEY, _encodingCombo.getText());
    this.getDialogSettings()
        .put(WOComponentCreationPage.API_CHECKBOX_KEY, _apiCheckbox.getSelection());

    // logPreferences();
    IRunnableWithProgress op = new WorkspaceModifyDelegatingOperation(componentCreator);
    return createResourceOperation(op);
  }
  protected String chooseSuperclass() {
    Set<String> superclasses = new HashSet<String>();
    try {
      IProject actualProject =
          ResourcesPlugin.getWorkspace().getRoot().getProject(getContainerFullPath().segment(0));
      IJavaProject javaProject =
          JavaModelManager.getJavaModelManager().getJavaModel().getJavaProject(actualProject);

      TypeNameCollector typeNameCollector = new TypeNameCollector(javaProject, false);
      BindingReflectionUtils.findMatchingElementClassNames(
          "", SearchPattern.R_PREFIX_MATCH, typeNameCollector, new NullProgressMonitor());
      for (String typeName : typeNameCollector.getTypeNames()) {
        // int dotIndex = typeName.lastIndexOf('.');
        // if (dotIndex != -1) {
        // typeName = typeName.substring(dotIndex + 1);
        // }
        // validValues.add("\"" + typeName + "\"");
        superclasses.add(typeName);
      }
    } catch (JavaModelException e) {
      // JTourBusPlugin.log(e);
      e.printStackTrace();
    }

    ElementListSelectionDialog dialog =
        new ElementListSelectionDialog(getShell(), new StringLabelProvider());
    dialog.setIgnoreCase(true);
    dialog.setTitle(NewWizardMessages.NewTypeWizardPage_SuperClassDialog_title);
    dialog.setMessage(NewWizardMessages.NewTypeWizardPage_SuperClassDialog_message);
    // dialog.setEmptyListMessage(NewWizardMessages.NewTypeWiz);
    dialog.setFilter(_superclassDialogField.getText());
    dialog.setElements(superclasses.toArray());
    if (dialog.open() == Window.OK) {
      return (String) dialog.getFirstResult();
    }
    return null;
  }
  protected IPackageFragment choosePackage() {
    List<IJavaElement> packagesList = new LinkedList<IJavaElement>();
    try {
      IProject actualProject =
          ResourcesPlugin.getWorkspace().getRoot().getProject(getContainerFullPath().segment(0));
      IJavaProject javaProject =
          JavaModelManager.getJavaModelManager().getJavaModel().getJavaProject(actualProject);
      IPackageFragmentRoot[] roots = javaProject.getPackageFragmentRoots();
      for (int k = 0; k < roots.length; k++) {
        if (roots[k].getKind() == IPackageFragmentRoot.K_SOURCE) {
          IJavaElement[] children = roots[k].getChildren();
          for (int i = 0; i < children.length; i++) {
            packagesList.add(children[i]);
          }
        }
      }
    } catch (JavaModelException e) {
      // JTourBusPlugin.log(e);
      e.printStackTrace();
    }
    IJavaElement[] packages = packagesList.toArray(new IJavaElement[packagesList.size()]);

    ElementListSelectionDialog dialog =
        new ElementListSelectionDialog(
            getShell(), new JavaElementLabelProvider(JavaElementLabelProvider.SHOW_DEFAULT));
    dialog.setIgnoreCase(false);
    dialog.setTitle(NewWizardMessages.NewTypeWizardPage_ChoosePackageDialog_title);
    dialog.setMessage(NewWizardMessages.NewTypeWizardPage_ChoosePackageDialog_description);
    dialog.setEmptyListMessage(NewWizardMessages.NewTypeWizardPage_ChoosePackageDialog_empty);
    dialog.setFilter(_packageDialogField.getText());
    dialog.setElements(packages);
    if (dialog.open() == Window.OK) {
      return (IPackageFragment) dialog.getFirstResult();
    }
    return null;
  }
  /** (non-Javadoc) Method declared on IDialogPage. */
  public void createControl(Composite parent) {
    // inherit default container and name specification widgets
    super.createControl(parent);

    Composite composite = (Composite) getControl();
    // WorkbenchHelp.setHelp(composite,
    // IReadmeConstants.CREATION_WIZARD_PAGE_CONTEXT);
    this.setFileName(Messages.getString("WOComponentCreationPage.newComponent.defaultName"));

    // new Label(composite, SWT.NONE); // vertical spacer

    Group javaGroup = new Group(composite, SWT.NONE);
    javaGroup.setText(Messages.getString("WOComponentCreationPage.creationOptions.javaFile.group"));
    GridLayout javaLayout = new GridLayout();
    javaLayout.numColumns = 4;
    javaGroup.setLayout(javaLayout);
    javaGroup.setLayoutData(
        new GridData(GridData.GRAB_HORIZONTAL | GridData.HORIZONTAL_ALIGN_FILL));

    PackageButtonAdapter packageButtonAdapter = new PackageButtonAdapter();
    _packageDialogField = new StringButtonStatusDialogField(packageButtonAdapter);
    _packageDialogField.setDialogFieldListener(packageButtonAdapter);
    _packageDialogField.setLabelText(NewWizardMessages.NewTypeWizardPage_package_label);
    _packageDialogField.setButtonLabel(NewWizardMessages.NewTypeWizardPage_package_button);
    _packageDialogField.setStatusWidthHint(NewWizardMessages.NewTypeWizardPage_default);
    _packageDialogField.doFillIntoGrid(javaGroup, 4);
    Text packageText = _packageDialogField.getTextControl(null);
    LayoutUtil.setWidthHint(packageText, convertWidthInCharsToPixels(40));
    LayoutUtil.setHorizontalGrabbing(packageText);
    // JavaPackageCompletionProcessor packageCompletionProcessor= new
    // JavaPackageCompletionProcessor();
    // ControlContentAssistHelper.createTextContentAssistant(text,
    // packageCompletionProcessor);

    if (_currentSelection instanceof IPackageFragment) {
      _packageDialogField.setText(((IPackageFragment) _currentSelection).getElementName());
    } else {
      String _package = null;
      IResource _resource =
          ResourcesPlugin.getWorkspace().getRoot().findMember(this.getContainerFullPath());
      if (_resource instanceof IFolder) {
        _package = packageNameForComponentFolder((IFolder) _resource);
      }
      if (_package == null && (_package = packageNameForComponent("Main")) == null) {
        _package = "";
      }
      _packageDialogField.setText(_package);
    }

    SuperclassButtonAdapter superclassButtonAdapter = new SuperclassButtonAdapter();
    _superclassDialogField = new StringButtonStatusDialogField(superclassButtonAdapter);
    _superclassDialogField.setDialogFieldListener(superclassButtonAdapter);
    _superclassDialogField.setLabelText(NewWizardMessages.NewTypeWizardPage_superclass_label);
    _superclassDialogField.setButtonLabel(NewWizardMessages.NewTypeWizardPage_superclass_button);
    _superclassDialogField.setStatusWidthHint(NewWizardMessages.NewTypeWizardPage_default);
    _superclassDialogField.doFillIntoGrid(javaGroup, 4);
    String superclass = this.getDialogSettings().get(WOComponentCreationPage.SUPERCLASS_KEY);
    if (superclass == null || superclass.length() == 0) {
      _superclassDialogField.setText("com.webobjects.appserver.WOComponent");
    } else {
      _superclassDialogField.setText(superclass);
    }
    Text superclassText = _superclassDialogField.getTextControl(null);
    LayoutUtil.setWidthHint(superclassText, convertWidthInCharsToPixels(40));
    LayoutUtil.setHorizontalGrabbing(superclassText);

    new Label(composite, SWT.NONE); // vertical spacer

    /*
     * HTML body generation options
     */
    Group optionalFilesGroup = new Group(composite, SWT.NONE);
    optionalFilesGroup.setLayout(new GridLayout(3, false));
    optionalFilesGroup.setText(Messages.getString("WOComponentCreationPage.creationOptions.group"));
    optionalFilesGroup.setLayoutData(
        new GridData(GridData.GRAB_HORIZONTAL | GridData.HORIZONTAL_ALIGN_FILL));

    ButtonSelectionAdaptor listener = new ButtonSelectionAdaptor();
    _bodyCheckbox = new Button(optionalFilesGroup, SWT.CHECK);
    _bodyCheckbox.setText(
        Messages.getString("WOComponentCreationPage.creationOptions.bodyTag.button"));
    _bodyCheckbox.setSelection(this.getDialogSettings().getBoolean(BODY_CHECKBOX_KEY));
    _bodyCheckbox.setAlignment(SWT.CENTER);
    _bodyCheckbox.addListener(SWT.Selection, this);
    _bodyCheckbox.addSelectionListener(listener);

    Label htmlLabel = new Label(optionalFilesGroup, SWT.RIGHT);
    htmlLabel.setText(Messages.getString("WOComponentCreationPage.creationOptions.bodyTag.label"));
    htmlLabel.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
    _htmlCombo = new Combo(optionalFilesGroup, SWT.DROP_DOWN);
    _htmlCombo.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
    populateHTMLCombo(_htmlCombo);
    refreshButtonSettings(_bodyCheckbox);

    _apiCheckbox = new Button(optionalFilesGroup, SWT.CHECK);
    GridData apiLayoutData = new GridData();
    //		apiLayoutData.horizontalSpan = 3;
    _apiCheckbox.setLayoutData(apiLayoutData);
    _apiCheckbox.setText(
        Messages.getString("WOComponentCreationPage.creationOptions.apiFile.button"));
    _apiCheckbox.setSelection(this.getDialogSettings().getBoolean(API_CHECKBOX_KEY));
    _apiCheckbox.addListener(SWT.Selection, this);
    _apiCheckbox.addSelectionListener(listener);

    Label encodingLabel = new Label(optionalFilesGroup, SWT.RIGHT);
    encodingLabel.setText(
        Messages.getString("WOComponentCreationPage.creationOptions.wooFile.label"));
    encodingLabel.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
    _encodingCombo = new Combo(optionalFilesGroup, SWT.DROP_DOWN);
    _encodingCombo.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
    populateStringEncodingCombo(_encodingCombo);

    setPageComplete(validatePage());
  }