|
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.tracebuilder.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 */ |
|
83 public void setID(int id) { |
|
84 if (this.id != id) { |
|
85 this.id = id; |
|
86 model.notifyPropertyUpdated(this, TraceModelListener.ID); |
|
87 } |
|
88 } |
|
89 |
|
90 /** |
|
91 * Sets the ID of this object but does not generate any notifications |
|
92 * |
|
93 * @param id |
|
94 * the new ID |
|
95 */ |
|
96 void internalSetID(int id) { |
|
97 this.id = id; |
|
98 } |
|
99 |
|
100 /** |
|
101 * Returns the ID of this object. |
|
102 * |
|
103 * @return the ID |
|
104 */ |
|
105 public int getID() { |
|
106 return id; |
|
107 } |
|
108 |
|
109 /** |
|
110 * Sets the name of this object. Generates propertyUpdated event to model |
|
111 * listeners if the name changes |
|
112 * |
|
113 * @see TraceModelListener#propertyUpdated(TraceObject, int) |
|
114 * @param name |
|
115 * the new name |
|
116 */ |
|
117 public void setName(String name) { |
|
118 if (name == null) { |
|
119 name = ""; //$NON-NLS-1$ |
|
120 } |
|
121 if (!name.equals(this.name)) { |
|
122 this.name = name; |
|
123 model.notifyPropertyUpdated(this, TraceModelListener.NAME); |
|
124 } |
|
125 } |
|
126 |
|
127 /** |
|
128 * Returns the name of this object. This never returns null |
|
129 * |
|
130 * @return the name |
|
131 */ |
|
132 public String getName() { |
|
133 return name; |
|
134 } |
|
135 |
|
136 /** |
|
137 * Adds an extension to this object. Generates extensionAdded event to model |
|
138 * listeners |
|
139 * |
|
140 * @param extension |
|
141 * the new extension |
|
142 */ |
|
143 public void addExtension(TraceModelExtension extension) { |
|
144 if (extension == null) { |
|
145 throw new NullPointerException(); |
|
146 } |
|
147 extensions.add(extension); |
|
148 extension.setOwner(this); |
|
149 model.notifyExtensionAdded(this, extension); |
|
150 } |
|
151 |
|
152 /** |
|
153 * Removes an extension from this object. Generates extensionRemoved event |
|
154 * to model listeners if the extension was actually removed |
|
155 * |
|
156 * @param extension |
|
157 * the extension to be removed |
|
158 */ |
|
159 public void removeExtension(TraceModelExtension extension) { |
|
160 if (extensions.remove(extension)) { |
|
161 extension.setOwner(null); |
|
162 model.notifyExtensionRemoved(this, extension); |
|
163 } |
|
164 } |
|
165 |
|
166 /** |
|
167 * Removes all extensions which are of given type |
|
168 * |
|
169 * @param <T> |
|
170 * the type of the extension |
|
171 * @param extClass |
|
172 * the extension type |
|
173 */ |
|
174 public <T extends TraceModelExtension> void removeExtensions( |
|
175 Class<T> extClass) { |
|
176 Iterator<TraceModelExtension> itr = extensions.iterator(); |
|
177 while (itr.hasNext()) { |
|
178 TraceModelExtension ext = itr.next(); |
|
179 if (extClass.isAssignableFrom(ext.getClass())) { |
|
180 itr.remove(); |
|
181 ext.setOwner(null); |
|
182 model.notifyExtensionRemoved(this, ext); |
|
183 } |
|
184 } |
|
185 } |
|
186 |
|
187 /** |
|
188 * Returns the first extension which is of given type. The extensions are |
|
189 * stored in a list in the order they have been added. This returns the |
|
190 * first instance found from the list. |
|
191 * |
|
192 * @param <T> |
|
193 * the type of the extension |
|
194 * @param extClass |
|
195 * the extension type |
|
196 * @return the extension or null if not found |
|
197 */ |
|
198 @SuppressWarnings("unchecked") |
|
199 public <T extends TraceModelExtension> T getExtension(Class<T> extClass) { |
|
200 T ret = null; |
|
201 for (TraceModelExtension ext : extensions) { |
|
202 if (extClass.isAssignableFrom(ext.getClass())) { |
|
203 ret = (T) ext; |
|
204 break; |
|
205 } |
|
206 } |
|
207 return ret; |
|
208 } |
|
209 |
|
210 /** |
|
211 * Returns the extensions which are of given type. |
|
212 * |
|
213 * @param <T> |
|
214 * the type of the extension |
|
215 * @param extClass |
|
216 * the extension type |
|
217 * @return iterator over the list of extension |
|
218 */ |
|
219 @SuppressWarnings("unchecked") |
|
220 public <T extends TraceModelExtension> Iterator<T> getExtensions( |
|
221 Class<T> extClass) { |
|
222 ArrayList<T> list = new ArrayList<T>(); |
|
223 for (TraceModelExtension ext : extensions) { |
|
224 if (extClass.isAssignableFrom(ext.getClass())) { |
|
225 list.add((T) ext); |
|
226 } |
|
227 } |
|
228 return list.iterator(); |
|
229 } |
|
230 |
|
231 /** |
|
232 * Resets the ID and name |
|
233 */ |
|
234 void reset() { |
|
235 id = 0; |
|
236 name = ""; //$NON-NLS-1$ |
|
237 } |
|
238 |
|
239 /** |
|
240 * Sets the complete flag and fires complete event |
|
241 */ |
|
242 void setComplete() { |
|
243 complete = true; |
|
244 model.notifyObjectCreationComplete(this); |
|
245 } |
|
246 |
|
247 /** |
|
248 * Calls OnDelete rules if the object owns them |
|
249 * |
|
250 * @param object |
|
251 * the object that was removed |
|
252 */ |
|
253 protected void notifyOnDelete(TraceObject object) { |
|
254 Iterator<TraceObjectRuleOnDelete> rules = object |
|
255 .getExtensions(TraceObjectRuleOnDelete.class); |
|
256 while (rules.hasNext()) { |
|
257 rules.next().objectDeleted(); |
|
258 } |
|
259 } |
|
260 |
|
261 /** |
|
262 * Gets the complete flag |
|
263 * |
|
264 * @return the flag |
|
265 */ |
|
266 public boolean isComplete() { |
|
267 return complete; |
|
268 } |
|
269 |
|
270 } |