KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > openide > text > EditorSupportLineSet


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-2007 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19 package org.openide.text;
20
21 import java.lang.ref.Reference JavaDoc;
22 import java.util.*;
23
24 import javax.swing.event.*;
25 import javax.swing.text.Position JavaDoc;
26 import javax.swing.text.StyledDocument JavaDoc;
27
28
29 /** Line set for an EditorSupport.
30 *
31 * @author Jaroslav Tulach, David Konecny
32 */

33 final class EditorSupportLineSet extends DocumentLine.Set {
34     /** support we are attached to */
35     private CloneableEditorSupport support;
36
37     /** Constructor.
38     * @param support support to work with
39     * @param doc document to use
40     */

41     public EditorSupportLineSet(CloneableEditorSupport support, StyledDocument JavaDoc doc) {
42         super(doc, support);
43         this.support = support;
44     }
45
46     /** Shares the whm with other line sets based on the same support.
47      */

48     Map<Line,Reference JavaDoc<Line>> findWeakHashMap() {
49         return support.findWeakHashMap();
50     }
51
52     /** Creates a Line for given offset.
53     * @param offset the begining of line
54     * @return line that should represent the given line
55     */

56     public Line createLine(int offset) {
57         PositionRef ref = new PositionRef(support.getPositionManager(), offset, Position.Bias.Forward);
58
59         return new SupportLine(support.getLookup(), ref, support);
60     }
61
62     /** Line for my work.
63     */

64     private static final class SupportLine extends DocumentLine {
65         static final long serialVersionUID = 7282223299866986051L;
66
67         /** Position reference to a place in document
68         */

69         public SupportLine(org.openide.util.Lookup obj, PositionRef ref, CloneableEditorSupport support) {
70             super(obj, ref);
71         }
72
73         /** Shows the line.
74         * @param kind one of SHOW_XXX constants.
75         * @column the column of this line which should be selected
76         */

77         public void show(int kind, int column) {
78             CloneableEditorSupport support = pos.getCloneableEditorSupport();
79
80             if ((kind == SHOW_TRY_SHOW) && !support.isDocumentLoaded()) {
81                 return;
82             }
83
84             CloneableEditorSupport.Pane editor;
85             
86             if (kind == SHOW_REUSE || kind == SHOW_REUSE_NEW) {
87                 editor = support.openReuse(pos, column, kind);
88             } else {
89                 editor = support.openAt(pos, column);
90                 if (kind == SHOW_TOFRONT) editor.getComponent().toFront();
91             }
92             editor.getComponent().requestActive();
93         }
94
95         /** This method will be used for annotation of part of the text on the line.*/
96         public Line.Part createPart(int column, int length) {
97             DocumentLine.Part part = new DocumentLine.Part(
98                     this,
99                     new PositionRef(
100                         pos.getCloneableEditorSupport().getPositionManager(), pos.getOffset() + column,
101                         Position.Bias.Forward
102                     ), length
103                 );
104             addLinePart(part);
105
106             return part;
107         }
108
109         /** Let's the support determine the name */
110         public String JavaDoc getDisplayName() {
111             CloneableEditorSupport support = pos.getCloneableEditorSupport();
112
113             return support.messageLine(this);
114         }
115
116         public String JavaDoc toString() {
117             return "SupportLine@" + Integer.toHexString(System.identityHashCode(this)) + " at line: " +
118             getLineNumber(); // NOI18N
119
}
120     }
121
122     /** Line set for closed EditorSupport.
123     *
124     * @author Jaroslav Tulach
125     */

126     static class Closed extends Line.Set implements ChangeListener {
127         /** support we are attached to */
128         private CloneableEditorSupport support;
129
130         /** line set to delegate to or null if the editor is still closed,
131         * is set to non null when the editor opens
132         */

133         private Line.Set delegate;
134
135         /** Constructor.
136         * @param support support to work with
137         * @param doc document to use
138         */

139         public Closed(CloneableEditorSupport support) {
140             this.support = support;
141             support.addChangeListener(org.openide.util.WeakListeners.change(this, support));
142         }
143
144         /** Shares the whm with other line sets based on the same support.
145          */

146         Map<Line,Reference JavaDoc<Line>> findWeakHashMap() {
147             return support.findWeakHashMap();
148         }
149
150         /** Returns a set of line objects sorted by their
151         * line numbers. This immutable list will contains all lines held by this
152         * line set.
153         *
154         * @return list of element type {@link Line}
155         */

156         public List<? extends Line> getLines() {
157             if (delegate != null) {
158                 return delegate.getLines();
159             }
160
161             // PENDING
162
return new ArrayList<Line>();
163         }
164
165         /** Find line object in the line set corresponding to original line number.
166         * That is, finds the line in the current document which originally had the indicated line number.
167         * If there have been modifications of that line, find one as close as possible.
168         *
169         * @param line number of the line
170         * @return line object
171         * @exception IndexOutOfBoundsException if <code>line</code> is an invalid index for the original set of lines
172         */

173         public Line getOriginal(int line) throws IndexOutOfBoundsException JavaDoc {
174             if (delegate != null) {
175                 return delegate.getOriginal(line);
176             }
177
178             return getCurrent(line);
179         }
180
181         /** Find line object in the line set corresponding to current line number.
182         *
183         * @param line number of the line
184         * @return line object
185         * @exception IndexOutOfBoundsException if <code>line</code> is an invalid index for the original set of lines
186         */

187         public Line getCurrent(int line) throws IndexOutOfBoundsException JavaDoc {
188             PositionRef ref = new PositionRef(support.getPositionManager(), line, 0, Position.Bias.Forward);
189
190             // obj can be null, sorry...
191
org.openide.util.Lookup obj = support.getLookup();
192
193             return this.registerLine(new SupportLine(obj, ref, support));
194         }
195
196         /** Arrives when the document is opened.
197         */

198         public synchronized void stateChanged(ChangeEvent ev) {
199             if (delegate == null) {
200                 StyledDocument JavaDoc doc = support.getDocument();
201
202                 if (doc != null) {
203                     delegate = new EditorSupportLineSet(support, doc);
204                 }
205             } else {
206                 if (support.getDocument() == null) {
207                     delegate = null;
208                 }
209             }
210         }
211     }
212 }
213
Popular Tags