use of org.eclipse.jface.text.ITextSelection in project webtools.sourceediting by eclipse.
the class StructuredSelectPreviousXMLHandler method getNewSelectionRegion2.
/**
* This method was separated out from getNewSelectionRegion2 because the
* code in here is allowed to be called recursively.
*
* @param indexedRegion
* @param textSelection
* @return new region to select or null if none
*/
protected Region getNewSelectionRegion2(IndexedRegion indexedRegion, ITextSelection textSelection) {
Region newRegion = null;
if (indexedRegion instanceof Node) {
Node node = (Node) indexedRegion;
Node newNode = node.getPreviousSibling();
if (newNode == null) {
newNode = node.getParentNode();
if (newNode instanceof IndexedRegion) {
IndexedRegion newIndexedRegion = (IndexedRegion) newNode;
newRegion = new Region(newIndexedRegion.getStartOffset(), newIndexedRegion.getEndOffset() - newIndexedRegion.getStartOffset());
}
} else {
if (newNode instanceof IndexedRegion) {
IndexedRegion newIndexedRegion = (IndexedRegion) newNode;
newRegion = new Region(newIndexedRegion.getStartOffset(), textSelection.getOffset() + textSelection.getLength() - newIndexedRegion.getStartOffset());
if (newNode.getNodeType() == Node.TEXT_NODE) {
newRegion = getNewSelectionRegion2(newIndexedRegion, new TextSelection(newRegion.getOffset(), newRegion.getLength()));
}
}
}
}
return newRegion;
}
use of org.eclipse.jface.text.ITextSelection in project webtools.sourceediting by eclipse.
the class StructuredSelectNextXMLActionDelegate method getNewSelectionRegion2.
/**
* This method was separated out from getNewSelectionRegion2 because the
* code in here is allowed to be called recursively.
*
* @param indexedRegion
* @param textSelection
* @return new region to select or null if none
*/
private Region getNewSelectionRegion2(IndexedRegion indexedRegion, ITextSelection textSelection) {
Region newRegion = null;
if (indexedRegion instanceof Node) {
Node node = (Node) indexedRegion;
Node newNode = node.getNextSibling();
if (newNode == null) {
newNode = node.getParentNode();
if (newNode instanceof IndexedRegion) {
IndexedRegion newIndexedRegion = (IndexedRegion) newNode;
newRegion = new Region(newIndexedRegion.getStartOffset(), newIndexedRegion.getEndOffset() - newIndexedRegion.getStartOffset());
}
} else {
if (newNode instanceof IndexedRegion) {
IndexedRegion newIndexedRegion = (IndexedRegion) newNode;
newRegion = new Region(textSelection.getOffset(), newIndexedRegion.getEndOffset() - textSelection.getOffset());
if (newNode.getNodeType() == Node.TEXT_NODE) {
newRegion = getNewSelectionRegion2(newIndexedRegion, new TextSelection(newRegion.getOffset(), newRegion.getLength()));
}
}
}
}
return newRegion;
}
use of org.eclipse.jface.text.ITextSelection in project webtools.sourceediting by eclipse.
the class XMLMultiPageEditorPart method connectDesignPage.
/**
* Connects the design viewer with the viewer selection manager. Should be
* done after createSourcePage() is done because we need to get the
* ViewerSelectionManager from the TextEditor. setModel is also done here
* because getModel() needs to reference the TextEditor.
*/
private void connectDesignPage() {
if (fDesignViewer != null) {
fDesignViewer.setDocument(getDocument());
}
/*
* Connect selection from the Design page to the selection provider
* for the XMLMultiPageEditorPart so that selection changes in the
* Design page will propagate across the workbench
*/
if (fDesignViewer.getSelectionProvider() instanceof IPostSelectionProvider) {
((IPostSelectionProvider) fDesignViewer.getSelectionProvider()).addPostSelectionChangedListener(new ISelectionChangedListener() {
public void selectionChanged(SelectionChangedEvent event) {
if (getActivePage() != fSourcePageIndex) {
((MultiPageSelectionProvider) getSite().getSelectionProvider()).firePostSelectionChanged(event);
}
}
});
}
fDesignViewer.getSelectionProvider().addSelectionChangedListener(new ISelectionChangedListener() {
public void selectionChanged(SelectionChangedEvent event) {
if (getActivePage() != fSourcePageIndex) {
((MultiPageSelectionProvider) getSite().getSelectionProvider()).fireSelectionChanged(event);
}
}
});
fDesignViewer.getSelectionProvider().addSelectionChangedListener(new ISelectionChangedListener() {
public void selectionChanged(SelectionChangedEvent event) {
if (getActivePage() != fSourcePageIndex) {
try {
updateStatusLine(event.getSelection());
} catch (Exception exception) {
Logger.logException(exception);
}
}
}
});
/*
* Handle double-click in the Design page by selecting the
* corresponding amount of text in the Source page.
*
* Warning: This implies more knowledge of the design viewer's underlying
* Control than expressed in the IDesignViewer interface
*/
fDesignViewer.getControl().addListener(SWT.MouseDoubleClick, new Listener() {
public void handleEvent(Event event) {
ISelection selection = fDesignViewer.getSelectionProvider().getSelection();
int start = -1;
int length = -1;
if (selection instanceof IStructuredSelection) {
/*
* selection goes from the start of the first object to
* the end of the last
*/
IStructuredSelection structuredSelection = (IStructuredSelection) selection;
Object o = structuredSelection.getFirstElement();
Object o2 = null;
if (structuredSelection.size() > 1) {
o2 = structuredSelection.toArray()[structuredSelection.size() - 1];
} else {
o2 = o;
}
if (o instanceof IndexedRegion) {
start = ((IndexedRegion) o).getStartOffset();
length = ((IndexedRegion) o2).getEndOffset() - start;
} else if (o2 instanceof ITextRegion) {
start = ((ITextRegion) o).getStart();
length = ((ITextRegion) o2).getEnd() - start;
}
} else if (selection instanceof ITextSelection) {
start = ((ITextSelection) selection).getOffset();
length = ((ITextSelection) selection).getLength();
}
if ((start > -1) && (length > -1)) {
getTextEditor().selectAndReveal(start, length);
}
}
});
/*
* Connect selection from the Source page to the selection provider of
* the Design page so that selection in the Source page will drive
* selection in the Design page. Prefer post selection.
*/
ISelectionProvider provider = getTextEditor().getSelectionProvider();
if (fTextEditorSelectionListener == null) {
fTextEditorSelectionListener = new TextEditorPostSelectionAdapter();
}
if (provider instanceof IPostSelectionProvider) {
fTextEditorSelectionListener.forcePostSelection = false;
((IPostSelectionProvider) provider).addPostSelectionChangedListener(fTextEditorSelectionListener);
} else {
fTextEditorSelectionListener.forcePostSelection = true;
provider.addSelectionChangedListener(fTextEditorSelectionListener);
}
}
use of org.eclipse.jface.text.ITextSelection in project webtools.sourceediting by eclipse.
the class XPathTemplateCompletionProcessor method computeCompletionProposals.
/*
* Copied from super class except instead of calling createContext(viewer,
* region) call createContext(viewer, region, offset) instead
*/
public ICompletionProposal[] computeCompletionProposals(ITextViewer viewer, int offset) {
ITextSelection selection = (ITextSelection) viewer.getSelectionProvider().getSelection();
// adjust offset to end of normalized selection
if (selection.getOffset() == offset) {
offset = selection.getOffset() + selection.getLength();
}
String prefix = extractPrefix(viewer, offset);
Region region = new Region(offset - prefix.length(), prefix.length());
TemplateContext context = createContext(viewer, region, offset);
if (context == null) {
return new ICompletionProposal[0];
}
// name of the selection variables {line, word}_selection
// $NON-NLS-1$
context.setVariable("selection", selection.getText());
Template[] templates = getTemplates(context.getContextType().getId());
List matches = new ArrayList();
for (int i = 0; i < templates.length; i++) {
Template template = templates[i];
try {
context.getContextType().validate(template.getPattern());
} catch (TemplateException e) {
continue;
}
if (template.matches(prefix, context.getContextType().getId())) {
matches.add(createProposal(template, context, (IRegion) region, getRelevance(template, prefix)));
}
}
Collections.sort(matches, fgProposalComparator);
return (ICompletionProposal[]) matches.toArray(new ICompletionProposal[matches.size()]);
}
use of org.eclipse.jface.text.ITextSelection 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));
}
}
}
}
Aggregations