KickJava   Java API By Example, From Geeks To Geeks.

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


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.ISimpleCSConditionalSubItem;
23 import org.eclipse.pde.internal.core.icheatsheet.simple.ISimpleCSModel;
24 import org.eclipse.pde.internal.core.icheatsheet.simple.ISimpleCSModelFactory;
25 import org.eclipse.pde.internal.core.icheatsheet.simple.ISimpleCSObject;
26 import org.eclipse.pde.internal.core.icheatsheet.simple.ISimpleCSSubItem;
27 import org.eclipse.pde.internal.core.icheatsheet.simple.ISimpleCSSubItemObject;
28 import org.w3c.dom.Element JavaDoc;
29 import org.w3c.dom.Node JavaDoc;
30 import org.w3c.dom.NodeList JavaDoc;
31
32 /**
33  * SimpleCSConditionalSubItem
34  *
35  */

36 public class SimpleCSConditionalSubItem extends SimpleCSObject implements
37         ISimpleCSConditionalSubItem {
38
39     /**
40      * Attribute: condition
41      */

42     private String JavaDoc fCondition;
43
44     /**
45      * Elements: subitem
46      */

47     private ArrayList JavaDoc fSubItems;
48     
49     /**
50      *
51      */

52     private static final long serialVersionUID = 1L;
53
54     /**
55      * @param model
56      * @param parent
57      */

58     public SimpleCSConditionalSubItem(ISimpleCSModel model, ISimpleCSObject parent) {
59         super(model, parent);
60         reset();
61     }
62
63     /* (non-Javadoc)
64      * @see org.eclipse.pde.internal.core.icheatsheet.simple.ISimpleCSConditionalSubItem#getCondition()
65      */

66     public String JavaDoc getCondition() {
67         return fCondition;
68     }
69
70     /* (non-Javadoc)
71      * @see org.eclipse.pde.internal.core.icheatsheet.simple.ISimpleCSConditionalSubItem#getSubItems()
72      */

73     public ISimpleCSSubItem[] getSubItems() {
74         return (ISimpleCSSubItem[]) fSubItems.toArray(
75                 new ISimpleCSSubItemObject[fSubItems.size()]);
76     }
77
78     /* (non-Javadoc)
79      * @see org.eclipse.pde.internal.core.icheatsheet.simple.ISimpleCSConditionalSubItem#setCondition(java.lang.String)
80      */

81     public void setCondition(String JavaDoc condition) {
82         String JavaDoc old = fCondition;
83         fCondition = condition;
84         if (isEditable()) {
85             firePropertyChanged(ATTRIBUTE_CONDITION, old, fCondition);
86         }
87     }
88
89     /* (non-Javadoc)
90      * @see org.eclipse.pde.internal.core.icheatsheet.simple.ISimpleCSObject#parse(org.w3c.dom.Element)
91      */

92     public void parse(Element JavaDoc element) {
93
94         // Process condition attribute
95
// Read as is. Do not translate
96
fCondition = element.getAttribute(ATTRIBUTE_CONDITION);
97         
98         // Process children
99

100         NodeList JavaDoc children = element.getChildNodes();
101         ISimpleCSModelFactory factory = getModel().getFactory();
102         for (int i = 0; i < children.getLength(); i++) {
103             Node JavaDoc child = children.item(i);
104             if (child.getNodeType() == Node.ELEMENT_NODE) {
105                 String JavaDoc name = child.getNodeName();
106                 Element JavaDoc childElement = (Element JavaDoc)child;
107
108                 if (name.equals(ELEMENT_SUBITEM)) {
109                     ISimpleCSSubItem subitem = factory.createSimpleCSSubItem(this);
110                     fSubItems.add(subitem);
111                     subitem.parse(childElement);
112                 }
113             }
114         }
115         
116     }
117
118     /* (non-Javadoc)
119      * @see org.eclipse.pde.core.IWritable#write(java.lang.String, java.io.PrintWriter)
120      */

121     public void write(String JavaDoc indent, PrintWriter JavaDoc writer) {
122
123         StringBuffer JavaDoc buffer = new StringBuffer JavaDoc();
124         String JavaDoc newIndent = indent + XMLPrintHandler.XML_INDENT;
125
126         try {
127             // Print conditional-subitem element
128
buffer.append(ELEMENT_CONDITIONAL_SUBITEM);
129             // Print condition attribute
130
// Write as is. Do not translate
131
if ((fCondition != null) &&
132                     (fCondition.length() > 0)) {
133                 buffer.append(XMLPrintHandler.wrapAttribute(
134                         ATTRIBUTE_CONDITION, fCondition));
135             }
136             // Start element
137
XMLPrintHandler.printBeginElement(writer, buffer.toString(),
138                     indent, false);
139             // Print subitems
140
Iterator JavaDoc iterator = fSubItems.iterator();
141             while (iterator.hasNext()) {
142                 ISimpleCSSubItem subitem = (ISimpleCSSubItem)iterator.next();
143                 subitem.write(newIndent, writer);
144             }
145             // End element
146
XMLPrintHandler.printEndElement(writer, ELEMENT_CONDITIONAL_SUBITEM, indent);
147             
148         } catch (IOException JavaDoc e) {
149             // Suppress
150
//e.printStackTrace();
151
}
152         
153     }
154
155     /* (non-Javadoc)
156      * @see org.eclipse.pde.internal.core.icheatsheet.simple.ISimpleCSObject#reset()
157      */

158     public void reset() {
159         fCondition = null;
160         fSubItems = new ArrayList JavaDoc();
161     }
162
163     /* (non-Javadoc)
164      * @see org.eclipse.pde.internal.core.icheatsheet.simple.ISimpleCSObject#getType()
165      */

166     public int getType() {
167         return TYPE_CONDITIONAL_SUBITEM;
168     }
169
170     /* (non-Javadoc)
171      * @see org.eclipse.pde.internal.core.cheatsheet.simple.SimpleCSObject#getName()
172      */

173     public String JavaDoc getName() {
174         // Leave as is. Not supported in editor UI
175
return ELEMENT_CONDITIONAL_SUBITEM;
176     }
177
178     /* (non-Javadoc)
179      * @see org.eclipse.pde.internal.core.cheatsheet.simple.SimpleCSObject#getChildren()
180      */

181     public List JavaDoc getChildren() {
182         ArrayList JavaDoc list = new ArrayList JavaDoc();
183         // Add subitems
184
if (fSubItems.size() > 0) {
185             list.addAll(fSubItems);
186         }
187         return list;
188     }
189
190     /* (non-Javadoc)
191      * @see org.eclipse.pde.internal.core.icheatsheet.simple.ISimpleCSConditionalSubItem#addSubItem(org.eclipse.pde.internal.core.icheatsheet.simple.ISimpleCSSubItem)
192      */

193     public void addSubItem(ISimpleCSSubItem subitem) {
194         fSubItems.add(subitem);
195         
196         if (isEditable()) {
197             fireStructureChanged(subitem, IModelChangedEvent.INSERT);
198         }
199     }
200
201     /* (non-Javadoc)
202      * @see org.eclipse.pde.internal.core.icheatsheet.simple.ISimpleCSConditionalSubItem#removeSubItem(org.eclipse.pde.internal.core.icheatsheet.simple.ISimpleCSSubItem)
203      */

204     public void removeSubItem(ISimpleCSSubItem subitem) {
205         fSubItems.remove(subitem);
206         
207         if (isEditable()) {
208             fireStructureChanged(subitem, IModelChangedEvent.REMOVE);
209         }
210     }
211
212 }
213
Popular Tags