Search in sources :

Example 91 with TextSelection

use of org.eclipse.jface.text.TextSelection in project titan.EclipsePlug-ins by eclipse.

the class AddImport method run.

@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(null, 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();
        Module newModule = projectSourceParser.containedModule(newfile);
        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);
        Module actualModule = projectSourceParser.containedModule(actualFile);
        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(null, "Study feature", "Adding a missing importation is still under study");
        }
    });
}
Also used : TTCN3Editor(org.eclipse.titan.designer.editors.ttcn3editor.TTCN3Editor) InsertEdit(org.eclipse.text.edits.InsertEdit) IFile(org.eclipse.core.resources.IFile) DeclarationCollector(org.eclipse.titan.designer.editors.actions.DeclarationCollector) TTCN3ReferenceParser(org.eclipse.titan.designer.editors.ttcn3editor.TTCN3ReferenceParser) TextSelection(org.eclipse.jface.text.TextSelection) Assignments(org.eclipse.titan.designer.AST.Assignments) OpenDeclarationLabelProvider(org.eclipse.titan.designer.editors.actions.OpenDeclarationLabelProvider) IPreferencesService(org.eclipse.core.runtime.preferences.IPreferencesService) ProjectSourceParser(org.eclipse.titan.designer.parsers.ProjectSourceParser) DeclarationCollectionHelper(org.eclipse.titan.designer.editors.actions.DeclarationCollectionHelper) RewriteSessionEditProcessor(org.eclipse.jface.text.RewriteSessionEditProcessor) ElementListSelectionDialog(org.eclipse.ui.dialogs.ElementListSelectionDialog) Module(org.eclipse.titan.designer.AST.Module) TTCN3Module(org.eclipse.titan.designer.AST.TTCN3.definitions.TTCN3Module) MultiTextEdit(org.eclipse.text.edits.MultiTextEdit) BadLocationException(org.eclipse.jface.text.BadLocationException)

Example 92 with TextSelection

use of org.eclipse.jface.text.TextSelection in project titan.EclipsePlug-ins by eclipse.

the class GotoMatchingBracketAction method execute.

@Override
public Object execute(final ExecutionEvent event) throws ExecutionException {
    IEditorPart targetEditor = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage().getActiveEditor();
    if (targetEditor instanceof TTCN3Editor) {
        this.targetEditor = (TTCN3Editor) targetEditor;
    } else {
        this.targetEditor = null;
    }
    if (targetEditor == null) {
        return null;
    }
    if (!selection.isEmpty()) {
        if (selection instanceof TextSelection) {
            TextSelection tSelection = (TextSelection) selection;
            if (tSelection.getLength() != 0) {
                return null;
            }
        }
    }
    IDocument document = this.targetEditor.getDocument();
    int carretOffset = this.targetEditor.getCarretOffset();
    PairMatcher pairMatcher = new PairMatcher();
    IRegion region = pairMatcher.match(document, carretOffset);
    if (region == null) {
        return null;
    }
    int targetOffset;
    if (region.getOffset() + 1 == carretOffset) {
        targetOffset = region.getOffset() + region.getLength();
    } else {
        targetOffset = region.getOffset() + 1;
    }
    this.targetEditor.setCarretOffset(targetOffset);
    this.targetEditor.selectAndReveal(targetOffset, 0);
    return null;
}
Also used : TTCN3Editor(org.eclipse.titan.designer.editors.ttcn3editor.TTCN3Editor) PairMatcher(org.eclipse.titan.designer.editors.ttcn3editor.PairMatcher) TextSelection(org.eclipse.jface.text.TextSelection) IEditorPart(org.eclipse.ui.IEditorPart) IDocument(org.eclipse.jface.text.IDocument) IRegion(org.eclipse.jface.text.IRegion)

Example 93 with TextSelection

use of org.eclipse.jface.text.TextSelection in project titan.EclipsePlug-ins by eclipse.

the class GotoMatchingBracketAction method run.

@Override
public final void run(final IAction action) {
    if (targetEditor == null) {
        return;
    }
    if (!selection.isEmpty()) {
        if (selection instanceof TextSelection) {
            TextSelection tSelection = (TextSelection) selection;
            if (tSelection.getLength() != 0) {
                return;
            }
        }
    }
    IDocument document = targetEditor.getDocument();
    int carretOffset = targetEditor.getCarretOffset();
    PairMatcher pairMatcher = new PairMatcher();
    IRegion region = pairMatcher.match(document, carretOffset);
    if (region == null) {
        return;
    }
    int targetOffset;
    if (region.getOffset() + 1 == carretOffset) {
        targetOffset = region.getOffset() + region.getLength();
    } else {
        targetOffset = region.getOffset() + 1;
    }
    targetEditor.setCarretOffset(targetOffset);
    targetEditor.selectAndReveal(targetOffset, 0);
}
Also used : PairMatcher(org.eclipse.titan.designer.editors.ttcn3editor.PairMatcher) TextSelection(org.eclipse.jface.text.TextSelection) IDocument(org.eclipse.jface.text.IDocument) IRegion(org.eclipse.jface.text.IRegion)

Example 94 with TextSelection

use of org.eclipse.jface.text.TextSelection in project titan.EclipsePlug-ins by eclipse.

the class FindDefinitionAction method doFindDefinition.

private void doFindDefinition() {
    final DefinitionListSelectionDialog dialog = new DefinitionListSelectionDialog(Display.getDefault().getActiveShell(), new DefinitionLabelProvider(), getCurrentProject());
    dialog.setTitle("Find Definition");
    dialog.setMessage("Type the name of a definition");
    dialog.setHelpAvailable(false);
    if (targetEditor instanceof TTCN3Editor) {
        selection = ((TTCN3Editor) targetEditor).getSelectionProvider().getSelection();
    }
    if (selection instanceof TextSelection && !selection.isEmpty()) {
        final IPreferencesService prefs = Platform.getPreferencesService();
        final boolean reportDebugInformation = prefs.getBoolean(ProductConstants.PRODUCT_ID_DESIGNER, PreferenceConstants.DISPLAYDEBUGINFORMATION, true, null);
        if (reportDebugInformation) {
            TITANDebugConsole.println("text selected: " + ((TextSelection) selection).getText());
        }
        final TextSelection tSelection = (TextSelection) selection;
        dialog.setFilter(tSelection.getText());
    }
    dialog.init();
    if (dialog.open() == Window.CANCEL || dialog.getFirstResult() == null) {
        return;
    }
    final Object result = dialog.getFirstResult();
    if (!(result instanceof ILocateableNode)) {
        return;
    }
    showInEditor((ILocateableNode) result);
}
Also used : TTCN3Editor(org.eclipse.titan.designer.editors.ttcn3editor.TTCN3Editor) DefinitionListSelectionDialog(org.eclipse.titan.designer.finddefinition.DefinitionListSelectionDialog) TextSelection(org.eclipse.jface.text.TextSelection) ILocateableNode(org.eclipse.titan.designer.AST.ILocateableNode) IPreferencesService(org.eclipse.core.runtime.preferences.IPreferencesService)

Example 95 with TextSelection

use of org.eclipse.jface.text.TextSelection in project titan.EclipsePlug-ins by eclipse.

the class SelectionFinder method findSelection.

private Definition findSelection() {
    // getting the active editor
    final IEditorPart editor = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage().getActiveEditor();
    if (editor == null || !(editor instanceof TTCN3Editor)) {
        return null;
    }
    final TTCN3Editor targetEditor = (TTCN3Editor) editor;
    // iterating through part of the module
    final IResource selectedRes = extractResource(targetEditor);
    if (!(selectedRes instanceof IFile)) {
        ErrorReporter.logError("SelectionFinder.findSelection(): Selected resource `" + selectedRes.getName() + "' is not a file.");
        return null;
    }
    final IFile selectedFile = (IFile) selectedRes;
    sourceProj = selectedFile.getProject();
    final ProjectSourceParser projectSourceParser = GlobalParser.getProjectSourceParser(sourceProj);
    final Module selectedModule = projectSourceParser.containedModule(selectedFile);
    // getting current selection
    final ISelectionService selectionService = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getSelectionService();
    final TextSelection textSelection = extractSelection(selectionService.getSelection());
    // getting current selection nodes
    final int selectionOffset = textSelection.getOffset() + textSelection.getLength();
    final SelectionFinderVisitor selVisitor = new SelectionFinderVisitor(selectionOffset);
    selectedModule.accept(selVisitor);
    final Definition selectedDef = selVisitor.getSelection();
    if (selectedDef == null) {
        ErrorReporter.logWarning("SelectionFinder.findSelection(): Visitor did not find a definition in the selection.");
        final IStatusLineManager statusLineManager = targetEditor.getEditorSite().getActionBars().getStatusLineManager();
        statusLineManager.setErrorMessage(ERR_MSG_NO_SELECTION);
        return null;
    }
    return selectedDef;
}
Also used : TTCN3Editor(org.eclipse.titan.designer.editors.ttcn3editor.TTCN3Editor) IFile(org.eclipse.core.resources.IFile) IStatusLineManager(org.eclipse.jface.action.IStatusLineManager) TextSelection(org.eclipse.jface.text.TextSelection) Definition(org.eclipse.titan.designer.AST.TTCN3.definitions.Definition) ISelectionService(org.eclipse.ui.ISelectionService) IEditorPart(org.eclipse.ui.IEditorPart) Module(org.eclipse.titan.designer.AST.Module) IResource(org.eclipse.core.resources.IResource) ProjectSourceParser(org.eclipse.titan.designer.parsers.ProjectSourceParser)

Aggregations

TextSelection (org.eclipse.jface.text.TextSelection)105 IFile (org.eclipse.core.resources.IFile)35 ITextSelection (org.eclipse.jface.text.ITextSelection)24 ISelection (org.eclipse.jface.viewers.ISelection)24 Test (org.junit.Test)23 IDocument (org.eclipse.jface.text.IDocument)21 IEditorPart (org.eclipse.ui.IEditorPart)19 InputStream (java.io.InputStream)14 ByteArrayInputStream (java.io.ByteArrayInputStream)13 Path (org.eclipse.core.runtime.Path)13 ITextEditor (org.eclipse.ui.texteditor.ITextEditor)11 BadLocationException (org.eclipse.jface.text.BadLocationException)10 IRegion (org.eclipse.jface.text.IRegion)10 XtextResource (org.eclipse.xtext.resource.XtextResource)9 IPreferencesService (org.eclipse.core.runtime.preferences.IPreferencesService)8 Module (org.eclipse.titan.designer.AST.Module)8 ProjectSourceParser (org.eclipse.titan.designer.parsers.ProjectSourceParser)8 XExpression (org.eclipse.xtext.xbase.XExpression)8 TTCN3Editor (org.eclipse.titan.designer.editors.ttcn3editor.TTCN3Editor)7 XtextEditor (org.eclipse.xtext.ui.editor.XtextEditor)7