Java tutorial
/******************************************************************************* * Copyright (c) 2013, 2014 ETAS GmbH. * All rights reserved. This program and the accompanying materials * are made available under the terms of the Eclipse Public License v1.0 * which accompanies this distribution, and is available at * http://www.eclipse.org/legal/epl-v10.html * * Contributors: * Dennis Eder (ETAS GmbH) - initial API and implementation and/or initial documentation *******************************************************************************/ package mbtarranger.editors; import java.io.IOException; import mbtarranger.BadBank; import mbtarranger.editors.GenericColorManager; import mbtarranger.editors.EditorSourcelineSelection; import mbtarranger.explorer.ViewContent_TreeNodeV2; import org.antlr.v4.runtime.ParserRuleContext; import org.eclipse.core.resources.IFile; import org.eclipse.core.runtime.IPath; import org.eclipse.jface.text.BadLocationException; import org.eclipse.jface.text.IDocument; import org.eclipse.jface.text.IRegion; import org.eclipse.jface.viewers.ISelection; import org.eclipse.jface.viewers.IStructuredSelection; import org.eclipse.ui.IEditorPart; import org.eclipse.ui.ISelectionListener; import org.eclipse.ui.IWorkbenchPart; import org.eclipse.ui.IWorkbenchPartSite; import org.eclipse.ui.editors.text.TextEditor; import org.eclipse.ui.part.FileEditorInput; import org.eclipse.ui.texteditor.ITextEditor; public class GenericEditor extends TextEditor { private final class SelectionListener implements ISelectionListener { private final GenericEditor me; private SelectionListener(GenericEditor me) { this.me = me; } @Override public void selectionChanged(IWorkbenchPart part, ISelection selection) { if (selection instanceof EditorSourcelineSelection) { EditorSourcelineSelection pthFndSel = (EditorSourcelineSelection) selection; /* check if this editor currently holds the intended document */ IFile file = ((FileEditorInput) me.getEditorInput()).getFile(); IPath path = file.getRawLocation(); try { if ((path != null) && (pthFndSel.file != null) && (path.toFile().getCanonicalPath().equals(pthFndSel.file.getCanonicalPath()) && (pthFndSel.line > -1))) { highlight(me, pthFndSel.line, pthFndSel.line); } } catch (IOException e) { BadBank.reportProblem(pthFndSel.line, pthFndSel.file, e.toString()); e.printStackTrace(); } } if (selection instanceof IStructuredSelection) { /* * -- for * deprecated * Editor -- */ Object first = ((IStructuredSelection) selection).getFirstElement(); if (first instanceof ViewContent_TreeNodeV2) { ViewContent_TreeNodeV2<ParserRuleContext> vc = (ViewContent_TreeNodeV2<ParserRuleContext>) first; /* * check if this editor currently holds the intended * document */ IFile file = ((FileEditorInput) me.getEditorInput()).getFile(); IPath path = file.getRawLocation(); try { if ((path != null) && (vc.source != null) && (path.toFile().getCanonicalPath().equals(vc.source.getCanonicalPath()) && (vc.userInputScope != null) && (vc.userInputScope.getStart() != null))) { highlight(me, vc.userInputScope.getStart().getLine(), vc.userInputScope.getStop().getLine()); } } catch (IOException e) { BadBank.reportProblem(-1, vc.source, e.toString()); e.printStackTrace(); } } if (first instanceof ParserRuleContext) { /* * -- for MarView * Explorer -- */ ParserRuleContext tparr = (ParserRuleContext) first; IFile file = ((FileEditorInput) me.getEditorInput()).getFile(); try { boolean synchronizedDummy = true; // TODO: implement button input /* * check if editor and explorer are currently * synchronized */ if (synchronizedDummy) { IPath path = file.getRawLocation(); if ((tparr != null) && (tparr.getStart() != null)) { highlight(me, tparr.getStart(), tparr.getStop()); } } } catch (RuntimeException e) { BadBank.report(e, file, tparr); } } } } } protected GenericColorManager colorManager; protected GenericEditor() { super(); } private static void highlight(IEditorPart editorPart, int startLine, int stopline) { if (!(editorPart instanceof ITextEditor) || startLine <= 0) { return; } ITextEditor editor = (ITextEditor) editorPart; IDocument document = editor.getDocumentProvider().getDocument(editor.getEditorInput()); if (document != null) { IRegion startlineInfo = null; IRegion stoplineInfo = null; try { // startline count internaly starts with 0, and not with 1 like // in // GUI startlineInfo = document.getLineInformation(startLine - 1); stoplineInfo = document.getLineInformation(stopline - 1); } catch (BadLocationException e) { // ignored because line number may not really exist in document, // we guess this... } if ((startlineInfo != null) && (stoplineInfo != null)) { // editor.selectAndReveal(lineInfo.getOffset(), // lineInfo.getLength()); editor.setHighlightRange(startlineInfo.getOffset(), stoplineInfo.getOffset() - startlineInfo.getOffset() + stoplineInfo.getLength(), true); } } } private static void highlight(IEditorPart editorPart, org.antlr.v4.runtime.Token startCtx, org.antlr.v4.runtime.Token stopCtx) { if (!(editorPart instanceof ITextEditor)) { return; } ITextEditor editor = (ITextEditor) editorPart; IDocument document = editor.getDocumentProvider().getDocument(editor.getEditorInput()); if (document != null) { int start = startCtx.getStartIndex(); int stop = stopCtx.getStopIndex() - startCtx.getStartIndex(); editor.setHighlightRange(start, stop + 1, true); editor.selectAndReveal(start, stop + 1); } } @Override protected void setSite(IWorkbenchPartSite site) { super.setSite(site); final GenericEditor me = this; getEditorSite().getPage().addSelectionListener(new SelectionListener(me)); } public void dispose() { colorManager.dispose(); super.dispose(); } }