1 /*******************************************************************************
2 * Copyright (c) 2013, 2016 Ericsson
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
10 * Alexandre Montplaisir - Initial API and implementation
11 * Patrick Tasse - Support selection range
12 * Xavier Raynaud - Support filters tracking
13 *******************************************************************************/
15 package org
.eclipse
.tracecompass
.tmf
.core
.trace
;
17 import java
.util
.HashMap
;
20 import org
.eclipse
.core
.resources
.IFile
;
21 import org
.eclipse
.jdt
.annotation
.NonNull
;
22 import org
.eclipse
.jdt
.annotation
.NonNullByDefault
;
23 import org
.eclipse
.jdt
.annotation
.Nullable
;
24 import org
.eclipse
.tracecompass
.tmf
.core
.filter
.ITmfFilter
;
25 import org
.eclipse
.tracecompass
.tmf
.core
.timestamp
.TmfTimeRange
;
26 import org
.eclipse
.tracecompass
.tmf
.core
.timestamp
.TmfTimestamp
;
28 import com
.google
.common
.collect
.ImmutableMap
;
31 * Context of a trace, which is the representation of the "view" the user
32 * currently has on this trace (window time range, selected time or time range).
34 * TODO could be extended to support the notion of current location too.
36 * FIXME Is this really the right place for the Editor File ?
38 * @author Alexandre Montplaisir
42 public class TmfTraceContext
implements ITraceContextSignalHandler
{
44 static final TmfTraceContext NULL_CONTEXT
= new TmfTraceContext(new TmfTimeRange(TmfTimestamp
.BIG_CRUNCH
, TmfTimestamp
.BIG_CRUNCH
),
45 TmfTimeRange
.NULL_RANGE
, null, null);
47 private final TmfTimeRange fSelection
;
48 private final TmfTimeRange fWindowRange
;
49 private final @Nullable IFile fEditorFile
;
50 private final @Nullable ITmfFilter fFilter
;
51 private final Map
<@NonNull String
, @NonNull Object
> fData
;
54 * Build a new trace context.
57 * The selected time range
59 * The visible window's time range
61 * The file representing the selected editor
63 * The currently applied filter. 'null' for none.
65 public TmfTraceContext(TmfTimeRange selection
, TmfTimeRange windowRange
,
66 @Nullable IFile editorFile
, @Nullable ITmfFilter filter
) {
67 fSelection
= selection
;
68 fWindowRange
= windowRange
;
69 fEditorFile
= editorFile
;
71 fData
= new HashMap
<>();
75 * Constructs a new trace context with data taken from a builder.
81 public TmfTraceContext(Builder builder
) {
82 fSelection
= builder
.selection
;
83 fWindowRange
= builder
.windowRange
;
84 fEditorFile
= builder
.editorFile
;
85 fFilter
= builder
.filter
;
86 fData
= new HashMap
<>(builder
.data
);
90 * Return the time range representing the current active selection.
92 * @return The selected time range
94 public TmfTimeRange
getSelectionRange() {
99 * Return the current window time range.
101 * @return The current window time range
103 public TmfTimeRange
getWindowRange() {
108 * Get the editor's file
110 * @return The editor file
112 public @Nullable IFile
getEditorFile() {
117 * Gets the filter applied to the current trace
119 * @return The current filter, or <code>null</code> if there is none
121 public @Nullable ITmfFilter
getFilter() {
126 * Store a data for the trace
131 * The value of the data
134 public synchronized void setData(String key
, Object value
) {
135 fData
.put(key
, value
);
139 * Copy data into the data map
142 * The map of data to copy
145 public synchronized void setData(Map
<String
, Object
> data
) {
150 * Get the data for the specific key
154 * @return The data or null if the key do not exist
157 public synchronized @Nullable Object
getData(String key
) {
158 return fData
.get(key
);
162 * Get a copy of the data map
164 * @return The data map copy
167 public synchronized Map
<String
, Object
> getData() {
168 return ImmutableMap
.copyOf(fData
);
172 * Returns a new builder that is initialized with the data from this trace
175 * @return the builder
178 public Builder
builder() {
179 return new Builder(this);
183 * A builder for creating trace context instances.
187 public class Builder
{
188 private TmfTimeRange selection
;
189 private TmfTimeRange windowRange
;
190 private @Nullable IFile editorFile
;
191 private @Nullable ITmfFilter filter
;
192 private Map
<String
, Object
> data
;
198 * the trace context used to initialize the builder
200 public Builder(TmfTraceContext ctx
) {
201 this.selection
= ctx
.fSelection
;
202 this.windowRange
= ctx
.fWindowRange
;
203 this.editorFile
= ctx
.fEditorFile
;
204 this.filter
= ctx
.fFilter
;
205 this.data
= new HashMap
<>(ctx
.fData
);
209 * Build the trace context.
211 * @return a trace context
213 public TmfTraceContext
build() {
214 return new TmfTraceContext(this);
218 * Sets the selected time range.
221 * the selected time range
222 * @return this {@code Builder} object
224 public Builder
setSelection(TmfTimeRange selection
) {
225 this.selection
= selection
;
230 * Sets the window range.
234 * @return this {@code Builder} object
236 public Builder
setWindowRange(TmfTimeRange windowRange
) {
237 this.windowRange
= windowRange
;
242 * Sets the current filter.
246 * @return this {@code Builder} object
248 public Builder
setFilter(@Nullable ITmfFilter filter
) {
249 this.filter
= filter
;
255 public String
toString() {
256 return getClass().getSimpleName() + "[fSelection=" + fSelection
+ //$NON-NLS-1$
257 ", fWindowRange=" + fWindowRange
+ ']'; //$NON-NLS-1$