1 package freemarker.eclipse.editors; 2 3 import org.eclipse.core.resources.IFile; 4 import org.eclipse.core.resources.IMarker; 5 import org.eclipse.jface.action.IStatusLineManager; 6 import org.eclipse.jface.text.BadLocationException; 7 import org.eclipse.jface.text.IDocument; 8 import org.eclipse.jface.text.Position; 9 import org.eclipse.jface.text.source.ISourceViewer; 10 import org.eclipse.ui.IEditorActionBarContributor; 11 import org.eclipse.ui.IFileEditorInput; 12 import org.eclipse.ui.editors.text.TextEditor; 13 import org.eclipse.ui.part.EditorActionBarContributor; 14 import org.eclipse.ui.texteditor.MarkerAnnotation; 15 import org.eclipse.ui.views.contentoutline.IContentOutlinePage; 16 17 import freemarker.eclipse.outline.OutlinePage; 18 19 23 public class FreemarkerEditor extends TextEditor { 24 25 private ITokenManager tokenManager; 26 private OutlinePage fOutlinePage; 27 private ReconcilingStrategy fReconcilingStrategy; 28 private int fLastCursorLine = -1; 29 30 31 public FreemarkerEditor() { 32 super(); 33 tokenManager = new TokenManager(); 34 fReconcilingStrategy = new ReconcilingStrategy(this); 35 setSourceViewerConfiguration(new Configuration(this, tokenManager)); 36 setDocumentProvider(new DocumentProvider()); 37 } 38 public void dispose() { 39 tokenManager.dispose(); 40 super.dispose(); 41 } 42 43 public Object getAdapter(Class aClass) { 44 Object adapter; 45 if (aClass.equals(IContentOutlinePage.class)) { 46 if (fOutlinePage == null) { 47 fOutlinePage = new OutlinePage(this); 48 if (getEditorInput() != null) { 49 fOutlinePage.setInput(getEditorInput()); 50 } 51 } 52 adapter = fOutlinePage; 53 } else { 54 adapter = super.getAdapter(aClass); 55 } 56 return adapter; 57 } 58 59 protected void handleCursorPositionChanged() { 60 super.handleCursorPositionChanged(); 61 int line = getCursorLine(); 62 if (line > 0 && line != fLastCursorLine) { 63 fLastCursorLine = line; 64 if (fOutlinePage != null) { 65 66 } 67 } 68 } 69 70 protected void handleEditorInputChanged() { 71 72 } 73 74 public IDocument getDocument() { 75 ISourceViewer viewer = getSourceViewer(); 76 if (viewer != null) { 77 return viewer.getDocument(); 78 } 79 return null; 80 } 81 82 public int getCursorLine() { 83 int line = -1; 84 85 ISourceViewer sourceViewer = getSourceViewer(); 86 if (sourceViewer != null) { 87 int caret = sourceViewer.getVisibleRegion().getOffset() + 88 sourceViewer.getTextWidget().getCaretOffset(); 89 IDocument document = sourceViewer.getDocument(); 90 if (document != null) { 91 try { 92 line = document.getLineOfOffset(caret) + 1; 93 } catch (BadLocationException e) { 94 e.printStackTrace(); 95 } 96 } 97 } 98 return line; 99 } 100 101 public void selectTextRange(int beginLine, int beginColumn, int length) { 102 int bOffset = 0; 103 int eOffset = 0; 104 try { 105 bOffset = getDocument().getLineInformation(beginLine-1).getOffset(); 106 107 setHighlightRange(bOffset+beginColumn-1, length , true); 108 109 } catch (Exception e) { 110 111 } 112 } 113 114 public void updateOutlinePage() { 115 if (fOutlinePage != null) { 116 fOutlinePage.update(); 117 } 118 } 119 120 public ReconcilingStrategy getReconcilingStrategy() { 121 return fReconcilingStrategy; 122 } 123 124 125 public void displayErrorMessage(String aMessage) { 126 IEditorActionBarContributor contributor = 127 getEditorSite().getActionBarContributor(); 128 if (contributor != null && 129 contributor instanceof EditorActionBarContributor) { 130 IStatusLineManager manager = ((EditorActionBarContributor) 131 contributor).getActionBars().getStatusLineManager(); 132 manager.setErrorMessage(aMessage); 133 134 } 135 } 136 137 public void addProblemMarker(String aMessage, int aLine) { 138 IFile file = ((IFileEditorInput)getEditorInput()).getFile(); 139 try { 140 IMarker marker = file.createMarker(IMarker.PROBLEM); 141 marker.setAttribute(IMarker.SEVERITY, IMarker.SEVERITY_ERROR); 142 marker.setAttribute(IMarker.LINE_NUMBER, aLine); 143 marker.setAttribute(IMarker.MESSAGE, aMessage); 144 Position pos = new Position(getDocument().getLineOffset(aLine - 1)); 145 getSourceViewer().getAnnotationModel().addAnnotation( 146 new MarkerAnnotation(marker), pos); 147 148 } catch (Exception e) { 149 150 } 151 } 152 153 154 } 155 | Popular Tags |