@Override public Object execute(ExecutionEvent event) throws ExecutionException { IEditorPart editor = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage().getActiveEditor(); final boolean enableRiskyRefactoring = Platform.getPreferencesService() .getBoolean( ProductConstants.PRODUCT_ID_DESIGNER, PreferenceConstants.ENABLERISKYREFACTORING, false, null); if (!enableRiskyRefactoring) { ErrorReporter.logError("Risky refactoring is not enabled!"); return null; } if (editor == null || !(editor instanceof TTCN3Editor || (editor instanceof TTCNPPEditor && enableRiskyRefactoring))) { ErrorReporter.logError("The editor is not found or not a Titan TTCN-3 editor"); return null; } IFile file = (IFile) editor.getEditorInput().getAdapter(IFile.class); TextFileChange change = null; try { change = OrganizeImports.organizeImportsChange(file); } catch (CoreException e) { ErrorReporter.logExceptionStackTrace("Error while creating the needed import changes", e); return null; } try { change.perform(new NullProgressMonitor()); } catch (CoreException e) { ErrorReporter.logExceptionStackTrace("Error while performing the needed import changes", e); } IProject project = file.getProject(); GlobalParser.getProjectSourceParser(project).reportOutdating(file); GlobalParser.getProjectSourceParser(project).analyzeAll(); return null; }
@Override public void run(final IAction action) { TITANDebugConsole.println("Add import called: "); if (targetEditor == null || !(targetEditor instanceof TTCN3Editor)) { return; } targetEditor.getEditorSite().getActionBars().getStatusLineManager().setErrorMessage(null); IFile file = (IFile) targetEditor.getEditorInput().getAdapter(IFile.class); if (file == null) { targetEditor .getEditorSite() .getActionBars() .getStatusLineManager() .setErrorMessage(FILENOTIDENTIFIABLE); return; } if (!TITANNature.hasTITANNature(file.getProject())) { targetEditor .getEditorSite() .getActionBars() .getStatusLineManager() .setErrorMessage(TITANNature.NO_TITAN_FILE_NATURE_FOUND); return; } IPreferencesService prefs = Platform.getPreferencesService(); boolean reportDebugInformation = prefs.getBoolean( ProductConstants.PRODUCT_ID_DESIGNER, PreferenceConstants.DISPLAYDEBUGINFORMATION, true, null); int offset; if (!selection.isEmpty() && selection instanceof TextSelection && !"".equals(((TextSelection) selection).getText())) { if (reportDebugInformation) { TITANDebugConsole.println("text selected: " + ((TextSelection) selection).getText()); } TextSelection tSelection = (TextSelection) selection; offset = tSelection.getOffset() + tSelection.getLength(); } else { offset = ((TTCN3Editor) targetEditor).getCarretOffset(); } DeclarationCollector declarationCollector = OpenDeclarationHelper.findVisibleDeclarations( targetEditor, new TTCN3ReferenceParser(false), ((TTCN3Editor) targetEditor).getDocument(), offset, false); if (declarationCollector == null) { return; } List<DeclarationCollectionHelper> collected = declarationCollector.getCollected(); if (collected.isEmpty()) { // FIXME add semantic check guard on project level. ProjectSourceParser projectSourceParser = GlobalParser.getProjectSourceParser(file.getProject()); if (reportDebugInformation) { TITANDebugConsole.println("No visible elements found"); } for (String moduleName2 : projectSourceParser.getKnownModuleNames()) { Module module2 = projectSourceParser.getModuleByName(moduleName2); if (module2 != null) { // Visit each file in the project one by // one instead of // "module2.getAssignments().addDeclaration(declarationCollector)". Assignments assignments = module2.getAssignments(); for (int i = 0; i < assignments.getNofAssignments(); i++) { assignments.getAssignmentByIndex(i).addDeclaration(declarationCollector, 0); } } } if (declarationCollector.getCollectionSize() == 0) { targetEditor .getEditorSite() .getActionBars() .getStatusLineManager() .setErrorMessage(NOTTTCN3DECLARATION); return; } if (reportDebugInformation) { TITANDebugConsole.println("Elements were only found in not visible modules"); } DeclarationCollectionHelper resultToInsert = null; if (collected.size() == 1) { resultToInsert = collected.get(0); } else { OpenDeclarationLabelProvider labelProvider = new OpenDeclarationLabelProvider(); ElementListSelectionDialog dialog = new ElementListSelectionDialog(new Shell(Display.getDefault()), labelProvider); dialog.setTitle("Add Import"); dialog.setMessage("Choose element to generate an import statement for."); dialog.setElements(collected.toArray()); if (dialog.open() == Window.OK) { if (reportDebugInformation) { TITANDebugConsole.getConsole() .newMessageStream() .println("Selected: " + dialog.getFirstResult()); } resultToInsert = (DeclarationCollectionHelper) dialog.getFirstResult(); } } if (resultToInsert == null) { return; } IFile newfile = (IFile) resultToInsert.location.getFile(); String moduleName = projectSourceParser.containedModule(newfile); Module newModule = projectSourceParser.getModuleByName(moduleName); if (newModule == null) { targetEditor .getEditorSite() .getActionBars() .getStatusLineManager() .setErrorMessage("Could not identify the module in file " + newfile.getName()); return; } String ttcnName = newModule.getIdentifier().getTtcnName(); TITANDebugConsole.println("the new module to insert: " + ttcnName); final IFile actualFile = (IFile) targetEditor.getEditorInput().getAdapter(IFile.class); String actualModuleName = projectSourceParser.containedModule(actualFile); Module actualModule = projectSourceParser.getModuleByName(actualModuleName); int insertionOffset = ((TTCN3Module) actualModule).getAssignmentsScope().getLocation().getOffset() + 1; MultiTextEdit multiEdit = new MultiTextEdit(insertionOffset, 0); RewriteSessionEditProcessor processor = new RewriteSessionEditProcessor( ((TTCN3Editor) targetEditor).getDocument(), multiEdit, TextEdit.UPDATE_REGIONS | TextEdit.CREATE_UNDO); multiEdit.addChild(new InsertEdit(insertionOffset, "\nimport from " + ttcnName + " all;\n")); try { processor.performEdits(); } catch (BadLocationException e) { ErrorReporter.logExceptionStackTrace(e); } } else { if (reportDebugInformation) { for (DeclarationCollectionHelper foundDeclaration : collected) { TITANDebugConsole.println( "declaration:" + foundDeclaration.location.getFile() + ": " + foundDeclaration.location.getOffset() + " - " + foundDeclaration.location.getEndOffset() + " is available"); } } } Display.getDefault() .asyncExec( new Runnable() { @Override public void run() { MessageDialog.openWarning( new Shell(Display.getDefault()), "Study feature", "Adding a missing importation is still under study"); } }); }