Commit | Line | Data |
---|---|---|
6e651d8b MAL |
1 | /******************************************************************************* |
2 | * Copyright (c) 2013 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.linuxtools.internal.tmf.ui.project.wizards.tracepkg; | |
14 | ||
a6ee485c | 15 | import org.eclipse.core.runtime.IPath; |
a6ee485c | 16 | import org.eclipse.linuxtools.tmf.ui.project.model.TmfCommonProjectElement; |
6e651d8b MAL |
17 | import org.eclipse.linuxtools.tmf.ui.project.model.TmfNavigatorLabelProvider; |
18 | import org.eclipse.linuxtools.tmf.ui.project.model.TmfTraceElement; | |
19 | import org.eclipse.swt.graphics.Image; | |
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 final String fImportName; | |
31 | private final 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() { | |
a6ee485c MAL |
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; | |
393e0002 MAL |
81 | String fileName = tracePackageFilesElement.getFileName(); |
82 | String parentDir = removeLastSegment(fileName); | |
83 | return append(parentDir, traceName); | |
a6ee485c MAL |
84 | } |
85 | } | |
86 | ||
87 | return traceName; | |
6e651d8b MAL |
88 | } |
89 | ||
393e0002 MAL |
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 | ||
6e651d8b MAL |
123 | /** |
124 | * @return the associated TmfTraceElement | |
125 | */ | |
126 | public TmfTraceElement getTraceElement() { | |
127 | return fTraceElement; | |
128 | } | |
129 | ||
a6ee485c MAL |
130 | /** |
131 | * @return the import name | |
132 | */ | |
133 | public String getImportName() { | |
134 | return fImportName; | |
135 | } | |
136 | ||
6e651d8b MAL |
137 | /** |
138 | * @return the trace type of this trace | |
139 | */ | |
140 | public String getTraceType() { | |
141 | return fTraceType; | |
142 | } | |
143 | ||
144 | @Override | |
145 | public Image getImage() { | |
146 | TmfNavigatorLabelProvider tmfNavigatorLabelProvider = new TmfNavigatorLabelProvider(); | |
147 | return tmfNavigatorLabelProvider.getImage(fTraceElement); | |
148 | } | |
149 | } |