1 /*******************************************************************************
2 * Copyright (c) 2013, 2014 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 * Marc-Andre Laperle - Initial API and implementation
11 *******************************************************************************/
13 package org
.eclipse
.tracecompass
.internal
.tmf
.ui
.project
.wizards
.tracepkg
;
15 import org
.eclipse
.core
.runtime
.IPath
;
16 import org
.eclipse
.swt
.graphics
.Image
;
17 import org
.eclipse
.tracecompass
.tmf
.ui
.project
.model
.TmfCommonProjectElement
;
18 import org
.eclipse
.tracecompass
.tmf
.ui
.project
.model
.TmfNavigatorLabelProvider
;
19 import org
.eclipse
.tracecompass
.tmf
.ui
.project
.model
.TmfTraceElement
;
22 * An ExportTraceElement associated to a TmfTraceElement. This will be the
23 * parent of other elements (events, supplementary files, bookmarks, etc).
25 * @author Marc-Andre Laperle
27 public class TracePackageTraceElement
extends TracePackageElement
{
29 private final TmfTraceElement fTraceElement
;
30 private String fImportName
;
31 private String fTraceType
;
34 * Construct an instance associated to a TmfTraceElement. For exporting.
37 * the parent of this element, can be set to null
39 * the associated TmfTraceElement
41 public TracePackageTraceElement(TracePackageElement parent
, TmfTraceElement traceElement
) {
43 fTraceElement
= traceElement
;
49 * Construct an instance associated to a TmfTraceElement. For importing.
52 * the parent of this element, can be set to null
54 * the name to use to identify this trace
56 * the trace type to set for this trace
58 public TracePackageTraceElement(TracePackageElement parent
, String importName
, String traceType
) {
60 fImportName
= importName
;
62 fTraceType
= traceType
;
66 public String
getText() {
67 return fTraceElement
!= null ? fTraceElement
.getElementPath() : getDestinationElementPath();
71 * Return the target TmfCommonProjectElement element path for a given trace
72 * package element. {@link TmfCommonProjectElement#getElementPath()}
74 * @return the element path
76 public String
getDestinationElementPath() {
77 String traceName
= getImportName();
78 for (TracePackageElement element
: getChildren()) {
79 if (element
instanceof TracePackageFilesElement
) {
80 TracePackageFilesElement tracePackageFilesElement
= (TracePackageFilesElement
) element
;
81 String fileName
= tracePackageFilesElement
.getFileName();
82 String parentDir
= removeLastSegment(fileName
);
83 return append(parentDir
, traceName
);
91 * We do this outside of the Path class because we don't want it to convert
92 * \ to / on Windows in the presence of regular expressions
94 private static String
removeLastSegment(String str
) {
95 String ret
= removeAllTrailing(str
, IPath
.SEPARATOR
);
96 int lastIndexOf
= ret
.lastIndexOf(IPath
.SEPARATOR
);
97 if (lastIndexOf
!= -1) {
98 ret
= ret
.substring(0, lastIndexOf
);
99 ret
= removeAllTrailing(ret
, IPath
.SEPARATOR
);
101 ret
= ""; //$NON-NLS-1$
107 private static String
removeAllTrailing(String str
, char toRemove
) {
109 while (ret
.endsWith(Character
.toString(toRemove
))) {
110 ret
= ret
.substring(0, ret
.length() - 1);
115 private static String
append(String path
, String str
) {
116 if (!path
.isEmpty()) {
117 return path
+ IPath
.SEPARATOR
+ str
;
124 * @return the associated TmfTraceElement
126 public TmfTraceElement
getTraceElement() {
127 return fTraceElement
;
131 * Set the import name.
133 * @param importName the import name.
135 public void setImportName(String importName
) {
136 fImportName
= importName
;
140 * @return the import name
142 public String
getImportName() {
147 * @return the trace type of this trace
149 public String
getTraceType() {
154 * Set the trace type of this trace.
156 * @param traceType the trace type of this trace
158 public void setTraceType(String traceType
) {
159 fTraceType
= traceType
;
163 public Image
getImage() {
164 TmfNavigatorLabelProvider tmfNavigatorLabelProvider
= new TmfNavigatorLabelProvider();
165 return tmfNavigatorLabelProvider
.getImage(fTraceElement
);