KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > text > edits > TextEditGroup


1 /*******************************************************************************
2  * Copyright (c) 2000, 2006 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.text.edits;
12
13 import java.util.ArrayList JavaDoc;
14 import java.util.Arrays JavaDoc;
15 import java.util.List JavaDoc;
16
17 import org.eclipse.core.runtime.Assert;
18
19 import org.eclipse.jface.text.IRegion;
20
21
22 /**
23  * A text edit group combines a list of {@link TextEdit}s
24  * and a name into a single object. The name must be a human
25  * readable string use to present the text edit group in the
26  * user interface.
27  * <p>
28  * Clients may extend this class to add extra information to
29  * a text edit group.
30  * </p>
31  *
32  * @since 3.0
33  */

34 public class TextEditGroup {
35
36     private String JavaDoc fDescription;
37     private List JavaDoc fEdits;
38
39     /**
40      * Creates a new text edit group with the given name.
41      *
42      * @param name the name of the text edit group. Must be
43      * a human readable string
44      */

45     public TextEditGroup(String JavaDoc name) {
46         super();
47         Assert.isNotNull(name);
48         fDescription= name;
49         fEdits= new ArrayList JavaDoc(3);
50     }
51
52     /**
53      * Creates a new text edit group with a name and a single
54      * {@link TextEdit}.
55      *
56      * @param name the name of the text edit group. Must be
57      * a human readable string
58      * @param edit the edit to manage
59      */

60     public TextEditGroup(String JavaDoc name, TextEdit edit) {
61         Assert.isNotNull(name);
62         Assert.isNotNull(edit);
63         fDescription= name;
64         fEdits= new ArrayList JavaDoc(1);
65         fEdits.add(edit);
66     }
67
68     /**
69      * Creates a new text edit group with the given name and
70      * array of edits.
71      *
72      * @param name the name of the text edit group. Must be
73      * a human readable string
74      * @param edits the array of edits
75      */

76     public TextEditGroup(String JavaDoc name, TextEdit[] edits) {
77         super();
78         Assert.isNotNull(name);
79         Assert.isNotNull(edits);
80         fDescription= name;
81         fEdits= new ArrayList JavaDoc(Arrays.asList(edits));
82     }
83
84     /**
85      * Returns the edit group's name.
86      *
87      * @return the edit group's name
88      */

89     public String JavaDoc getName() {
90         return fDescription;
91     }
92
93     /**
94      * Adds the given {@link TextEdit} to this group.
95      *
96      * @param edit the edit to add
97      */

98     public void addTextEdit(TextEdit edit) {
99         fEdits.add(edit);
100     }
101     
102     /**
103      * Removes the given {@link TextEdit} from this group.
104      *
105      * @param edit the edit to remove
106      * @return <code>true</code> if this group contained the specified edit.
107      * @since 3.3
108      */

109     public boolean removeTextEdit(TextEdit edit) {
110       return fEdits.remove(edit);
111     }
112
113     /**
114      * Removes all text edits from this group.
115      *
116      * @since 3.3
117      */

118     public void clearTextEdits() {
119       fEdits.clear();
120     }
121
122     
123
124     /**
125      * Returns <code>true</code> if the list of managed
126      * {@link TextEdit}s is empty; otherwise <code>false
127      * </code> is returned.
128      *
129      * @return whether the list of managed text edits is
130      * empty or not
131      */

132     public boolean isEmpty() {
133         return fEdits.isEmpty();
134     }
135
136     /**
137      * Returns an array of {@link TextEdit}s containing
138      * the edits managed by this group.
139      *
140      * @return the managed text edits
141      */

142     public TextEdit[] getTextEdits() {
143         return (TextEdit[]) fEdits.toArray(new TextEdit[fEdits.size()]);
144     }
145
146     /**
147      * Returns the text region covered by the edits managed via this
148      * edit group. If the group doesn't manage any edits <code>null
149      * </code> is returned.
150      *
151      * @return the text region covered by this edit group or <code>
152      * null</code> if no edits are managed
153      */

154     public IRegion getRegion() {
155         int size= fEdits.size();
156         if (size == 0) {
157             return null;
158         } else if (size == 1) {
159             return ((TextEdit)fEdits.get(0)).getRegion();
160         } else {
161             return TextEdit.getCoverage((TextEdit[])fEdits.toArray(new TextEdit[fEdits.size()]));
162         }
163     }
164 }
165
Popular Tags