Search in sources :

Example 46 with TextSelection

use of org.eclipse.jface.text.TextSelection in project webtools.sourceediting by eclipse.

the class GoToMatchingTagAction method runWithEvent.

/*
	 * (non-Javadoc)
	 * 
	 * @see org.eclipse.jface.action.Action#runWithEvent(org.eclipse.swt.widgets.Event)
	 */
public void runWithEvent(Event event) {
    super.runWithEvent(event);
    if (getTextEditor() == null)
        return;
    ISelection selection = getTextEditor().getSelectionProvider().getSelection();
    if (!selection.isEmpty() && selection instanceof IStructuredSelection && selection instanceof ITextSelection) {
        Object o = ((IStructuredSelection) selection).getFirstElement();
        if (o instanceof IDOMNode) {
            int offset = ((ITextSelection) selection).getOffset();
            IStructuredDocumentRegion matchRegion = null;
            if (((Node) o).getNodeType() == Node.ATTRIBUTE_NODE) {
                o = ((Attr) o).getOwnerElement();
            }
            int targetOffset = -1;
            if (o instanceof IDOMNode) {
                IDOMNode node = (IDOMNode) o;
                IStructuredDocumentRegion startStructuredDocumentRegion = node.getStartStructuredDocumentRegion();
                if (startStructuredDocumentRegion != null && startStructuredDocumentRegion.containsOffset(offset)) {
                    matchRegion = ((IDOMNode) o).getEndStructuredDocumentRegion();
                    if (matchRegion != null)
                        targetOffset = matchRegion.getStartOffset() + 2;
                } else {
                    IStructuredDocumentRegion endStructuredDocumentRegion = node.getEndStructuredDocumentRegion();
                    if (endStructuredDocumentRegion != null && endStructuredDocumentRegion.containsOffset(offset)) {
                        matchRegion = ((IDOMNode) o).getStartStructuredDocumentRegion();
                        if (matchRegion != null)
                            targetOffset = matchRegion.getStartOffset() + 1;
                    }
                }
            }
            if (targetOffset >= 0) {
                getTextEditor().getSelectionProvider().setSelection(new TextSelection(targetOffset, 0));
            }
        }
    }
}
Also used : IStructuredDocumentRegion(org.eclipse.wst.sse.core.internal.provisional.text.IStructuredDocumentRegion) IDOMNode(org.eclipse.wst.xml.core.internal.provisional.document.IDOMNode) ITextSelection(org.eclipse.jface.text.ITextSelection) TextSelection(org.eclipse.jface.text.TextSelection) ISelection(org.eclipse.jface.viewers.ISelection) IStructuredSelection(org.eclipse.jface.viewers.IStructuredSelection) ITextSelection(org.eclipse.jface.text.ITextSelection)

Example 47 with TextSelection

use of org.eclipse.jface.text.TextSelection in project webtools.sourceediting by eclipse.

the class ToggleCommentActionXMLDelegate method updateCurrentSelection.

private void updateCurrentSelection(Position selectionPosition, IDocument document, boolean updateStartOffset) {
    if (fEditor instanceof ITextEditor) {
        // update the selection if text selection changed
        if (selectionPosition != null) {
            ITextSelection selection = null;
            if (updateStartOffset) {
                selection = new TextSelection(document, selectionPosition.getOffset() - OPEN_COMMENT.length(), selectionPosition.getLength() + OPEN_COMMENT.length());
            } else {
                selection = new TextSelection(document, selectionPosition.getOffset(), selectionPosition.getLength());
            }
            ISelectionProvider provider = ((ITextEditor) fEditor).getSelectionProvider();
            if (provider != null) {
                provider.setSelection(selection);
            }
            document.removePosition(selectionPosition);
        }
    }
}
Also used : ITextEditor(org.eclipse.ui.texteditor.ITextEditor) ISelectionProvider(org.eclipse.jface.viewers.ISelectionProvider) ITextSelection(org.eclipse.jface.text.ITextSelection) TextSelection(org.eclipse.jface.text.TextSelection) ITextSelection(org.eclipse.jface.text.ITextSelection)

Example 48 with TextSelection

use of org.eclipse.jface.text.TextSelection in project dbeaver by serge-rider.

the class PrefPageSQLFormat method formatSQL.

private void formatSQL() {
    try {
        try (final InputStream sqlStream = getClass().getResourceAsStream(FORMAT_FILE_NAME)) {
            final String sqlText = ContentUtils.readToString(sqlStream, StandardCharsets.UTF_8);
            sqlViewer.setInput(new StringEditorInput("SQL preview", sqlText, true, GeneralUtils.getDefaultFileEncoding()));
            sqlViewer.getTextViewer().setSelection(new TextSelection(0, sqlText.length()));
            sqlViewer.getTextViewer().doOperation(ISourceViewer.FORMAT);
            sqlViewer.getTextViewer().setSelection(new TextSelection(0, 0));
            sqlViewer.reloadSyntaxRules();
        }
    } catch (Exception e) {
        log.error(e);
    }
}
Also used : InputStream(java.io.InputStream) TextSelection(org.eclipse.jface.text.TextSelection) StringEditorInput(org.jkiss.dbeaver.ui.editors.StringEditorInput) DBException(org.jkiss.dbeaver.DBException)

Example 49 with TextSelection

use of org.eclipse.jface.text.TextSelection in project dbeaver by serge-rider.

the class OpenLinkInWindowHandler method execute.

@Override
public Object execute(ExecutionEvent event) throws ExecutionException {
    SQLEditor editor = RuntimeUtils.getObjectAdapter(HandlerUtil.getActiveEditor(event), SQLEditor.class);
    if (editor == null) {
        DBWorkbench.getPlatformUI().showError(TITLE, "No suitable editor was found for SQL");
        return null;
    }
    ISelection selection = editor.getSelectionProvider().getSelection();
    if (isSelectedTextNullOrEmpty(selection)) {
        DBWorkbench.getPlatformUI().showError(TITLE, "No text was selected");
        return null;
    }
    TextSelection textSelection = (TextSelection) selection;
    String googleLink = SEARCH_WEB_ADDRESS_PREFIX + textSelection.getText().replaceAll(" ", "%20").trim();
    if (Desktop.isDesktopSupported() && Desktop.getDesktop().isSupported(Desktop.Action.BROWSE)) {
        UIUtils.launchProgram(googleLink);
    } else {
        DBWorkbench.getPlatformUI().showError(TITLE, "Desktop is not supported.");
    }
    return null;
}
Also used : SQLEditor(org.jkiss.dbeaver.ui.editors.sql.SQLEditor) TextSelection(org.eclipse.jface.text.TextSelection) ISelection(org.eclipse.jface.viewers.ISelection)

Example 50 with TextSelection

use of org.eclipse.jface.text.TextSelection in project dbeaver by serge-rider.

the class GenerateUUIDHandler method execute.

@Override
public Object execute(ExecutionEvent event) throws ExecutionException {
    IWorkbenchPart activePart = HandlerUtil.getActivePart(event);
    if (activePart == null) {
        return null;
    }
    IResultSetController rsc = activePart.getAdapter(IResultSetController.class);
    if (rsc != null && UIUtils.hasFocus(rsc.getControl())) {
        IResultSetSelection selection = rsc.getSelection();
        if (selection != null && !selection.isEmpty()) {
            for (Object cell : selection.toArray()) {
                DBDAttributeBinding attr = selection.getElementAttribute(cell);
                ResultSetRow row = selection.getElementRow(cell);
                if (row != null && attr != null) {
                    ResultSetValueController valueController = new ResultSetValueController(rsc, attr, row, IValueController.EditType.NONE, null);
                    DBDValueHandler valueHandler = valueController.getValueHandler();
                    String uuid = generateUUID();
                    valueController.updateValue(uuid, false);
                }
            }
            rsc.redrawData(false, false);
            rsc.updateEditControls();
        }
    } else {
        ITextViewer textViewer = activePart.getAdapter(ITextViewer.class);
        if (textViewer != null) {
            ISelection selection = textViewer.getSelectionProvider().getSelection();
            if (selection instanceof TextSelection) {
                try {
                    int offset = ((TextSelection) selection).getOffset();
                    int length = ((TextSelection) selection).getLength();
                    String uuid = generateUUID();
                    textViewer.getDocument().replace(offset, length, uuid);
                    textViewer.getSelectionProvider().setSelection(new TextSelection(offset + uuid.length(), 0));
                } catch (BadLocationException e) {
                    DBWorkbench.getPlatformUI().showError("Insert UUID", "Error inserting UUID in text editor", e);
                }
            }
        }
    }
    return null;
}
Also used : TextSelection(org.eclipse.jface.text.TextSelection) IResultSetSelection(org.jkiss.dbeaver.ui.controls.resultset.IResultSetSelection) DBDValueHandler(org.jkiss.dbeaver.model.data.DBDValueHandler) ResultSetRow(org.jkiss.dbeaver.ui.controls.resultset.ResultSetRow) IResultSetController(org.jkiss.dbeaver.ui.controls.resultset.IResultSetController) DBDAttributeBinding(org.jkiss.dbeaver.model.data.DBDAttributeBinding) ITextViewer(org.eclipse.jface.text.ITextViewer) IWorkbenchPart(org.eclipse.ui.IWorkbenchPart) ISelection(org.eclipse.jface.viewers.ISelection) ResultSetValueController(org.jkiss.dbeaver.ui.controls.resultset.ResultSetValueController) BadLocationException(org.eclipse.jface.text.BadLocationException)

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