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