|
1 /* |
|
2 * Copyright (c) 2007 Nokia Corporation and/or its subsidiary(-ies). |
|
3 * All rights reserved. |
|
4 * This component and the accompanying materials are made available |
|
5 * under the terms of "Eclipse Public License v1.0" |
|
6 * which accompanies this distribution, and is available |
|
7 * at the URL "http://www.eclipse.org/legal/epl-v10.html". |
|
8 * |
|
9 * Initial Contributors: |
|
10 * Nokia Corporation - initial contribution. |
|
11 * |
|
12 * Contributors: |
|
13 * |
|
14 * Description: |
|
15 * |
|
16 * Base class for all trace model objects |
|
17 * |
|
18 */ |
|
19 package com.nokia.tracecompiler.model; |
|
20 |
|
21 import java.util.ArrayList; |
|
22 import java.util.Iterator; |
|
23 |
|
24 /** |
|
25 * Base class for all trace model objects |
|
26 * |
|
27 */ |
|
28 public abstract class TraceObject { |
|
29 |
|
30 /** |
|
31 * The extensions can be used by trace providers to associate extra data to |
|
32 * the object. |
|
33 */ |
|
34 private ArrayList<TraceModelExtension> extensions = new ArrayList<TraceModelExtension>(); |
|
35 |
|
36 /** |
|
37 * Object ID. |
|
38 */ |
|
39 private int id; |
|
40 |
|
41 /** |
|
42 * Object name. |
|
43 */ |
|
44 private String name = ""; //$NON-NLS-1$ |
|
45 |
|
46 /** |
|
47 * The trace model. |
|
48 */ |
|
49 private TraceModel model; |
|
50 |
|
51 /** |
|
52 * Complete flag |
|
53 */ |
|
54 private boolean complete; |
|
55 |
|
56 /** |
|
57 * Associates this object to a trace model. |
|
58 * |
|
59 * @param model |
|
60 * the model where this object belongs to |
|
61 */ |
|
62 void setModel(TraceModel model) { |
|
63 this.model = model; |
|
64 } |
|
65 |
|
66 /** |
|
67 * Returns the trace model this object is associated to. |
|
68 * |
|
69 * @return the trace model |
|
70 */ |
|
71 public TraceModel getModel() { |
|
72 return model; |
|
73 } |
|
74 |
|
75 /** |
|
76 * Sets the ID of this object. Generates propertyUpdated event to model |
|
77 * listeners if the ID changes |
|
78 * |
|
79 * @see TraceModelListener#propertyUpdated(TraceObject, int) |
|
80 * @param id |
|
81 * the new ID |
|
82 * @throws TraceCompilerException |
|
83 */ |
|
84 public void setID(int id) throws TraceCompilerException { |
|
85 if (this.id != id) { |
|
86 this.id = id; |
|
87 model.notifyPropertyUpdated(this, TraceModelListener.ID); |
|
88 } |
|
89 } |
|
90 |
|
91 /** |
|
92 * Sets the ID of this object but does not generate any notifications |
|
93 * |
|
94 * @param id |
|
95 * the new ID |
|
96 */ |
|
97 void internalSetID(int id) { |
|
98 this.id = id; |
|
99 } |
|
100 |
|
101 /** |
|
102 * Returns the ID of this object. |
|
103 * |
|
104 * @return the ID |
|
105 */ |
|
106 public int getID() { |
|
107 return id; |
|
108 } |
|
109 |
|
110 /** |
|
111 * Sets the name of this object. Generates propertyUpdated event to model |
|
112 * listeners if the name changes |
|
113 * |
|
114 * @see TraceModelListener#propertyUpdated(TraceObject, int) |
|
115 * @param name |
|
116 * the new name |
|
117 * @throws TraceCompilerException |
|
118 */ |
|
119 public void setName(String name) throws TraceCompilerException { |
|
120 if (name == null) { |
|
121 name = ""; //$NON-NLS-1$ |
|
122 } |
|
123 if (!name.equals(this.name)) { |
|
124 this.name = name; |
|
125 model.notifyPropertyUpdated(this, TraceModelListener.NAME); |
|
126 } |
|
127 } |
|
128 |
|
129 /** |
|
130 * Returns the name of this object. This never returns null |
|
131 * |
|
132 * @return the name |
|
133 */ |
|
134 public String getName() { |
|
135 return name; |
|
136 } |
|
137 |
|
138 /** |
|
139 * Adds an extension to this object. Generates extensionAdded event to model |
|
140 * listeners |
|
141 * |
|
142 * @param extension |
|
143 * the new extension |
|
144 */ |
|
145 public void addExtension(TraceModelExtension extension) { |
|
146 if (extension == null) { |
|
147 throw new NullPointerException(); |
|
148 } |
|
149 extensions.add(extension); |
|
150 extension.setOwner(this); |
|
151 model.notifyExtensionAdded(this, extension); |
|
152 } |
|
153 |
|
154 /** |
|
155 * Removes an extension from this object. Generates extensionRemoved event |
|
156 * to model listeners if the extension was actually removed |
|
157 * |
|
158 * @param extension |
|
159 * the extension to be removed |
|
160 */ |
|
161 public void removeExtension(TraceModelExtension extension) { |
|
162 if (extensions.remove(extension)) { |
|
163 extension.setOwner(null); |
|
164 model.notifyExtensionRemoved(this, extension); |
|
165 } |
|
166 } |
|
167 |
|
168 /** |
|
169 * Removes all extensions which are of given type |
|
170 * |
|
171 * @param <T> |
|
172 * the type of the extension |
|
173 * @param extClass |
|
174 * the extension type |
|
175 */ |
|
176 public <T extends TraceModelExtension> void removeExtensions( |
|
177 Class<T> extClass) { |
|
178 Iterator<TraceModelExtension> itr = extensions.iterator(); |
|
179 while (itr.hasNext()) { |
|
180 TraceModelExtension ext = itr.next(); |
|
181 if (extClass.isAssignableFrom(ext.getClass())) { |
|
182 itr.remove(); |
|
183 ext.setOwner(null); |
|
184 model.notifyExtensionRemoved(this, ext); |
|
185 } |
|
186 } |
|
187 } |
|
188 |
|
189 /** |
|
190 * Returns the first extension which is of given type. The extensions are |
|
191 * stored in a list in the order they have been added. This returns the |
|
192 * first instance found from the list. |
|
193 * |
|
194 * @param <T> |
|
195 * the type of the extension |
|
196 * @param extClass |
|
197 * the extension type |
|
198 * @return the extension or null if not found |
|
199 */ |
|
200 @SuppressWarnings("unchecked") |
|
201 public <T extends TraceModelExtension> T getExtension(Class<T> extClass) { |
|
202 T ret = null; |
|
203 for (TraceModelExtension ext : extensions) { |
|
204 if (extClass.isAssignableFrom(ext.getClass())) { |
|
205 ret = (T) ext; |
|
206 break; |
|
207 } |
|
208 } |
|
209 return ret; |
|
210 } |
|
211 |
|
212 /** |
|
213 * Returns the extensions which are of given type. |
|
214 * |
|
215 * @param <T> |
|
216 * the type of the extension |
|
217 * @param extClass |
|
218 * the extension type |
|
219 * @return iterator over the list of extension |
|
220 */ |
|
221 @SuppressWarnings("unchecked") |
|
222 public <T extends TraceModelExtension> Iterator<T> getExtensions( |
|
223 Class<T> extClass) { |
|
224 ArrayList<T> list = new ArrayList<T>(); |
|
225 for (TraceModelExtension ext : extensions) { |
|
226 if (extClass.isAssignableFrom(ext.getClass())) { |
|
227 list.add((T) ext); |
|
228 } |
|
229 } |
|
230 return list.iterator(); |
|
231 } |
|
232 |
|
233 /** |
|
234 * Resets the ID and name |
|
235 */ |
|
236 void reset() { |
|
237 id = 0; |
|
238 name = ""; //$NON-NLS-1$ |
|
239 } |
|
240 |
|
241 /** |
|
242 * Sets the complete flag and fires complete event |
|
243 * @throws TraceCompilerException |
|
244 */ |
|
245 void setComplete() throws TraceCompilerException { |
|
246 complete = true; |
|
247 model.notifyObjectCreationComplete(this); |
|
248 } |
|
249 |
|
250 /** |
|
251 * Calls OnDelete rules if the object owns them |
|
252 * |
|
253 * @param object |
|
254 * the object that was removed |
|
255 */ |
|
256 protected void notifyOnDelete(TraceObject object) { |
|
257 Iterator<TraceObjectRuleOnDelete> rules = object |
|
258 .getExtensions(TraceObjectRuleOnDelete.class); |
|
259 while (rules.hasNext()) { |
|
260 rules.next().objectDeleted(); |
|
261 } |
|
262 } |
|
263 |
|
264 /** |
|
265 * Gets the complete flag |
|
266 * |
|
267 * @return the flag |
|
268 */ |
|
269 public boolean isComplete() { |
|
270 return complete; |
|
271 } |
|
272 |
|
273 } |