KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > editor > lib2 > highlighting > OffsetGapList


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.modules.editor.lib2.highlighting;
21
22 /**
23  * The implementation of <code>AbstractOffsetGapList</code> with
24  * <code>Offset</code> elements.
25  *
26  * @author Vita Stejskal
27  */

28 public final class OffsetGapList<E extends OffsetGapList.Offset> extends AbstractOffsetGapList<E> {
29     
30     /** Creates a new instance of SimpleOffsetGapList */
31     public OffsetGapList() {
32     }
33
34     protected int elementRawOffset(E elem) {
35         return elem.getRawOffset();
36     }
37
38     protected void setElementRawOffset(E elem, int rawOffset) {
39         elem.setRawOffset(rawOffset);
40     }
41
42     protected int attachElement(E elem) {
43         return elem.attach(this);
44     }
45
46     protected void detachElement(E elem) {
47         elem.detach(this);
48     }
49
50     protected E getAttachedElement(Object JavaDoc o) {
51         if ((o instanceof Offset) && ((Offset) o).checkOwner(this)) {
52             @SuppressWarnings JavaDoc("unchecked") //NOI18N
53
E element = (E) o;
54             return element;
55         } else {
56             return null;
57         }
58     }
59
60     /**
61      * An offset gap list element. The <code>OffsetGapList</code> can accomodate
62      * either instances of this class or any of its subclass.
63      */

64     public static class Offset {
65         
66         private int originalOrRawOffset;
67         private OffsetGapList list;
68         
69         /**
70          * Creates a new <code>Offset</code> object and sets its original offset
71          * to the value passed in.
72          *
73          * @param offset The original offset of this <code>Offset</code> object.
74          */

75         public Offset(int offset) {
76             this.originalOrRawOffset = offset;
77         }
78
79         /**
80          * Gets the offset of this <code>Offset</code> object. The offset is
81          * either the original offset passed to the constructor if this <code>Offset</code>
82          * instance has not been attached to a list yet or it is the real
83          * offset of this instance, which reflects all offset updates in the list
84          * (i.e. it gets updated when {@link AbstractOffsetGapList#defaultInsertUpdate} or
85          * {@link AbstractOffsetGapList#defaultRemoveUpdate} is called).
86          *
87          * @return The offset of this <code>Offset</code> instance.
88          */

89         public final int getOffset() {
90             if (list == null) {
91                 return originalOrRawOffset;
92             } else {
93                 return list.raw2Offset(getRawOffset());
94             }
95         }
96         
97         private int attach(OffsetGapList list) {
98             assert this.list == null : "Offset instances can only be added to one OffsetGapList."; //NOI18N
99
this.list = list;
100             return originalOrRawOffset;
101         }
102
103         private void detach(OffsetGapList list) {
104             assert this.list == list : "Can't detach from a foreign list."; //NOI18N
105
this.list = null;
106         }
107         
108         private boolean checkOwner(OffsetGapList list) {
109             return this.list == list;
110         }
111         
112         private int getRawOffset() {
113             return originalOrRawOffset;
114         }
115         
116         private void setRawOffset(int rawOffset) {
117             this.originalOrRawOffset = rawOffset;
118         }
119     } // End of Offset class
120
}
121
Popular Tags