| 1 | /*******************************************************************************\r |
| 2 | * Copyright (c) 2010 Ericsson\r |
| 3 | * \r |
| 4 | * All rights reserved. This program and the accompanying materials are\r |
| 5 | * made available under the terms of the Eclipse Public License v1.0 which\r |
| 6 | * accompanies this distribution, and is available at\r |
| 7 | * http://www.eclipse.org/legal/epl-v10.html\r |
| 8 | * \r |
| 9 | * Contributors:\r |
| 10 | * Patrick Tasse - Initial API and implementation\r |
| 11 | *******************************************************************************/\r |
| 12 | \r |
| 13 | package org.eclipse.linuxtools.tmf.ui.views.timechart;\r |
| 14 | \r |
| 15 | import java.util.ArrayList;\r |
| 16 | import java.util.Iterator;\r |
| 17 | \r |
| 18 | import org.eclipse.linuxtools.tmf.core.event.TmfEvent;\r |
| 19 | import org.eclipse.linuxtools.tmf.ui.viewers.timeAnalysis.model.ITimeEvent;\r |
| 20 | import org.eclipse.linuxtools.tmf.ui.viewers.timeAnalysis.model.ITmfTimeAnalysisEntry;\r |
| 21 | import org.eclipse.linuxtools.tmf.ui.views.colors.ColorSettingsManager;\r |
| 22 | \r |
| 23 | public class TimeChartEvent implements ITimeEvent {\r |
| 24 | \r |
| 25 | private static final byte TIMESTAMP_SCALE = -9;\r |
| 26 | \r |
| 27 | private TimeChartAnalysisEntry fParentEntry;\r |
| 28 | private long fTime;\r |
| 29 | private long fDuration;\r |
| 30 | private long fFirstRank;\r |
| 31 | private long fLastRank;\r |
| 32 | private RankRangeList fRankRangeList;\r |
| 33 | private long fNbEvents;\r |
| 34 | private int fColorSettingPriority;\r |
| 35 | private boolean fIsBookmark;\r |
| 36 | private boolean fIsVisible;\r |
| 37 | private boolean fIsSearchMatch;\r |
| 38 | private TimeChartAnalysisEntry fItemizedEntry;\r |
| 39 | private boolean fItemizing;\r |
| 40 | \r |
| 41 | public TimeChartEvent(TimeChartAnalysisEntry parentEntry, TmfEvent event, long rank, TimeChartDecorationProvider decorationProvider) {\r |
| 42 | fParentEntry = parentEntry;\r |
| 43 | fTime = event.getTimestamp().synchronize((long) 0, TIMESTAMP_SCALE).getValue();\r |
| 44 | fDuration = 0;\r |
| 45 | fFirstRank = fLastRank = rank;\r |
| 46 | fRankRangeList = new RankRangeList(rank);\r |
| 47 | fNbEvents = 1;\r |
| 48 | fColorSettingPriority = ColorSettingsManager.getColorSettingPriority(event);\r |
| 49 | fIsBookmark = decorationProvider.isBookmark(rank);\r |
| 50 | fIsVisible = decorationProvider.isVisible(event);\r |
| 51 | fIsSearchMatch = decorationProvider.isSearchMatch(event);\r |
| 52 | }\r |
| 53 | \r |
| 54 | @Override\r |
| 55 | public ITmfTimeAnalysisEntry getEntry() {\r |
| 56 | return fParentEntry;\r |
| 57 | }\r |
| 58 | \r |
| 59 | @Override\r |
| 60 | public long getTime() {\r |
| 61 | return fTime;\r |
| 62 | }\r |
| 63 | \r |
| 64 | @Override\r |
| 65 | public long getDuration() {\r |
| 66 | return fDuration;\r |
| 67 | }\r |
| 68 | \r |
| 69 | public long getFirstRank() {\r |
| 70 | return fFirstRank;\r |
| 71 | }\r |
| 72 | \r |
| 73 | public long getLastRank() {\r |
| 74 | return fLastRank;\r |
| 75 | }\r |
| 76 | \r |
| 77 | public RankRangeList getRankRangeList() {\r |
| 78 | return fRankRangeList;\r |
| 79 | }\r |
| 80 | \r |
| 81 | public void merge(TimeChartEvent event) {\r |
| 82 | mergeDecorations(event);\r |
| 83 | if (fTime == event.getTime() && fDuration == event.getDuration()) return;\r |
| 84 | long endTime = Math.max(fTime + fDuration, event.getTime() + event.getDuration());\r |
| 85 | fTime = Math.min(fTime, event.getTime());\r |
| 86 | fDuration = endTime - fTime;\r |
| 87 | fFirstRank = Math.min(fFirstRank, event.fFirstRank);\r |
| 88 | fLastRank = Math.max(fLastRank, event.fLastRank);\r |
| 89 | fNbEvents += event.fNbEvents;\r |
| 90 | fItemizedEntry = null;\r |
| 91 | synchronized (fRankRangeList) {\r |
| 92 | fRankRangeList.merge(event.getRankRangeList());\r |
| 93 | }\r |
| 94 | }\r |
| 95 | \r |
| 96 | public void mergeDecorations(TimeChartEvent event) {\r |
| 97 | fColorSettingPriority = Math.min(fColorSettingPriority, event.getColorSettingPriority());\r |
| 98 | fIsBookmark |= event.fIsBookmark;\r |
| 99 | fIsVisible |= event.fIsVisible;\r |
| 100 | fIsSearchMatch |= event.fIsSearchMatch;\r |
| 101 | }\r |
| 102 | \r |
| 103 | public long getNbEvents() {\r |
| 104 | return fNbEvents;\r |
| 105 | }\r |
| 106 | \r |
| 107 | public int getColorSettingPriority() {\r |
| 108 | return fColorSettingPriority;\r |
| 109 | }\r |
| 110 | \r |
| 111 | public void setColorSettingPriority(int priority) {\r |
| 112 | fColorSettingPriority = priority;\r |
| 113 | }\r |
| 114 | \r |
| 115 | public boolean isBookmarked() {\r |
| 116 | return fIsBookmark;\r |
| 117 | }\r |
| 118 | \r |
| 119 | public void setIsBookmarked(boolean isBookmarked) {\r |
| 120 | fIsBookmark = isBookmarked;\r |
| 121 | }\r |
| 122 | \r |
| 123 | public boolean isVisible() {\r |
| 124 | return fIsVisible;\r |
| 125 | }\r |
| 126 | \r |
| 127 | public void setIsVisible(boolean isVisible) {\r |
| 128 | fIsVisible = isVisible;\r |
| 129 | }\r |
| 130 | \r |
| 131 | public boolean isSearchMatch() {\r |
| 132 | return fIsSearchMatch;\r |
| 133 | }\r |
| 134 | \r |
| 135 | public void setIsSearchMatch(boolean isSearchMatch) {\r |
| 136 | fIsSearchMatch = isSearchMatch;\r |
| 137 | }\r |
| 138 | \r |
| 139 | public void setItemizedEntry(TimeChartAnalysisEntry timeAnalysisEntry) {\r |
| 140 | fItemizedEntry = timeAnalysisEntry;\r |
| 141 | }\r |
| 142 | \r |
| 143 | public TimeChartAnalysisEntry getItemizedEntry() {\r |
| 144 | return fItemizedEntry;\r |
| 145 | }\r |
| 146 | \r |
| 147 | public boolean isItemizing() {\r |
| 148 | return fItemizing;\r |
| 149 | }\r |
| 150 | \r |
| 151 | public void setItemizing(boolean itemizing) {\r |
| 152 | fItemizing = itemizing;\r |
| 153 | }\r |
| 154 | \r |
| 155 | public class RankRange {\r |
| 156 | private long firstRank;\r |
| 157 | private long lastRank;\r |
| 158 | \r |
| 159 | public RankRange(long firstRank, long lastRank) {\r |
| 160 | this.firstRank = firstRank;\r |
| 161 | this.lastRank = lastRank;\r |
| 162 | }\r |
| 163 | \r |
| 164 | public long getFirstRank() {\r |
| 165 | return firstRank;\r |
| 166 | }\r |
| 167 | \r |
| 168 | public long getLastRank() {\r |
| 169 | return lastRank;\r |
| 170 | }\r |
| 171 | \r |
| 172 | public long distanceFrom(RankRange range) {\r |
| 173 | if (range.lastRank < fFirstRank) {\r |
| 174 | return fFirstRank - range.lastRank;\r |
| 175 | } else if (range.firstRank > fLastRank) {\r |
| 176 | return range.firstRank - fLastRank;\r |
| 177 | } else {\r |
| 178 | return 0;\r |
| 179 | }\r |
| 180 | }\r |
| 181 | \r |
| 182 | @Override\r |
| 183 | public String toString() {\r |
| 184 | return "["+firstRank+","+lastRank+"]"; //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$\r |
| 185 | }\r |
| 186 | }\r |
| 187 | \r |
| 188 | private class RankRangeList extends ArrayList<RankRange> {\r |
| 189 | \r |
| 190 | private static final long serialVersionUID = 6060485531208535986L;\r |
| 191 | \r |
| 192 | public RankRangeList(long rank) {\r |
| 193 | super(1);\r |
| 194 | add(new RankRange(rank, rank));\r |
| 195 | }\r |
| 196 | \r |
| 197 | public void merge(RankRangeList rankRangeList) {\r |
| 198 | long threshold = fParentEntry.getTrace().getCacheSize();\r |
| 199 | for (RankRange newRange : rankRangeList) {\r |
| 200 | boolean merged = false;\r |
| 201 | for (RankRange oldRange : fRankRangeList) {\r |
| 202 | if (newRange.distanceFrom(oldRange) <= threshold) {\r |
| 203 | oldRange.firstRank = Math.min(oldRange.firstRank, newRange.firstRank);\r |
| 204 | oldRange.lastRank = Math.max(oldRange.lastRank, newRange.lastRank);\r |
| 205 | merged = true;\r |
| 206 | break;\r |
| 207 | }\r |
| 208 | }\r |
| 209 | if (!merged) {\r |
| 210 | add(newRange);\r |
| 211 | }\r |
| 212 | }\r |
| 213 | Iterator<RankRange> iterator = fRankRangeList.iterator();\r |
| 214 | RankRange previous = null;\r |
| 215 | while (iterator.hasNext()) {\r |
| 216 | RankRange range = iterator.next();\r |
| 217 | if (previous != null && range.distanceFrom(previous) <= threshold) {\r |
| 218 | previous.firstRank = Math.min(previous.firstRank, range.firstRank);\r |
| 219 | previous.lastRank = Math.max(previous.lastRank, range.lastRank);\r |
| 220 | iterator.remove();\r |
| 221 | }\r |
| 222 | previous = range;\r |
| 223 | }\r |
| 224 | }\r |
| 225 | }\r |
| 226 | }\r |