/** * @param action * @throws Exception */ @Override public void runAction() throws Exception { // final TemplateCache templateCache = TemplateCache.getInstance(); final CreateSnippetData createSnippetData = getCreateSnippetData(EMPTY_STR); this.snippetSelection = true; if (createSnippetData == null || createSnippetData.getTemplateType() == null) { return; } if (!actionClassesMap.containsKey(createSnippetData.getTemplateType())) { super.createSnippetData = createSnippetData; super.runAction(); return; } final Class<? extends IActionDelegate> clazz = actionClassesMap.get(createSnippetData.getTemplateType()); try { final IActionDelegate instance = clazz.newInstance(); if (this.window != null) { this.editorPart = Activator.getDefault() .getWorkbench() .getActiveWorkbenchWindow() .getActivePage() .getActiveEditor(); } ((AbstractCreateNewSnippetAction) instance).editorPart = this.editorPart; ((AbstractCreateNewSnippetAction) instance).useLast = this.useLast; ((AbstractCreateNewSnippetAction) instance).createSnippetData = createSnippetData; ((AbstractCreateNewSnippetAction) instance).snippetSelection = true; ((AbstractCreateNewSnippetAction) instance).runAction(); } catch (final Exception ex) { ex.printStackTrace(); throw new Exception(ex.getMessage()); } finally { this.snippetSelection = false; } }
/* (non-Javadoc) * @see org.fastcode.popup.actions.snippet.AbstractCreateNewSnippetAction#getCreateSnippetData(java.lang.String) */ @Override protected CreateSnippetData getCreateSnippetData(final String fileName) throws Exception { final CreateSnippetData createSnippetData = new CreateSnippetData(); createSnippetData.setTemplatePrefix(this.templatePrefix); createSnippetData.setSnippetTypes(getTemplateTypes()); // createSnippetData.setFromTempSettings(this.fromTemplateSetting); createSnippetData.setTemplateVariationField(getTemplateVariationField()); createSnippetData.setClassNames(classNames); // createSnippetData.setEditorFileName(fileName); IJavaProject project; ICompilationUnit compUnit = null; if (this.editorPart != null) { createSnippetData.setEditorpart(this.editorPart); compUnit = getCompilationUnitFromEditor(); if (compUnit == null) { final IFile file = (IFile) this.editorPart.getEditorInput().getAdapter(IFile.class); project = JavaCore.create(file.getProject()); } else { project = compUnit.getJavaProject(); } createSnippetData.setJavaProject(project); final ISelection selection = this.editorPart.getEditorSite().getSelectionProvider().getSelection(); if (selection instanceof ITextSelection) { final String selectedText = ((ITextSelection) selection).getText().trim(); if (!isEmpty(selectedText)) { createSnippetData.setSelectedText(selectedText); } } } final CreateSnippetDialog createSnippetDialog = new CreateSnippetDialog(new Shell(), createSnippetData); getLocalVariables(compUnit, createSnippetData); if (createSnippetDialog.open() == Window.CANCEL) { return null; } final Shell parentShell = MessageUtil.getParentShell(); final Shell shell = parentShell == null ? new Shell() : parentShell; if (createSnippetData.getFromClass() != null && checkForErrors(createSnippetData.getFromClass().getResource()) && MessageDialog.openQuestion( shell, "Error", "There seems to be some problems associated with " + createSnippetData.getFromClass().getElementName() + ". It is better to fix those problems and try again. Want to abort?")) { return null; } if (createSnippetData.getToClass() != null && checkForErrors(createSnippetData.getToClass().getResource()) && MessageDialog.openQuestion( shell, "Error", "There seems to be some problems associated with " + createSnippetData.getToClass().getElementName() + ". It is better to fix those problems and try again. Want to abort?")) { return null; } return createSnippetData; }