Commit | Line | Data |
---|---|---|
7b79ee46 FLN |
1 | /******************************************************************************* |
2 | * Copyright (c) 2015 Ericsson | |
3 | * | |
4 | * All rights reserved. This program and the accompanying materials are | |
5 | * made available under the terms of the Eclipse Public License v1.0 which | |
6 | * accompanies this distribution, and is available at | |
7 | * http://www.eclipse.org/legal/epl-v10.html | |
8 | * | |
9 | * Contributors: | |
10 | * France Lapointe Nguyen - Initial API and implementation | |
b9fff7ad | 11 | * Bernd Hufmann - MOve abstract class to TMF |
7b79ee46 FLN |
12 | *******************************************************************************/ |
13 | ||
b9fff7ad | 14 | package org.eclipse.tracecompass.internal.analysis.timing.ui.views.segmentstore; |
7b79ee46 | 15 | |
aa353506 AM |
16 | import static org.eclipse.tracecompass.common.core.NonNullUtils.checkNotNullContents; |
17 | ||
7b79ee46 | 18 | import java.util.Arrays; |
6f9a98e1 | 19 | import java.util.Collection; |
7b79ee46 FLN |
20 | import java.util.Comparator; |
21 | ||
aa353506 | 22 | import org.eclipse.jdt.annotation.NonNull; |
7b79ee46 FLN |
23 | import org.eclipse.jdt.annotation.Nullable; |
24 | import org.eclipse.jface.viewers.TableViewer; | |
25 | import org.eclipse.jface.viewers.Viewer; | |
26 | import org.eclipse.tracecompass.segmentstore.core.ISegment; | |
27 | import org.eclipse.tracecompass.segmentstore.core.ISegmentStore; | |
28 | import org.eclipse.tracecompass.tmf.ui.viewers.table.ISortingLazyContentProvider; | |
29 | ||
30 | import com.google.common.collect.Iterables; | |
31 | ||
32 | /** | |
33 | * Content provider for the latency table viewers. | |
34 | * | |
35 | * @author France Lapointe Nguyen | |
b9fff7ad | 36 | * @since 2.0 |
7b79ee46 | 37 | */ |
b9fff7ad | 38 | public class SegmentStoreContentProvider implements ISortingLazyContentProvider { |
7b79ee46 FLN |
39 | |
40 | /** | |
41 | * Array of all the segments in the segment store of the current trace | |
42 | */ | |
4c4e2816 | 43 | private ISegment @Nullable [] fSegmentArray = null; |
7b79ee46 FLN |
44 | |
45 | /** | |
46 | * Table viewer of the latency table viewer | |
47 | */ | |
48 | private @Nullable TableViewer fTableViewer = null; | |
49 | ||
50 | /** | |
51 | * Segment comparator | |
52 | */ | |
53 | private @Nullable Comparator<ISegment> fComparator = null; | |
54 | ||
55 | @Override | |
56 | public void updateElement(int index) { | |
57 | final TableViewer tableViewer = fTableViewer; | |
aa353506 | 58 | final ISegment @Nullable [] segmentArray = fSegmentArray; |
7b79ee46 FLN |
59 | if (tableViewer != null && segmentArray != null) { |
60 | tableViewer.replace(segmentArray[index], index); | |
61 | } | |
62 | } | |
63 | ||
64 | @Override | |
65 | public void dispose() { | |
66 | fSegmentArray = null; | |
67 | fTableViewer = null; | |
68 | fComparator = null; | |
69 | } | |
70 | ||
71 | @Override | |
72 | public void inputChanged(@Nullable Viewer viewer, @Nullable Object oldInput, @Nullable Object newInput) { | |
73 | fTableViewer = (TableViewer) viewer; | |
6f9a98e1 | 74 | if (newInput instanceof Collection<?> || newInput instanceof ISegmentStore) { |
aa353506 | 75 | @SuppressWarnings("unchecked") |
6f9a98e1 | 76 | Collection<ISegment> segmentStore = (Collection<@NonNull ISegment>) newInput; |
aa353506 AM |
77 | ISegment[] array = Iterables.toArray(segmentStore, ISegment.class); |
78 | @NonNull ISegment[] checkedArray = checkNotNullContents(array); | |
7b79ee46 | 79 | if (fComparator != null) { |
aa353506 | 80 | Arrays.sort(checkedArray, fComparator); |
7b79ee46 | 81 | } |
aa353506 | 82 | fSegmentArray = checkedArray; |
bd53eb28 | 83 | } else if (newInput instanceof ISegment[]) { |
aa353506 AM |
84 | /* |
85 | * Ensure that there are no null elements in the array, so we can | |
86 | * set it back to fSegmentArray, which does not allow nulls. | |
87 | */ | |
88 | @NonNull ISegment[] checkedArray = checkNotNullContents((@Nullable ISegment[]) newInput); | |
bd53eb28 | 89 | if (fComparator != null) { |
aa353506 | 90 | Arrays.sort(checkedArray, fComparator); |
bd53eb28 | 91 | } |
aa353506 | 92 | fSegmentArray = checkedArray; |
7b79ee46 FLN |
93 | } else { |
94 | fSegmentArray = null; | |
95 | } | |
96 | } | |
97 | ||
98 | @Override | |
99 | public void setSortOrder(@Nullable Comparator<?> comparator) { | |
aa353506 | 100 | @NonNull ISegment @Nullable [] segmentArray = fSegmentArray; |
7b79ee46 FLN |
101 | if (comparator == null) { |
102 | return; | |
103 | } | |
aa353506 | 104 | if (segmentArray == null) { |
7b79ee46 FLN |
105 | return; |
106 | } | |
107 | final TableViewer tableViewer = fTableViewer; | |
108 | if (tableViewer == null) { | |
109 | return; | |
110 | } | |
aa353506 AM |
111 | @SuppressWarnings("unchecked") |
112 | Comparator<ISegment> comp = (Comparator<ISegment>) comparator; | |
113 | fComparator = comp; | |
114 | Arrays.sort(segmentArray, fComparator); | |
7b79ee46 FLN |
115 | tableViewer.refresh(); |
116 | } | |
117 | ||
118 | /** | |
119 | * Get the segment count | |
120 | * | |
121 | * @return the segment count | |
122 | */ | |
123 | public int getSegmentCount() { | |
124 | ISegment[] segmentArray = fSegmentArray; | |
125 | return (segmentArray == null ? 0 : segmentArray.length); | |
126 | } | |
127 | } |