Search in sources :

Example 46 with ITextViewer

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

the class AbstractXMLCompletionProposalComputer method computeAttributeProposals.

/**
 * <p>Similar to {@link #computeCompletionProposals(CompletionProposalInvocationContext, IProgressMonitor)} only specificly for
 * attribute proposals</p>
 *
 * <p>Implementers should not override this method, it is made available to implementers so that if they override
 * {@link #computeCompletionProposals(String, ITextRegion, IDOMNode, IDOMNode, CompletionProposalInvocationContext)}
 * they can call this method if needed</p>
 *
 * @param matchString
 * @param completionRegion
 * @param nodeAtOffset
 * @param node
 * @param context
 * @return
 */
protected final ContentAssistRequest computeAttributeProposals(String matchString, ITextRegion completionRegion, IDOMNode nodeAtOffset, IDOMNode node, CompletionProposalInvocationContext context) {
    int documentPosition = context.getInvocationOffset();
    ITextViewer viewer = context.getViewer();
    ContentAssistRequest contentAssistRequest = null;
    IStructuredDocumentRegion sdRegion = getStructuredDocumentRegion(documentPosition);
    // if the attribute name is selected, replace it instead of creating a new attribute
    if (documentPosition <= sdRegion.getStartOffset(completionRegion) && (viewer != null && viewer.getSelectedRange().y != (sdRegion.getEndOffset(completionRegion) - sdRegion.getStartOffset(completionRegion)))) {
        // setup to insert new attributes
        contentAssistRequest = new ContentAssistRequest(nodeAtOffset, node, sdRegion, completionRegion, documentPosition, 0, matchString);
    } else {
        // Setup to replace an existing attribute name
        contentAssistRequest = new ContentAssistRequest(nodeAtOffset, node, sdRegion, completionRegion, sdRegion.getStartOffset(completionRegion), completionRegion.getTextLength(), matchString);
    }
    addAttributeNameProposals(contentAssistRequest, context);
    contentAssistRequest.setReplacementBeginPosition(documentPosition);
    contentAssistRequest.setReplacementLength(0);
    if ((node.getFirstStructuredDocumentRegion() != null) && (!node.getFirstStructuredDocumentRegion().isEnded())) {
        addTagCloseProposals(contentAssistRequest, context);
    }
    return contentAssistRequest;
}
Also used : IStructuredDocumentRegion(org.eclipse.wst.sse.core.internal.provisional.text.IStructuredDocumentRegion) ITextViewer(org.eclipse.jface.text.ITextViewer)

Example 47 with ITextViewer

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

the class XSLTemplatesPage method insertTemplate.

// This code based on the JavaTemplatesPage code. Modified to work with
// SSE
@Override
protected void insertTemplate(Template template, IDocument document) {
    ISourceViewer contextViewer = fTextEditor.getTextViewer();
    ITextSelection textSelection = (ITextSelection) contextViewer.getSelectionProvider().getSelection();
    if (!isValidTemplate(document, template, textSelection.getOffset(), textSelection.getLength()))
        return;
    beginCompoundChange(contextViewer);
    /*
		 * The Editor checks whether a completion for a word exists before it
		 * allows for the template to be applied. We pickup the current text at
		 * the selection position and replace it with the first char of the
		 * template name for this to succeed. Another advantage by this method
		 * is that the template replaces the selected text provided the
		 * selection by itself is not used in the template pattern.
		 */
    String savedText;
    try {
        savedText = document.get(textSelection.getOffset(), textSelection.getLength());
        if (savedText.length() == 0) {
            String prefix = getIdentifierPart(document, template, textSelection.getOffset(), textSelection.getLength());
            if (prefix.length() > 0 && !template.getName().startsWith(prefix)) {
                return;
            }
            if (prefix.length() > 0) {
                contextViewer.setSelectedRange(textSelection.getOffset() - prefix.length(), prefix.length());
                textSelection = (ITextSelection) contextViewer.getSelectionProvider().getSelection();
            }
        }
        document.replace(textSelection.getOffset(), textSelection.getLength(), template.getName().substring(0, 1));
    } catch (BadLocationException e) {
        endCompoundChange(contextViewer);
        return;
    }
    Region region = new Region(textSelection.getOffset() + 1, 0);
    contextViewer.getSelectionProvider().setSelection(new TextSelection(textSelection.getOffset(), 1));
    DocumentTemplateContext context = getContext(document, template, textSelection.getOffset(), textSelection.getLength());
    // $NON-NLS-1$
    context.setVariable("selection", savedText);
    if (context.getKey().length() == 0) {
        try {
            document.replace(textSelection.getOffset(), 1, savedText);
        } catch (BadLocationException e) {
            endCompoundChange(contextViewer);
            return;
        }
    }
    ITextViewer viewer = fTextEditor.getTextViewer();
    int offset = viewer.getTextWidget().getCaretOffset();
    int startLength = offset - region.getOffset();
    // $NON-NLS-1$ //$NON-NLS-2$
    String pattern = template.getPattern().replace("${cursor}", "");
    CustomCompletionProposal proposal = new CustomCompletionProposal(pattern, offset, 0, startLength + pattern.length(), getImage(template), template.getName(), null, null, 0);
    fTextEditor.getSite().getPage().activate(fTextEditor);
    proposal.apply(fTextEditor.getTextViewer(), ' ', 0, offset);
    viewer.getTextWidget().setCaretOffset(offset + pattern.length() - 1);
    endCompoundChange(contextViewer);
}
Also used : DocumentTemplateContext(org.eclipse.jface.text.templates.DocumentTemplateContext) ITextSelection(org.eclipse.jface.text.ITextSelection) TextSelection(org.eclipse.jface.text.TextSelection) CustomCompletionProposal(org.eclipse.wst.xsl.ui.provisional.contentassist.CustomCompletionProposal) Region(org.eclipse.jface.text.Region) ISourceViewer(org.eclipse.jface.text.source.ISourceViewer) ITextSelection(org.eclipse.jface.text.ITextSelection) BadLocationException(org.eclipse.jface.text.BadLocationException) ITextViewer(org.eclipse.jface.text.ITextViewer)

Example 48 with ITextViewer

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

the class StructuredTextEditor method updateMenuText.

private void updateMenuText() {
    ITextViewer viewer = getTextViewer();
    StyledText widget = null;
    if (viewer != null)
        widget = viewer.getTextWidget();
    if (fStructuredModel != null && !fStructuredModel.isModelStateChanging() && viewer != null && widget != null && !widget.isDisposed()) {
        // performance: don't force an update of the action bars unless
        // required as it is expensive
        String previousUndoText = null;
        String previousUndoDesc = null;
        String previousRedoText = null;
        String previousRedoDesc = null;
        boolean updateActions = false;
        IAction undoAction = getAction(ITextEditorActionConstants.UNDO);
        IAction redoAction = getAction(ITextEditorActionConstants.REDO);
        if (undoAction != null) {
            previousUndoText = undoAction.getText();
            previousUndoDesc = undoAction.getDescription();
            updateActions = updateActions || previousUndoText == null || previousUndoDesc == null;
            undoAction.setText(UNDO_ACTION_TEXT_DEFAULT);
            undoAction.setDescription(UNDO_ACTION_DESC_DEFAULT);
        }
        if (redoAction != null) {
            previousRedoText = redoAction.getText();
            previousRedoDesc = redoAction.getDescription();
            updateActions = updateActions || previousRedoText == null || previousRedoDesc == null;
            redoAction.setText(REDO_ACTION_TEXT_DEFAULT);
            redoAction.setDescription(REDO_ACTION_DESC_DEFAULT);
        }
        if (fStructuredModel.getUndoManager() != null) {
            IStructuredTextUndoManager undoManager = fStructuredModel.getUndoManager();
            // get undo command
            Command undoCommand = undoManager.getUndoCommand();
            // set undo label and description
            if (undoAction != null) {
                undoAction.setEnabled(undoManager.undoable());
                if (undoCommand != null) {
                    String label = undoCommand.getLabel();
                    if (label != null) {
                        String customText = MessageFormat.format(UNDO_ACTION_TEXT, new String[] { label });
                        updateActions = updateActions || customText == null || previousUndoText == null || !customText.equals(previousUndoText);
                        undoAction.setText(customText);
                    }
                    String desc = undoCommand.getDescription();
                    if (desc != null) {
                        String customDesc = MessageFormat.format(UNDO_ACTION_DESC, new String[] { desc });
                        updateActions = updateActions || customDesc == null || previousRedoDesc == null || !customDesc.equals(previousUndoDesc);
                        undoAction.setDescription(customDesc);
                    }
                }
            }
            // get redo command
            Command redoCommand = undoManager.getRedoCommand();
            // set redo label and description
            if (redoAction != null) {
                redoAction.setEnabled(undoManager.redoable());
                if (redoCommand != null) {
                    String label = redoCommand.getLabel();
                    if (label != null) {
                        String customText = MessageFormat.format(REDO_ACTION_TEXT, new String[] { label });
                        updateActions = updateActions || customText == null || previousRedoText == null || !customText.equals(previousRedoText);
                        redoAction.setText(customText);
                    }
                    String desc = redoCommand.getDescription();
                    if (desc != null) {
                        String customDesc = MessageFormat.format(REDO_ACTION_DESC, new String[] { desc });
                        updateActions = updateActions || customDesc == null || previousRedoDesc == null || !customDesc.equals(previousRedoDesc);
                        redoAction.setDescription(customDesc);
                    }
                }
            }
        }
        // tell the action bars to update
        if (updateActions) {
            if (getEditorSite().getActionBars() != null) {
                getEditorSite().getActionBars().updateActionBars();
            } else if (getEditorPart() != null && getEditorPart().getEditorSite().getActionBars() != null) {
                getEditorPart().getEditorSite().getActionBars().updateActionBars();
            }
        }
    }
}
Also used : IStructuredTextUndoManager(org.eclipse.wst.sse.core.internal.undo.IStructuredTextUndoManager) StyledText(org.eclipse.swt.custom.StyledText) IAction(org.eclipse.jface.action.IAction) Command(org.eclipse.emf.common.command.Command) ITextViewer(org.eclipse.jface.text.ITextViewer)

Example 49 with ITextViewer

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

the class OpenFileHyperlinkTracker method install.

public void install(IPreferenceStore store) {
    fPreferenceStore = store;
    ITextViewer textViewer = getTextViewer();
    if (textViewer == null)
        return;
    StyledText text = textViewer.getTextWidget();
    if (text == null || text.isDisposed())
        return;
    updateColor(textViewer);
    textViewer.addTextInputListener(this);
    IDocument document = textViewer.getDocument();
    if (document != null)
        document.addDocumentListener(this);
    text.addKeyListener(this);
    text.addMouseListener(this);
    text.addMouseMoveListener(this);
    text.addFocusListener(this);
    text.addPaintListener(this);
    updateKeyModifierMask();
    fPreferenceStore.addPropertyChangeListener(this);
}
Also used : StyledText(org.eclipse.swt.custom.StyledText) IDocument(org.eclipse.jface.text.IDocument) ITextViewer(org.eclipse.jface.text.ITextViewer)

Example 50 with ITextViewer

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

the class OpenFileHyperlinkTracker method repairRepresentation.

private void repairRepresentation(boolean redrawAll) {
    if (fActiveRegion == null)
        return;
    int offset = fActiveRegion.getOffset();
    int length = fActiveRegion.getLength();
    fActiveRegion = null;
    ITextViewer viewer = getTextViewer();
    if (viewer != null) {
        resetCursor(viewer);
        // Remove underline
        if (viewer instanceof ITextViewerExtension5) {
            ITextViewerExtension5 extension = (ITextViewerExtension5) viewer;
            offset = extension.modelOffset2WidgetOffset(offset);
        } else {
            offset -= viewer.getVisibleRegion().getOffset();
        }
        try {
            StyledText text = viewer.getTextWidget();
            // need clearBackground to be true for paint event to be fired
            text.redrawRange(offset, length, true);
        } catch (IllegalArgumentException x) {
            Logger.logException(x);
        }
    }
}
Also used : StyledText(org.eclipse.swt.custom.StyledText) ITextViewerExtension5(org.eclipse.jface.text.ITextViewerExtension5) Point(org.eclipse.swt.graphics.Point) ITextViewer(org.eclipse.jface.text.ITextViewer)

Aggregations

ITextViewer (org.eclipse.jface.text.ITextViewer)75 BadLocationException (org.eclipse.jface.text.BadLocationException)13 IDocument (org.eclipse.jface.text.IDocument)13 Region (org.eclipse.jface.text.Region)13 StyledText (org.eclipse.swt.custom.StyledText)13 IRegion (org.eclipse.jface.text.IRegion)11 ArrayList (java.util.ArrayList)9 Point (org.eclipse.swt.graphics.Point)9 Test (org.junit.Test)9 ITextSelection (org.eclipse.jface.text.ITextSelection)8 IEditorPart (org.eclipse.ui.IEditorPart)8 ITextViewerExtension5 (org.eclipse.jface.text.ITextViewerExtension5)6 IStructuredDocumentRegion (org.eclipse.wst.sse.core.internal.provisional.text.IStructuredDocumentRegion)6 Document (org.eclipse.jface.text.Document)5 IHyperlink (org.eclipse.jface.text.hyperlink.IHyperlink)5 ISourceViewer (org.eclipse.jface.text.source.ISourceViewer)5 Display (org.eclipse.swt.widgets.Display)5 ITextRegion (org.eclipse.wst.sse.core.internal.provisional.text.ITextRegion)5 List (java.util.List)4 ITextViewerExtension (org.eclipse.jface.text.ITextViewerExtension)4