tmf: Move timestamps to their own package
[deliverable/tracecompass.git] / org.eclipse.linuxtools.tmf.ui / src / org / eclipse / linuxtools / tmf / ui / views / uml2sd / core / SyncMessage.java
CommitLineData
73005152 1/**********************************************************************
df0b8ff4
BH
2 * Copyright (c) 2005, 2006 IBM Corporation and others.
3 * Copyright (c) 2011, 2012 Ericsson.
4593bd5b 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
4593bd5b
AM
9 *
10 * Contributors:
73005152
BH
11 * IBM - Initial API and implementation
12 * Bernd Hufmann - Updated for TMF
13 **********************************************************************/
14package org.eclipse.linuxtools.tmf.ui.views.uml2sd.core;
15
16import java.util.Comparator;
17
3bd46eef
AM
18import org.eclipse.linuxtools.tmf.core.timestamp.ITmfTimestamp;
19import org.eclipse.linuxtools.tmf.core.timestamp.TmfTimestamp;
73005152 20import org.eclipse.linuxtools.tmf.ui.views.uml2sd.drawings.IGC;
df0b8ff4 21import org.eclipse.linuxtools.tmf.ui.views.uml2sd.preferences.ISDPreferences;
3145ec83 22import org.eclipse.linuxtools.tmf.ui.views.uml2sd.preferences.SDViewPref;
73005152
BH
23import 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:
4593bd5b 32 *
73005152
BH
33 * <pre>
34 * Frame frame;
35 * Lifeline lifeLine1;
36 * Lifeline lifeLine2;
4593bd5b 37 *
73005152
BH
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(&quot;Message label&quot;);
46 * // add the message to the frame
47 * frame.addMessage(message);
48 * </pre>
4593bd5b 49 *
73005152 50 * @see org.eclipse.linuxtools.tmf.ui.views.uml2sd.core.Lifeline Lifeline for more event occurence details
df0b8ff4 51 * @version 1.0
73005152 52 * @author sveyrier
4593bd5b 53 *
73005152
BH
54 */
55public 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.
4593bd5b 120 *
73005152
BH
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.
4593bd5b 134 *
73005152
BH
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.
4593bd5b 147 *
73005152
BH
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.
4593bd5b 165 *
73005152
BH
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>
4593bd5b 180 *
73005152
BH
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.
4593bd5b 192 *
73005152
BH
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
4593bd5b 201 *
73005152
BH
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
4593bd5b 210 *
73005152 211 * @param time the time when the message occurs
3bd46eef 212 * @since 2.0
73005152 213 */
4df4581d 214 public void setTime(ITmfTimestamp time) {
4593bd5b 215 fEventTime = time;
eb63f5ff 216 fHasTimeInfo = true;
df0b8ff4 217 if (getStartLifeline() != null && getStartLifeline().getFrame() != null) {
73005152 218 getStartLifeline().getFrame().setHasTimeInfo(true);
df0b8ff4 219 } else if (getEndLifeline() != null && getEndLifeline().getFrame() != null) {
73005152 220 getEndLifeline().getFrame().setHasTimeInfo(true);
df0b8ff4 221 }
73005152
BH
222 }
223
3bd46eef
AM
224 /**
225 * @since 2.0
73005152
BH
226 */
227 @Override
4df4581d 228 public ITmfTimestamp getEndTime() {
eb63f5ff 229 return fEventTime;
73005152
BH
230 }
231
3bd46eef
AM
232 /**
233 * @since 2.0
4593bd5b 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()
4593bd5b 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) {
3145ec83 255 if (!isVisible()) {
73005152 256 return;
3145ec83
BH
257 }
258
259 ISDPreferences pref = SDViewPref.getInstance();
260
df0b8ff4 261 // Draw it selected?
73005152 262 if (!isSelected()) {
3145ec83
BH
263 context.setBackground(pref.getBackGroundColor(fPrefId));
264 context.setForeground(pref.getForeGroundColor(fPrefId));
73005152
BH
265 }
266 super.draw(context);
267 }
268
df0b8ff4
BH
269 /*
270 * (non-Javadoc)
271 * @see org.eclipse.linuxtools.tmf.ui.views.uml2sd.core.BaseMessage#isVisible(int, int, int, int)
272 */
73005152
BH
273 @Override
274 public boolean isVisible(int x, int y, int width, int height) {
275 if (getY() > y + height +
df0b8ff4
BH
276 // take into account the message name drawn above the arrow
277 Metrics.MESSAGES_NAME_SPACING + Metrics.getMessageFontHeigth()) {
73005152 278 return false;
df0b8ff4 279 }
73005152
BH
280
281 // UML2 lost/found message visibility special case
282 // Others visibility cases are perform in the ***common*** case
eb63f5ff 283 if ((fEndLifeline == null && fStartLifeline != null) || (fEndLifeline != null && fStartLifeline == null)) {
df0b8ff4 284 if (x + width > getX() + getWidth() && x < getX() + getWidth()) {
73005152 285 return true;
df0b8ff4 286 }
73005152
BH
287 }
288 // ***Common*** syncMessages visibility
289 return super.isVisible(x, y, width, height);
290 }
291
df0b8ff4
BH
292 /*
293 * (non-Javadoc)
294 * @see org.eclipse.linuxtools.tmf.ui.views.uml2sd.core.GraphNode#getComparator()
295 */
73005152
BH
296 @Override
297 public Comparator<GraphNode> getComparator() {
298 return new SortSyncMessageComparator();
299 }
300
df0b8ff4
BH
301 /*
302 * (non-Javadoc)
303 * @see org.eclipse.linuxtools.tmf.ui.views.uml2sd.core.GraphNode#getArrayId()
304 */
73005152
BH
305 @Override
306 public String getArrayId() {
307 return SYNC_MESS_TAG;
308 }
309
df0b8ff4
BH
310 /*
311 * (non-Javadoc)
312 * @see org.eclipse.linuxtools.tmf.ui.views.uml2sd.core.GraphNode#positiveDistanceToPoint(int, int)
313 */
73005152
BH
314 @Override
315 public boolean positiveDistanceToPoint(int x, int y) {
df0b8ff4 316 if (getY() > y) {
73005152 317 return true;
df0b8ff4 318 }
73005152
BH
319 return false;
320 }
321}
This page took 0.050503 seconds and 5 git commands to generate.