6df1fa22289858f891e033e11c3aef69392ef607
[deliverable/tracecompass.git] / org.eclipse.linuxtools.tmf.ui / src / org / eclipse / linuxtools / tmf / ui / views / uml2sd / core / SyncMessage.java
1 /**********************************************************************
2 * Copyright (c) 2005, 2006 IBM Corporation and others.
3 * Copyright (c) 2011, 2012 Ericsson.
4 *
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
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
18 import org.eclipse.linuxtools.tmf.core.timestamp.ITmfTimestamp;
19 import org.eclipse.linuxtools.tmf.core.timestamp.TmfTimestamp;
20 import org.eclipse.linuxtools.tmf.ui.views.uml2sd.drawings.IGC;
21 import org.eclipse.linuxtools.tmf.ui.views.uml2sd.preferences.ISDPreferences;
22 import org.eclipse.linuxtools.tmf.ui.views.uml2sd.preferences.SDViewPref;
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(&quot;Message label&quot;);
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
51 * @version 1.0
52 * @author sveyrier
53 *
54 */
55 public class SyncMessage extends BaseMessage implements ITimeRange {
56
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
69 /**
70 * The associated message return
71 */
72 protected SyncMessageReturn fMessageReturn;
73 /**
74 * The time when the message occurs
75 */
76 protected ITmfTimestamp fEventTime = new TmfTimestamp();
77 /**
78 * Flag whether the message has time information available or not
79 */
80 protected boolean fHasTimeInfo = false;
81
82 // ------------------------------------------------------------------------
83 // Constructors
84 // ------------------------------------------------------------------------
85
86 /**
87 * Default constructor
88 */
89 public SyncMessage() {
90 fPrefId = ISDPreferences.PREF_SYNC_MESS;
91 }
92
93 // ------------------------------------------------------------------------
94 // Methods
95 // ------------------------------------------------------------------------
96
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;
103 if (getStartLifeline().getEventOccurrence() > getEndLifeline().getEventOccurrence()) {
104 newIndex = getStartLifeline().getEventOccurrence();
105 } else {
106 newIndex = getEndLifeline().getEventOccurrence();
107 }
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());
155 } else {
156 syncLifelinesEventOccurrence();
157 }
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());
173 } else {
174 syncLifelinesEventOccurrence();
175 }
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) {
186 fStartEventOccurrence = occurrence;
187 fEndEventOccurrence = occurrence;
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) {
196 fMessageReturn = message;
197 }
198
199 /**
200 * Returns the syncMessageReturn associated to this syncMessage
201 *
202 * @return the message return
203 */
204 public SyncMessageReturn getMessageReturn() {
205 return fMessageReturn;
206 }
207
208 /**
209 * Set the time when the message occurs
210 *
211 * @param time the time when the message occurs
212 * @since 2.0
213 */
214 public void setTime(ITmfTimestamp time) {
215 fEventTime = time;
216 fHasTimeInfo = true;
217 if (getStartLifeline() != null && getStartLifeline().getFrame() != null) {
218 getStartLifeline().getFrame().setHasTimeInfo(true);
219 } else if (getEndLifeline() != null && getEndLifeline().getFrame() != null) {
220 getEndLifeline().getFrame().setHasTimeInfo(true);
221 }
222 }
223
224 /**
225 * @since 2.0
226 */
227 @Override
228 public ITmfTimestamp getEndTime() {
229 return fEventTime;
230 }
231
232 /**
233 * @since 2.0
234 */
235 @Override
236 public ITmfTimestamp getStartTime() {
237 return fEventTime;
238 }
239
240 /*
241 * (non-Javadoc)
242 * @see org.eclipse.linuxtools.tmf.ui.views.uml2sd.core.ITimeRange#hasTimeInfo()
243 */
244 @Override
245 public boolean hasTimeInfo() {
246 return fHasTimeInfo;
247 }
248
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 */
253 @Override
254 public void draw(IGC context) {
255 if (!isVisible()) {
256 return;
257 }
258
259 ISDPreferences pref = SDViewPref.getInstance();
260
261 // Draw it selected?
262 if (!isSelected()) {
263 context.setBackground(pref.getBackGroundColor(fPrefId));
264 context.setForeground(pref.getForeGroundColor(fPrefId));
265 }
266 super.draw(context);
267 }
268
269 /*
270 * (non-Javadoc)
271 * @see org.eclipse.linuxtools.tmf.ui.views.uml2sd.core.BaseMessage#isVisible(int, int, int, int)
272 */
273 @Override
274 public boolean isVisible(int x, int y, int width, int height) {
275 if (getY() > y + height +
276 // take into account the message name drawn above the arrow
277 Metrics.MESSAGES_NAME_SPACING + Metrics.getMessageFontHeigth()) {
278 return false;
279 }
280
281 // UML2 lost/found message visibility special case
282 // Others visibility cases are perform in the ***common*** case
283 if ((fEndLifeline == null && fStartLifeline != null) || (fEndLifeline != null && fStartLifeline == null)) {
284 if (x + width > getX() + getWidth() && x < getX() + getWidth()) {
285 return true;
286 }
287 }
288 // ***Common*** syncMessages visibility
289 return super.isVisible(x, y, width, height);
290 }
291
292 /*
293 * (non-Javadoc)
294 * @see org.eclipse.linuxtools.tmf.ui.views.uml2sd.core.GraphNode#getComparator()
295 */
296 @Override
297 public Comparator<GraphNode> getComparator() {
298 return new SortSyncMessageComparator();
299 }
300
301 /*
302 * (non-Javadoc)
303 * @see org.eclipse.linuxtools.tmf.ui.views.uml2sd.core.GraphNode#getArrayId()
304 */
305 @Override
306 public String getArrayId() {
307 return SYNC_MESS_TAG;
308 }
309
310 /*
311 * (non-Javadoc)
312 * @see org.eclipse.linuxtools.tmf.ui.views.uml2sd.core.GraphNode#positiveDistanceToPoint(int, int)
313 */
314 @Override
315 public boolean positiveDistanceToPoint(int x, int y) {
316 if (getY() > y) {
317 return true;
318 }
319 return false;
320 }
321 }
This page took 0.037008 seconds and 5 git commands to generate.