| /******************************************************************************* |
| * Copyright (c) 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 |
| *******************************************************************************/ |
| package org.eclipse.equinox.internal.provisional.p2.ui.dialogs; |
| |
| import org.eclipse.equinox.internal.provisional.p2.metadata.IInstallableUnit; |
| import org.eclipse.equinox.internal.provisional.p2.ui.model.CategoryElement; |
| import org.eclipse.equinox.internal.provisional.p2.ui.model.IUElement; |
| import org.eclipse.equinox.internal.provisional.p2.ui.query.IUPropertyUtils; |
| import org.eclipse.equinox.internal.provisional.p2.ui.viewers.IUColumnConfig; |
| import org.eclipse.jface.viewers.Viewer; |
| import org.eclipse.ui.dialogs.PatternFilter; |
| |
| /** |
| * A class that handles filtering IU's based on a supplied |
| * matching string. |
| * |
| * @since 3.2 |
| * |
| */ |
| public class AvailableIUPatternFilter extends PatternFilter { |
| |
| boolean checkName, checkVersion, checkId = false; |
| String patternString; |
| |
| /** |
| * Create a new instance of a AvailableIUPatternFilter |
| */ |
| public AvailableIUPatternFilter(IUColumnConfig[] columnConfig) { |
| super(); |
| for (int i = 0; i < columnConfig.length; i++) { |
| int field = columnConfig[i].columnField; |
| if (field == IUColumnConfig.COLUMN_ID) |
| checkId = true; |
| else if (field == IUColumnConfig.COLUMN_NAME) |
| checkName = true; |
| else if (field == IUColumnConfig.COLUMN_VERSION) |
| checkVersion = true; |
| } |
| |
| } |
| |
| /* |
| * (non-Javadoc) |
| * @see org.eclipse.ui.internal.dialogs.PatternFilter#isElementSelectable(java.lang.Object) |
| */ |
| public boolean isElementSelectable(Object element) { |
| return element instanceof IUElement && !(element instanceof CategoryElement); |
| } |
| |
| /* |
| * Overridden to remember the pattern string for an optimization |
| * in isParentMatch |
| * (non-Javadoc) |
| * @see org.eclipse.ui.dialogs.PatternFilter#setPattern(java.lang.String) |
| */ |
| public void setPattern(String patternString) { |
| super.setPattern(patternString); |
| this.patternString = patternString; |
| } |
| |
| /* |
| * Overridden to avoid getting children unless there is actually |
| * a filter. |
| * (non-Javadoc) |
| * @see org.eclipse.ui.dialogs.PatternFilter#isParentMatch(org.eclipse.jface.viewers.Viewer, java.lang.Object) |
| */ |
| protected boolean isParentMatch(Viewer viewer, Object element) { |
| if (patternString == null || patternString.length() == 0) |
| return true; |
| return super.isParentMatch(viewer, element); |
| } |
| |
| /* (non-Javadoc) |
| * @see org.eclipse.ui.dialogs.PatternFilter#isElementMatch(org.eclipse.jface.viewers.Viewer, java.lang.Object) |
| */ |
| protected boolean isLeafMatch(Viewer viewer, Object element) { |
| if (element instanceof CategoryElement) { |
| return false; |
| } |
| |
| String text = null; |
| if (element instanceof IUElement) { |
| IInstallableUnit iu = ((IUElement) element).getIU(); |
| if (checkName) { |
| // Get the iu name in the default locale |
| text = IUPropertyUtils.getIUProperty(iu, IInstallableUnit.PROP_NAME); |
| if (text != null && wordMatches(text)) |
| return true; |
| } |
| if (checkId) { |
| text = iu.getId(); |
| if (wordMatches(text)) { |
| return true; |
| } |
| } |
| if (checkVersion) { |
| text = iu.getVersion().toString(); |
| if (wordMatches(text)) |
| return true; |
| } |
| } |
| return false; |
| } |
| } |