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
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
|
/*******************************************************************************
* Copyright (c) 2010 SAP AG.
* 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:
* Mathias Kinzler (SAP AG) - initial implementation
*******************************************************************************/
package org.eclipse.egit.ui;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Pattern;
import java.util.regex.PatternSyntaxException;
import org.eclipse.egit.ui.internal.components.RefContentProposal;
import org.eclipse.jface.bindings.keys.KeyStroke;
import org.eclipse.jface.bindings.keys.ParseException;
import org.eclipse.jface.dialogs.IDialogSettings;
import org.eclipse.jface.fieldassist.ContentProposalAdapter;
import org.eclipse.jface.fieldassist.ControlDecoration;
import org.eclipse.jface.fieldassist.FieldDecorationRegistry;
import org.eclipse.jface.fieldassist.IContentProposal;
import org.eclipse.jface.fieldassist.IContentProposalProvider;
import org.eclipse.jface.fieldassist.TextContentAdapter;
import org.eclipse.jface.resource.FontRegistry;
import org.eclipse.jgit.lib.Ref;
import org.eclipse.jgit.lib.Repository;
import org.eclipse.osgi.util.NLS;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.DisposeEvent;
import org.eclipse.swt.events.DisposeListener;
import org.eclipse.swt.graphics.Font;
import org.eclipse.swt.graphics.Resource;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Text;
import org.eclipse.swt.widgets.Widget;
import org.eclipse.ui.PlatformUI;
/**
* Some utilities for UI code
*/
public class UIUtils {
/**
* these activate the content assist; alphanumeric, space plus some expected
* special chars
*/
private static final char[] VALUE_HELP_ACTIVATIONCHARS = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123457890*@ <>".toCharArray(); //$NON-NLS-1$
/**
* Handles a "previously used values" content assist.
* <p>
* Adding this to a text field will enable "content assist" by keeping track
* of the previously used valued for this field. The previously used values
* will be shown in the order they were last used (most recently used ones
* coming first in the list) and the number of entries is limited.
* <p>
* A "bulb" decorator will indicate that content assist is available for the
* field, and a tool tip is provided giving more information.
* <p>
* Content assist is activated by either typing in the field or by using a
* dedicated key stroke which is indicated in the tool tip. The list will be
* filtered with the content already in the text field with '*' being usable
* as wild card.
* <p>
* Note that the application must issue a call to {@link #updateProposals()}
* in order to add a new value to the "previously used values" list.
* <p>
* The list will be persisted in the plug-in dialog settings.
*
* @noextend not to be extended by clients
* @noimplement not to be implemented by clients, use
* {@link UIUtils#addPreviousValuesContentProposalToText(Text, String)}
* to create instances of this
*/
public interface IPreviousValueProposalHandler {
/**
* Updates the proposal list from the value in the text field.
* <p>
* The value will be truncated to the first 2000 characters in order to
* limit data size.
* <p>
* Note that this must be called in the UI thread, since it accesses the
* text field.
* <p>
* If the value is already in the list, it will become the first entry,
* otherwise it will be added at the beginning. Note that empty Strings
* will not be stored. The length of the list is limited, and the
* "oldest" entries will be removed once the limit is exceeded.
* <p>
* This call should only be issued if the value in the text field is
* "valid" in terms of the application.
*/
public void updateProposals();
}
/**
* Used for
* {@link UIUtils#addRefContentProposalToText(Text, Repository, IRefListProvider)}
*/
public interface IRefListProvider {
/**
* @return the List of {@link Ref}s to propose
*/
public List<Ref> getRefList();
}
/**
* @param id
* see {@link FontRegistry#get(String)}
* @return the font
*/
public static Font getFont(final String id) {
return PlatformUI.getWorkbench().getThemeManager().getCurrentTheme()
.getFontRegistry().get(id);
}
/**
* @param id
* see {@link FontRegistry#getBold(String)}
* @return the font
*/
public static Font getBoldFont(final String id) {
return PlatformUI.getWorkbench().getThemeManager().getCurrentTheme()
.getFontRegistry().getBold(id);
}
/**
* @param id
* see {@link FontRegistry#getItalic(String)}
* @return the font
*/
public static Font getItalicFont(final String id) {
return PlatformUI.getWorkbench().getThemeManager().getCurrentTheme()
.getFontRegistry().getItalic(id);
}
/**
* Adds little bulb decoration to given control. Bulb will appear in top
* left corner of control after giving focus for this control.
*
* After clicking on bulb image text from <code>tooltip</code> will appear.
*
* @param control
* instance of {@link Control} object with should be decorated
* @param tooltip
* text value which should appear after clicking on bulb image.
*/
public static void addBulbDecorator(final Control control,
final String tooltip) {
ControlDecoration dec = new ControlDecoration(control, SWT.TOP
| SWT.LEFT);
dec.setImage(FieldDecorationRegistry.getDefault().getFieldDecoration(
FieldDecorationRegistry.DEC_CONTENT_PROPOSAL).getImage());
dec.setShowOnlyOnFocus(true);
dec.setShowHover(true);
dec.setDescriptionText(tooltip);
}
/**
* Adds a "previously used values" content proposal handler to a text field.
* <p>
* The keyboard shortcut will be "M1+SPACE" and the list will be limited to
* 10 values.
*
* @param textField
* the text field
* @param preferenceKey
* the key under which to store the "previously used values" in
* the dialog settings
* @return the handler the proposal handler
*/
public static IPreviousValueProposalHandler addPreviousValuesContentProposalToText(
final Text textField, final String preferenceKey) {
KeyStroke stroke;
try {
stroke = KeyStroke.getInstance("M1+SPACE"); //$NON-NLS-1$
addBulbDecorator(textField, NLS.bind(
UIText.UIUtils_PressShortcutMessage, stroke.format()));
} catch (ParseException e1) {
Activator.handleError(e1.getMessage(), e1, false);
stroke = null;
addBulbDecorator(textField,
UIText.UIUtils_StartTypingForPreviousValuesMessage);
}
IContentProposalProvider cp = new IContentProposalProvider() {
public IContentProposal[] getProposals(String contents, int position) {
List<IContentProposal> resultList = new ArrayList<IContentProposal>();
// make the simplest possible pattern check: allow "*"
// for multiple characters
String patternString = contents;
// ignore spaces in the beginning
while (patternString.length() > 0
&& patternString.charAt(0) == ' ') {
patternString = patternString.substring(1);
}
// we quote the string as it may contain spaces
// and other stuff colliding with the Pattern
patternString = Pattern.quote(patternString);
patternString = patternString.replaceAll("\\x2A", ".*"); //$NON-NLS-1$ //$NON-NLS-2$
// make sure we add a (logical) * at the end
if (!patternString.endsWith(".*")) { //$NON-NLS-1$
patternString = patternString + ".*"; //$NON-NLS-1$
}
// let's compile a case-insensitive pattern (assumes ASCII only)
Pattern pattern;
try {
pattern = Pattern.compile(patternString,
Pattern.CASE_INSENSITIVE);
} catch (PatternSyntaxException e) {
pattern = null;
}
String[] proposals = org.eclipse.egit.ui.Activator.getDefault()
.getDialogSettings().getArray(preferenceKey);
if (proposals != null)
for (final String uriString : proposals) {
if (pattern != null
&& !pattern.matcher(uriString).matches())
continue;
IContentProposal propsal = new IContentProposal() {
public String getLabel() {
return null;
}
public String getDescription() {
return null;
}
public int getCursorPosition() {
return 0;
}
public String getContent() {
return uriString;
}
};
resultList.add(propsal);
}
return resultList.toArray(new IContentProposal[resultList
.size()]);
}
};
ContentProposalAdapter adapter = new ContentProposalAdapter(textField,
new TextContentAdapter(), cp, stroke,
VALUE_HELP_ACTIVATIONCHARS);
// set the acceptance style to always replace the complete content
adapter
.setProposalAcceptanceStyle(ContentProposalAdapter.PROPOSAL_REPLACE);
return new IPreviousValueProposalHandler() {
public void updateProposals() {
String value = textField.getText();
// don't store empty values
if (value.length() > 0) {
// we don't want to save too much in the preferences
if (value.length() > 2000) {
value = value.substring(0, 1999);
}
// now we need to mix the value into the list
IDialogSettings settings = org.eclipse.egit.ui.Activator
.getDefault().getDialogSettings();
String[] existingValues = settings.getArray(preferenceKey);
if (existingValues == null) {
existingValues = new String[] { value };
settings.put(preferenceKey, existingValues);
} else {
List<String> values = new ArrayList<String>(
existingValues.length + 1);
for (String existingValue : existingValues)
values.add(existingValue);
// if it is already the first value, we don't need to do
// anything
if (values.indexOf(value) == 0)
return;
values.remove(value);
// we insert at the top
values.add(0, value);
// make sure to not store more than the maximum number
// of values
while (values.size() > 10)
values.remove(values.size() - 1);
settings.put(preferenceKey, values
.toArray(new String[values.size()]));
}
}
}
};
}
/**
* Adds a content proposal for {@link Ref}s (branches, tags...) to a text
* field
*
* @param textField
* the text field
* @param repository
* the repository
* @param refListProvider
* provides the {@link Ref}s to show in the proposal
*/
public static final void addRefContentProposalToText(final Text textField,
final Repository repository, final IRefListProvider refListProvider) {
KeyStroke stroke;
try {
stroke = KeyStroke.getInstance("M1+SPACE"); //$NON-NLS-1$
UIUtils.addBulbDecorator(textField, NLS.bind(
UIText.UIUtils_PressShortcutMessage, stroke.format()));
} catch (ParseException e1) {
Activator.handleError(e1.getMessage(), e1, false);
stroke = null;
UIUtils.addBulbDecorator(textField,
UIText.UIUtils_StartTypingForPreviousValuesMessage);
}
IContentProposalProvider cp = new IContentProposalProvider() {
public IContentProposal[] getProposals(String contents, int position) {
List<IContentProposal> resultList = new ArrayList<IContentProposal>();
// make the simplest possible pattern check: allow "*"
// for multiple characters
String patternString = contents;
// ignore spaces in the beginning
while (patternString.length() > 0
&& patternString.charAt(0) == ' ') {
patternString = patternString.substring(1);
}
// we quote the string as it may contain spaces
// and other stuff colliding with the Pattern
patternString = Pattern.quote(patternString);
patternString = patternString.replaceAll("\\x2A", ".*"); //$NON-NLS-1$ //$NON-NLS-2$
// make sure we add a (logical) * at the end
if (!patternString.endsWith(".*")) { //$NON-NLS-1$
patternString = patternString + ".*"; //$NON-NLS-1$
}
// let's compile a case-insensitive pattern (assumes ASCII only)
Pattern pattern;
try {
pattern = Pattern.compile(patternString,
Pattern.CASE_INSENSITIVE);
} catch (PatternSyntaxException e) {
pattern = null;
}
List<Ref> proposals = refListProvider.getRefList();
if (proposals != null)
for (final Ref ref : proposals) {
final String shortenedName = Repository
.shortenRefName(ref.getName());
if (pattern != null
&& !pattern.matcher(ref.getName()).matches()
&& !pattern.matcher(shortenedName).matches())
continue;
IContentProposal propsal = new RefContentProposal(
repository, ref);
resultList.add(propsal);
}
return resultList.toArray(new IContentProposal[resultList
.size()]);
}
};
ContentProposalAdapter adapter = new ContentProposalAdapter(textField,
new TextContentAdapter(), cp, stroke,
UIUtils.VALUE_HELP_ACTIVATIONCHARS);
// set the acceptance style to always replace the complete content
adapter
.setProposalAcceptanceStyle(ContentProposalAdapter.PROPOSAL_REPLACE);
}
/**
* Set enabled state of the control and all its children
* @param control
* @param enable
*/
public static void setEnabledRecursively(final Control control,
final boolean enable) {
control.setEnabled(enable);
if (control instanceof Composite)
for (final Control child : ((Composite) control).getChildren())
setEnabledRecursively(child, enable);
}
/**
* Dispose of the resource when the widget is disposed
*
* @param widget
* @param resource
*/
public static void hookDisposal(Widget widget, final Resource resource) {
if (widget == null || resource == null)
return;
widget.addDisposeListener(new DisposeListener() {
public void widgetDisposed(DisposeEvent e) {
resource.dispose();
}
});
}
}
|