KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ui > texteditor > MarkRegionTarget


1 /*******************************************************************************
2  * Copyright (c) 2000, 2005 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

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 /**
25  * Default implementation of <code>IMarkRegionTarget</code> using <code>ITextViewer</code>
26  * and <code>IStatusLineManager</code>.
27  *
28  * @since 2.0
29  */

30 public class MarkRegionTarget implements IMarkRegionTarget {
31
32     /** The text viewer. */
33     private final ITextViewer fViewer;
34     /** The status line. */
35     private final IStatusLineManager fStatusLine;
36
37     /**
38      * Creates a MarkRegionTaret.
39      *
40      * @param viewer the text viewer
41      * @param manager the status line manager
42      */

43     public MarkRegionTarget(ITextViewer viewer, IStatusLineManager manager) {
44         fViewer= viewer;
45         fStatusLine= manager;
46     }
47
48     /*
49      * @see IMarkregion#setMarkAtCursor(boolean)
50      */

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(""); //$NON-NLS-1$
63
fStatusLine.setMessage(EditorMessages.Editor_mark_status_message_mark_set);
64
65         } else {
66             viewerExtension.setMark(-1);
67
68             fStatusLine.setErrorMessage(""); //$NON-NLS-1$
69
fStatusLine.setMessage(EditorMessages.Editor_mark_status_message_mark_cleared);
70         }
71     }
72
73     /*
74      * @see IMarkregion#swapMarkAndCursor()
75      */

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(""); //$NON-NLS-1$
87
return;
88         }
89
90         if (!isVisible(fViewer, markPosition)) {
91             fStatusLine.setErrorMessage(EditorMessages.MarkRegionTarget_markNotVisible);
92             fStatusLine.setMessage(""); //$NON-NLS-1$
93
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(""); //$NON-NLS-1$
103
fStatusLine.setMessage(EditorMessages.Editor_mark_status_message_mark_swapped);
104     }
105
106     /**
107      * Tells whether the given offset is visible in the given text viewer.
108      *
109      * @param viewer the text viewer
110      * @param offset the offset to check
111      * @return <code>true</code> if the given offset is visible in the given text viewer
112      *
113      * @since 2.1
114      */

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