Skip to main content
summaryrefslogtreecommitdiffstats
blob: 6179cc4e3baf9ce9c79f90b1a470b829029272d9 (plain) (blame)
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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
/*******************************************************************************
 * Copyright (c) 2000, 2008 IBM Corporation 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:
 *     IBM Corporation - initial API and implementation
 *     Perforce - enhancements for bug 319469
 *******************************************************************************/

package org.eclipse.mylyn.internal.tasks.ui.search;

import org.eclipse.jface.resource.JFaceResources;
import org.eclipse.jface.util.IPropertyChangeListener;
import org.eclipse.jface.util.PropertyChangeEvent;
import org.eclipse.jface.viewers.ColumnViewer;
import org.eclipse.jface.viewers.DecoratingStyledCellLabelProvider;
import org.eclipse.jface.viewers.IColorProvider;
import org.eclipse.jface.viewers.IDecorationContext;
import org.eclipse.jface.viewers.IFontProvider;
import org.eclipse.jface.viewers.ILabelDecorator;
import org.eclipse.jface.viewers.ILabelProvider;
import org.eclipse.jface.viewers.StyledCellLabelProvider;
import org.eclipse.jface.viewers.StyledString;
import org.eclipse.jface.viewers.ViewerColumn;
import org.eclipse.search.internal.ui.text.DecoratingFileSearchLabelProvider;
import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.StyleRange;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.graphics.Font;
import org.eclipse.swt.graphics.Image;
import org.eclipse.ui.IWorkbenchPreferenceConstants;
import org.eclipse.ui.PlatformUI;
import org.eclipse.ui.internal.misc.StringMatcher;
import org.eclipse.ui.internal.misc.StringMatcher.Position;

/**
 * Repository search styled label provider. Based on {@link DecoratingFileSearchLabelProvider}.
 * 
 * @author Kevin Sawicki
 * @see DecoratingFileSearchLabelProvider
 */
public class RepositorySearchStyledLabelProvider extends DecoratingStyledCellLabelProvider implements
		IPropertyChangeListener, ILabelProvider {

	/**
	 * Color to use to decorate matches.
	 */
	public static final String HIGHLIGHT_BG_COLOR_NAME = "org.eclipse.jdt.ui.ColoredLabels.match_highlight"; //$NON-NLS-1$

	private static class PatternStyledLabelProvider extends StyledCellLabelProvider implements IStyledLabelProvider,
			IColorProvider, IFontProvider {

		private final SearchResultsLabelProvider labelProvider;

		private StringMatcher matcher = null;

		public PatternStyledLabelProvider(SearchResultsLabelProvider provider) {
			this.labelProvider = provider;
		}

		/**
		 * Set the pattern to highlight
		 * 
		 * @param pattern
		 */
		public void setPattern(String pattern) {
			if (pattern != null && pattern.length() > 0) {
				this.matcher = new StringMatcher(pattern, true, false);
			} else {
				this.matcher = null;
			}
		}

		public StyledString getStyledText(Object element) {
			StyledString styled = null;
			String label = this.labelProvider.getText(element);
			if (matcher == null || label.length() == 0) {
				styled = new StyledString(label);
			} else {
				styled = new StyledString();
				int start = 0;
				int end = 0;
				int length = label.length();
				Position position = matcher.find(label, start, length);
				while (position != null) {
					end = position.getStart();
					styled.append(label.substring(start, end));
					start = position.getEnd();
					styled.append(label.substring(end, start), DecoratingFileSearchLabelProvider.HIGHLIGHT_STYLE);
					position = matcher.find(label, start, length);
				}
				if (start > 0 && start < length) {
					styled.append(label.substring(start));
				}
			}
			return styled;
		}

		public Image getImage(Object element) {
			return this.labelProvider.getImage(element);
		}

		public Font getFont(Object element) {
			return this.labelProvider.getFont(element);
		}

		public Color getForeground(Object element) {
			return this.labelProvider.getForeground(element);
		}

		public Color getBackground(Object element) {
			return this.labelProvider.getBackground(element);
		}

	}

	private final ILabelProvider labelProvider;

	/**
	 * Create a new repository search styled label provider that wraps an {@link ILabelProvider}
	 * 
	 * @param labelProvider
	 * @param decorator
	 * @param decorationContext
	 */
	public RepositorySearchStyledLabelProvider(SearchResultsLabelProvider labelProvider, ILabelDecorator decorator,
			IDecorationContext decorationContext) {
		super(new PatternStyledLabelProvider(labelProvider), decorator, decorationContext);
		this.labelProvider = labelProvider;
	}

	/**
	 * @see org.eclipse.jface.viewers.StyledCellLabelProvider#initialize(org.eclipse.jface.viewers.ColumnViewer,
	 *      org.eclipse.jface.viewers.ViewerColumn)
	 */
	@Override
	public void initialize(ColumnViewer viewer, ViewerColumn column) {
		PlatformUI.getPreferenceStore().addPropertyChangeListener(this);
		JFaceResources.getColorRegistry().addListener(this);

		setOwnerDrawEnabled(PlatformUI.getPreferenceStore()
				.getBoolean(IWorkbenchPreferenceConstants.USE_COLORED_LABELS));

		super.initialize(viewer, column);
	}

	/**
	 * Get underyling label provider
	 * 
	 * @return label provider
	 */
	public ILabelProvider getLabelProvider() {
		return this.labelProvider;
	}

	/**
	 * @see org.eclipse.jface.viewers.DecoratingStyledCellLabelProvider#dispose()
	 */
	@Override
	public void dispose() {
		PlatformUI.getPreferenceStore().removePropertyChangeListener(this);
		JFaceResources.getColorRegistry().removeListener(this);
		this.labelProvider.dispose();
		super.dispose();
	}

	/**
	 * Set the pattern to highlight
	 * 
	 * @param pattern
	 */
	public void setPattern(String pattern) {
		((PatternStyledLabelProvider) getStyledStringProvider()).setPattern(pattern);
	}

	/**
	 * Refresh the labels on viewer associated with this label provider. This method must be called on the UI-thread.
	 */
	protected void refresh() {
		ColumnViewer viewer = getViewer();
		if (viewer != null) {
			boolean coloredLabels = PlatformUI.getPreferenceStore().getBoolean(
					IWorkbenchPreferenceConstants.USE_COLORED_LABELS);
			if (coloredLabels || coloredLabels != isOwnerDrawEnabled()) {
				setOwnerDrawEnabled(coloredLabels);
				viewer.refresh();
			}
		}
	}

	/**
	 * Schedule a refresh of this label provider. This method can be called from any thread.
	 */
	protected void scheduleRefresh() {
		PlatformUI.getWorkbench().getDisplay().asyncExec(new Runnable() {
			public void run() {
				refresh();
			}
		});
	}

	/**
	 * @param event
	 */
	public void propertyChange(PropertyChangeEvent event) {
		String property = event.getProperty();
		if (IWorkbenchPreferenceConstants.USE_COLORED_LABELS.equals(property)
				|| HIGHLIGHT_BG_COLOR_NAME.equals(property)) {
			scheduleRefresh();
		}
	}

	/**
	 * Get text of element from underyling label provider
	 * 
	 * @see org.eclipse.jface.viewers.ILabelProvider#getText(java.lang.Object)
	 */
	public String getText(Object element) {
		return this.labelProvider.getText(element);
	}

	/**
	 * Override preparation of style range to add border dot about highlight regions that don't have colors applied
	 * 
	 * @see org.eclipse.jface.viewers.StyledCellLabelProvider#prepareStyleRange(org.eclipse.swt.custom.StyleRange,
	 *      boolean)
	 */
	@Override
	protected StyleRange prepareStyleRange(StyleRange styleRange, boolean applyColors) {
		boolean addBorder = !applyColors && styleRange.background != null;
		styleRange = super.prepareStyleRange(styleRange, applyColors);
		if (addBorder) {
			styleRange.borderStyle = SWT.BORDER_DOT;
		}
		return styleRange;
	}

}

Back to the top