summaryrefslogtreecommitdiffstats
path: root/org.eclipse.cdt.codan.ui/src/org/eclipse/cdt/codan/internal/ui/widgets/BasicElementLabels.java
blob: cc2f966826c961ea4a3e3aec7b7909867614d6c0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
/*******************************************************************************
 * Copyright (c) 2010 Alena Laskavaia and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     Alena Laskavaia - initial API and implementation
 *******************************************************************************/
package org.eclipse.cdt.codan.internal.ui.widgets;

import java.io.File;

import org.eclipse.core.resources.IResource;
import org.eclipse.core.runtime.IPath;
import org.eclipse.osgi.util.TextProcessor;
import org.eclipse.ui.IWorkingSet;

/**
 * A label provider for basic elements like paths. The label provider will make
 * sure that the labels are correctly
 * shown in RTL environments.
 * 
 * @since 3.4
 */
public class BasicElementLabels {
	// TextProcessor delimiters
	private static final String CODE_DELIMITERS = TextProcessor
			.getDefaultDelimiters() + "<>()?,{}+-*!%=^|&;[]~"; //$NON-NLS-1$
	private static final String FILE_PATTERN_DELIMITERS = TextProcessor
			.getDefaultDelimiters() + "*.?"; //$NON-NLS-1$
	private static final String URL_DELIMITERS = TextProcessor
			.getDefaultDelimiters() + ":@?-"; //$NON-NLS-1$

	/**
	 * Returns the label of a path.
	 * 
	 * @param path
	 *            the path
	 * @param isOSPath
	 *            if <code>true</code>, the path represents an OS path, if
	 *            <code>false</code> it is a workspace path.
	 * @return the label of the path to be used in the UI.
	 */
	public static String getPathLabel(IPath path, boolean isOSPath) {
		String label;
		if (isOSPath) {
			label = path.toOSString();
		} else {
			label = path.makeRelative().toString();
		}
		return markLTR(label);
	}

	/**
	 * Returns the label of the path of a file.
	 * 
	 * @param file
	 *            the file
	 * @return the label of the file path to be used in the UI.
	 */
	public static String getPathLabel(File file) {
		return markLTR(file.getAbsolutePath());
	}

	/**
	 * Returns the label for a file pattern like '*.java'
	 * 
	 * @param name
	 *            the pattern
	 * @return the label of the pattern.
	 */
	public static String getFilePattern(String name) {
		return markLTR(name, FILE_PATTERN_DELIMITERS);
	}

	/**
	 * Returns the label for a URL, URI or URL part. Example is
	 * 'http://www.x.xom/s.html#1'
	 * 
	 * @param name
	 *            the URL string
	 * @return the label of the URL.
	 */
	public static String getURLPart(String name) {
		return markLTR(name, URL_DELIMITERS);
	}

	/**
	 * Returns a label for a resource name.
	 * 
	 * @param resource
	 *            the resource
	 * @return the label of the resource name.
	 */
	public static String getResourceName(IResource resource) {
		return markLTR(resource.getName());
	}

	/**
	 * Returns a label for a resource name.
	 * 
	 * @param resourceName
	 *            the resource name
	 * @return the label of the resource name.
	 */
	public static String getResourceName(String resourceName) {
		return markLTR(resourceName);
	}

	/**
	 * Returns a label for Java code snippet used in a label. Example is 'Test
	 * test= new Test<? extends List>() { ...}'.
	 * 
	 * @param string
	 *            the Java code snippet
	 * @return the label for the Java code snippet
	 */
	public static String getJavaCodeString(String string) {
		return markLTR(string, CODE_DELIMITERS);
	}

	/**
	 * Returns a label for a version name. Example is '1.4.1'
	 * 
	 * @param name
	 *            the version string
	 * @return the version label
	 */
	public static String getVersionName(String name) {
		return markLTR(name);
	}

	/**
	 * Returns a label for a working set
	 * 
	 * @param set
	 *            the working set
	 * @return the label of the working set
	 */
	public static String getWorkingSetLabel(IWorkingSet set) {
		return markLTR(set.getLabel());
	}

	/**
	 * It does not do anything now, but just in case we need to do the same as
	 * JDT does (see String.markLTR in JDT)
	 */
	private static String markLTR(String label) {
		return label;
	}

	private static String markLTR(String label, String delim) {
		return label;
	}
}