Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 26de601cf57619bfd133ec78e2be03fc048ed522 (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
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
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
/*******************************************************************************
 * Copyright (C) 2019, Tim Neumann <Tim.Neumann@advantest.com>
 *
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License 2.0
 * which accompanies this distribution, and is available at
 * https://www.eclipse.org/legal/epl-2.0/
 *
 * SPDX-License-Identifier: EPL-2.0
 *******************************************************************************/
package org.eclipse.egit.ui.internal.history;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.LinkedHashMap;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.function.Function;
import java.util.stream.Collectors;

import org.apache.tools.ant.types.selectors.TokenizedPath;
import org.apache.tools.ant.types.selectors.TokenizedPattern;
import org.eclipse.egit.ui.Activator;
import org.eclipse.egit.ui.UIPreferences;
import org.eclipse.jface.preference.IPersistentPreferenceStore;
import org.eclipse.jface.preference.IPreferenceStore;
import org.eclipse.jgit.annotations.NonNull;
import org.eclipse.jgit.lib.Constants;
import org.eclipse.jgit.lib.Ref;
import org.eclipse.jgit.lib.RefDatabase;
import org.eclipse.jgit.lib.Repository;

/**
 * A class for handling the config reading, writing and parsing for the ref
 * filters as well as matching refs against a filter.
 */
public class RefFilterHelper {

	private static final String ANY = "**"; //$NON-NLS-1$

	private static final String REF_SEPARATOR = ":"; //$NON-NLS-1$

	private static final String MACRO_CURRENT_BRANCH = "[CURRENT-BRANCH]"; //$NON-NLS-1$

	private static final String DEFAULT_SELECTED_REFS = Constants.HEAD;

	private static final String DEFAULT_SELECTED_REFS_ALL_BRANCHES =
			Constants.HEAD + REF_SEPARATOR
			+ Constants.R_HEADS + ANY + REF_SEPARATOR
			+ Constants.R_REMOTES + ANY + REF_SEPARATOR
			+ Constants.R_TAGS + ANY;

	private final IPreferenceStore store;

	@NonNull
	private final Repository repository;

	private List<RefFilter> preconfiguredFilters;

	private List<RefFilter> filtersForHEAD;
	private List<RefFilter> filtersForCurrentBranch;
	private List<RefFilter> filtersForAllBranchesAndTags;

	private Map<String, Function<Repository, String>> macros;

	private static @NonNull IPreferenceStore checkNull(IPreferenceStore store) {
		if (store == null) {
			throw new NullPointerException("Preference store is null."); //$NON-NLS-1$
		}
		return store;
	}

	/**
	 * Create a new RefFilterHelper for the given repository using the
	 * preference store provided by the activator of this ui plugin.
	 *
	 * @param repository
	 *            The repository to create the helper for; must not be null
	 */
	public RefFilterHelper(@NonNull Repository repository) {
		this(repository,
				checkNull(Activator.getDefault().getPreferenceStore()));
	}

	/**
	 * Create a new RefFilterHelper for the given repository using the given
	 * preference store.
	 *
	 * @param repository
	 *            The repository to create the helper for; must not be null
	 * @param store
	 *            The preference store to use; must not be null
	 */
	public RefFilterHelper(@NonNull Repository repository,
			@NonNull IPreferenceStore store) {
		this.repository = repository;
		this.store = store;
		setupPreconfiguredFilters();
		setupMacros();
		// Just always init the repo defaults as we don't know if this repo had
		// a helper before.
		initDefaultsForRepo();
	}

	private RefFilter newPreConfFilter(String filter) {
		return new RefFilter(filter, true);
	}

	private RefFilter newPreConfPrefixFilter(String prefix) {
		return newPreConfFilter(prefix + "**"); //$NON-NLS-1$
	}

	private void setupPreconfiguredFilters() {
		preconfiguredFilters = new ArrayList<>();
		filtersForHEAD = new ArrayList<>();
		filtersForCurrentBranch = new ArrayList<>();
		filtersForAllBranchesAndTags = new ArrayList<>();

		RefFilter head = newPreConfFilter(Constants.HEAD);
		preconfiguredFilters.add(head);
		filtersForHEAD.add(head);
		filtersForAllBranchesAndTags.add(head);

		RefFilter currentBranch = newPreConfFilter(
				Constants.R_REFS + "**/" + MACRO_CURRENT_BRANCH); //$NON-NLS-1$
		preconfiguredFilters.add(currentBranch);
		filtersForCurrentBranch.add(currentBranch);

		RefFilter branches = newPreConfPrefixFilter(Constants.R_HEADS);
		preconfiguredFilters.add(branches);
		filtersForAllBranchesAndTags.add(branches);

		RefFilter remoteBranches = newPreConfPrefixFilter(Constants.R_REMOTES);
		preconfiguredFilters.add(remoteBranches);
		filtersForAllBranchesAndTags.add(remoteBranches);

		RefFilter tags = newPreConfPrefixFilter(Constants.R_TAGS);
		preconfiguredFilters.add(tags);
		filtersForAllBranchesAndTags.add(tags);
	}

	private void setupMacros() {
		macros = new LinkedHashMap<>();
		macros.put(MACRO_CURRENT_BRANCH, repo -> {
			try {
				return repo.getBranch();
			} catch (IOException e) {
				Activator.handleError(e.getMessage(), e, false);
			}
			return ""; //$NON-NLS-1$
		});
	}

	private void setDefaultSelectionBasedOnShowAllBranches() {
		String currentDefault = store.getDefaultString(
				UIPreferences.RESOURCEHISTORY_SELECTED_REF_FILTERS);

		if (currentDefault != DEFAULT_SELECTED_REFS
				&& currentDefault != DEFAULT_SELECTED_REFS_ALL_BRANCHES) {
			// The default was changed elsewhere.
			// Probably a plugin customization.
			// Don't overwrite it.
			return;
		}

		boolean showAll = store
				.getBoolean(UIPreferences.RESOURCEHISTORY_SHOW_ALL_BRANCHES);

		if (showAll) {
			store.setDefault(UIPreferences.RESOURCEHISTORY_SELECTED_REF_FILTERS,
					DEFAULT_SELECTED_REFS_ALL_BRANCHES);
		} else {
			store.setDefault(UIPreferences.RESOURCEHISTORY_SELECTED_REF_FILTERS,
					DEFAULT_SELECTED_REFS);
		}
	}

	private void initDefaultForRepo(String preferenceName) {
		String repoSpecificPrefName = Activator.getDefault().getRepositoryUtil()
				.getRepositorySpecificPreferenceKey(this.repository,
						preferenceName);

		store.setDefault(repoSpecificPrefName,
				store.getDefaultString(preferenceName));
	}

	/**
	 * Init the default of the repo specific pref from global default. This
	 * needs to happen after each eclipse startup (as the default is not
	 * persisted) for each repo.
	 */
	private void initDefaultsForRepo() {
		setDefaultSelectionBasedOnShowAllBranches();
		initDefaultForRepo(UIPreferences.RESOURCEHISTORY_REF_FILTERS);
		initDefaultForRepo(UIPreferences.RESOURCEHISTORY_SELECTED_REF_FILTERS);
		initDefaultForRepo(
				UIPreferences.RESOURCEHISTORY_LAST_SELECTED_REF_FILTERS);
	}

	/**
	 * Get a preference string from the preference store. This should return the
	 * repository specific preference string if applicable.
	 *
	 * @param preferenceName
	 *            the name of the preference
	 * @return the string-valued preference
	 */
	protected String getPreferenceString(String preferenceName) {
		String repoSpecificPrefName = Activator.getDefault().getRepositoryUtil()
				.getRepositorySpecificPreferenceKey(this.repository,
						preferenceName);

		return store.getString(repoSpecificPrefName);
	}

	private List<String> getFiltersFromPref(String preferenceName) {
		String refFiltersString = getPreferenceString(preferenceName);
		String[] filters = refFiltersString.split(REF_SEPARATOR);

		return Arrays.asList(filters);
	}

	private void savePreferencStoreIfNeeded() {
		if (store.needsSaving()
				&& store instanceof IPersistentPreferenceStore) {
			try {
				((IPersistentPreferenceStore) store).save();
			} catch (IOException e) {
				Activator.handleError(e.getMessage(), e, false);
			}
		}
	}

	private void setFiltersInPref(String preferenceName, List<String> filters,
			boolean save) {
		String repoSpecificPrefName = Activator.getDefault().getRepositoryUtil()
				.getRepositorySpecificPreferenceKey(this.repository,
						preferenceName);
		String refFiltersString = String.join(REF_SEPARATOR, filters);
		store.setValue(repoSpecificPrefName, refFiltersString);

		if (save) {
			savePreferencStoreIfNeeded();
		}
	}

	/**
	 * Get the configured ref filters from the preference store.
	 *
	 * @return A list of all configured ref filter strings
	 */
	public List<String> getConfiguredFilters() {
		return getFiltersFromPref(UIPreferences.RESOURCEHISTORY_REF_FILTERS);
	}

	/**
	 * Set the configured ref filters in the preference store.
	 *
	 * @param filters
	 *            The list of configured ref filter strings to set
	 * @param save
	 *            Whether to save the preference store if necessary
	 */
	public void setConfiguredFilters(List<String> filters, boolean save) {
		setFiltersInPref(UIPreferences.RESOURCEHISTORY_REF_FILTERS, filters,
				save);
	}

	/**
	 * Get the selected ref filters from the preference store.
	 *
	 * @return A list of all selected ref filter strings
	 */
	public List<String> getSelectedFilters() {
		return getFiltersFromPref(
				UIPreferences.RESOURCEHISTORY_SELECTED_REF_FILTERS);
	}

	/**
	 * Set the selected ref filters in the preference store.
	 *
	 * @param filters
	 *            The list of selected ref filter strings to set
	 * @param save
	 *            Whether to save the preference store if necessary
	 */
	public void setSelectedFilters(List<String> filters, boolean save) {
		setFiltersInPref(UIPreferences.RESOURCEHISTORY_SELECTED_REF_FILTERS,
				filters, save);
	}

	/**
	 * Get the last selected ref filters from the preference store.
	 *
	 * @return A list of the last selected ref filter strings
	 */
	public List<String> getLastSelectedFilters() {
		return getFiltersFromPref(
				UIPreferences.RESOURCEHISTORY_LAST_SELECTED_REF_FILTERS);
	}

	/**
	 * Set the last selected ref filters in the preference store.
	 *
	 * @param filters
	 *            The list of last selected ref filter strings to set
	 * @param save
	 *            Whether to save the preference store if necessary
	 */
	public void setLastSelectedFilters(List<String> filters, boolean save) {
		setFiltersInPref(
				UIPreferences.RESOURCEHISTORY_LAST_SELECTED_REF_FILTERS,
				filters, save);
	}

	private void addPreconfiguredFilters(Map<String, RefFilter> filters) {
		for (RefFilter filter : preconfiguredFilters) {
			// Don't use the existing object because selection states would be
			// persisted immediately without calling setRefFilters.
			filters.put(filter.getFilterString(), new RefFilter(filter));
		}
	}

	/**
	 * @return the set of all ref filters
	 */
	public Set<RefFilter> getRefFilters() {
		Map<String, RefFilter> filters = new LinkedHashMap<>();
		addPreconfiguredFilters(filters);

		for (String filter : getConfiguredFilters()) {
			if (filter == null || filter.isEmpty()) {
				continue;
			}
			filters.put(filter, new RefFilter(filter, false));
		}

		for (String filter : getSelectedFilters()) {
			if (filter == null || filter.isEmpty()) {
				continue;
			}
			// A user could change the pref files manually
			// Therefore we need to make sure all selected filters are also
			// available.
			// So we add them to the set if they are not already there
			filters.putIfAbsent(filter, new RefFilter(filter, false));
			filters.get(filter).setSelected(true);
		}
		return new LinkedHashSet<>(filters.values());
	}

	/**
	 * Restore the last selection state.
	 * @param filters The set of filters to restore the state for.
	 */
	public void restoreLastSelectionState(Set<RefFilter> filters) {
		for(RefFilter filter : filters) {
			filter.setSelected(getLastSelectedFilters()
					.contains(filter.getFilterString()));
		}
	}

	/**
	 * Set the given rev filters in the preference store.
	 * <p>
	 * This overrides the selected and the configured filters in the preference
	 * store.
	 * <p>
	 *
	 * @param filters
	 *            The set of filters to save.
	 */
	public void setRefFilters(Set<RefFilter> filters) {
		List<String> selected = filters.stream().filter(RefFilter::isSelected)
				.map(RefFilter::getFilterString).collect(Collectors.toList());
		setSelectedFilters(selected, false);

		List<String> configured = filters.stream()
				.filter(f -> !f.isPreconfigured())
				.map(RefFilter::getFilterString).collect(Collectors.toList());
		setConfiguredFilters(configured, false);

		savePreferencStoreIfNeeded();
	}

	/**
	 * Save the selection state of the given filter set as the last selection
	 * state.
	 *
	 * @param filters
	 *            The filters to get the selection state from
	 */
	public void saveSelectionStateAsLastSelectionState(Set<RefFilter> filters) {
		List<String> selected = new ArrayList<>();
		for(RefFilter filter : filters) {
			if (filter.isSelected()) {
				selected.add(filter.getFilterString());
			}
		}
		setLastSelectedFilters(selected, true);
	}

	/**
	 * Reset the last selection state to the default.
	 */
	public void resetLastSelectionStateToDefault() {
		String repoSpecificPrefName = Activator.getDefault().getRepositoryUtil()
				.getRepositorySpecificPreferenceKey(this.repository,
						UIPreferences.RESOURCEHISTORY_LAST_SELECTED_REF_FILTERS);
		store.setToDefault(repoSpecificPrefName);
		savePreferencStoreIfNeeded();
	}

	/**
	 * Get all matching refs in the given repository for the currently selected
	 * ref filters.
	 *
	 * @return All matching refs from the repo
	 * @throws IOException
	 *             the reference space cannot be accessed.
	 */
	public Set<Ref> getMatchingRefsForSelectedRefFilters()
			throws IOException {
		RefDatabase db = this.repository.getRefDatabase();
		Set<Ref> result = new LinkedHashSet<>();
		Set<RefFilter> selectedFilters = getRefFilters().stream()
				.filter(f -> f.isSelected())
				.collect(Collectors.toCollection(LinkedHashSet::new));

		for (Ref ref : db.getRefs()) {
			TokenizedPath refPath = new TokenizedPath(
					ref.getName().replace('/', File.separatorChar));
			for (RefFilter filter : selectedFilters) {
				if (filter.matches(refPath)) {
					result.add(ref);
					break;
				}
			}
		}

		return result;
	}

	/**
	 * Select only the HEAD preconfigured ref filter.
	 * <p>
	 * This will modify objects in the given list.
	 * </p>
	 *
	 * @param filters
	 *            The filters to change the selection of
	 */
	public void selectOnlyHEAD(Set<RefFilter> filters) {
		for (RefFilter filter : filters) {
			filter.setSelected(filtersForHEAD.contains(filter));
		}
	}

	/**
	 * Check whether only the HEAD preconfigured ref filter is selected.
	 *
	 * @param filters
	 *            The filters to check
	 * @return Whether exactly HEAD is selected
	 */
	public boolean isOnlyHEADSelected(Set<RefFilter> filters) {
		for (RefFilter filter : filters) {
			if (filter.isSelected()) {
				if (!filtersForHEAD.contains(filter)) {
					return false;
				}
			} else {
				if (filtersForHEAD.contains(filter)) {
					return false;
				}
			}
		}
		return true;
	}

	/**
	 * Select only the preconfigured ref filter for the current branch (local
	 * and remote).
	 * <p>
	 * This will modify objects in the given list.
	 * </p>
	 *
	 * @param filters
	 *            The filters to change the selection of
	 */
	public void selectOnlyCurrentBranch(Set<RefFilter> filters) {
		for (RefFilter filter : filters) {
			filter.setSelected(filtersForCurrentBranch.contains(filter));
		}
	}

	/**
	 * Check whether only the preconfigured ref filter for the current branch
	 * (local and remote) is selected.
	 *
	 * @param filters
	 *            The filters to check
	 * @return Whether exactly the current branch is selected
	 */
	public boolean isOnlyCurrentBranchSelected(Set<RefFilter> filters) {
		for (RefFilter filter : filters) {
			if (filter.isSelected()) {
				if (!filtersForCurrentBranch.contains(filter)) {
					return false;
				}
			} else {
				if (filtersForCurrentBranch.contains(filter)) {
					return false;
				}
			}
		}
		return true;
	}

	/**
	 * Select exactly the preconfigured ref filters, which represent all
	 * branches and tags.
	 * <p>
	 * This will modify objects in the given list.
	 * </p>
	 *
	 * @param filters
	 *            The filters to change the selection of.
	 */
	public void selectExactlyAllBranchesAndTags(Set<RefFilter> filters) {
		for (RefFilter filter : filters) {
			filter.setSelected(filtersForAllBranchesAndTags.contains(filter));
		}
	}

	/**
	 * Check whether exactly the preconfigured ref filters which represent all
	 * branches and tags.
	 *
	 * @param filters
	 *            The filters to check
	 * @return Whether exactly all branches and tags are selected
	 */
	public boolean isExactlyAllBranchesAndTagsSelected(Set<RefFilter> filters) {
		for (RefFilter filter : filters) {
			if (filter.isSelected()) {
				if (!filtersForAllBranchesAndTags.contains(filter))
					return false;
			} else {
				if (filtersForAllBranchesAndTags.contains(filter))
					return false;
			}
		}
		return true;
	}

	/**
	 * Get the default ref filters
	 *
	 * @return a set of the default ref filters.
	 */
	public Set<RefFilter> getDefaults() {
		setDefaultSelectionBasedOnShowAllBranches();
		RefFilterHelper defaultsHelper = new RefFilterHelper(this.repository) {
			@Override
			protected String getPreferenceString(String preferenceName) {
				return store.getDefaultString(preferenceName);
			}
		};
		return defaultsHelper.getRefFilters();
	}

	/**
	 * Representation of a ref filter
	 */
	public class RefFilter {

		private final boolean preconfigured;

		private String filterString;
		private TokenizedPattern filterPattern;

		private TokenizedPattern expandedFilterPattern;

		private boolean selected;

		/**
		 * Create a ref filter as the copy of an original.
		 * <p>
		 * The new filter has the same values as the original, but future
		 * changes on either of the original or this new filter won't affect the
		 * other one.
		 * </p>
		 *
		 * @param original
		 */
		public RefFilter(RefFilter original) {
			this(original.getFilterString(), original.isPreconfigured());
			this.selected = original.isSelected();
		}

		/**
		 * Create a new ref filter
		 *
		 * @param filterString
		 *            The filter string for the new ref filter; must not be
		 *            null; must not be empty.
		 *
		 * @throws IllegalArgumentException
		 *             if the filter string is null or empty
		 */
		public RefFilter(String filterString) {
			this(filterString, false);
		}

		/**
		 * Create a new ref filter
		 *
		 * @param filterString
		 *            The filter string for the new ref filter; must not be
		 *            null; must not be empty.
		 * @param isPreconfigured
		 *            Whether the new Filter is a preconfigured one
		 *
		 * @throws IllegalArgumentException
		 *             if the filter string is null or empty
		 */
		private RefFilter(String filterString, boolean isPreconfigured) {
			if (filterString == null || filterString.isEmpty()) {
				throw new IllegalArgumentException(
						"Filter string is null or empty."); //$NON-NLS-1$
			}
			this.filterString = filterString;
			this.filterPattern = createPattern(filterString);
			this.preconfigured = isPreconfigured;
		}

		/**
		 * @return whether this is a preconfigured filter
		 */
		public boolean isPreconfigured() {
			return preconfigured;
		}

		private TokenizedPattern patternWithExpandedMacros() {
			if (expandedFilterPattern == null) {
				expandedFilterPattern = expandMacros();
			}
			return expandedFilterPattern;
		}

		private TokenizedPattern expandMacros() {
			TokenizedPattern currentPattern = filterPattern;
			for(Map.Entry<String, Function<Repository, String>> macro : macros.entrySet()) {
				String macroString = macro.getKey();
				if (currentPattern.containsPattern(macroString)) {
					String replacingString = macro.getValue().apply(repository);
					String newString = currentPattern.getPattern()
							.replace(macroString, replacingString);
					currentPattern = createPattern(newString);
				}
			}
			return currentPattern;
		}

		private TokenizedPattern createPattern(String pattern) {
			return new TokenizedPattern(
					pattern.replace('/', File.separatorChar));
		}

		/**
		 * Tries to match the given ref against this filter.
		 *
		 * @param refPath
		 *            The path of the ref to match
		 * @return true if the ref path matches the pattern of this filter
		 */
		public boolean matches(TokenizedPath refPath) {
			return patternWithExpandedMacros().matchPath(refPath,
					true);
		}

		/**
		 * @return the filter string; cannot be null; cannot be empty
		 */
		public String getFilterString() {
			return this.filterString;
		}

		/**
		 * @param filterString
		 *            the filterString to set; must not be null; must not be
		 *            empty
		 * @throws IllegalArgumentException
		 *             if the filter string is null or empty
		 * @throws IllegalStateException
		 *             if this is a preconfigured filter
		 */
		public void setFilterString(String filterString) {
			if (filterString == null || filterString.isEmpty()) {
				throw new IllegalArgumentException(
						"Filter string is null or empty."); //$NON-NLS-1$
			}
			if (preconfigured) {
				throw new IllegalStateException(
						"Cannot change a preconfigured filter."); //$NON-NLS-1$
			}
			this.filterString = filterString;
			this.filterPattern = createPattern(filterString);
			this.expandedFilterPattern = null;
		}

		/**
		 * @return whether this filter is currently selected
		 */
		public boolean isSelected() {
			return selected;
		}

		/**
		 * @param selected
		 *            whether this filter is selected
		 */
		public void setSelected(boolean selected) {
			this.selected = selected;
		}

		@Override
		public int hashCode() {
			return filterPattern.hashCode();
		}

		@Override
		public boolean equals(Object obj) {
			if (!(obj instanceof RefFilter)) {
				return false;
			}
			return filterPattern.equals(((RefFilter) obj).filterPattern);
		}

		@Override
		public String toString() {
			StringBuilder builder = new StringBuilder();
			builder.append("RefFilter ["); //$NON-NLS-1$
			if (filterPattern != null) {
				builder.append("pattern="); //$NON-NLS-1$
				builder.append(filterPattern);
				builder.append(", "); //$NON-NLS-1$
				if (expandedFilterPattern != null
						&& !expandedFilterPattern.equals(filterPattern)) {
					builder.append("expandedPattern="); //$NON-NLS-1$
					builder.append(expandedFilterPattern);
					builder.append(", "); //$NON-NLS-1$
				}
			}
			builder.append("preconfigured="); //$NON-NLS-1$
			builder.append(preconfigured);
			builder.append(", selected="); //$NON-NLS-1$
			builder.append(selected);
			builder.append("]"); //$NON-NLS-1$
			return builder.toString();
		}
	}
}

Back to the top