protected void createNewFileFromTemplate(final TemplateElement template) { IStructuredSelection selection = getActiveSelection(); if (!selection.isEmpty()) { Object element = selection.getFirstElement(); if (element instanceof IAdaptable) { IFileStore fileStore = (IFileStore) ((IAdaptable) element).getAdapter(IFileStore.class); if (fileStore != null) { // this is a non-workspace selection String filetype = template.getFiletype(); // strips the leading * before . if there is one int index = filetype.lastIndexOf('.'); if (index > -1) { filetype = filetype.substring(index); } NewFileAction action = new NewFileAction("new_file" + filetype, template); // $NON-NLS-1$ action.updateSelection(selection); action.run(); return; } } } NewTemplateFileWizard wizard = new NewTemplateFileWizard(template); wizard.init(PlatformUI.getWorkbench(), selection); WizardDialog dialog = new WizardDialog(UIUtils.getActiveShell(), wizard); dialog.open(); }
protected void createNewBlankFile(String editorType, String fileExtension) { final String initialFileName = "new_file." + fileExtension; // $NON-NLS-1$ IStructuredSelection selection = getActiveSelection(); if (!selection.isEmpty()) { Object element = selection.getFirstElement(); if (element instanceof IAdaptable) { IFileStore fileStore = (IFileStore) ((IAdaptable) element).getAdapter(IFileStore.class); if (fileStore != null) { // this is a non-workspace selection NewFileAction action = new NewFileAction(initialFileName) { @Override protected InputStream getInitialContents() { // empty content return new ByteArrayInputStream(ArrayUtil.NO_BYTES); } }; action.updateSelection(selection); action.run(); return; } } } BasicNewFileResourceWizard wizard = new BasicNewFileResourceWizard() { @Override public void addPages() { super.addPages(); ((WizardNewFileCreationPage) getPages()[0]).setFileName(initialFileName); } }; wizard.init(PlatformUI.getWorkbench(), selection); WizardDialog dialog = new WizardDialog(UIUtils.getActiveShell(), wizard); dialog.open(); }