use of org.eclipse.jface.text.ITextSelection in project webtools.sourceediting by eclipse.
the class GoToMatchingTagAction method updateFor.
void updateFor(ISelection selection) {
ITextEditor textEditor = getTextEditor();
if (textEditor == null) {
if (DEBUG) {
// $NON-NLS-1$
System.out.println("no editor");
}
return;
}
IDocumentProvider documentProvider = textEditor.getDocumentProvider();
if (documentProvider == null) {
if (DEBUG) {
// $NON-NLS-1$
System.out.println("no document provider");
}
return;
}
IAnnotationModel annotationModel = documentProvider.getAnnotationModel(textEditor.getEditorInput());
if (annotationModel == null || !(annotationModel instanceof IAnnotationModelExtension)) {
if (DEBUG) {
// $NON-NLS-1$
System.out.println("no annotation model");
}
return;
}
List oldAnnotations = new ArrayList(2);
Iterator annotationIterator = annotationModel.getAnnotationIterator();
while (annotationIterator.hasNext()) {
Annotation annotation = (Annotation) annotationIterator.next();
if (ANNOTATION_TYPE.equals(annotation.getType())) {
annotation.markDeleted(true);
if (DEBUG) {
// $NON-NLS-1$
System.out.println("removing " + annotation);
}
oldAnnotations.add(annotation);
}
}
Map newAnnotations = new HashMap();
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();
}
Position pStart = null;
Position pEnd = null;
// $NON-NLS-1$
String tag = "";
if (o instanceof IDOMNode) {
IDOMNode node = (IDOMNode) o;
IStructuredDocumentRegion startStructuredDocumentRegion = node.getStartStructuredDocumentRegion();
if (startStructuredDocumentRegion != null && startStructuredDocumentRegion.containsOffset(offset)) {
if (startStructuredDocumentRegion.getNumberOfRegions() > 1) {
ITextRegion nameRegion = startStructuredDocumentRegion.getRegions().get(1);
pStart = new Position(startStructuredDocumentRegion.getStartOffset(nameRegion), nameRegion.getTextLength());
tag = startStructuredDocumentRegion.getText(nameRegion);
}
matchRegion = ((IDOMNode) o).getEndStructuredDocumentRegion();
if (matchRegion != null && matchRegion.getNumberOfRegions() > 1) {
ITextRegion nameRegion = matchRegion.getRegions().get(1);
pEnd = new Position(matchRegion.getStartOffset(nameRegion), nameRegion.getTextLength());
}
} else {
IStructuredDocumentRegion endStructuredDocumentRegion = node.getEndStructuredDocumentRegion();
if (endStructuredDocumentRegion != null && endStructuredDocumentRegion.containsOffset(offset)) {
if (endStructuredDocumentRegion.getNumberOfRegions() > 1) {
ITextRegion nameRegion = endStructuredDocumentRegion.getRegions().get(1);
pEnd = new Position(endStructuredDocumentRegion.getStartOffset(nameRegion), nameRegion.getTextLength());
tag = endStructuredDocumentRegion.getText(nameRegion);
}
matchRegion = ((IDOMNode) o).getStartStructuredDocumentRegion();
if (matchRegion != null && matchRegion.getNumberOfRegions() > 1) {
ITextRegion nameRegion = matchRegion.getRegions().get(1);
pStart = new Position(matchRegion.getStartOffset(nameRegion), nameRegion.getTextLength());
}
}
}
}
if (pStart != null && pEnd != null) {
Annotation annotation = new Annotation(ANNOTATION_TYPE, false, NLS.bind(XMLUIMessages.gotoMatchingTag_start, tag));
newAnnotations.put(annotation, pStart);
if (DEBUG) {
// $NON-NLS-1$
System.out.println("adding " + annotation);
}
annotation = new Annotation(ANNOTATION_TYPE, false, NLS.bind(XMLUIMessages.gotoMatchingTag_end, tag));
newAnnotations.put(annotation, pEnd);
if (DEBUG) {
// $NON-NLS-1$
System.out.println("adding " + annotation);
}
}
}
}
((IAnnotationModelExtension) annotationModel).replaceAnnotations((Annotation[]) oldAnnotations.toArray(new Annotation[oldAnnotations.size()]), newAnnotations);
}
use of org.eclipse.jface.text.ITextSelection in project webtools.sourceediting by eclipse.
the class CleanupActionXMLDelegate method run.
public void run(IAction action) {
if (fEditor instanceof ITextEditor) {
final ITextEditor editor = (ITextEditor) fEditor;
Dialog cleanupDialog = new CleanupDialogXML(editor.getSite().getShell());
if (cleanupDialog.open() == Window.OK) {
// setup runnable
Runnable runnable = new Runnable() {
public void run() {
IStructuredCleanupProcessor cleanupProcessor = getCleanupProcessor();
if (cleanupProcessor != null) {
IStructuredModel model = null;
try {
model = StructuredModelManager.getModelManager().getExistingModelForEdit(editor.getDocumentProvider().getDocument(editor.getEditorInput()));
if (model != null) {
cleanupProcessor.cleanupModel(model);
}
} finally {
if (model != null) {
model.releaseFromEdit();
}
}
}
}
};
// TODO: make independent of 'model'.
IStructuredModel model = null;
try {
model = StructuredModelManager.getModelManager().getExistingModelForEdit(editor.getDocumentProvider().getDocument(editor.getEditorInput()));
if (model != null) {
// begin recording
ITextSelection selection = (ITextSelection) editor.getSelectionProvider().getSelection();
model.beginRecording(this, SSEUIMessages.Cleanup_Document_UI_, SSEUIMessages.Cleanup_Document_UI_, selection.getOffset(), selection.getLength());
// tell the model that we are about to make a big
// model change
model.aboutToChangeModel();
// run
BusyIndicator.showWhile(fEditor.getEditorSite().getWorkbenchWindow().getShell().getDisplay(), runnable);
}
} finally {
if (model != null) {
// tell the model that we are done with the big
// model
// change
model.changedModel();
// end recording
ITextSelection selection = (ITextSelection) editor.getSelectionProvider().getSelection();
model.endRecording(this, selection.getOffset(), selection.getLength());
model.releaseFromEdit();
}
}
}
}
}
use of org.eclipse.jface.text.ITextSelection 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);
}
}
}
use of org.eclipse.jface.text.ITextSelection in project pmd-eclipse-plugin by pmd.
the class ReviewCodeHandler method computeSelectedResources.
/**
* Computes the selected resources.
*/
protected final void computeSelectedResources() {
if (fResources != null || fLocation != null) {
return;
}
ISelection selection = getSelection();
if (selection instanceof IStructuredSelection) {
IStructuredSelection structuredSelection = (IStructuredSelection) selection;
List resources = new ArrayList(structuredSelection.size());
Iterator<?> e = structuredSelection.iterator();
while (e.hasNext()) {
Object element = e.next();
if (element instanceof IResource) {
resources.add(element);
} else if (element instanceof IAdaptable) {
IAdaptable adaptable = (IAdaptable) element;
Object adapter = adaptable.getAdapter(IResource.class);
if (adapter instanceof IResource) {
resources.add(adapter);
}
}
}
if (!resources.isEmpty()) {
fResources = (IResource[]) resources.toArray(new IResource[resources.size()]);
}
} else if (selection instanceof ITextSelection) {
IWorkbenchWindow window = getWorkbenchWindow();
if (window != null) {
IWorkbenchPart workbenchPart = window.getPartService().getActivePart();
if (workbenchPart instanceof IEditorPart) {
IEditorPart editorPart = (IEditorPart) workbenchPart;
IEditorInput input = editorPart.getEditorInput();
Object adapter = input.getAdapter(IResource.class);
if (adapter instanceof IResource) {
fResources = new IResource[] { (IResource) adapter };
} else {
adapter = input.getAdapter(ILocationProvider.class);
if (adapter instanceof ILocationProvider) {
ILocationProvider provider = (ILocationProvider) adapter;
fLocation = provider.getPath(input);
}
}
}
}
}
}
use of org.eclipse.jface.text.ITextSelection in project dbeaver by serge-rider.
the class AbstractCommentHandler method execute.
public final Object execute(ExecutionEvent event) throws ExecutionException {
BaseTextEditor textEditor = BaseTextEditor.getTextEditor(HandlerUtil.getActiveEditor(event));
if (textEditor != null) {
ICommentsSupport commentsSupport = textEditor.getCommentsSupport();
IDocument document = textEditor.getDocumentProvider().getDocument(textEditor.getEditorInput());
if (document != null && commentsSupport != null) {
// get current text selection
ISelectionProvider provider = textEditor.getSelectionProvider();
if (provider != null) {
ISelection selection = provider.getSelection();
if (selection instanceof ITextSelection) {
ITextSelection textSelection = (ITextSelection) selection;
if (!textSelection.isEmpty()) {
try {
processAction(textEditor.getSelectionProvider(), commentsSupport, document, textSelection);
} catch (BadLocationException e) {
log.warn(e);
}
}
}
}
}
}
return null;
}
Aggregations