/*******************************************************************************
 * Copyright (c) 2000, 2011 IBM Corporation and others.
 *
 * 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
 *
 * Contributors:
 *     IBM Corporation - initial API and implementation
 *******************************************************************************/

package org.eclipse.jdt.internal.ui.viewsupport;

import org.eclipse.jface.viewers.Viewer;
import org.eclipse.jface.viewers.ViewerFilter;

/**
 * filter with a live cycle
 */
public abstract class JavaViewerFilter extends ViewerFilter {

	private int fCount= 0;

	/**
	 * To be overridden by implement
	 */
	protected abstract void initFilter();

	protected abstract void freeFilter();

	public final void filteringStart() {
		if (fCount == 0)
			initFilter();
		fCount++;
	}

	public final void filteringEnd() {
		fCount--;
		if (fCount == 0)
			freeFilter();
	}

	/*
 	 * Overrides method from ViewerFilter
 	 */
	@Override
	public Object[] filter(Viewer viewer, Object parent, Object[] elements) {
		try {
			filteringStart();
			return super.filter(viewer, parent, elements);
		} finally {
			filteringEnd();
		}
	}


}
