KickJava   Java API By Example, From Geeks To Geeks.

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


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.List JavaDoc;
18
19 import org.eclipse.pde.internal.core.XMLPrintHandler;
20 import org.eclipse.pde.internal.core.icheatsheet.simple.ISimpleCSAction;
21 import org.eclipse.pde.internal.core.icheatsheet.simple.ISimpleCSModel;
22 import org.eclipse.pde.internal.core.icheatsheet.simple.ISimpleCSObject;
23 import org.eclipse.pde.internal.core.util.PDETextHelper;
24 import org.w3c.dom.Attr JavaDoc;
25 import org.w3c.dom.Element JavaDoc;
26
27 /**
28  * SimpleCSAction
29  *
30  */

31 public class SimpleCSAction extends SimpleCSObject implements ISimpleCSAction {
32
33     /**
34      * Attribute: class
35      */

36     private String JavaDoc fClazz;
37     
38     /**
39      * Attribute: pluginId
40      */

41     private String JavaDoc fPluginId;
42     
43     /**
44      * Attribute: confirm
45      */

46     private boolean fConfirm;
47
48     /**
49      * Attribute: when
50      */

51     private String JavaDoc fWhen;
52
53     /**
54      * Attribute: translate
55      */

56     private String JavaDoc fTranslate;
57     
58     /**
59      * Attributes: param1, param2, ..., param9
60      */

61     private ArrayList JavaDoc fParams;
62     
63     private static final int F_MAX_PARAMS = 9;
64     
65     /**
66      *
67      */

68     private static final long serialVersionUID = 1L;
69
70     /**
71      * @param model
72      * @param parent
73      */

74     public SimpleCSAction(ISimpleCSModel model, ISimpleCSObject parent) {
75         super(model, parent);
76         reset();
77     }
78
79     /* (non-Javadoc)
80      * @see org.eclipse.pde.internal.core.icheatsheet.simple.ISimpleCSAction#getClazz()
81      */

82     public String JavaDoc getClazz() {
83         return fClazz;
84     }
85
86     /* (non-Javadoc)
87      * @see org.eclipse.pde.internal.core.icheatsheet.simple.ISimpleCSAction#getConfirm()
88      */

89     public boolean getConfirm() {
90         return fConfirm;
91     }
92
93     /* (non-Javadoc)
94      * @see org.eclipse.pde.internal.core.icheatsheet.simple.ISimpleCSAction#getParams()
95      */

96     public String JavaDoc[] getParams() {
97         return (String JavaDoc[])fParams.toArray(new String JavaDoc[fParams.size()]);
98     }
99
100     /* (non-Javadoc)
101      * @see org.eclipse.pde.internal.core.icheatsheet.simple.ISimpleCSAction#getPluginId()
102      */

103     public String JavaDoc getPluginId() {
104         return fPluginId;
105     }
106
107     /* (non-Javadoc)
108      * @see org.eclipse.pde.internal.core.icheatsheet.simple.ISimpleCSAction#setClazz(java.lang.String)
109      */

110     public void setClazz(String JavaDoc clazz) {
111         String JavaDoc old = fClazz;
112         fClazz = clazz;
113         if (isEditable()) {
114             firePropertyChanged(ATTRIBUTE_CLASS, old, fClazz);
115         }
116     }
117
118     /* (non-Javadoc)
119      * @see org.eclipse.pde.internal.core.icheatsheet.simple.ISimpleCSAction#setConfirm(boolean)
120      */

121     public void setConfirm(boolean confirm) {
122         Boolean JavaDoc old = Boolean.valueOf(fConfirm);
123         fConfirm = confirm;
124         if (isEditable()) {
125             firePropertyChanged(ATTRIBUTE_CONFIRM, old, Boolean.valueOf(fConfirm));
126         }
127     }
128
129     /* (non-Javadoc)
130      * @see org.eclipse.pde.internal.core.icheatsheet.simple.ISimpleCSAction#setPluginId(java.lang.String)
131      */

132     public void setPluginId(String JavaDoc pluginId) {
133         String JavaDoc old = fPluginId;
134         fPluginId = pluginId;
135         if (isEditable()) {
136             firePropertyChanged(ATTRIBUTE_PLUGINID, old, fPluginId);
137         }
138     }
139
140     /* (non-Javadoc)
141      * @see org.eclipse.pde.internal.core.icheatsheet.simple.ISimpleCSObject#parse(org.w3c.dom.Element)
142      */

143     public void parse(Element JavaDoc element) {
144         // Process class attribute
145
// Read as is. Do not translate
146
fClazz = element.getAttribute(ATTRIBUTE_CLASS);
147         // Process pluginId attribute
148
// Read as is. Do not translate
149
fPluginId = element.getAttribute(ATTRIBUTE_PLUGINID);
150         // Process confirm attribute
151
if (element.getAttribute(ATTRIBUTE_CONFIRM).compareTo(
152                 ATTRIBUTE_VALUE_TRUE) == 0) {
153             fConfirm = true;
154         }
155         // Process when attribute
156
// Read as is. Do not translate
157
fWhen = element.getAttribute(ATTRIBUTE_WHEN);
158         // Process translate attribute
159
// Read as is. Do not translate
160
// Need to be able to write out the empty string
161
Attr JavaDoc translateAttribute = element.getAttributeNode(ATTRIBUTE_TRANSLATE);
162         if (translateAttribute == null) {
163             fTranslate = null;
164         } else {
165             fTranslate = translateAttribute.getValue();
166         }
167         // Process attributes: param1, param2, ..., param9
168
for (int i = 0; i < F_MAX_PARAMS; i++) {
169             int adjustedIndex = i + 1;
170             String JavaDoc parameter = ATTRIBUTE_PARAM + adjustedIndex;
171             // Read as is. Do not translate
172
String JavaDoc value = element.getAttribute(parameter);
173             fParams.add(i, value);
174         }
175     }
176
177     /* (non-Javadoc)
178      * @see org.eclipse.pde.core.IWritable#write(java.lang.String, java.io.PrintWriter)
179      */

180     public void write(String JavaDoc indent, PrintWriter JavaDoc writer) {
181
182         StringBuffer JavaDoc buffer = new StringBuffer JavaDoc();
183         
184         try {
185             // Print action element
186
buffer.append(ELEMENT_ACTION);
187             // Print class attribute
188
if ((fClazz != null) &&
189                     (fClazz.length() > 0)) {
190                 // Write as is. Do not translate
191
buffer.append(XMLPrintHandler.wrapAttribute(
192                         ATTRIBUTE_CLASS, fClazz));
193             }
194             // Print pluginId attribute
195
if ((fPluginId != null) &&
196                     (fPluginId.length() > 0)) {
197                 // Write as is. Do not translate
198
buffer.append(XMLPrintHandler.wrapAttribute(
199                         ATTRIBUTE_PLUGINID, fPluginId));
200             }
201             // Print confirm attribute
202
buffer.append(XMLPrintHandler.wrapAttribute(
203                     ATTRIBUTE_CONFIRM, new Boolean JavaDoc(fConfirm).toString()));
204             // Print when attribute
205
if ((fWhen != null) &&
206                     (fWhen.length() > 0)) {
207                 // Write as is. Do not translate
208
buffer.append(XMLPrintHandler.wrapAttribute(
209                         ATTRIBUTE_WHEN, fWhen));
210             }
211             // Print translate attribute
212
if (fTranslate != null) {
213                 // Write as is. Do not translate
214
buffer.append(XMLPrintHandler.wrapAttribute(
215                         ATTRIBUTE_TRANSLATE, fTranslate));
216             }
217             // Print attributes: param1, param2, ..., param9
218
for (int i = 0; i < F_MAX_PARAMS; i++) {
219                 int adjustedIndex = i + 1;
220                 String JavaDoc parameter = ATTRIBUTE_PARAM + adjustedIndex;
221                 String JavaDoc value = (String JavaDoc)fParams.get(i);
222                 // Preserve cheat sheet validity
223
// Ignore Semantic Rule: Only contiguously defined parameters allowed
224
// Write only if defined
225
if (PDETextHelper.isDefined(value)) {
226                     // Write as is. Do not translate
227
buffer.append(XMLPrintHandler.wrapAttribute(
228                             parameter, value));
229                 }
230             }
231             
232             // Start element
233
XMLPrintHandler.printBeginElement(writer, buffer.toString(),
234                     indent, false);
235             // End element
236
XMLPrintHandler.printEndElement(writer, ELEMENT_ACTION, indent);
237             
238         } catch (IOException JavaDoc e) {
239             // Suppress
240
//e.printStackTrace();
241
}
242     }
243
244     /* (non-Javadoc)
245      * @see org.eclipse.pde.internal.core.icheatsheet.simple.ISimpleCSObject#reset()
246      */

247     public void reset() {
248         fClazz = null;
249         fPluginId = null;
250         fConfirm = false;
251         fWhen = null;
252         fParams = new ArrayList JavaDoc(F_MAX_PARAMS);
253         fTranslate = null;
254     }
255
256     /* (non-Javadoc)
257      * @see org.eclipse.pde.internal.core.icheatsheet.simple.ISimpleCSObject#getType()
258      */

259     public int getType() {
260         return TYPE_ACTION;
261     }
262
263     /* (non-Javadoc)
264      * @see org.eclipse.pde.internal.core.icheatsheet.simple.ISimpleCSRunObject#getWhen()
265      */

266     public String JavaDoc getWhen() {
267         return fWhen;
268     }
269
270     /* (non-Javadoc)
271      * @see org.eclipse.pde.internal.core.icheatsheet.simple.ISimpleCSRunObject#getTranslate()
272      */

273     public String JavaDoc getTranslate() {
274         return fTranslate;
275     }
276     
277     /* (non-Javadoc)
278      * @see org.eclipse.pde.internal.core.icheatsheet.simple.ISimpleCSRunObject#setWhen(java.lang.String)
279      */

280     public void setWhen(String JavaDoc when) {
281         String JavaDoc old = fWhen;
282         fWhen = when;
283         if (isEditable()) {
284             firePropertyChanged(ATTRIBUTE_WHEN, old, fWhen);
285         }
286     }
287     
288     /* (non-Javadoc)
289      * @see org.eclipse.pde.internal.core.icheatsheet.simple.ISimpleCSRunObject#setTranslate(java.lang.String)
290      */

291     public void setTranslate(String JavaDoc translate) {
292         String JavaDoc old = fTranslate;
293         fTranslate = translate;
294         if (isEditable()) {
295             firePropertyChanged(ATTRIBUTE_TRANSLATE, old, fTranslate);
296         }
297     }
298
299     /* (non-Javadoc)
300      * @see org.eclipse.pde.internal.core.cheatsheet.simple.SimpleCSObject#getName()
301      */

302     public String JavaDoc getName() {
303         // Leave as is. Not a separate node in tree view
304
return ELEMENT_ACTION;
305     }
306
307     /* (non-Javadoc)
308      * @see org.eclipse.pde.internal.core.cheatsheet.simple.SimpleCSObject#getChildren()
309      */

310     public List JavaDoc getChildren() {
311         return new ArrayList JavaDoc();
312     }
313
314     /* (non-Javadoc)
315      * @see org.eclipse.pde.internal.core.icheatsheet.simple.ISimpleCSAction#getParam(int)
316      */

317     public String JavaDoc getParam(int index) {
318         // Note: index is mapped to ID value rather than to actual storage
319
// i.e. getParam(1) returns index 0
320
if ((index < 1) ||
321                 (index > F_MAX_PARAMS)) {
322             return null;
323         }
324         int actualIndex = index - 1;
325         return (String JavaDoc)fParams.get(actualIndex);
326     }
327
328     /* (non-Javadoc)
329      * @see org.eclipse.pde.internal.core.icheatsheet.simple.ISimpleCSAction#setParam(java.lang.String, int)
330      */

331     public void setParam(String JavaDoc param, int index) {
332         // Note: index is mapped to ID value rather than to actual storage
333
// i.e. getParam(1) returns index 0
334
if ((index < 1) ||
335                 (index > F_MAX_PARAMS)) {
336             return;
337         }
338         int actualIndex = index - 1;
339         String JavaDoc old = (String JavaDoc)fParams.get(actualIndex);
340         fParams.set(actualIndex, param);
341
342         if (isEditable()) {
343             firePropertyChanged(ATTRIBUTE_PARAM, old, param);
344         }
345     }
346
347 }
348
Popular Tags