1 /******************************************************************************* 2 * Copyright (c) 2014 Red Hat Inc. 3 * All rights reserved. This program and the accompanying materials 4 * are made available under the terms of the Eclipse Public License v1.0 5 * which accompanies this distribution, and is available at 6 * http://www.eclipse.org/legal/epl-v10.html 7 * 8 * Contributors: 9 * Mickael Istria (Red Hat Inc.) - 226046 Allow user to specify filters 10 ******************************************************************************/ 11 package org.eclipse.ui.internal.navigator.filters; 12 13 import org.eclipse.jface.preference.PreferenceStore; 14 import org.eclipse.ui.IMemento; 15 import org.eclipse.ui.dialogs.SearchPattern; 16 import org.eclipse.ui.internal.navigator.NavigatorPlugin; 17 import org.eclipse.ui.navigator.CommonViewer; 18 19 /** 20 * This class is a bean that represents a resource filter configured by user 21 * based on a regexp for resource name. They can be edited via the {@link UserFiltersTab}, are supposed to be attached 22 * as a data to {@link CommonViewer#setData(String, Object)} with key {@link NavigatorPlugin#RESOURCE_REGEXP_FILTER_DATA}. 23 * The view or UI element that creates the {@link CommonViewer} is supposed to set the data with the right value. A typical 24 * usage is to load initial filters from a {@link IMemento} or {@link PreferenceStore} 25 */ 26 public class UserFilter { 27 private String regexp = "*.something"; //$NON-NLS-1$ 28 private boolean enabled = false; 29 private SearchPattern searchPattern; 30 31 /** 32 * Instantiate a new UserFilter with default values 33 */ UserFilter()34 public UserFilter() { } 35 36 /** 37 * Instantiates a new UserFilter with specified value 38 * @param regexp 39 * @param enable 40 */ UserFilter(String regexp, boolean enable)41 public UserFilter(String regexp, boolean enable) { 42 this.regexp = regexp; 43 this.enabled = enable; 44 } 45 46 /** 47 * 48 * @return the regexp configured by this filter 49 */ getRegexp()50 public String getRegexp() { 51 return this.regexp; 52 } 53 54 /** 55 * 56 * @param newRegexp a new value for the regexp 57 */ setRegexp(String newRegexp)58 public void setRegexp(String newRegexp) { 59 this.regexp = newRegexp; 60 } 61 62 /** 63 * 64 * @return whether this filter should be processed or not. 65 * If not enabled, a filter shouldn't have effect on the content 66 * of the viewer. 67 */ isEnabled()68 public boolean isEnabled() { 69 return this.enabled; 70 } 71 72 /** 73 * 74 * @param enable whether to enable this filter or not in the viewer 75 */ setEnabled(boolean enable)76 public void setEnabled(boolean enable) { 77 this.enabled = enable; 78 } 79 80 /** 81 * @param string 82 * @return whether the given string matches the filter definition 83 */ matches(String string)84 public boolean matches(String string) { 85 if (this.searchPattern == null) { 86 this.searchPattern = new SearchPattern(); 87 this.searchPattern.setPattern(this.regexp); 88 } 89 return this.searchPattern.matches(string); 90 } 91 }