Commit | Line | Data |
---|---|---|
73005152 | 1 | /********************************************************************** |
df0b8ff4 BH |
2 | * Copyright (c) 2005, 2006 IBM Corporation and others. |
3 | * Copyright (c) 2011, 2012 Ericsson. | |
4 | * | |
73005152 BH |
5 | * All rights reserved. This program and the accompanying materials |
6 | * are made available under the terms of the Eclipse Public License v1.0 | |
7 | * which accompanies this distribution, and is available at | |
8 | * http://www.eclipse.org/legal/epl-v10.html | |
73005152 BH |
9 | * |
10 | * Contributors: | |
11 | * IBM - Initial API and implementation | |
12 | * Bernd Hufmann - Updated for TMF | |
13 | **********************************************************************/ | |
14 | package org.eclipse.linuxtools.tmf.ui.views.uml2sd.core; | |
15 | ||
16 | import java.util.Comparator; | |
17 | ||
4df4581d | 18 | import org.eclipse.linuxtools.tmf.core.event.ITmfTimestamp; |
6c13869b | 19 | import org.eclipse.linuxtools.tmf.core.event.TmfTimestamp; |
73005152 | 20 | import org.eclipse.linuxtools.tmf.ui.views.uml2sd.drawings.IGC; |
df0b8ff4 | 21 | import org.eclipse.linuxtools.tmf.ui.views.uml2sd.preferences.ISDPreferences; |
73005152 BH |
22 | import org.eclipse.linuxtools.tmf.ui.views.uml2sd.util.SortSyncMessageComparator; |
23 | ||
24 | /** | |
25 | * A SyncMessage is a synchronous message which appear at the same event occurrence on both lifeline ends (sender and | |
26 | * receiver).<br> | |
27 | * A Sync message is usually drawn horizontally.<br> | |
28 | * <br> | |
29 | * <br> | |
30 | * Usage example: | |
31 | * | |
32 | * <pre> | |
33 | * Frame frame; | |
34 | * Lifeline lifeLine1; | |
35 | * Lifeline lifeLine2; | |
36 | * | |
37 | * SyncMessage message = new SyncMessage(); | |
38 | * // Create a new event occurrence on each lifeline | |
39 | * lifeline1.getNewOccurrenceIndex(); | |
40 | * lifeline2.getNewOccurrenceIndex(); | |
41 | * // Set the message sender and receiver | |
42 | * message.setStartLifeline(lifeLine1); | |
43 | * message.setEndLifline(lifeline2); | |
44 | * message.setName("Message label"); | |
45 | * // add the message to the frame | |
46 | * frame.addMessage(message); | |
47 | * </pre> | |
48 | * | |
49 | * @see org.eclipse.linuxtools.tmf.ui.views.uml2sd.core.Lifeline Lifeline for more event occurence details | |
df0b8ff4 | 50 | * @version 1.0 |
73005152 BH |
51 | * @author sveyrier |
52 | * | |
53 | */ | |
54 | public class SyncMessage extends BaseMessage implements ITimeRange { | |
55 | ||
df0b8ff4 BH |
56 | // ------------------------------------------------------------------------ |
57 | // Constants | |
58 | // ------------------------------------------------------------------------ | |
59 | /** | |
60 | * The graphNode ID | |
61 | */ | |
62 | public static final String SYNC_MESS_TAG = "SyncMessage"; //$NON-NLS-1$ | |
63 | ||
64 | // ------------------------------------------------------------------------ | |
65 | // Attributes | |
66 | // ------------------------------------------------------------------------ | |
67 | ||
73005152 BH |
68 | /** |
69 | * The associated message return | |
70 | */ | |
eb63f5ff | 71 | protected SyncMessageReturn fMessageReturn; |
73005152 BH |
72 | /** |
73 | * The time when the message occurs | |
74 | */ | |
eb63f5ff | 75 | protected ITmfTimestamp fEventTime = new TmfTimestamp(); |
df0b8ff4 BH |
76 | /** |
77 | * Flag whether the message has time information available or not | |
78 | */ | |
eb63f5ff | 79 | protected boolean fHasTimeInfo = false; |
73005152 | 80 | |
df0b8ff4 BH |
81 | // ------------------------------------------------------------------------ |
82 | // Constructors | |
83 | // ------------------------------------------------------------------------ | |
84 | ||
85 | /** | |
86 | * Default constructor | |
87 | */ | |
73005152 | 88 | public SyncMessage() { |
eb63f5ff | 89 | fPrefId = ISDPreferences.PREF_SYNC_MESS; |
73005152 BH |
90 | } |
91 | ||
df0b8ff4 BH |
92 | // ------------------------------------------------------------------------ |
93 | // Methods | |
94 | // ------------------------------------------------------------------------ | |
95 | ||
73005152 BH |
96 | /** |
97 | * Ensure both lifelines have the same event occurrence (the greater found on each lifeline) | |
98 | */ | |
99 | protected void syncLifelinesEventOccurrence() { | |
100 | if ((getStartLifeline() != null) && (getEndLifeline() != null)) { | |
101 | int newIndex = 0; | |
df0b8ff4 | 102 | if (getStartLifeline().getEventOccurrence() > getEndLifeline().getEventOccurrence()) { |
73005152 | 103 | newIndex = getStartLifeline().getEventOccurrence(); |
df0b8ff4 | 104 | } else { |
73005152 | 105 | newIndex = getEndLifeline().getEventOccurrence(); |
df0b8ff4 | 106 | } |
73005152 BH |
107 | getStartLifeline().setCurrentEventOccurrence(newIndex); |
108 | getEndLifeline().setCurrentEventOccurrence(newIndex); | |
109 | setEventOccurrence(getStartLifeline().getEventOccurrence()); | |
110 | } | |
111 | } | |
112 | ||
113 | /** | |
114 | * Set the lifeLine from which the message has been sent.<br> | |
115 | * A new event occurrence will be created on this lifeLine.<br> | |
116 | * SyncMessage must occur at the same event occurrence on both lifeline, this method is responsible to synchronize the | |
117 | * event occurrence on each lifeline (the greater value will be used).<br> | |
118 | * This synchronization is only done if the end lifeline has already been set. | |
119 | * | |
120 | * @param lifeline the message sender | |
121 | */ | |
122 | public void autoSetStartLifeline(Lifeline lifeline) { | |
123 | lifeline.getNewEventOccurrence(); | |
124 | setStartLifeline(lifeline); | |
125 | } | |
126 | ||
127 | /** | |
128 | * Set the lifeLine which has receiver the message.<br> | |
129 | * A new EventOccurence will be create on this lifeLine.<br> | |
130 | * SyncMessage must occur at the same event occurrence on both lifeline, this method is responsible to synchronize the | |
131 | * event occurrence on each lifeline (the greater value will be used).<br> | |
132 | * This synchronization is only done if the start lifeline has already been set. | |
133 | * | |
134 | * @param lifeline the message receiver | |
135 | */ | |
136 | public void autoSetEndLifeline(Lifeline lifeline) { | |
137 | lifeline.getNewEventOccurrence(); | |
138 | setEndLifeline(lifeline); | |
139 | } | |
140 | ||
141 | /** | |
142 | * Set the lifeLine which has receiver the message.<br> | |
143 | * SyncMessage must occur at the same event occurrence on both lifeline, this method is responsible to synchronize the | |
144 | * event occurrence on each lifeline (the greater value will be used).<br> | |
145 | * This synchronization is only done if the start lifeline has already been set. | |
146 | * | |
147 | * @param lifeline the message receiver | |
148 | */ | |
149 | @Override | |
150 | public void setStartLifeline(Lifeline lifeline) { | |
151 | super.setStartLifeline(lifeline); | |
152 | if ((getEndLifeline() == null)) { | |
153 | setEventOccurrence(getStartLifeline().getEventOccurrence()); | |
df0b8ff4 | 154 | } else { |
73005152 | 155 | syncLifelinesEventOccurrence(); |
df0b8ff4 | 156 | } |
73005152 BH |
157 | } |
158 | ||
159 | /** | |
160 | * Set the lifeLine which has receiver the message.<br> | |
161 | * SyncMessage must occur at the same event occurrence on both lifelines, this method is responsible to synchronize the | |
162 | * event occurrence on each lifeline (the greater value will be used).<br> | |
163 | * This synchronization is only done if the start lifeline has already been set. | |
164 | * | |
165 | * @param lifeline the message receiver | |
166 | */ | |
167 | @Override | |
168 | public void setEndLifeline(Lifeline lifeline) { | |
169 | super.setEndLifeline(lifeline); | |
170 | if ((getStartLifeline() == null)) { | |
171 | setEventOccurrence(getEndLifeline().getEventOccurrence()); | |
df0b8ff4 | 172 | } else { |
73005152 | 173 | syncLifelinesEventOccurrence(); |
df0b8ff4 | 174 | } |
73005152 BH |
175 | } |
176 | ||
177 | /** | |
178 | * Set the event occurrence when this message occurs.<br> | |
179 | * | |
180 | * @param occurrence the event occurrence to assign to this message.<br> | |
181 | * @see Lifeline Lifeline for more event occurence details | |
182 | */ | |
183 | @Override | |
184 | protected void setEventOccurrence(int occurrence) { | |
eb63f5ff BH |
185 | fStartEventOccurrence = occurrence; |
186 | fEndEventOccurrence = occurrence; | |
73005152 BH |
187 | } |
188 | ||
189 | /** | |
190 | * Set the message return associated with this message. | |
191 | * | |
192 | * @param message the message return to associate | |
193 | */ | |
194 | protected void setMessageReturn(SyncMessageReturn message) { | |
eb63f5ff | 195 | fMessageReturn = message; |
73005152 BH |
196 | } |
197 | ||
198 | /** | |
199 | * Returns the syncMessageReturn associated to this syncMessage | |
200 | * | |
201 | * @return the message return | |
202 | */ | |
203 | public SyncMessageReturn getMessageReturn() { | |
eb63f5ff | 204 | return fMessageReturn; |
73005152 BH |
205 | } |
206 | ||
207 | /** | |
208 | * Set the time when the message occurs | |
209 | * | |
210 | * @param time the time when the message occurs | |
211 | */ | |
4df4581d | 212 | public void setTime(ITmfTimestamp time) { |
eb63f5ff BH |
213 | fEventTime = time.clone(); |
214 | fHasTimeInfo = true; | |
df0b8ff4 | 215 | if (getStartLifeline() != null && getStartLifeline().getFrame() != null) { |
73005152 | 216 | getStartLifeline().getFrame().setHasTimeInfo(true); |
df0b8ff4 | 217 | } else if (getEndLifeline() != null && getEndLifeline().getFrame() != null) { |
73005152 | 218 | getEndLifeline().getFrame().setHasTimeInfo(true); |
df0b8ff4 | 219 | } |
73005152 BH |
220 | } |
221 | ||
df0b8ff4 BH |
222 | /* |
223 | * (non-Javadoc) | |
224 | * @see org.eclipse.linuxtools.tmf.ui.views.uml2sd.core.ITimeRange#getEndTime() | |
73005152 BH |
225 | */ |
226 | @Override | |
4df4581d | 227 | public ITmfTimestamp getEndTime() { |
eb63f5ff | 228 | return fEventTime; |
73005152 BH |
229 | } |
230 | ||
df0b8ff4 BH |
231 | /* |
232 | * (non-Javadoc) | |
233 | * @see org.eclipse.linuxtools.tmf.ui.views.uml2sd.core.ITimeRange#getStartTime() | |
234 | */ | |
73005152 | 235 | @Override |
4df4581d | 236 | public ITmfTimestamp getStartTime() { |
eb63f5ff | 237 | return fEventTime; |
73005152 BH |
238 | } |
239 | ||
df0b8ff4 BH |
240 | /* |
241 | * (non-Javadoc) | |
242 | * @see org.eclipse.linuxtools.tmf.ui.views.uml2sd.core.ITimeRange#hasTimeInfo() | |
243 | */ | |
73005152 BH |
244 | @Override |
245 | public boolean hasTimeInfo() { | |
eb63f5ff | 246 | return fHasTimeInfo; |
73005152 BH |
247 | } |
248 | ||
df0b8ff4 BH |
249 | /* |
250 | * (non-Javadoc) | |
251 | * @see org.eclipse.linuxtools.tmf.ui.views.uml2sd.core.BaseMessage#draw(org.eclipse.linuxtools.tmf.ui.views.uml2sd.drawings.IGC) | |
252 | */ | |
73005152 BH |
253 | @Override |
254 | public void draw(IGC context) { | |
255 | if (!isVisible()) | |
256 | return; | |
df0b8ff4 | 257 | // Draw it selected? |
73005152 | 258 | if (!isSelected()) { |
eb63f5ff BH |
259 | context.setBackground(Frame.getUserPref().getBackGroundColor(fPrefId)); |
260 | context.setForeground(Frame.getUserPref().getForeGroundColor(fPrefId)); | |
73005152 BH |
261 | } |
262 | super.draw(context); | |
263 | } | |
264 | ||
df0b8ff4 BH |
265 | /* |
266 | * (non-Javadoc) | |
267 | * @see org.eclipse.linuxtools.tmf.ui.views.uml2sd.core.BaseMessage#isVisible(int, int, int, int) | |
268 | */ | |
73005152 BH |
269 | @Override |
270 | public boolean isVisible(int x, int y, int width, int height) { | |
271 | if (getY() > y + height + | |
df0b8ff4 BH |
272 | // take into account the message name drawn above the arrow |
273 | Metrics.MESSAGES_NAME_SPACING + Metrics.getMessageFontHeigth()) { | |
73005152 | 274 | return false; |
df0b8ff4 | 275 | } |
73005152 BH |
276 | |
277 | // UML2 lost/found message visibility special case | |
278 | // Others visibility cases are perform in the ***common*** case | |
eb63f5ff | 279 | if ((fEndLifeline == null && fStartLifeline != null) || (fEndLifeline != null && fStartLifeline == null)) { |
df0b8ff4 | 280 | if (x + width > getX() + getWidth() && x < getX() + getWidth()) { |
73005152 | 281 | return true; |
df0b8ff4 | 282 | } |
73005152 BH |
283 | } |
284 | // ***Common*** syncMessages visibility | |
285 | return super.isVisible(x, y, width, height); | |
286 | } | |
287 | ||
df0b8ff4 BH |
288 | /* |
289 | * (non-Javadoc) | |
290 | * @see org.eclipse.linuxtools.tmf.ui.views.uml2sd.core.GraphNode#getComparator() | |
291 | */ | |
73005152 BH |
292 | @Override |
293 | public Comparator<GraphNode> getComparator() { | |
294 | return new SortSyncMessageComparator(); | |
295 | } | |
296 | ||
df0b8ff4 BH |
297 | /* |
298 | * (non-Javadoc) | |
299 | * @see org.eclipse.linuxtools.tmf.ui.views.uml2sd.core.GraphNode#getArrayId() | |
300 | */ | |
73005152 BH |
301 | @Override |
302 | public String getArrayId() { | |
303 | return SYNC_MESS_TAG; | |
304 | } | |
305 | ||
df0b8ff4 BH |
306 | /* |
307 | * (non-Javadoc) | |
308 | * @see org.eclipse.linuxtools.tmf.ui.views.uml2sd.core.GraphNode#positiveDistanceToPoint(int, int) | |
309 | */ | |
73005152 BH |
310 | @Override |
311 | public boolean positiveDistanceToPoint(int x, int y) { | |
df0b8ff4 | 312 | if (getY() > y) { |
73005152 | 313 | return true; |
df0b8ff4 | 314 | } |
73005152 BH |
315 | return false; |
316 | } | |
317 | } |