1 11 12 package org.eclipse.ui.texteditor; 13 14 15 import org.eclipse.swt.graphics.Point; 16 17 import org.eclipse.jface.action.IStatusLineManager; 18 import org.eclipse.jface.text.IMarkRegionTarget; 19 import org.eclipse.jface.text.IRegion; 20 import org.eclipse.jface.text.ITextViewer; 21 import org.eclipse.jface.text.ITextViewerExtension; 22 import org.eclipse.jface.text.ITextViewerExtension5; 23 24 30 public class MarkRegionTarget implements IMarkRegionTarget { 31 32 33 private final ITextViewer fViewer; 34 35 private final IStatusLineManager fStatusLine; 36 37 43 public MarkRegionTarget(ITextViewer viewer, IStatusLineManager manager) { 44 fViewer= viewer; 45 fStatusLine= manager; 46 } 47 48 51 public void setMarkAtCursor(boolean set) { 52 53 if (!(fViewer instanceof ITextViewerExtension)) 54 return; 55 56 ITextViewerExtension viewerExtension= ((ITextViewerExtension) fViewer); 57 58 if (set) { 59 Point selection= fViewer.getSelectedRange(); 60 viewerExtension.setMark(selection.x); 61 62 fStatusLine.setErrorMessage(""); fStatusLine.setMessage(EditorMessages.Editor_mark_status_message_mark_set); 64 65 } else { 66 viewerExtension.setMark(-1); 67 68 fStatusLine.setErrorMessage(""); fStatusLine.setMessage(EditorMessages.Editor_mark_status_message_mark_cleared); 70 } 71 } 72 73 76 public void swapMarkAndCursor() { 77 78 if (!(fViewer instanceof ITextViewerExtension)) 79 return; 80 81 ITextViewerExtension viewerExtension= ((ITextViewerExtension) fViewer); 82 83 int markPosition= viewerExtension.getMark(); 84 if (markPosition == -1) { 85 fStatusLine.setErrorMessage(EditorMessages.MarkRegionTarget_markNotSet); 86 fStatusLine.setMessage(""); return; 88 } 89 90 if (!isVisible(fViewer, markPosition)) { 91 fStatusLine.setErrorMessage(EditorMessages.MarkRegionTarget_markNotVisible); 92 fStatusLine.setMessage(""); return; 94 } 95 96 Point selection= fViewer.getSelectedRange(); 97 viewerExtension.setMark(selection.x); 98 99 fViewer.setSelectedRange(markPosition, 0); 100 fViewer.revealRange(markPosition, 0); 101 102 fStatusLine.setErrorMessage(""); fStatusLine.setMessage(EditorMessages.Editor_mark_status_message_mark_swapped); 104 } 105 106 115 protected final static boolean isVisible(ITextViewer viewer, int offset) { 116 if (viewer instanceof ITextViewerExtension5) { 117 ITextViewerExtension5 extension= (ITextViewerExtension5) viewer; 118 return extension.modelOffset2WidgetOffset(offset) >= 0; 119 } 120 IRegion region= viewer.getVisibleRegion(); 121 int vOffset= region.getOffset(); 122 return (vOffset <= offset && offset <= vOffset + region.getLength()); 123 } 124 } 125 | Popular Tags |