Copyright header update, 2015 edition
[deliverable/tracecompass.git] / org.eclipse.tracecompass.tmf.ui / src / org / eclipse / tracecompass / internal / tmf / ui / project / wizards / tracepkg / TracePackageTraceElement.java
1 /*******************************************************************************
2 * Copyright (c) 2013, 2014 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 * Marc-Andre Laperle - Initial API and implementation
11 *******************************************************************************/
12
13 package org.eclipse.tracecompass.internal.tmf.ui.project.wizards.tracepkg;
14
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;
20
21 /**
22 * An ExportTraceElement associated to a TmfTraceElement. This will be the
23 * parent of other elements (events, supplementary files, bookmarks, etc).
24 *
25 * @author Marc-Andre Laperle
26 */
27 public class TracePackageTraceElement extends TracePackageElement {
28
29 private final TmfTraceElement fTraceElement;
30 private String fImportName;
31 private String fTraceType;
32
33 /**
34 * Construct an instance associated to a TmfTraceElement. For exporting.
35 *
36 * @param parent
37 * the parent of this element, can be set to null
38 * @param traceElement
39 * the associated TmfTraceElement
40 */
41 public TracePackageTraceElement(TracePackageElement parent, TmfTraceElement traceElement) {
42 super(parent);
43 fTraceElement = traceElement;
44 fImportName = null;
45 fTraceType = null;
46 }
47
48 /**
49 * Construct an instance associated to a TmfTraceElement. For importing.
50 *
51 * @param parent
52 * the parent of this element, can be set to null
53 * @param importName
54 * the name to use to identify this trace
55 * @param traceType
56 * the trace type to set for this trace
57 */
58 public TracePackageTraceElement(TracePackageElement parent, String importName, String traceType) {
59 super(parent);
60 fImportName = importName;
61 fTraceElement = null;
62 fTraceType = traceType;
63 }
64
65 @Override
66 public String getText() {
67 return fTraceElement != null ? fTraceElement.getElementPath() : getDestinationElementPath();
68 }
69
70 /**
71 * Return the target TmfCommonProjectElement element path for a given trace
72 * package element. {@link TmfCommonProjectElement#getElementPath()}
73 *
74 * @return the element path
75 */
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);
84 }
85 }
86
87 return traceName;
88 }
89
90 /**
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
93 */
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);
100 } else {
101 ret = ""; //$NON-NLS-1$
102 }
103
104 return ret;
105 }
106
107 private static String removeAllTrailing(String str, char toRemove) {
108 String ret = str;
109 while (ret.endsWith(Character.toString(toRemove))) {
110 ret = ret.substring(0, ret.length() - 1);
111 }
112 return ret;
113 }
114
115 private static String append(String path, String str) {
116 if (!path.isEmpty()) {
117 return path + IPath.SEPARATOR + str;
118 }
119
120 return str;
121 }
122
123 /**
124 * @return the associated TmfTraceElement
125 */
126 public TmfTraceElement getTraceElement() {
127 return fTraceElement;
128 }
129
130 /**
131 * Set the import name.
132 *
133 * @param importName the import name.
134 */
135 public void setImportName(String importName) {
136 fImportName = importName;
137 }
138
139 /**
140 * @return the import name
141 */
142 public String getImportName() {
143 return fImportName;
144 }
145
146 /**
147 * @return the trace type of this trace
148 */
149 public String getTraceType() {
150 return fTraceType;
151 }
152
153 /**
154 * Set the trace type of this trace.
155 *
156 * @param traceType the trace type of this trace
157 */
158 public void setTraceType(String traceType) {
159 fTraceType = traceType;
160 }
161
162 @Override
163 public Image getImage() {
164 TmfNavigatorLabelProvider tmfNavigatorLabelProvider = new TmfNavigatorLabelProvider();
165 return tmfNavigatorLabelProvider.getImage(fTraceElement);
166 }
167 }
This page took 0.03489 seconds and 5 git commands to generate.