KickJava   Java API By Example, From Geeks To Geeks.

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


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 package org.eclipse.ui.texteditor;
12
13 import org.eclipse.jface.text.BadLocationException;
14 import org.eclipse.jface.text.IDocument;
15 import org.eclipse.jface.text.Position;
16
17 import org.eclipse.core.resources.IMarker;
18
19
20 /**
21  * Updates a marker's positional attributes which are
22  * start position, end position, and line number.
23  */

24 public final class BasicMarkerUpdater implements IMarkerUpdater {
25
26     private final static String JavaDoc[] ATTRIBUTES= {
27         IMarker.CHAR_START,
28         IMarker.CHAR_END,
29         IMarker.LINE_NUMBER
30     };
31
32     /**
33      * Creates a new basic marker updater.
34      */

35     public BasicMarkerUpdater() {
36         super();
37     }
38
39     /*
40      * @see IMarkerUpdater#getAttribute()
41      */

42     public String JavaDoc[] getAttribute() {
43         return ATTRIBUTES;
44     }
45
46     /*
47      * @see IMarkerUpdater#getMarkerType()
48      */

49     public String JavaDoc getMarkerType() {
50         return null;
51     }
52
53     /*
54      * @see IMarkerUpdater#updateMarker(IMarker, IDocument, Position)
55      */

56     public boolean updateMarker(IMarker marker, IDocument document, Position position) {
57
58         if (position == null)
59             return true;
60
61         if (position.isDeleted())
62             return false;
63
64         boolean offsetsInitialized= false;
65         boolean offsetsChanged= false;
66         int markerStart= MarkerUtilities.getCharStart(marker);
67         int markerEnd= MarkerUtilities.getCharEnd(marker);
68
69         if (markerStart != -1 && markerEnd != -1) {
70
71             offsetsInitialized= true;
72
73             int offset= position.getOffset();
74             if (markerStart != offset) {
75                 MarkerUtilities.setCharStart(marker, offset);
76                 offsetsChanged= true;
77             }
78
79             offset += position.getLength();
80             if (markerEnd != offset) {
81                 MarkerUtilities.setCharEnd(marker, offset);
82                 offsetsChanged= true;
83             }
84         }
85
86         if (!offsetsInitialized || (offsetsChanged && MarkerUtilities.getLineNumber(marker) != -1)) {
87             try {
88                 // marker line numbers are 1-based
89
MarkerUtilities.setLineNumber(marker, document.getLineOfOffset(position.getOffset()) + 1);
90             } catch (BadLocationException x) {
91             }
92         }
93
94         return true;
95     }
96 }
97
Popular Tags