KickJava   Java API By Example, From Geeks To Geeks.

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


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

37 public class SimpleCSPerformWhen extends SimpleCSObject implements
38         ISimpleCSPerformWhen {
39
40     /**
41      * Attribute: condition
42      */

43     private String JavaDoc fCondition;
44     
45     /**
46      * Elements: action, command
47      */

48     private ArrayList JavaDoc fExecutables;
49     
50     /**
51      *
52      */

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

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

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

74     public ISimpleCSRunObject[] getExecutables() {
75         return (ISimpleCSRunObject[]) fExecutables.toArray(
76                 new ISimpleCSRunObject[fExecutables.size()]);
77     }
78
79     /* (non-Javadoc)
80      * @see org.eclipse.pde.internal.core.icheatsheet.simple.ISimpleCSPerformWhen#setCondition(java.lang.String)
81      */

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

93     public void parse(Element JavaDoc element) {
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_COMMAND)) {
109                     ISimpleCSCommand command = factory.createSimpleCSCommand(this);
110                     fExecutables.add(command);
111                     command.parse(childElement);
112                 } else if (name.equals(ELEMENT_ACTION)) {
113                     ISimpleCSAction action = factory.createSimpleCSAction(this);
114                     fExecutables.add(action);
115                     action.parse(childElement);
116                 }
117             }
118         }
119         
120     }
121
122     /* (non-Javadoc)
123      * @see org.eclipse.pde.core.IWritable#write(java.lang.String, java.io.PrintWriter)
124      */

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

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

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

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

184     public List JavaDoc getChildren() {
185         return new ArrayList JavaDoc();
186     }
187
188     /* (non-Javadoc)
189      * @see org.eclipse.pde.internal.core.icheatsheet.simple.ISimpleCSPerformWhen#addExecutable(org.eclipse.pde.internal.core.icheatsheet.simple.ISimpleCSRunObject)
190      */

191     public void addExecutable(ISimpleCSRunObject executable) {
192         fExecutables.add(executable);
193         
194         if (isEditable()) {
195             fireStructureChanged(executable, IModelChangedEvent.INSERT);
196         }
197     }
198
199     /* (non-Javadoc)
200      * @see org.eclipse.pde.internal.core.icheatsheet.simple.ISimpleCSPerformWhen#removeExecutable(org.eclipse.pde.internal.core.icheatsheet.simple.ISimpleCSRunObject)
201      */

202     public void removeExecutable(ISimpleCSRunObject executable) {
203         fExecutables.remove(executable);
204         
205         if (isEditable()) {
206             fireStructureChanged(executable, IModelChangedEvent.REMOVE);
207         }
208     }
209
210 }
211
Popular Tags