KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > pde > internal > core > cheatsheet > simple > SimpleCS


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.simple;
13
14 import java.io.IOException JavaDoc;
15 import java.io.PrintWriter JavaDoc;
16 import java.util.ArrayList JavaDoc;
17 import java.util.Iterator JavaDoc;
18 import java.util.List JavaDoc;
19
20 import org.eclipse.pde.core.IModelChangedEvent;
21 import org.eclipse.pde.internal.core.XMLPrintHandler;
22 import org.eclipse.pde.internal.core.icheatsheet.simple.ISimpleCS;
23 import org.eclipse.pde.internal.core.icheatsheet.simple.ISimpleCSIntro;
24 import org.eclipse.pde.internal.core.icheatsheet.simple.ISimpleCSItem;
25 import org.eclipse.pde.internal.core.icheatsheet.simple.ISimpleCSModel;
26 import org.eclipse.pde.internal.core.icheatsheet.simple.ISimpleCSModelFactory;
27 import org.eclipse.pde.internal.core.icheatsheet.simple.ISimpleCSObject;
28 import org.eclipse.pde.internal.core.util.PDETextHelper;
29 import org.w3c.dom.Element JavaDoc;
30 import org.w3c.dom.Node JavaDoc;
31 import org.w3c.dom.NodeList JavaDoc;
32
33 /**
34  * SimpleCS
35  *
36  */

37 public class SimpleCS extends SimpleCSObject implements ISimpleCS {
38
39     /**
40      * Element: intro
41      */

42     private ISimpleCSIntro fIntro;
43     
44     /**
45      * Attribute: title
46      */

47     private String JavaDoc fTitle;
48     
49     /**
50      * Element: item
51      */

52     private ArrayList JavaDoc fItems;
53     
54     /**
55      *
56      */

57     private static final long serialVersionUID = 1L;
58
59     /**
60      * @param model
61      */

62     public SimpleCS(ISimpleCSModel model) {
63         super(model, null);
64         reset();
65     }
66
67     /* (non-Javadoc)
68      * @see org.eclipse.pde.internal.core.icheatsheet.simple.ISimpleCS#getIntro()
69      */

70     public ISimpleCSIntro getIntro() {
71         return fIntro;
72     }
73
74     /* (non-Javadoc)
75      * @see org.eclipse.pde.internal.core.icheatsheet.simple.ISimpleCS#getItems()
76      */

77     public ISimpleCSItem[] getItems() {
78         return (ISimpleCSItem[])fItems.toArray(new ISimpleCSItem[fItems.size()]);
79     }
80
81     /* (non-Javadoc)
82      * @see org.eclipse.pde.internal.core.icheatsheet.simple.ISimpleCS#getTitle()
83      */

84     public String JavaDoc getTitle() {
85         return fTitle;
86     }
87
88     /* (non-Javadoc)
89      * @see org.eclipse.pde.internal.core.icheatsheet.simple.ISimpleCSObject#reset()
90      */

91     public void reset() {
92         fIntro = null;
93         fTitle = null;
94         fItems = new ArrayList JavaDoc();
95     }
96
97     /* (non-Javadoc)
98      * @see org.eclipse.pde.internal.core.icheatsheet.simple.ISimpleCS#setIntro(org.eclipse.pde.internal.core.icheatsheet.simple.ISimpleCSIntro)
99      */

100     public void setIntro(ISimpleCSIntro intro) {
101         ISimpleCSObject old = fIntro;
102         fIntro = intro;
103
104         if (isEditable()) {
105             fireStructureChanged(intro, old);
106         }
107     }
108
109     /* (non-Javadoc)
110      * @see org.eclipse.pde.internal.core.icheatsheet.simple.ISimpleCS#setTitle(java.lang.String)
111      */

112     public void setTitle(String JavaDoc title) {
113         String JavaDoc old = fTitle;
114         fTitle = title;
115         if (isEditable()) {
116             firePropertyChanged(ATTRIBUTE_TITLE, old, fTitle);
117         }
118     }
119
120     /* (non-Javadoc)
121      * @see org.eclipse.pde.internal.core.icheatsheet.simple.ISimpleCSObject#parse(org.w3c.dom.Node)
122      */

123     public void parse(Element JavaDoc element) {
124         // Process cheatsheet element
125
if (element.getNodeName().equals(ELEMENT_CHEATSHEET)) {
126             // Process title attribute
127
// Trim leading and trailing whitespace
128
fTitle = element.getAttribute(ATTRIBUTE_TITLE).trim();
129             // Process children
130
NodeList JavaDoc children = element.getChildNodes();
131             ISimpleCSModelFactory factory = getModel().getFactory();
132             for (int i = 0; i < children.getLength(); i++) {
133                 Node JavaDoc child = children.item(i);
134                 if (child.getNodeType() == Node.ELEMENT_NODE) {
135                     String JavaDoc name = child.getNodeName();
136                     Element JavaDoc childElement = (Element JavaDoc)child;
137                     if (name.equals(ELEMENT_INTRO)) {
138                         fIntro = factory.createSimpleCSIntro(this);
139                         fIntro.parse(childElement);
140                     } else if (name.equals(ELEMENT_ITEM)) {
141                         ISimpleCSItem item = factory.createSimpleCSItem(this);
142                         fItems.add(item);
143                         item.parse(childElement);
144                     }
145                 }
146             }
147         }
148         
149     }
150     
151     /* (non-Javadoc)
152      * @see org.eclipse.pde.core.IWritable#write(java.lang.String, java.io.PrintWriter)
153      */

154     public void write(String JavaDoc indent, PrintWriter JavaDoc writer) {
155
156         StringBuffer JavaDoc buffer = new StringBuffer JavaDoc();
157         String JavaDoc newIndent = indent + XMLPrintHandler.XML_INDENT;
158         
159         try {
160             // Print XML decl
161
XMLPrintHandler.printHead(writer, ATTRIBUTE_VALUE_ENCODING);
162             // Print cheatsheet element
163
buffer.append(ELEMENT_CHEATSHEET);
164             // Print title attribute
165
if ((fTitle != null) &&
166                     (fTitle.length() > 0)) {
167                 // Trim leading and trailing whitespace
168
// Encode characters
169
buffer.append(XMLPrintHandler.wrapAttribute(
170                         ATTRIBUTE_TITLE,
171                         PDETextHelper.translateWriteText(
172                                 fTitle.trim(), SUBSTITUTE_CHARS)));
173             }
174             // Start element
175
XMLPrintHandler.printBeginElement(writer, buffer.toString(),
176                     indent, false);
177             // Print intro element
178
if (fIntro != null) {
179                 fIntro.write(newIndent, writer);
180             }
181             // Print item elements
182
Iterator JavaDoc iterator = fItems.iterator();
183             while (iterator.hasNext()) {
184                 ISimpleCSItem item = (ISimpleCSItem)iterator.next();
185                 item.write(newIndent, writer);
186             }
187             // End element
188
XMLPrintHandler.printEndElement(writer, ELEMENT_CHEATSHEET, indent);
189             
190         } catch (IOException JavaDoc e) {
191             // Suppress
192
//e.printStackTrace();
193
}
194     }
195
196     /* (non-Javadoc)
197      * @see org.eclipse.pde.internal.core.icheatsheet.simple.ISimpleCS#addItem(org.eclipse.pde.internal.core.icheatsheet.simple.ISimpleCSItem)
198      */

199     public void addItem(ISimpleCSItem item) {
200         fItems.add(item);
201         
202         if (isEditable()) {
203             fireStructureChanged(item, IModelChangedEvent.INSERT);
204         }
205     }
206
207     /* (non-Javadoc)
208      * @see org.eclipse.pde.internal.core.icheatsheet.simple.ISimpleCS#removeItem(org.eclipse.pde.internal.core.icheatsheet.simple.ISimpleCSItem)
209      */

210     public void removeItem(ISimpleCSItem item) {
211         fItems.remove(item);
212         
213         if (isEditable()) {
214             fireStructureChanged(item, IModelChangedEvent.REMOVE);
215         }
216     }
217
218     /* (non-Javadoc)
219      * @see org.eclipse.pde.internal.core.icheatsheet.simple.ISimpleCSObject#getType()
220      */

221     public int getType() {
222         return TYPE_CHEAT_SHEET;
223     }
224
225     /* (non-Javadoc)
226      * @see org.eclipse.pde.internal.core.cheatsheet.simple.SimpleCSObject#getName()
227      */

228     public String JavaDoc getName() {
229         return fTitle;
230     }
231
232     /* (non-Javadoc)
233      * @see org.eclipse.pde.internal.core.cheatsheet.simple.SimpleCSObject#getChildren()
234      */

235     public List JavaDoc getChildren() {
236         ArrayList JavaDoc list = new ArrayList JavaDoc();
237         // Add intro
238
if (fIntro != null) {
239             list.add(fIntro);
240         }
241         // Add items
242
if (fItems.size() > 0) {
243             list.addAll(fItems);
244         }
245         return list;
246     }
247
248     /* (non-Javadoc)
249      * @see org.eclipse.pde.internal.core.icheatsheet.simple.ISimpleCS#isFirstItem(org.eclipse.pde.internal.core.icheatsheet.simple.ISimpleCSItem)
250      */

251     public boolean isFirstItem(ISimpleCSItem item) {
252         int position = fItems.indexOf(item);
253         if (position == 0) {
254             return true;
255         }
256         return false;
257     }
258
259     /* (non-Javadoc)
260      * @see org.eclipse.pde.internal.core.icheatsheet.simple.ISimpleCS#isLastItem(org.eclipse.pde.internal.core.icheatsheet.simple.ISimpleCSItem)
261      */

262     public boolean isLastItem(ISimpleCSItem item) {
263         int position = fItems.indexOf(item);
264         int lastPosition = fItems.size() - 1;
265         if (position == lastPosition) {
266             return true;
267         }
268         return false;
269     }
270
271     /* (non-Javadoc)
272      * @see org.eclipse.pde.internal.core.icheatsheet.simple.ISimpleCS#addItem(int, org.eclipse.pde.internal.core.icheatsheet.simple.ISimpleCSItem)
273      */

274     public void addItem(int index, ISimpleCSItem item) {
275         
276         if (index < 0) {
277             return;
278         }
279         if (index >= fItems.size()) {
280             fItems.add(item);
281         } else {
282             fItems.add(index, item);
283         }
284         
285         if (isEditable()) {
286             fireStructureChanged(item, IModelChangedEvent.INSERT);
287         }
288         
289     }
290
291     /* (non-Javadoc)
292      * @see org.eclipse.pde.internal.core.icheatsheet.simple.ISimpleCS#indexOfItem(org.eclipse.pde.internal.core.icheatsheet.simple.ISimpleCSItem)
293      */

294     public int indexOfItem(ISimpleCSItem item) {
295         return fItems.indexOf(item);
296     }
297
298     /* (non-Javadoc)
299      * @see org.eclipse.pde.internal.core.icheatsheet.simple.ISimpleCS#removeItem(int, org.eclipse.pde.internal.core.icheatsheet.simple.ISimpleCSItem)
300      */

301     public void removeItem(int index) {
302         
303         if ((index < 0) ||
304                 (index > (fItems.size() - 1))) {
305             return;
306         }
307         
308         ISimpleCSItem item = (ISimpleCSItem)fItems.remove(index);
309         
310         if (isEditable()) {
311             fireStructureChanged(item, IModelChangedEvent.REMOVE);
312         }
313     }
314
315     /* (non-Javadoc)
316      * @see org.eclipse.pde.internal.core.icheatsheet.simple.ISimpleCS#getItemCount()
317      */

318     public int getItemCount() {
319         return fItems.size();
320     }
321
322     /* (non-Javadoc)
323      * @see org.eclipse.pde.internal.core.icheatsheet.simple.ISimpleCS#hasItems()
324      */

325     public boolean hasItems() {
326         if (fItems.isEmpty()) {
327             return false;
328         }
329         return true;
330     }
331
332     /* (non-Javadoc)
333      * @see org.eclipse.pde.internal.core.icheatsheet.simple.ISimpleCS#getNextSibling(org.eclipse.pde.internal.core.icheatsheet.simple.ISimpleCSItem)
334      */

335     public ISimpleCSItem getNextSibling(ISimpleCSItem item) {
336         int position = fItems.indexOf(item);
337         int lastIndex = fItems.size() - 1;
338         if ((position == -1) ||
339                 (position == lastIndex)) {
340             // Either the item was not found or the item was found but it is
341
// at the last index
342
return null;
343         }
344         return (ISimpleCSItem)fItems.get(position + 1);
345     }
346
347     /* (non-Javadoc)
348      * @see org.eclipse.pde.internal.core.icheatsheet.simple.ISimpleCS#getPreviousSibling(org.eclipse.pde.internal.core.icheatsheet.simple.ISimpleCSItem)
349      */

350     public ISimpleCSItem getPreviousSibling(ISimpleCSItem item) {
351         int position = fItems.indexOf(item);
352         if ((position == -1) ||
353                 (position == 0)) {
354             // Either the item was not found or the item was found but it is
355
// at the first index
356
return null;
357         }
358         return (ISimpleCSItem)fItems.get(position - 1);
359     }
360
361     /* (non-Javadoc)
362      * @see org.eclipse.pde.internal.core.icheatsheet.simple.ISimpleCS#moveItem(org.eclipse.pde.internal.core.icheatsheet.simple.ISimpleCSItem, int)
363      */

364     public void moveItem(ISimpleCSItem item, int newRelativeIndex) {
365         // Get the current index of the task object
366
int currentIndex = fItems.indexOf(item);
367         // Ensure the object is found
368
if (currentIndex == -1) {
369             return;
370         }
371         // Calculate the new index
372
int newIndex = newRelativeIndex + currentIndex;
373         // Validate the new index
374
if ((newIndex < 0) ||
375                 (newIndex >= fItems.size())) {
376             return;
377         }
378         // Remove the task object
379
fItems.remove(item);
380         // Add the task object back at the specified index
381
fItems.add(newIndex, item);
382         // Send an insert event
383
if (isEditable()) {
384             fireStructureChanged(item, IModelChangedEvent.INSERT);
385         }
386     }
387
388 }
389
Popular Tags