use of org.eclipse.ui.texteditor.ITextEditor 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.ui.texteditor.ITextEditor 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.ui.texteditor.ITextEditor 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.ui.texteditor.ITextEditor in project pmd-eclipse-plugin by pmd.
the class CPDViewTooltipListener2 method highlight.
// open file and jump to the startline
private void highlight(Match match, Mark entry) {
IPath path = Path.fromOSString(entry.getFilename());
IFile file = ResourcesPlugin.getWorkspace().getRoot().getFileForLocation(path);
if (file == null) {
return;
}
try {
// open editor
IWorkbenchPage page = view.getSite().getPage();
IEditorPart part = IDE.openEditor(page, file);
if (part instanceof ITextEditor) {
// select text
ITextEditor textEditor = (ITextEditor) part;
IDocument document = textEditor.getDocumentProvider().getDocument(textEditor.getEditorInput());
int offset = document.getLineOffset(entry.getBeginLine() - 1);
int length = document.getLineOffset(entry.getBeginLine() - 1 + match.getLineCount()) - offset - 1;
textEditor.selectAndReveal(offset, length);
}
} catch (PartInitException pie) {
PMDPlugin.getDefault().logError(getString(StringKeys.ERROR_VIEW_EXCEPTION), pie);
} catch (BadLocationException ble) {
PMDPlugin.getDefault().logError(getString(StringKeys.ERROR_VIEW_EXCEPTION), ble);
}
}
use of org.eclipse.ui.texteditor.ITextEditor in project pmd-eclipse-plugin by pmd.
the class MarkerContributionFactory method createContributionItems.
@Override
public void createContributionItems(IServiceLocator serviceLocator, IContributionRoot additions) {
IWorkbenchPage page = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage();
ITextEditor editor = (ITextEditor) page.getActivePart();
additions.addContributionItem(new MarkerMenuFiller(editor), null);
}
Aggregations