129
|
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 * Port to the D programming language:
|
|
11 * Frank Benoit <benoit@tionex.de>
|
|
12 *******************************************************************************/
|
|
13 module dwtx.text.edits.TextEditGroup;
|
|
14
|
|
15 import dwt.dwthelper.utils;
|
|
16
|
|
17 import java.util.ArrayList;
|
|
18 import java.util.Arrays;
|
|
19 import java.util.List;
|
|
20
|
|
21 import dwtx.core.runtime.Assert;
|
|
22 import dwtx.jface.text.IRegion;
|
|
23
|
|
24
|
|
25 /**
|
|
26 * A text edit group combines a list of {@link TextEdit}s
|
|
27 * and a name into a single object. The name must be a human
|
|
28 * readable string use to present the text edit group in the
|
|
29 * user interface.
|
|
30 * <p>
|
|
31 * Clients may extend this class to add extra information to
|
|
32 * a text edit group.
|
|
33 * </p>
|
|
34 *
|
|
35 * @since 3.0
|
|
36 */
|
|
37 public class TextEditGroup {
|
|
38
|
|
39 private String fDescription;
|
|
40 private List fEdits;
|
|
41
|
|
42 /**
|
|
43 * Creates a new text edit group with the given name.
|
|
44 *
|
|
45 * @param name the name of the text edit group. Must be
|
|
46 * a human readable string
|
|
47 */
|
|
48 public TextEditGroup(String name) {
|
|
49 super();
|
|
50 Assert.isNotNull(name);
|
|
51 fDescription= name;
|
|
52 fEdits= new ArrayList(3);
|
|
53 }
|
|
54
|
|
55 /**
|
|
56 * Creates a new text edit group with a name and a single
|
|
57 * {@link TextEdit}.
|
|
58 *
|
|
59 * @param name the name of the text edit group. Must be
|
|
60 * a human readable string
|
|
61 * @param edit the edit to manage
|
|
62 */
|
|
63 public TextEditGroup(String name, TextEdit edit) {
|
|
64 Assert.isNotNull(name);
|
|
65 Assert.isNotNull(edit);
|
|
66 fDescription= name;
|
|
67 fEdits= new ArrayList(1);
|
|
68 fEdits.add(edit);
|
|
69 }
|
|
70
|
|
71 /**
|
|
72 * Creates a new text edit group with the given name and
|
|
73 * array of edits.
|
|
74 *
|
|
75 * @param name the name of the text edit group. Must be
|
|
76 * a human readable string
|
|
77 * @param edits the array of edits
|
|
78 */
|
|
79 public TextEditGroup(String name, TextEdit[] edits) {
|
|
80 super();
|
|
81 Assert.isNotNull(name);
|
|
82 Assert.isNotNull(edits);
|
|
83 fDescription= name;
|
|
84 fEdits= new ArrayList(Arrays.asList(edits));
|
|
85 }
|
|
86
|
|
87 /**
|
|
88 * Returns the edit group's name.
|
|
89 *
|
|
90 * @return the edit group's name
|
|
91 */
|
|
92 public String getName() {
|
|
93 return fDescription;
|
|
94 }
|
|
95
|
|
96 /**
|
|
97 * Adds the given {@link TextEdit} to this group.
|
|
98 *
|
|
99 * @param edit the edit to add
|
|
100 */
|
|
101 public void addTextEdit(TextEdit edit) {
|
|
102 fEdits.add(edit);
|
|
103 }
|
|
104
|
|
105 /**
|
|
106 * Removes the given {@link TextEdit} from this group.
|
|
107 *
|
|
108 * @param edit the edit to remove
|
|
109 * @return <code>true</code> if this group contained the specified edit.
|
|
110 * @since 3.3
|
|
111 */
|
|
112 public bool removeTextEdit(TextEdit edit) {
|
|
113 return fEdits.remove(edit);
|
|
114 }
|
|
115
|
|
116 /**
|
|
117 * Removes all text edits from this group.
|
|
118 *
|
|
119 * @since 3.3
|
|
120 */
|
|
121 public void clearTextEdits() {
|
|
122 fEdits.clear();
|
|
123 }
|
|
124
|
|
125
|
|
126
|
|
127 /**
|
|
128 * Returns <code>true</code> if the list of managed
|
|
129 * {@link TextEdit}s is empty; otherwise <code>false
|
|
130 * </code> is returned.
|
|
131 *
|
|
132 * @return whether the list of managed text edits is
|
|
133 * empty or not
|
|
134 */
|
|
135 public bool isEmpty() {
|
|
136 return fEdits.isEmpty();
|
|
137 }
|
|
138
|
|
139 /**
|
|
140 * Returns an array of {@link TextEdit}s containing
|
|
141 * the edits managed by this group.
|
|
142 *
|
|
143 * @return the managed text edits
|
|
144 */
|
|
145 public TextEdit[] getTextEdits() {
|
|
146 return (TextEdit[]) fEdits.toArray(new TextEdit[fEdits.size()]);
|
|
147 }
|
|
148
|
|
149 /**
|
|
150 * Returns the text region covered by the edits managed via this
|
|
151 * edit group. If the group doesn't manage any edits <code>null
|
|
152 * </code> is returned.
|
|
153 *
|
|
154 * @return the text region covered by this edit group or <code>
|
|
155 * null</code> if no edits are managed
|
|
156 */
|
|
157 public IRegion getRegion() {
|
|
158 int size= fEdits.size();
|
|
159 if (size is 0) {
|
|
160 return null;
|
|
161 } else if (size is 1) {
|
|
162 return ((TextEdit)fEdits.get(0)).getRegion();
|
|
163 } else {
|
|
164 return TextEdit.getCoverage((TextEdit[])fEdits.toArray(new TextEdit[fEdits.size()]));
|
|
165 }
|
|
166 }
|
|
167 }
|