KickJava   Java API By Example, From Geeks To Geeks.

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


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.HashMap JavaDoc;
18 import java.util.List JavaDoc;
19
20 import org.eclipse.pde.internal.core.XMLPrintHandler;
21 import org.eclipse.pde.internal.core.icheatsheet.simple.ISimpleCSConstants;
22 import org.eclipse.pde.internal.core.icheatsheet.simple.ISimpleCSModel;
23 import org.eclipse.pde.internal.core.icheatsheet.simple.ISimpleCSModelFactory;
24 import org.eclipse.pde.internal.core.icheatsheet.simple.ISimpleCSObject;
25 import org.eclipse.pde.internal.core.icheatsheet.simple.ISimpleCSRunContainerObject;
26 import org.eclipse.pde.internal.core.icheatsheet.simple.ISimpleCSSubItem;
27 import org.eclipse.pde.internal.core.util.PDETextHelper;
28 import org.w3c.dom.Element JavaDoc;
29 import org.w3c.dom.Node JavaDoc;
30 import org.w3c.dom.NodeList JavaDoc;
31
32 /**
33  * SimpleCSSubItem
34  *
35  */

36 public class SimpleCSSubItem extends SimpleCSObject implements ISimpleCSSubItem {
37
38     
39     /**
40      * Attribute: label
41      */

42     private String JavaDoc fLabel;
43     
44     /**
45      * Attribute: skip
46      */

47     private boolean fSkip;
48     
49     /**
50      * Attribute: when
51      */

52     private String JavaDoc fWhen;
53     
54     /**
55      * Elements: action, command, perform-when
56      */

57     private ISimpleCSRunContainerObject fExecutable;
58     
59     /**
60      *
61      */

62     private static final long serialVersionUID = 1L;
63
64     private static final HashMap JavaDoc LABEL_SUBSTITUTE_CHARS = new HashMap JavaDoc(7);
65     
66     static {
67         LABEL_SUBSTITUTE_CHARS.putAll(SUBSTITUTE_CHARS);
68         LABEL_SUBSTITUTE_CHARS.put(new Character JavaDoc('\n'), " "); //$NON-NLS-1$
69
LABEL_SUBSTITUTE_CHARS.put(new Character JavaDoc('\r'), ""); //$NON-NLS-1$
70
}
71     
72     /**
73      * @param model
74      * @param parent
75      */

76     public SimpleCSSubItem(ISimpleCSModel model, ISimpleCSObject parent) {
77         super(model, parent);
78         reset();
79     }
80
81     /* (non-Javadoc)
82      * @see org.eclipse.pde.internal.core.icheatsheet.simple.ISimpleCSSubItem#getExecutable()
83      */

84     public ISimpleCSRunContainerObject getExecutable() {
85         return fExecutable;
86     }
87
88     /* (non-Javadoc)
89      * @see org.eclipse.pde.internal.core.icheatsheet.simple.ISimpleCSSubItem#getLabel()
90      */

91     public String JavaDoc getLabel() {
92         return fLabel;
93     }
94
95     /* (non-Javadoc)
96      * @see org.eclipse.pde.internal.core.icheatsheet.simple.ISimpleCSSubItem#getSkip()
97      */

98     public boolean getSkip() {
99         return fSkip;
100     }
101
102     /* (non-Javadoc)
103      * @see org.eclipse.pde.internal.core.icheatsheet.simple.ISimpleCSSubItem#getWhen()
104      */

105     public String JavaDoc getWhen() {
106         return fWhen;
107     }
108
109     /* (non-Javadoc)
110      * @see org.eclipse.pde.internal.core.icheatsheet.simple.ISimpleCSSubItem#setExecutable(org.eclipse.pde.internal.core.icheatsheet.simple.ISimpleCSRunContainerObject)
111      */

112     public void setExecutable(ISimpleCSRunContainerObject executable) {
113         ISimpleCSObject old = fExecutable;
114         fExecutable = executable;
115
116         if (isEditable()) {
117             fireStructureChanged(executable, old);
118         }
119     }
120
121     /* (non-Javadoc)
122      * @see org.eclipse.pde.internal.core.icheatsheet.simple.ISimpleCSSubItem#setLabel(java.lang.String)
123      */

124     public void setLabel(String JavaDoc label) {
125         String JavaDoc old = fLabel;
126         fLabel = label;
127         if (isEditable()) {
128             firePropertyChanged(ATTRIBUTE_LABEL, old, fLabel);
129         }
130     }
131
132     /* (non-Javadoc)
133      * @see org.eclipse.pde.internal.core.icheatsheet.simple.ISimpleCSSubItem#setSkip(boolean)
134      */

135     public void setSkip(boolean skip) {
136         Boolean JavaDoc old = Boolean.valueOf(fSkip);
137         fSkip = skip;
138         if (isEditable()) {
139             firePropertyChanged(ATTRIBUTE_SKIP, old, Boolean.valueOf(fSkip));
140         }
141     }
142
143     /* (non-Javadoc)
144      * @see org.eclipse.pde.internal.core.icheatsheet.simple.ISimpleCSSubItem#setWhen(java.lang.String)
145      */

146     public void setWhen(String JavaDoc when) {
147         String JavaDoc old = fWhen;
148         fWhen = when;
149         if (isEditable()) {
150             firePropertyChanged(ATTRIBUTE_WHEN, old, fWhen);
151         }
152     }
153
154     /* (non-Javadoc)
155      * @see org.eclipse.pde.internal.core.icheatsheet.simple.ISimpleCSObject#parse(org.w3c.dom.Node)
156      */

157     public void parse(Element JavaDoc element) {
158         // Process label attribute
159
fLabel = element.getAttribute(ATTRIBUTE_LABEL).trim();
160         // Process skip attribute
161
if (element.getAttribute(ATTRIBUTE_SKIP).compareTo(
162                 ATTRIBUTE_VALUE_TRUE) == 0) {
163             fSkip = true;
164         }
165         // Process when attribute
166
// Read as is. Do not translate
167
fWhen = element.getAttribute(ATTRIBUTE_WHEN);
168         // Process children
169
NodeList JavaDoc children = element.getChildNodes();
170         ISimpleCSModelFactory factory = getModel().getFactory();
171         for (int i = 0; i < children.getLength(); i++) {
172             Node JavaDoc child = children.item(i);
173             if (child.getNodeType() == Node.ELEMENT_NODE) {
174                 String JavaDoc name = child.getNodeName();
175                 Element JavaDoc childElement = (Element JavaDoc)child;
176
177                 if (name.equals(ELEMENT_ACTION)) {
178                     fExecutable = factory.createSimpleCSAction(this);
179                     fExecutable.parse(childElement);
180                 } else if (name.equals(ELEMENT_COMMAND)) {
181                     fExecutable = factory.createSimpleCSCommand(this);
182                     fExecutable.parse(childElement);
183                 } else if (name.equals(ELEMENT_PERFORM_WHEN)) {
184                     fExecutable = factory.createSimpleCSPerformWhen(this);
185                     fExecutable.parse(childElement);
186                 }
187             }
188         }
189         
190     }
191
192     /* (non-Javadoc)
193      * @see org.eclipse.pde.core.IWritable#write(java.lang.String, java.io.PrintWriter)
194      */

195     public void write(String JavaDoc indent, PrintWriter JavaDoc writer) {
196         
197         StringBuffer JavaDoc buffer = new StringBuffer JavaDoc();
198         String JavaDoc newIndent = indent + XMLPrintHandler.XML_INDENT;
199         
200         try {
201             // Print subitem element
202
buffer.append(ELEMENT_SUBITEM); //$NON-NLS-1$
203
// Print label attribute
204
if ((fLabel != null) &&
205                     (fLabel.length() > 0)) {
206                 buffer.append(XMLPrintHandler.wrapAttribute(
207                         ATTRIBUTE_LABEL,
208                         PDETextHelper.translateWriteText(
209                                 fLabel.trim(), LABEL_SUBSTITUTE_CHARS)));
210             }
211             // Print skip attribute
212
buffer.append(XMLPrintHandler.wrapAttribute(
213                     ATTRIBUTE_SKIP, new Boolean JavaDoc(fSkip).toString()));
214             // Print when attribute
215
if ((fWhen != null) &&
216                     (fWhen.length() > 0)) {
217                 // Write as is. Do not translate
218
buffer.append(XMLPrintHandler.wrapAttribute(
219                         ATTRIBUTE_WHEN, fWhen));
220             }
221             // Start element
222
XMLPrintHandler.printBeginElement(writer, buffer.toString(),
223                     indent, false);
224             // Print action | command | perform-when element
225
if (fExecutable != null) {
226                 fExecutable.write(newIndent, writer);
227             }
228             // End element
229
XMLPrintHandler.printEndElement(writer, ELEMENT_SUBITEM, indent);
230             
231         } catch (IOException JavaDoc e) {
232             // Suppress
233
//e.printStackTrace();
234
}
235         
236     }
237
238     /* (non-Javadoc)
239      * @see org.eclipse.pde.internal.core.icheatsheet.simple.ISimpleCSObject#reset()
240      */

241     public void reset() {
242         fLabel = null;
243         fSkip = false;
244         fWhen = null;
245         fExecutable = null;
246     }
247
248     /* (non-Javadoc)
249      * @see org.eclipse.pde.internal.core.icheatsheet.simple.ISimpleCSObject#getType()
250      */

251     public int getType() {
252         return TYPE_SUBITEM;
253     }
254
255     /* (non-Javadoc)
256      * @see org.eclipse.pde.internal.core.cheatsheet.simple.SimpleCSObject#getName()
257      */

258     public String JavaDoc getName() {
259         return fLabel;
260     }
261
262     /* (non-Javadoc)
263      * @see org.eclipse.pde.internal.core.cheatsheet.simple.SimpleCSObject#getChildren()
264      */

265     public List JavaDoc getChildren() {
266         ArrayList JavaDoc list = new ArrayList JavaDoc();
267         if ((fExecutable != null) &&
268                 (fExecutable.getType() == ISimpleCSConstants.TYPE_PERFORM_WHEN)) {
269             list.add(fExecutable);
270         }
271         return list;
272     }
273
274 }
275
Popular Tags