KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*******************************************************************************
2  * Copyright (c) 2006, 2007 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.ISimpleCSConditionalSubItem;
23 import org.eclipse.pde.internal.core.icheatsheet.simple.ISimpleCSConstants;
24 import org.eclipse.pde.internal.core.icheatsheet.simple.ISimpleCSDescription;
25 import org.eclipse.pde.internal.core.icheatsheet.simple.ISimpleCSItem;
26 import org.eclipse.pde.internal.core.icheatsheet.simple.ISimpleCSModel;
27 import org.eclipse.pde.internal.core.icheatsheet.simple.ISimpleCSModelFactory;
28 import org.eclipse.pde.internal.core.icheatsheet.simple.ISimpleCSObject;
29 import org.eclipse.pde.internal.core.icheatsheet.simple.ISimpleCSOnCompletion;
30 import org.eclipse.pde.internal.core.icheatsheet.simple.ISimpleCSRepeatedSubItem;
31 import org.eclipse.pde.internal.core.icheatsheet.simple.ISimpleCSRunContainerObject;
32 import org.eclipse.pde.internal.core.icheatsheet.simple.ISimpleCSSubItem;
33 import org.eclipse.pde.internal.core.icheatsheet.simple.ISimpleCSSubItemObject;
34 import org.eclipse.pde.internal.core.util.PDETextHelper;
35 import org.w3c.dom.Element JavaDoc;
36 import org.w3c.dom.Node JavaDoc;
37 import org.w3c.dom.NodeList JavaDoc;
38
39 /**
40  * SimpleCSItem
41  *
42  */

43 public class SimpleCSItem extends SimpleCSObject implements ISimpleCSItem {
44
45     
46     /**
47      * Element: onCompletion
48      */

49     private ISimpleCSOnCompletion fOnCompletion;
50     
51     /**
52      * Elements: action, command, perform-when
53      */

54     private ISimpleCSRunContainerObject fExecutable;
55     
56     /**
57      * Attribute: skip
58      */

59     private boolean fSkip;
60
61     /**
62      * Attribute: dialog
63      */

64     private boolean fDialog;
65     
66     /**
67      * Element: description
68      */

69     private ISimpleCSDescription fDescription;
70     
71     /**
72      * Attribute: title
73      */

74     private String JavaDoc fTitle;
75
76     /**
77      * Attribute: contextId
78      */

79     private String JavaDoc fContextId;
80
81     /**
82      * Attribute: href
83      */

84     private String JavaDoc fHref;
85     
86     /**
87      * Elements: subitem
88      */

89     private ArrayList JavaDoc fSubItems;
90     
91     /**
92      *
93      */

94     private static final long serialVersionUID = 1L;
95
96     /**
97      * @param model
98      * @param parent
99      */

100     public SimpleCSItem(ISimpleCSModel model, ISimpleCSObject parent) {
101         super(model, parent);
102         reset();
103     }
104
105     /* (non-Javadoc)
106      * @see org.eclipse.pde.internal.core.icheatsheet.simple.ISimpleCSItem#getContextId()
107      */

108     public String JavaDoc getContextId() {
109         return fContextId;
110     }
111
112     /* (non-Javadoc)
113      * @see org.eclipse.pde.internal.core.icheatsheet.simple.ISimpleCSItem#getDescription()
114      */

115     public ISimpleCSDescription getDescription() {
116         return fDescription;
117     }
118
119     /* (non-Javadoc)
120      * @see org.eclipse.pde.internal.core.icheatsheet.simple.ISimpleCSItem#getDialog()
121      */

122     public boolean getDialog() {
123         return fDialog;
124     }
125
126     /* (non-Javadoc)
127      * @see org.eclipse.pde.internal.core.icheatsheet.simple.ISimpleCSItem#getExecutable()
128      */

129     public ISimpleCSRunContainerObject getExecutable() {
130         return fExecutable;
131     }
132
133     /* (non-Javadoc)
134      * @see org.eclipse.pde.internal.core.icheatsheet.simple.ISimpleCSItem#getHref()
135      */

136     public String JavaDoc getHref() {
137         return fHref;
138     }
139
140     /* (non-Javadoc)
141      * @see org.eclipse.pde.internal.core.icheatsheet.simple.ISimpleCSItem#getSkip()
142      */

143     public boolean getSkip() {
144         return fSkip;
145     }
146
147     /* (non-Javadoc)
148      * @see org.eclipse.pde.internal.core.icheatsheet.simple.ISimpleCSItem#getSubItems()
149      */

150     public ISimpleCSSubItemObject[] getSubItems() {
151         return (ISimpleCSSubItemObject[]) fSubItems
152                 .toArray(new ISimpleCSSubItemObject[fSubItems.size()]);
153     }
154
155     /* (non-Javadoc)
156      * @see org.eclipse.pde.internal.core.icheatsheet.simple.ISimpleCSItem#getTitle()
157      */

158     public String JavaDoc getTitle() {
159         return fTitle;
160     }
161
162     /* (non-Javadoc)
163      * @see org.eclipse.pde.internal.core.icheatsheet.simple.ISimpleCSItem#setContextId(java.lang.String)
164      */

165     public void setContextId(String JavaDoc contextId) {
166         String JavaDoc old = fContextId;
167         fContextId = contextId;
168         if (isEditable()) {
169             firePropertyChanged(ATTRIBUTE_CONTEXTID, old, fContextId);
170         }
171     }
172
173     /* (non-Javadoc)
174      * @see org.eclipse.pde.internal.core.icheatsheet.simple.ISimpleCSItem#setDescription(java.lang.String)
175      */

176     public void setDescription(ISimpleCSDescription description) {
177         ISimpleCSObject old = fDescription;
178         fDescription = description;
179
180         if (isEditable()) {
181             fireStructureChanged(description, old);
182         }
183     }
184
185     /* (non-Javadoc)
186      * @see org.eclipse.pde.internal.core.icheatsheet.simple.ISimpleCSItem#setDialog(boolean)
187      */

188     public void setDialog(boolean dialog) {
189         Boolean JavaDoc old = Boolean.valueOf(fDialog);
190         fDialog = dialog;
191         if (isEditable()) {
192             firePropertyChanged(ATTRIBUTE_DIALOG, old, Boolean.valueOf(fDialog));
193         }
194     }
195
196     /* (non-Javadoc)
197      * @see org.eclipse.pde.internal.core.icheatsheet.simple.ISimpleCSItem#setExecutable(org.eclipse.pde.internal.core.icheatsheet.simple.ISimpleCSRunContainerObject)
198      */

199     public void setExecutable(ISimpleCSRunContainerObject executable) {
200         ISimpleCSObject old = fExecutable;
201         fExecutable = executable;
202
203         if (isEditable()) {
204             fireStructureChanged(executable, old);
205         }
206     }
207
208     /* (non-Javadoc)
209      * @see org.eclipse.pde.internal.core.icheatsheet.simple.ISimpleCSItem#setHref(java.lang.String)
210      */

211     public void setHref(String JavaDoc href) {
212         String JavaDoc old = fHref;
213         fHref = href;
214         if (isEditable()) {
215             firePropertyChanged(ATTRIBUTE_HREF, old, fHref);
216         }
217     }
218
219     /* (non-Javadoc)
220      * @see org.eclipse.pde.internal.core.icheatsheet.simple.ISimpleCSItem#setSkip(boolean)
221      */

222     public void setSkip(boolean skip) {
223         Boolean JavaDoc old = Boolean.valueOf(fSkip);
224         fSkip = skip;
225         if (isEditable()) {
226             firePropertyChanged(ATTRIBUTE_SKIP, old, Boolean.valueOf(fSkip));
227         }
228     }
229
230     /* (non-Javadoc)
231      * @see org.eclipse.pde.internal.core.icheatsheet.simple.ISimpleCSItem#setTitle(java.lang.String)
232      */

233     public void setTitle(String JavaDoc title) {
234         String JavaDoc old = fTitle;
235         fTitle = title;
236         if (isEditable()) {
237             firePropertyChanged(ATTRIBUTE_TITLE, old, fTitle);
238         }
239     }
240
241     /* (non-Javadoc)
242      * @see org.eclipse.pde.internal.core.icheatsheet.simple.ISimpleCSObject#parse(org.w3c.dom.Element)
243      */

244     public void parse(Element JavaDoc element) {
245         // Process title attribute
246
fTitle = element.getAttribute(ATTRIBUTE_TITLE).trim();
247         // Process dialog attribute
248
if (element.getAttribute(ATTRIBUTE_DIALOG).compareTo(
249                 ATTRIBUTE_VALUE_FALSE) == 0) {
250             fDialog = false;
251         }
252         // Process skip attribute
253
if (element.getAttribute(ATTRIBUTE_SKIP).compareTo(
254                 ATTRIBUTE_VALUE_TRUE) == 0) {
255             fSkip = true;
256         }
257         // Process contextId attribute
258
fContextId = element.getAttribute(ATTRIBUTE_CONTEXTID).trim();
259         // Process href attribute
260
fHref = element.getAttribute(ATTRIBUTE_HREF).trim();
261         
262         // Process children
263

264         NodeList JavaDoc children = element.getChildNodes();
265         ISimpleCSModelFactory factory = getModel().getFactory();
266         for (int i = 0; i < children.getLength(); i++) {
267             Node JavaDoc child = children.item(i);
268             if (child.getNodeType() == Node.ELEMENT_NODE) {
269                 String JavaDoc name = child.getNodeName();
270                 Element JavaDoc childElement = (Element JavaDoc)child;
271
272                 if (name.equals(ELEMENT_DESCRIPTION)) {
273                     fDescription = factory.createSimpleCSDescription(this);
274                     fDescription.parse(childElement);
275                 } else if (name.equals(ELEMENT_ACTION)) {
276                     fExecutable = factory.createSimpleCSAction(this);
277                     fExecutable.parse(childElement);
278                 } else if (name.equals(ELEMENT_COMMAND)) {
279                     fExecutable = factory.createSimpleCSCommand(this);
280                     fExecutable.parse(childElement);
281                 } else if (name.equals(ELEMENT_PERFORM_WHEN)) {
282                     fExecutable = factory.createSimpleCSPerformWhen(this);
283                     fExecutable.parse(childElement);
284                 } else if (name.equals(ELEMENT_SUBITEM)) {
285                     ISimpleCSSubItem subitem = factory.createSimpleCSSubItem(this);
286                     fSubItems.add(subitem);
287                     subitem.parse(childElement);
288                 } else if (name.equals(ELEMENT_REPEATED_SUBITEM)) {
289                     ISimpleCSRepeatedSubItem subitem = factory.createSimpleCSRepeatedSubItem(this);
290                     fSubItems.add(subitem);
291                     subitem.parse(childElement);
292                 } else if (name.equals(ELEMENT_CONDITIONAL_SUBITEM)) {
293                     ISimpleCSConditionalSubItem subitem = factory.createSimpleCSConditionalSubItem(this);
294                     fSubItems.add(subitem);
295                     subitem.parse(childElement);
296                 } else if (name.equals(ELEMENT_ONCOMPLETION)) {
297                     fOnCompletion = factory.createSimpleCSOnCompletion(this);
298                     fOnCompletion.parse(childElement);
299                 }
300             }
301         }
302     }
303
304     /* (non-Javadoc)
305      * @see org.eclipse.pde.core.IWritable#write(java.lang.String, java.io.PrintWriter)
306      */

307     public void write(String JavaDoc indent, PrintWriter JavaDoc writer) {
308
309         StringBuffer JavaDoc buffer = new StringBuffer JavaDoc();
310         String JavaDoc newIndent = indent + XMLPrintHandler.XML_INDENT;
311
312         try {
313             // Print item element
314
buffer.append(ELEMENT_ITEM); //$NON-NLS-1$
315
// Print title attribute
316
if ((fTitle != null) &&
317                     (fTitle.length() > 0)) {
318                 buffer.append(XMLPrintHandler.wrapAttribute(
319                         ATTRIBUTE_TITLE,
320                         PDETextHelper.translateWriteText(
321                                 fTitle.trim(), SUBSTITUTE_CHARS)));
322             }
323             // Print dialog attribute
324
buffer.append(XMLPrintHandler.wrapAttribute(
325                     ATTRIBUTE_DIALOG, new Boolean JavaDoc(fDialog).toString()));
326             // Print skip attribute
327
buffer.append(XMLPrintHandler.wrapAttribute(
328                     ATTRIBUTE_SKIP, new Boolean JavaDoc(fSkip).toString()));
329             // Print contextId attribute
330
// Print href attribute
331
if ((fContextId != null) &&
332                     (fContextId.length() > 0)) {
333                 buffer.append(XMLPrintHandler.wrapAttribute(
334                         ATTRIBUTE_CONTEXTID,
335                         PDETextHelper.translateWriteText(
336                                 fContextId.trim(), SUBSTITUTE_CHARS)));
337             } else if ((fHref != null) &&
338                             (fHref.length() > 0)) {
339                 buffer.append(XMLPrintHandler.wrapAttribute(
340                         ATTRIBUTE_HREF,
341                         PDETextHelper.translateWriteText(
342                                 fHref.trim(), SUBSTITUTE_CHARS)));
343             }
344             // Start element
345
XMLPrintHandler.printBeginElement(writer, buffer.toString(),
346                     indent, false);
347             // Print description element
348
if (fDescription != null) {
349                 fDescription.write(newIndent, writer);
350             }
351             // Print action | command | perform-when element
352
if (fExecutable != null) {
353                 fExecutable.write(newIndent, writer);
354             }
355             // Print subitem | repeated-subitem | conditional-subitem elements
356
Iterator JavaDoc iterator = fSubItems.iterator();
357             while (iterator.hasNext()) {
358                 ISimpleCSSubItemObject subitem = (ISimpleCSSubItemObject)iterator.next();
359                 subitem.write(newIndent, writer);
360             }
361             // Print onCompletion element
362
if (fOnCompletion != null) {
363                 fOnCompletion.write(newIndent, writer);
364             }
365             // End element
366
XMLPrintHandler.printEndElement(writer, ELEMENT_ITEM, indent);
367             
368         } catch (IOException JavaDoc e) {
369             // Suppress
370
//e.printStackTrace();
371
}
372         
373     }
374
375     /* (non-Javadoc)
376      * @see org.eclipse.pde.internal.core.icheatsheet.simple.ISimpleCSObject#reset()
377      */

378     public void reset() {
379         fOnCompletion = null;
380         fExecutable = null;
381         fSkip = false;
382         fDialog = true;
383         fDescription = null;
384         fTitle = null;
385         fContextId = null;
386         fHref = null;
387         fSubItems = new ArrayList JavaDoc();
388     }
389
390     /* (non-Javadoc)
391      * @see org.eclipse.pde.internal.core.icheatsheet.simple.ISimpleCSItem#addSubItem(org.eclipse.pde.internal.core.icheatsheet.simple.ISimpleCSSubItemObject)
392      */

393     public void addSubItem(ISimpleCSSubItemObject subitem) {
394         fSubItems.add(subitem);
395         
396         if (isEditable()) {
397             fireStructureChanged(subitem, IModelChangedEvent.INSERT);
398         }
399     }
400
401     /* (non-Javadoc)
402      * @see org.eclipse.pde.internal.core.icheatsheet.simple.ISimpleCSItem#getOnCompletion()
403      */

404     public ISimpleCSOnCompletion getOnCompletion() {
405         return fOnCompletion;
406     }
407
408     /* (non-Javadoc)
409      * @see org.eclipse.pde.internal.core.icheatsheet.simple.ISimpleCSItem#removeSubItem(org.eclipse.pde.internal.core.icheatsheet.simple.ISimpleCSSubItemObject)
410      */

411     public void removeSubItem(ISimpleCSSubItemObject subitem) {
412         fSubItems.remove(subitem);
413         
414         if (isEditable()) {
415             fireStructureChanged(subitem, IModelChangedEvent.REMOVE);
416         }
417     }
418
419     /* (non-Javadoc)
420      * @see org.eclipse.pde.internal.core.icheatsheet.simple.ISimpleCSItem#setOnCompletion(org.eclipse.pde.internal.core.icheatsheet.simple.ISimpleCSOnCompletion)
421      */

422     public void setOnCompletion(ISimpleCSOnCompletion onCompletion) {
423         ISimpleCSObject old = fOnCompletion;
424         fOnCompletion = onCompletion;
425
426         if (isEditable()) {
427             fireStructureChanged(onCompletion, old);
428         }
429     }
430
431     /* (non-Javadoc)
432      * @see org.eclipse.pde.internal.core.icheatsheet.simple.ISimpleCSObject#getType()
433      */

434     public int getType() {
435         return TYPE_ITEM;
436     }
437
438     /* (non-Javadoc)
439      * @see org.eclipse.pde.internal.core.cheatsheet.simple.SimpleCSObject#getName()
440      */

441     public String JavaDoc getName() {
442         return fTitle;
443     }
444
445     /* (non-Javadoc)
446      * @see org.eclipse.pde.internal.core.cheatsheet.simple.SimpleCSObject#getChildren()
447      */

448     public List JavaDoc getChildren() {
449         ArrayList JavaDoc list = new ArrayList JavaDoc();
450         // Add subitems
451
if (fSubItems.size() > 0) {
452             list.addAll(fSubItems);
453         }
454         // Add unsupported perform-when if it is set as the executable
455
if ((fExecutable != null) &&
456                 (fExecutable.getType() == ISimpleCSConstants.TYPE_PERFORM_WHEN)) {
457             list.add(fExecutable);
458         }
459         return list;
460     }
461
462     /* (non-Javadoc)
463      * @see org.eclipse.pde.internal.core.icheatsheet.simple.ISimpleCSItem#addSubItem(int, org.eclipse.pde.internal.core.icheatsheet.simple.ISimpleCSSubItemObject)
464      */

465     public void addSubItem(int index, ISimpleCSSubItemObject subitem) {
466         if (index < 0){
467             return;
468         }
469         if (index >= fSubItems.size()) {
470             fSubItems.add(subitem);
471         } else {
472             fSubItems.add(index, subitem);
473         }
474         
475         if (isEditable()) {
476             fireStructureChanged(subitem, IModelChangedEvent.INSERT);
477         }
478         
479     }
480
481     /* (non-Javadoc)
482      * @see org.eclipse.pde.internal.core.icheatsheet.simple.ISimpleCSItem#indexOfSubItem(org.eclipse.pde.internal.core.icheatsheet.simple.ISimpleCSSubItemObject)
483      */

484     public int indexOfSubItem(ISimpleCSSubItemObject subitem) {
485         return fSubItems.indexOf(subitem);
486     }
487
488     /* (non-Javadoc)
489      * @see org.eclipse.pde.internal.core.icheatsheet.simple.ISimpleCSItem#isFirstSubItem(org.eclipse.pde.internal.core.icheatsheet.simple.ISimpleCSSubItemObject)
490      */

491     public boolean isFirstSubItem(ISimpleCSSubItemObject subitem) {
492         int position = fSubItems.indexOf(subitem);
493         if (position == 0) {
494             return true;
495         }
496         return false;
497     }
498
499     /* (non-Javadoc)
500      * @see org.eclipse.pde.internal.core.icheatsheet.simple.ISimpleCSItem#isLastSubItem(org.eclipse.pde.internal.core.icheatsheet.simple.ISimpleCSSubItemObject)
501      */

502     public boolean isLastSubItem(ISimpleCSSubItemObject subitem) {
503         int position = fSubItems.indexOf(subitem);
504         int lastPosition = fSubItems.size() - 1;
505         if (position == lastPosition) {
506             return true;
507         }
508         return false;
509     }
510
511     /* (non-Javadoc)
512      * @see org.eclipse.pde.internal.core.icheatsheet.simple.ISimpleCSItem#removeSubItem(int)
513      */

514     public void removeSubItem(int index) {
515         
516         if ((index < 0) ||
517                 (index > (fSubItems.size() - 1))) {
518             return;
519         }
520         
521         ISimpleCSSubItemObject subitem = (ISimpleCSSubItemObject)fSubItems.remove(index);
522         
523         if (isEditable()) {
524             fireStructureChanged(subitem, IModelChangedEvent.REMOVE);
525         }
526     }
527
528     /* (non-Javadoc)
529      * @see org.eclipse.pde.internal.core.icheatsheet.simple.ISimpleCSItem#getSubItemCount()
530      */

531     public int getSubItemCount() {
532         return fSubItems.size();
533     }
534
535     /* (non-Javadoc)
536      * @see org.eclipse.pde.internal.core.icheatsheet.simple.ISimpleCSItem#hasSubItems()
537      */

538     public boolean hasSubItems() {
539         if (fSubItems.isEmpty()) {
540             return false;
541         }
542         return true;
543     }
544
545     /* (non-Javadoc)
546      * @see org.eclipse.pde.internal.core.icheatsheet.simple.ISimpleCSItem#getNextSibling(org.eclipse.pde.internal.core.icheatsheet.simple.ISimpleCSSubItemObject)
547      */

548     public ISimpleCSSubItemObject getNextSibling(ISimpleCSSubItemObject subitem) {
549         int position = fSubItems.indexOf(subitem);
550         int lastIndex = fSubItems.size() - 1;
551         if ((position == -1) ||
552                 (position == lastIndex)) {
553             // Either the subitem was not found or the subitem was found but it is
554
// at the last index
555
return null;
556         }
557         return (ISimpleCSSubItemObject)fSubItems.get(position + 1);
558     }
559
560     /* (non-Javadoc)
561      * @see org.eclipse.pde.internal.core.icheatsheet.simple.ISimpleCSItem#getPreviousSibling(org.eclipse.pde.internal.core.icheatsheet.simple.ISimpleCSSubItemObject)
562      */

563     public ISimpleCSSubItemObject getPreviousSibling(
564             ISimpleCSSubItemObject subitem) {
565         int position = fSubItems.indexOf(subitem);
566         if ((position == -1) ||
567                 (position == 0)) {
568             // Either the sub item was not found or the subitem was found but it is
569
// at the first index
570
return null;
571         }
572         return (ISimpleCSSubItemObject)fSubItems.get(position - 1);
573     }
574
575     /* (non-Javadoc)
576      * @see org.eclipse.pde.internal.core.icheatsheet.simple.ISimpleCSItem#moveSubItem(org.eclipse.pde.internal.core.icheatsheet.simple.ISimpleCSSubItemObject, int)
577      */

578     public void moveSubItem(ISimpleCSSubItemObject subitem, int newRelativeIndex) {
579         // Get the current index of the task object
580
int currentIndex = fSubItems.indexOf(subitem);
581         // Ensure the object is found
582
if (currentIndex == -1) {
583             return;
584         }
585         // Calculate the new index
586
int newIndex = newRelativeIndex + currentIndex;
587         // Validate the new index
588
if ((newIndex < 0) ||
589                 (newIndex >= fSubItems.size())) {
590             return;
591         }
592         // Remove the task object
593
fSubItems.remove(subitem);
594         // Add the task object back at the specified index
595
fSubItems.add(newIndex, subitem);
596         // Send an insert event
597
if (isEditable()) {
598             fireStructureChanged(subitem, IModelChangedEvent.INSERT);
599         }
600     }
601
602 }
603
Popular Tags