一次使用aWizardNewFileCreationPage时,如何使用BasicNewFileResourceWizard和初始文件上下文合并文件扩展名?

时间:2019-06-25 11:30:10

标签: eclipse plugins eclipse-plugin eclipse-rcp

我有一个用于扩展新创建文件的类。

public class MyNewFileWizard extends BasicNewFileResourceWizard
{
  @Override
  public void addPages()
  {
    super.addPages();
    MyWizardNewFileCreationPage page = (MyWizardNewFileCreationPage )getPage("newFilePage1");
    page.setFileExtension("css");
    addPage(page);
  }

  @Override
  public void init(IWorkbench workbench, IStructuredSelection currentSelection)
  {
    super.init(workbench, currentSelection);
    setNeedsProgressMonitor(true);
  }}

我还有一个类来提供创建的类的上下文

public class MyWizardNewFileCreationPage extends WizardNewFileCreationPage
{

...

@Override
  protected InputStream getInitialContents()
  {


//to give same strings context for every generated css file
    StringBuilder sb = new StringBuilder();
    sb.append("SAMPLE_CSS_FILE"); //$NON-NLS-1$
    sb.append("SECTION_1"); //$NON-NLS-1$
    sb.append("SECTION_1_BODY_1"); //$NON-NLS-1$

    return new ByteArrayInputStream(sb.toString().getBytes());
  }

plugin.xml

..
<wizard
        category="ui.category"
        id="ui.wizard.MyNewFileWizard"
        name="Create a new app.test File"
        icon="icons/project.png"
        class="MyNewFileWizard"
        project="false"
        >
        </wizard>

我的目标是创建一个具有已定义上下文的CSS类。所以以某种方式将这两个类结合在一起。

2 个答案:

答案 0 :(得分:0)

您无法致电super.addPages,因为那样会增加普通的WizardNewFileCreationPage。您只需添加自己的页面:

@Override
public void addPages()
{
   MyWizardNewFileCreationPage page = new MyWizardNewFileCreationPage("newFilePage1", getSelection());
   page.setFileExtension("css");
   addPage(page);
}

但是这样做意味着标准BasicNewFileResourceWizard.performFinish无法正常工作,因此您也必须覆盖它:

@Override
public boolean performFinish() {
  MyWizardNewFileCreationPage mainPage = (MyWizardNewFileCreationPage )getPage("newFilePage1");

  IFile file = mainPage.createNewFile();
  if (file == null) {
      return false;
  }

  selectAndReveal(file);

  // Open editor on new file.
  IWorkbenchWindow dw = getWorkbench().getActiveWorkbenchWindow();
  try {
      if (dw != null) {
          IWorkbenchPage page = dw.getActivePage();
          if (page != null) {
              IDE.openEditor(page, file, true);
          }
      }
  } catch (final PartInitException e) {
      // Show error
  }

  return true;

}

答案 1 :(得分:0)

public class MyNewFileWizard extends BasicNewFileResourceWizard
{
  WizardNewFileCreationPage mainPage;

  public static final String WIZARD_ID = "ui.wizard.MyNewFileWizard";

  public MyNewFileWizard()
  {
    super();
  }

  @Override
  public void addPages()
  {

    mainPage = new WizardNewFileCreationPage("New File Page", getSelection())
    {
      @Override
      protected InputStream getInitialContents()
      {
        StringBuilder sb = new StringBuilder();
        sb.append("SAMPLE_README_FILE");
        sb.append("SECTION_1");
        sb.append("SECTION_1_BODY_1");

        return new ByteArrayInputStream(sb.toString().getBytes());
      }
    };

    mainPage.setFileExtension("css");
    addPage(mainPage);
  }
  @Override
  protected void initializeDefaultPageImageDescriptor()
  {
    //setDefaultPageImageDescriptor(imageHelper.getImageDescriptor("icon_48x48.png"));
  }

  @Override
  public void init(IWorkbench workbench, IStructuredSelection currentSelection)
  {
    super.init(workbench, currentSelection);
    setWindowTitle("New Solidity file");
    setNeedsProgressMonitor(true);
  }

  @Override
  public boolean performFinish()
  {
    IFile file = mainPage.createNewFile();
    if (file == null) {
      return false;
    }

    selectAndReveal(file);

    // Open editor on new file.
    IWorkbenchWindow dw = getWorkbench().getActiveWorkbenchWindow();
    try {
      if (dw != null) {
        IWorkbenchPage page = dw.getActivePage();
        if (page != null) {
          IDE.openEditor(page, file, true);
        }
      }
    }
    catch (PartInitException e) {
      //openError(dw.getShell(), "Problems opening editor", e.getMessage(), e);
    }

    return true;
  }
}