001: /*******************************************************************************
002: * Copyright (c) 2000, 2005 IBM Corporation and others.
003: * All rights reserved. This program and the accompanying materials
004: * are made available under the terms of the Eclipse Public License v1.0
005: * which accompanies this distribution, and is available at
006: * http://www.eclipse.org/legal/epl-v10.html
007: *
008: * Contributors:
009: * IBM Corporation - initial API and implementation
010: *******************************************************************************/package org.eclipse.ui.texteditor;
011:
012: import org.eclipse.swt.graphics.Point;
013:
014: import org.eclipse.jface.action.IStatusLineManager;
015: import org.eclipse.jface.text.IMarkRegionTarget;
016: import org.eclipse.jface.text.IRegion;
017: import org.eclipse.jface.text.ITextViewer;
018: import org.eclipse.jface.text.ITextViewerExtension;
019: import org.eclipse.jface.text.ITextViewerExtension5;
020:
021: /**
022: * Default implementation of <code>IMarkRegionTarget</code> using <code>ITextViewer</code>
023: * and <code>IStatusLineManager</code>.
024: *
025: * @since 2.0
026: */
027: public class MarkRegionTarget implements IMarkRegionTarget {
028:
029: /** The text viewer. */
030: private final ITextViewer fViewer;
031: /** The status line. */
032: private final IStatusLineManager fStatusLine;
033:
034: /**
035: * Creates a MarkRegionTaret.
036: *
037: * @param viewer the text viewer
038: * @param manager the status line manager
039: */
040: public MarkRegionTarget(ITextViewer viewer,
041: IStatusLineManager manager) {
042: fViewer = viewer;
043: fStatusLine = manager;
044: }
045:
046: /*
047: * @see IMarkregion#setMarkAtCursor(boolean)
048: */
049: public void setMarkAtCursor(boolean set) {
050:
051: if (!(fViewer instanceof ITextViewerExtension))
052: return;
053:
054: ITextViewerExtension viewerExtension = ((ITextViewerExtension) fViewer);
055:
056: if (set) {
057: Point selection = fViewer.getSelectedRange();
058: viewerExtension.setMark(selection.x);
059:
060: fStatusLine.setErrorMessage(""); //$NON-NLS-1$
061: fStatusLine
062: .setMessage(EditorMessages.Editor_mark_status_message_mark_set);
063:
064: } else {
065: viewerExtension.setMark(-1);
066:
067: fStatusLine.setErrorMessage(""); //$NON-NLS-1$
068: fStatusLine
069: .setMessage(EditorMessages.Editor_mark_status_message_mark_cleared);
070: }
071: }
072:
073: /*
074: * @see IMarkregion#swapMarkAndCursor()
075: */
076: public void swapMarkAndCursor() {
077:
078: if (!(fViewer instanceof ITextViewerExtension))
079: return;
080:
081: ITextViewerExtension viewerExtension = ((ITextViewerExtension) fViewer);
082:
083: int markPosition = viewerExtension.getMark();
084: if (markPosition == -1) {
085: fStatusLine
086: .setErrorMessage(EditorMessages.MarkRegionTarget_markNotSet);
087: fStatusLine.setMessage(""); //$NON-NLS-1$
088: return;
089: }
090:
091: if (!isVisible(fViewer, markPosition)) {
092: fStatusLine
093: .setErrorMessage(EditorMessages.MarkRegionTarget_markNotVisible);
094: fStatusLine.setMessage(""); //$NON-NLS-1$
095: return;
096: }
097:
098: Point selection = fViewer.getSelectedRange();
099: viewerExtension.setMark(selection.x);
100:
101: fViewer.setSelectedRange(markPosition, 0);
102: fViewer.revealRange(markPosition, 0);
103:
104: fStatusLine.setErrorMessage(""); //$NON-NLS-1$
105: fStatusLine
106: .setMessage(EditorMessages.Editor_mark_status_message_mark_swapped);
107: }
108:
109: /**
110: * Tells whether the given offset is visible in the given text viewer.
111: *
112: * @param viewer the text viewer
113: * @param offset the offset to check
114: * @return <code>true</code> if the given offset is visible in the given text viewer
115: *
116: * @since 2.1
117: */
118: protected final static boolean isVisible(ITextViewer viewer,
119: int offset) {
120: if (viewer instanceof ITextViewerExtension5) {
121: ITextViewerExtension5 extension = (ITextViewerExtension5) viewer;
122: return extension.modelOffset2WidgetOffset(offset) >= 0;
123: }
124: IRegion region = viewer.getVisibleRegion();
125: int vOffset = region.getOffset();
126: return (vOffset <= offset && offset <= vOffset
127: + region.getLength());
128: }
129: }
|