KickJava   Java API By Example, From Geeks To Geeks.

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


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.List JavaDoc;
18
19 import org.eclipse.pde.internal.core.XMLPrintHandler;
20 import org.eclipse.pde.internal.core.icheatsheet.simple.ISimpleCSModel;
21 import org.eclipse.pde.internal.core.icheatsheet.simple.ISimpleCSModelFactory;
22 import org.eclipse.pde.internal.core.icheatsheet.simple.ISimpleCSObject;
23 import org.eclipse.pde.internal.core.icheatsheet.simple.ISimpleCSRepeatedSubItem;
24 import org.eclipse.pde.internal.core.icheatsheet.simple.ISimpleCSSubItem;
25 import org.w3c.dom.Element JavaDoc;
26 import org.w3c.dom.Node JavaDoc;
27 import org.w3c.dom.NodeList JavaDoc;
28
29 /**
30  * SimpleCSRepeatedSubItem
31  *
32  */

33 public class SimpleCSRepeatedSubItem extends SimpleCSObject implements
34         ISimpleCSRepeatedSubItem {
35
36     /**
37      * Attribute: values
38      */

39     private String JavaDoc fValues;
40     
41     /**
42      * Element: subitem
43      */

44     private ISimpleCSSubItem fSubItem;
45     
46     /**
47      *
48      */

49     private static final long serialVersionUID = 1L;
50
51     /**
52      * @param model
53      * @param parent
54      */

55     public SimpleCSRepeatedSubItem(ISimpleCSModel model, ISimpleCSObject parent) {
56         super(model, parent);
57         reset();
58     }
59
60     /* (non-Javadoc)
61      * @see org.eclipse.pde.internal.core.icheatsheet.simple.ISimpleCSRepeatedSubItem#getSubItem()
62      */

63     public ISimpleCSSubItem getSubItem() {
64         return fSubItem;
65     }
66
67     /* (non-Javadoc)
68      * @see org.eclipse.pde.internal.core.icheatsheet.simple.ISimpleCSRepeatedSubItem#getValues()
69      */

70     public String JavaDoc getValues() {
71         return fValues;
72     }
73
74     /* (non-Javadoc)
75      * @see org.eclipse.pde.internal.core.icheatsheet.simple.ISimpleCSRepeatedSubItem#setSubItem(org.eclipse.pde.internal.core.icheatsheet.simple.ISimpleCSSubItem)
76      */

77     public void setSubItem(ISimpleCSSubItem subitem) {
78         ISimpleCSObject old = fSubItem;
79         fSubItem = subitem;
80
81         if (isEditable()) {
82             fireStructureChanged(subitem, old);
83         }
84     }
85
86     /* (non-Javadoc)
87      * @see org.eclipse.pde.internal.core.icheatsheet.simple.ISimpleCSRepeatedSubItem#setValues(java.lang.String)
88      */

89     public void setValues(String JavaDoc values) {
90         String JavaDoc old = fValues;
91         fValues = values;
92         if (isEditable()) {
93             firePropertyChanged(ATTRIBUTE_VALUES, old, fValues);
94         }
95     }
96
97     /* (non-Javadoc)
98      * @see org.eclipse.pde.internal.core.icheatsheet.simple.ISimpleCSObject#parse(org.w3c.dom.Element)
99      */

100     public void parse(Element JavaDoc element) {
101         // Process values attribute
102
// Read as is. Do not translate
103
fValues = element.getAttribute(ATTRIBUTE_VALUES);
104
105         // Process subitem element
106
NodeList JavaDoc children = element.getChildNodes();
107         ISimpleCSModelFactory factory = getModel().getFactory();
108         for (int i = 0; i < children.getLength(); i++) {
109             Node JavaDoc child = children.item(i);
110             if (child.getNodeType() == Node.ELEMENT_NODE) {
111                 String JavaDoc name = child.getNodeName();
112                 Element JavaDoc childElement = (Element JavaDoc)child;
113
114                 if (name.equals(ELEMENT_SUBITEM)) {
115                     fSubItem = factory.createSimpleCSSubItem(this);
116                     fSubItem.parse(childElement);
117                     // We are looking for only one subitem
118
break;
119                 }
120             }
121         }
122         
123     }
124
125     /* (non-Javadoc)
126      * @see org.eclipse.pde.core.IWritable#write(java.lang.String, java.io.PrintWriter)
127      */

128     public void write(String JavaDoc indent, PrintWriter JavaDoc writer) {
129
130         StringBuffer JavaDoc buffer = new StringBuffer JavaDoc();
131         String JavaDoc newIndent = indent + XMLPrintHandler.XML_INDENT;
132
133         try {
134             // Print repeated-subitem element
135
buffer.append(ELEMENT_REPEATED_SUBITEM);
136             // Print values attribute
137
if ((fValues != null) &&
138                     (fValues.length() > 0)) {
139                 // Write as is. Do not translate
140
buffer.append(XMLPrintHandler.wrapAttribute(
141                         ATTRIBUTE_VALUES, fValues));
142             }
143             // Start element
144
XMLPrintHandler.printBeginElement(writer, buffer.toString(),
145                     indent, false);
146             // Print subitem
147
if (fSubItem != null) {
148                 fSubItem.write(newIndent, writer);
149             }
150             // End element
151
XMLPrintHandler.printEndElement(writer, ELEMENT_REPEATED_SUBITEM, indent);
152             
153         } catch (IOException JavaDoc e) {
154             // Suppress
155
//e.printStackTrace();
156
}
157         
158     }
159
160     /* (non-Javadoc)
161      * @see org.eclipse.pde.internal.core.icheatsheet.simple.ISimpleCSObject#reset()
162      */

163     public void reset() {
164         fValues = null;
165         fSubItem = null;
166     }
167
168     /* (non-Javadoc)
169      * @see org.eclipse.pde.internal.core.icheatsheet.simple.ISimpleCSObject#getType()
170      */

171     public int getType() {
172         return TYPE_REPEATED_SUBITEM;
173     }
174
175     /* (non-Javadoc)
176      * @see org.eclipse.pde.internal.core.cheatsheet.simple.SimpleCSObject#getName()
177      */

178     public String JavaDoc getName() {
179         // Leave as is. Not supported in editor UI
180
return ELEMENT_REPEATED_SUBITEM;
181     }
182
183     /* (non-Javadoc)
184      * @see org.eclipse.pde.internal.core.cheatsheet.simple.SimpleCSObject#getChildren()
185      */

186     public List JavaDoc getChildren() {
187         ArrayList JavaDoc list = new ArrayList JavaDoc();
188         // Add subitem
189
if (fSubItem != null) {
190             list.add(fSubItem);
191         }
192         return list;
193     }
194
195 }
196
Popular Tags