KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > pde > internal > core > cheatsheet > comp > CompCSTaskGroup


1 /*******************************************************************************
2  * Copyright (c) 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
12 package org.eclipse.pde.internal.core.cheatsheet.comp;
13
14 import java.io.PrintWriter JavaDoc;
15 import java.util.ArrayList JavaDoc;
16 import java.util.Iterator JavaDoc;
17 import java.util.List JavaDoc;
18
19 import org.eclipse.pde.core.IModelChangedEvent;
20 import org.eclipse.pde.internal.core.XMLPrintHandler;
21 import org.eclipse.pde.internal.core.icheatsheet.comp.ICompCSModel;
22 import org.eclipse.pde.internal.core.icheatsheet.comp.ICompCSModelFactory;
23 import org.eclipse.pde.internal.core.icheatsheet.comp.ICompCSObject;
24 import org.eclipse.pde.internal.core.icheatsheet.comp.ICompCSTask;
25 import org.eclipse.pde.internal.core.icheatsheet.comp.ICompCSTaskGroup;
26 import org.eclipse.pde.internal.core.icheatsheet.comp.ICompCSTaskObject;
27 import org.w3c.dom.Element JavaDoc;
28
29 /**
30  * CompCSTaskGroup
31  *
32  */

33 public class CompCSTaskGroup extends CompCSTaskObject implements
34         ICompCSTaskGroup {
35
36     
37     private ArrayList JavaDoc fFieldTaskObjects;
38     
39     /**
40      *
41      */

42     private static final long serialVersionUID = 1L;
43
44     /**
45      * @param model
46      * @param parent
47      */

48     public CompCSTaskGroup(ICompCSModel model, ICompCSObject parent) {
49         super(model, parent);
50         reset();
51     }
52
53     /* (non-Javadoc)
54      * @see org.eclipse.pde.internal.core.cheatsheet.comp.CompCSTaskObject#getChildren()
55      */

56     public List JavaDoc getChildren() {
57         ArrayList JavaDoc list = new ArrayList JavaDoc();
58         // Add task objects
59
if (fFieldTaskObjects.size() > 0) {
60             list.addAll(fFieldTaskObjects);
61         }
62         return list;
63     }
64
65     /* (non-Javadoc)
66      * @see org.eclipse.pde.internal.core.cheatsheet.comp.CompCSTaskObject#getName()
67      */

68     public String JavaDoc getName() {
69         return fFieldName;
70     }
71
72     /* (non-Javadoc)
73      * @see org.eclipse.pde.internal.core.cheatsheet.comp.CompCSTaskObject#getType()
74      */

75     public int getType() {
76         return TYPE_TASKGROUP;
77     }
78
79     /* (non-Javadoc)
80      * @see org.eclipse.pde.internal.core.cheatsheet.comp.CompCSTaskObject#reset()
81      */

82     public void reset() {
83         super.reset();
84
85         fFieldTaskObjects = new ArrayList JavaDoc();
86     }
87
88     /* (non-Javadoc)
89      * @see org.eclipse.pde.internal.core.icheatsheet.comp.ICompCSTaskGroup#addFieldTaskObject(org.eclipse.pde.internal.core.icheatsheet.comp.ICompCSTaskObject)
90      */

91     public void addFieldTaskObject(ICompCSTaskObject taskObject) {
92         fFieldTaskObjects.add(taskObject);
93         if (isEditable()) {
94             fireStructureChanged(taskObject, IModelChangedEvent.INSERT);
95         }
96     }
97
98     /* (non-Javadoc)
99      * @see org.eclipse.pde.internal.core.icheatsheet.comp.ICompCSTaskGroup#addFieldTaskObject(int, org.eclipse.pde.internal.core.icheatsheet.comp.ICompCSTaskObject)
100      */

101     public void addFieldTaskObject(int index, ICompCSTaskObject taskObject) {
102         if (index < 0){
103             return;
104         }
105         if (index >= fFieldTaskObjects.size()) {
106             fFieldTaskObjects.add(taskObject);
107         } else {
108             fFieldTaskObjects.add(index, taskObject);
109         }
110         
111         if (isEditable()) {
112             fireStructureChanged(taskObject, IModelChangedEvent.INSERT);
113         }
114     }
115
116     /* (non-Javadoc)
117      * @see org.eclipse.pde.internal.core.icheatsheet.comp.ICompCSTaskGroup#getFieldTaskObjectCount()
118      */

119     public int getFieldTaskObjectCount() {
120         return fFieldTaskObjects.size();
121     }
122
123     /* (non-Javadoc)
124      * @see org.eclipse.pde.internal.core.icheatsheet.comp.ICompCSTaskGroup#getFieldTaskObjects()
125      */

126     public ICompCSTaskObject[] getFieldTaskObjects() {
127         return (ICompCSTaskObject[]) fFieldTaskObjects
128         .toArray(new ICompCSTaskObject[fFieldTaskObjects.size()]);
129     }
130
131     /* (non-Javadoc)
132      * @see org.eclipse.pde.internal.core.icheatsheet.comp.ICompCSTaskGroup#hasFieldTaskObjects()
133      */

134     public boolean hasFieldTaskObjects() {
135         if (fFieldTaskObjects.isEmpty()) {
136             return false;
137         }
138         return true;
139     }
140
141     /* (non-Javadoc)
142      * @see org.eclipse.pde.internal.core.icheatsheet.comp.ICompCSTaskGroup#indexOfFieldTaskObject(org.eclipse.pde.internal.core.icheatsheet.comp.ICompCSTaskObject)
143      */

144     public int indexOfFieldTaskObject(ICompCSTaskObject taskObject) {
145         return fFieldTaskObjects.indexOf(taskObject);
146     }
147
148     /* (non-Javadoc)
149      * @see org.eclipse.pde.internal.core.icheatsheet.comp.ICompCSTaskGroup#isFirstFieldTaskObject(org.eclipse.pde.internal.core.icheatsheet.comp.ICompCSTaskObject)
150      */

151     public boolean isFirstFieldTaskObject(ICompCSTaskObject taskObject) {
152         int position = fFieldTaskObjects.indexOf(taskObject);
153         if (position == 0) {
154             return true;
155         }
156         return false;
157     }
158
159     /* (non-Javadoc)
160      * @see org.eclipse.pde.internal.core.icheatsheet.comp.ICompCSTaskGroup#isLastFieldTaskObject(org.eclipse.pde.internal.core.icheatsheet.comp.ICompCSTaskObject)
161      */

162     public boolean isLastFieldTaskObject(ICompCSTaskObject taskObject) {
163         int position = fFieldTaskObjects.indexOf(taskObject);
164         int lastPosition = fFieldTaskObjects.size() - 1;
165         if (position == lastPosition) {
166             return true;
167         }
168         return false;
169     }
170
171     /* (non-Javadoc)
172      * @see org.eclipse.pde.internal.core.icheatsheet.comp.ICompCSTaskGroup#removeFieldTaskObject(org.eclipse.pde.internal.core.icheatsheet.comp.ICompCSTaskObject)
173      */

174     public void removeFieldTaskObject(ICompCSTaskObject taskObject) {
175         fFieldTaskObjects.remove(taskObject);
176         if (isEditable()) {
177             fireStructureChanged(taskObject, IModelChangedEvent.REMOVE);
178         }
179     }
180
181     /* (non-Javadoc)
182      * @see org.eclipse.pde.internal.core.icheatsheet.comp.ICompCSTaskGroup#removeFieldTaskObject(int)
183      */

184     public void removeFieldTaskObject(int index) {
185         if ((index < 0) ||
186                 (index > (fFieldTaskObjects.size() - 1))) {
187             return;
188         }
189         ICompCSTaskObject taskObject =
190             (ICompCSTaskObject)fFieldTaskObjects.remove(index);
191         if (isEditable()) {
192             fireStructureChanged(taskObject, IModelChangedEvent.REMOVE);
193         }
194     }
195
196     /* (non-Javadoc)
197      * @see org.eclipse.pde.internal.core.cheatsheet.comp.CompCSObject#parseElement(org.w3c.dom.Element)
198      */

199     protected void parseElement(Element JavaDoc element) {
200         super.parseElement(element);
201         String JavaDoc name = element.getNodeName();
202         ICompCSModelFactory factory = getModel().getFactory();
203         
204         if (name.equals(ELEMENT_TASK)) {
205             // Process task element
206
ICompCSTask task = factory.createCompCSTask(this);
207             fFieldTaskObjects.add(task);
208             task.parse(element);
209         } else if (name.equals(ELEMENT_TASKGROUP)) {
210             // Process taskGroup element
211
ICompCSTaskGroup taskGroup = factory.createCompCSTaskGroup(this);
212             fFieldTaskObjects.add(taskGroup);
213             taskGroup.parse(element);
214         }
215     }
216
217     /* (non-Javadoc)
218      * @see org.eclipse.pde.internal.core.cheatsheet.comp.CompCSTaskObject#writeElements(java.lang.String, java.io.PrintWriter)
219      */

220     protected void writeElements(String JavaDoc indent, PrintWriter JavaDoc writer) {
221         super.writeElements(indent, writer);
222         String JavaDoc newIndent = indent + XMLPrintHandler.XML_INDENT;
223         // Print dependency elements
224
Iterator JavaDoc iterator = fFieldTaskObjects.iterator();
225         while (iterator.hasNext()) {
226             ICompCSTaskObject taskObject = (ICompCSTaskObject)iterator.next();
227             taskObject.write(newIndent, writer);
228         }
229     }
230
231     /* (non-Javadoc)
232      * @see org.eclipse.pde.internal.core.cheatsheet.comp.CompCSObject#getElement()
233      */

234     public String JavaDoc getElement() {
235         return ELEMENT_TASKGROUP;
236     }
237
238     /* (non-Javadoc)
239      * @see org.eclipse.pde.internal.core.icheatsheet.comp.ICompCSTaskGroup#getNextSibling(org.eclipse.pde.internal.core.icheatsheet.comp.ICompCSTaskObject)
240      */

241     public ICompCSTaskObject getNextSibling(ICompCSTaskObject taskObject) {
242         int position = fFieldTaskObjects.indexOf(taskObject);
243         int lastIndex = fFieldTaskObjects.size() - 1;
244         if ((position == -1) ||
245                 (position == lastIndex)) {
246             // Either the item was not found or the item was found but it is
247
// at the last index
248
return null;
249         }
250         return (ICompCSTaskObject)fFieldTaskObjects.get(position + 1);
251     }
252
253     /* (non-Javadoc)
254      * @see org.eclipse.pde.internal.core.icheatsheet.comp.ICompCSTaskGroup#getPreviousSibling(org.eclipse.pde.internal.core.icheatsheet.comp.ICompCSTaskObject)
255      */

256     public ICompCSTaskObject getPreviousSibling(ICompCSTaskObject taskObject) {
257         int position = fFieldTaskObjects.indexOf(taskObject);
258         if ((position == -1) ||
259                 (position == 0)) {
260             // Either the item was not found or the item was found but it is
261
// at the first index
262
return null;
263         }
264         return (ICompCSTaskObject)fFieldTaskObjects.get(position - 1);
265     }
266
267     /* (non-Javadoc)
268      * @see org.eclipse.pde.internal.core.icheatsheet.comp.ICompCSTaskGroup#moveFieldTaskObject(org.eclipse.pde.internal.core.icheatsheet.comp.ICompCSTaskObject, int)
269      */

270     public void moveFieldTaskObject(ICompCSTaskObject taskObject,
271             int newRelativeIndex) {
272         // Get the current index of the task object
273
int currentIndex = indexOfFieldTaskObject(taskObject);
274         // Ensure the object is found
275
if (currentIndex == -1) {
276             return;
277         }
278         // Calculate the new index
279
int newIndex = newRelativeIndex + currentIndex;
280         // Validate the new index
281
if ((newIndex < 0) ||
282                 (newIndex >= fFieldTaskObjects.size())) {
283             return;
284         }
285         // Remove the task object
286
fFieldTaskObjects.remove(taskObject);
287         // Add the task object back at the specified index
288
fFieldTaskObjects.add(newIndex, taskObject);
289         // Send an insert event
290
if (isEditable()) {
291             fireStructureChanged(taskObject, IModelChangedEvent.INSERT);
292         }
293     }
294     
295 }
296
Popular Tags