| 1 | /********************************************************************** |
| 2 | * Copyright (c) 2005, 2014 IBM Corporation, Ericsson |
| 3 | * All rights reserved. This program and the accompanying materials |
| 4 | * are made available under the terms of the Eclipse Public License v1.0 |
| 5 | * which accompanies this distribution, and is available at |
| 6 | * http://www.eclipse.org/legal/epl-v10.html |
| 7 | * |
| 8 | * Contributors: |
| 9 | * IBM - Initial API and implementation |
| 10 | * Bernd Hufmann - Updated for TMF |
| 11 | **********************************************************************/ |
| 12 | |
| 13 | package org.eclipse.tracecompass.tmf.ui.views.uml2sd.core; |
| 14 | |
| 15 | import org.eclipse.tracecompass.tmf.ui.views.uml2sd.drawings.IColor; |
| 16 | import org.eclipse.tracecompass.tmf.ui.views.uml2sd.drawings.IGC; |
| 17 | import org.eclipse.tracecompass.tmf.ui.views.uml2sd.preferences.ISDPreferences; |
| 18 | import org.eclipse.tracecompass.tmf.ui.views.uml2sd.preferences.SDViewPref; |
| 19 | |
| 20 | /** |
| 21 | * BasicExecutionOccurrence is the UML2 execution occurrence graphical representation. It is attached to one Lifeline, |
| 22 | * the event occurrence "duration" along the lifeline is defined by two event occurrences |
| 23 | * |
| 24 | * @see org.eclipse.tracecompass.tmf.ui.views.uml2sd.core.Lifeline Lifeline for more event occurence details |
| 25 | * @version 1.0 |
| 26 | * @author sveyrier |
| 27 | * |
| 28 | */ |
| 29 | public class BasicExecutionOccurrence extends GraphNode { |
| 30 | |
| 31 | // ------------------------------------------------------------------------ |
| 32 | // Constants |
| 33 | // ------------------------------------------------------------------------ |
| 34 | /** |
| 35 | * The grahNode ID constant |
| 36 | */ |
| 37 | public static final String EXEC_OCC_TAG = "Execution_Occ"; //$NON-NLS-1$ |
| 38 | |
| 39 | // ------------------------------------------------------------------------ |
| 40 | // Attributes |
| 41 | // ------------------------------------------------------------------------ |
| 42 | |
| 43 | /** |
| 44 | * The corresponding lifeline. |
| 45 | */ |
| 46 | private Lifeline fLifeline = null; |
| 47 | |
| 48 | // ------------------------------------------------------------------------ |
| 49 | // Constructors |
| 50 | // ------------------------------------------------------------------------ |
| 51 | /** |
| 52 | * Default constructore |
| 53 | */ |
| 54 | public BasicExecutionOccurrence() { |
| 55 | setColorPrefId(ISDPreferences.PREF_EXEC); |
| 56 | } |
| 57 | |
| 58 | // ------------------------------------------------------------------------ |
| 59 | // Constants |
| 60 | // ------------------------------------------------------------------------ |
| 61 | |
| 62 | @Override |
| 63 | public int getX() { |
| 64 | if (fLifeline == null) { |
| 65 | return 0; |
| 66 | } |
| 67 | return fLifeline.getX() + Metrics.getLifelineWidth() / 2 - Metrics.EXECUTION_OCCURRENCE_WIDTH / 2; |
| 68 | } |
| 69 | |
| 70 | @Override |
| 71 | public int getY() { |
| 72 | if (fLifeline == null) { |
| 73 | return 0; |
| 74 | } |
| 75 | return fLifeline.getY() + fLifeline.getHeight() + (Metrics.getMessageFontHeigth() + Metrics.getMessagesSpacing()) * getStartOccurrence(); |
| 76 | } |
| 77 | |
| 78 | @Override |
| 79 | public int getWidth() { |
| 80 | if (fLifeline == null) { |
| 81 | return 0; |
| 82 | } |
| 83 | return Metrics.EXECUTION_OCCURRENCE_WIDTH; |
| 84 | } |
| 85 | |
| 86 | @Override |
| 87 | public int getHeight() { |
| 88 | if (fLifeline == null) { |
| 89 | return 0; |
| 90 | } |
| 91 | return ((Metrics.getMessageFontHeigth() + Metrics.getMessagesSpacing())) * (getEndOccurrence() - getStartOccurrence()); |
| 92 | } |
| 93 | |
| 94 | @Override |
| 95 | public boolean contains(int xValue, int yValue) { |
| 96 | int x = getX(); |
| 97 | int y = getY(); |
| 98 | int width = getWidth(); |
| 99 | int height = getHeight(); |
| 100 | |
| 101 | if (GraphNode.contains(x, y, width, height, xValue, yValue)) { |
| 102 | return true; |
| 103 | } |
| 104 | |
| 105 | if (getNodeAt(xValue, yValue) != null) { |
| 106 | return true; |
| 107 | } |
| 108 | return false; |
| 109 | } |
| 110 | |
| 111 | @Override |
| 112 | public String getName() { |
| 113 | if (super.getName() == null || super.getName().equals("")) { //$NON-NLS-1$ |
| 114 | return fLifeline.getToolTipText(); |
| 115 | } |
| 116 | return super.getName(); |
| 117 | } |
| 118 | |
| 119 | /** |
| 120 | * Set the lifeline on which the execution occurrence appears. |
| 121 | * |
| 122 | * @param theLifeline - the parent lifeline |
| 123 | */ |
| 124 | public void setLifeline(Lifeline theLifeline) { |
| 125 | fLifeline = theLifeline; |
| 126 | } |
| 127 | |
| 128 | /** |
| 129 | * Get the lifeline on which the execution occurrence appears. |
| 130 | * |
| 131 | * @return - the parent lifeline |
| 132 | */ |
| 133 | public Lifeline getLifeline() { |
| 134 | return fLifeline; |
| 135 | } |
| 136 | |
| 137 | /** |
| 138 | * Set the execution start event occurrence |
| 139 | * |
| 140 | * @param occurrence the start event occurrence to set |
| 141 | */ |
| 142 | @Override |
| 143 | public void setStartOccurrence(int occurrence) { |
| 144 | super.setStartOccurrence(occurrence); |
| 145 | } |
| 146 | |
| 147 | /** |
| 148 | * Set the execution end event occurrence |
| 149 | * |
| 150 | * @param occurrence the end event occurrence to set |
| 151 | */ |
| 152 | @Override |
| 153 | public void setEndOccurrence(int occurrence) { |
| 154 | super.setEndOccurrence(occurrence); |
| 155 | } |
| 156 | |
| 157 | @Override |
| 158 | public void draw(IGC context) { |
| 159 | int x = getX(); |
| 160 | int y = getY(); |
| 161 | int width = getWidth(); |
| 162 | int height = getHeight(); |
| 163 | IColor tempFillColor = null; |
| 164 | IColor tempStrokeColor = null; |
| 165 | |
| 166 | ISDPreferences pref = SDViewPref.getInstance(); |
| 167 | |
| 168 | // The execution occurrence is selected |
| 169 | // if the owning lifeline is selected |
| 170 | if (fLifeline.isSelected() || isSelected()) { |
| 171 | context.setBackground(pref.getBackGroundColorSelection()); |
| 172 | context.setForeground(pref.getForeGroundColorSelection()); |
| 173 | } else { |
| 174 | tempFillColor = setUnselectedFillColor(context); |
| 175 | } |
| 176 | if (pref.useGradienColor()) { |
| 177 | context.fillGradientRectangle(x, y, width, height, false); |
| 178 | } else { |
| 179 | context.fillRectangle(x, y, width, height); |
| 180 | } |
| 181 | tempStrokeColor = setUnselectedStrokeColor(context); |
| 182 | context.drawRectangle(x, y, width, height); |
| 183 | if (tempFillColor != null) { |
| 184 | tempFillColor.dispose(); |
| 185 | } |
| 186 | if (tempStrokeColor != null) { |
| 187 | tempStrokeColor.dispose(); |
| 188 | } |
| 189 | if (hasFocus()) { |
| 190 | drawFocus(context); |
| 191 | } |
| 192 | super.drawChildenNodes(context); |
| 193 | } |
| 194 | |
| 195 | /** |
| 196 | * Rewrite this method in your extension in order to support customized fill colors |
| 197 | * |
| 198 | * @param context Graphics context |
| 199 | * @return IColor |
| 200 | */ |
| 201 | protected IColor setUnselectedFillColor(IGC context) { |
| 202 | |
| 203 | ISDPreferences pref = SDViewPref.getInstance(); |
| 204 | |
| 205 | if (pref.useGradienColor()) { |
| 206 | context.setGradientColor(pref.getBackGroundColor(ISDPreferences.PREF_EXEC)); |
| 207 | context.setBackground(pref.getBackGroundColor(ISDPreferences.PREF_FRAME)); |
| 208 | } else { |
| 209 | context.setBackground(pref.getBackGroundColor(ISDPreferences.PREF_EXEC)); |
| 210 | } |
| 211 | return null; |
| 212 | } |
| 213 | |
| 214 | /** |
| 215 | * Rewrite this method in your extension in order to support customized stroke colors |
| 216 | * |
| 217 | * @param context Graphics context |
| 218 | * @return IColor |
| 219 | */ |
| 220 | protected IColor setUnselectedStrokeColor(IGC context) { |
| 221 | context.setForeground(SDViewPref.getInstance().getForeGroundColor(ISDPreferences.PREF_EXEC)); |
| 222 | return null; |
| 223 | } |
| 224 | |
| 225 | @Override |
| 226 | public String getArrayId() { |
| 227 | return EXEC_OCC_TAG; |
| 228 | } |
| 229 | |
| 230 | @Override |
| 231 | public boolean positiveDistanceToPoint(int x, int y) { |
| 232 | if (getY() + getHeight() > y) { |
| 233 | return true; |
| 234 | } |
| 235 | return false; |
| 236 | } |
| 237 | |
| 238 | @Override |
| 239 | public boolean isVisible(int x, int y, int width, int height) { |
| 240 | if ((getLifeline() != null) && (getLifeline().isVisible(x, y, width, height))) { |
| 241 | int ly = getY(); |
| 242 | int lh = getHeight(); |
| 243 | if (ly >= y && ly < y + height) { |
| 244 | return true; |
| 245 | } |
| 246 | if (ly + lh > y && ly + lh <= y + height) { |
| 247 | return true; |
| 248 | } |
| 249 | if ((ly < y) && (ly + lh > y + height)) { |
| 250 | return true; |
| 251 | } |
| 252 | } |
| 253 | return false; |
| 254 | } |
| 255 | } |