KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > spi > editor > hints > ChangeInfo


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.spi.editor.hints;
21
22 import java.util.ArrayList JavaDoc;
23 import java.util.List JavaDoc;
24 import javax.swing.text.Position JavaDoc;
25 import org.openide.filesystems.FileObject;
26
27 /**
28  * Represents a set of changes made by a hint. Note that the start/end
29  * parameters refer to offsets that should be <i>selected</i>, not to
30  * the general offsets of the modified element.
31  * <p>
32  * In other words, a change generated by a hint can generate things like
33  * variable names; ChangeInfo provides a list of generated code which the
34  * user might want to modify/replace with their own text. The start/end offsets
35  * are offsets into the file in question, which determine selection start/end
36  * in the editor.
37  * <p>
38  * This class provides for a list of changes, anticipating &quot;live template&quot;
39  * support (where the user confirms each generated element). Currently only
40  * the first change provided is used.
41  *
42  * @author Tim Boudreau
43  */

44 public final class ChangeInfo {
45     private List JavaDoc<Change> changes = null;
46     
47     /** Create an instance of ChangeInfo prepopulated with a
48      * single change */

49     public ChangeInfo(FileObject fileObject, Position JavaDoc start, Position JavaDoc end) {
50         add (fileObject, start, end);
51     }
52     
53     public ChangeInfo(Position JavaDoc start, Position JavaDoc end) {
54         add (null, start, end);
55     }
56     
57     
58     public ChangeInfo() {
59     }
60     
61     public final int size() {
62         return changes != null ? changes.size() : 0;
63     }
64     
65     public final void add (FileObject fileObject, Position JavaDoc start, Position JavaDoc end) {
66         if (changes == null) {
67             changes = new ArrayList JavaDoc<Change>(5);
68         }
69         changes.add (new ChangeImpl (fileObject, start, end));
70     }
71     
72     public final Change get (int i) {
73         if (changes == null) {
74             throw new ArrayIndexOutOfBoundsException JavaDoc ("No changes");
75         }
76         return (Change) changes.get(i);
77     }
78     
79     public String JavaDoc toString() {
80         int size = size();
81         if (size == 0) {
82             return "Empty ChangeInfo";
83         } else {
84             StringBuffer JavaDoc sb = new StringBuffer JavaDoc (100);
85             sb.append ("ChangeInfo [");
86             for (int i=0; i < size; i++) {
87                 sb.append (get(i));
88                 if (i != size-1) {
89                     sb.append (',');
90                 }
91             }
92             sb.append ("]");
93             return sb.toString();
94         }
95     }
96     
97     /**
98      * Interface representing a single caret-positioning or user-modifiable
99      * change.
100      */

101     public static interface Change {
102         public Position JavaDoc getStart();
103         public Position JavaDoc getEnd();
104         public FileObject getFileObject();
105     }
106     
107     private static final class ChangeImpl implements Change {
108         Position JavaDoc start;
109         Position JavaDoc end;
110         FileObject fileObject;
111         
112         ChangeImpl(FileObject fileObject, Position JavaDoc start, Position JavaDoc end) {
113             this.fileObject = fileObject;
114             this.start = start;
115             this.end = end;
116         }
117         
118         public Position JavaDoc getStart() {
119             return start;
120         }
121         
122         public Position JavaDoc getEnd() {
123             return end;
124         }
125         
126         public FileObject getFileObject() {
127             return fileObject;
128         }
129         
130         public String JavaDoc toString() {
131             return "Change from " + start.getOffset() + " to " + end.getOffset() + " in " + fileObject;
132         }
133     }
134     
135 }
136
Popular Tags