001: /*******************************************************************************
002: * Copyright (c) 2000, 2006 IBM Corporation and others.
003: * All rights reserved. This program and the accompanying materials
004: * are made available under the terms of the Eclipse Public License v1.0
005: * which accompanies this distribution, and is available at
006: * http://www.eclipse.org/legal/epl-v10.html
007: *
008: * Contributors:
009: * IBM Corporation - initial API and implementation
010: *******************************************************************************/package org.eclipse.text.edits;
011:
012: import java.util.ArrayList;
013: import java.util.Arrays;
014: import java.util.List;
015:
016: import org.eclipse.core.runtime.Assert;
017:
018: import org.eclipse.jface.text.IRegion;
019:
020: /**
021: * A text edit group combines a list of {@link TextEdit}s
022: * and a name into a single object. The name must be a human
023: * readable string use to present the text edit group in the
024: * user interface.
025: * <p>
026: * Clients may extend this class to add extra information to
027: * a text edit group.
028: * </p>
029: *
030: * @since 3.0
031: */
032: public class TextEditGroup {
033:
034: private String fDescription;
035: private List fEdits;
036:
037: /**
038: * Creates a new text edit group with the given name.
039: *
040: * @param name the name of the text edit group. Must be
041: * a human readable string
042: */
043: public TextEditGroup(String name) {
044: super ();
045: Assert.isNotNull(name);
046: fDescription = name;
047: fEdits = new ArrayList(3);
048: }
049:
050: /**
051: * Creates a new text edit group with a name and a single
052: * {@link TextEdit}.
053: *
054: * @param name the name of the text edit group. Must be
055: * a human readable string
056: * @param edit the edit to manage
057: */
058: public TextEditGroup(String name, TextEdit edit) {
059: Assert.isNotNull(name);
060: Assert.isNotNull(edit);
061: fDescription = name;
062: fEdits = new ArrayList(1);
063: fEdits.add(edit);
064: }
065:
066: /**
067: * Creates a new text edit group with the given name and
068: * array of edits.
069: *
070: * @param name the name of the text edit group. Must be
071: * a human readable string
072: * @param edits the array of edits
073: */
074: public TextEditGroup(String name, TextEdit[] edits) {
075: super ();
076: Assert.isNotNull(name);
077: Assert.isNotNull(edits);
078: fDescription = name;
079: fEdits = new ArrayList(Arrays.asList(edits));
080: }
081:
082: /**
083: * Returns the edit group's name.
084: *
085: * @return the edit group's name
086: */
087: public String getName() {
088: return fDescription;
089: }
090:
091: /**
092: * Adds the given {@link TextEdit} to this group.
093: *
094: * @param edit the edit to add
095: */
096: public void addTextEdit(TextEdit edit) {
097: fEdits.add(edit);
098: }
099:
100: /**
101: * Removes the given {@link TextEdit} from this group.
102: *
103: * @param edit the edit to remove
104: * @return <code>true</code> if this group contained the specified edit.
105: * @since 3.3
106: */
107: public boolean removeTextEdit(TextEdit edit) {
108: return fEdits.remove(edit);
109: }
110:
111: /**
112: * Removes all text edits from this group.
113: *
114: * @since 3.3
115: */
116: public void clearTextEdits() {
117: fEdits.clear();
118: }
119:
120: /**
121: * Returns <code>true</code> if the list of managed
122: * {@link TextEdit}s is empty; otherwise <code>false
123: * </code> is returned.
124: *
125: * @return whether the list of managed text edits is
126: * empty or not
127: */
128: public boolean isEmpty() {
129: return fEdits.isEmpty();
130: }
131:
132: /**
133: * Returns an array of {@link TextEdit}s containing
134: * the edits managed by this group.
135: *
136: * @return the managed text edits
137: */
138: public TextEdit[] getTextEdits() {
139: return (TextEdit[]) fEdits.toArray(new TextEdit[fEdits.size()]);
140: }
141:
142: /**
143: * Returns the text region covered by the edits managed via this
144: * edit group. If the group doesn't manage any edits <code>null
145: * </code> is returned.
146: *
147: * @return the text region covered by this edit group or <code>
148: * null</code> if no edits are managed
149: */
150: public IRegion getRegion() {
151: int size = fEdits.size();
152: if (size == 0) {
153: return null;
154: } else if (size == 1) {
155: return ((TextEdit) fEdits.get(0)).getRegion();
156: } else {
157: return TextEdit.getCoverage((TextEdit[]) fEdits
158: .toArray(new TextEdit[fEdits.size()]));
159: }
160: }
161: }
|