Skip to main content
summaryrefslogtreecommitdiffstats
blob: bbff5516fccc3c024cc96eb8fbde501e532dc4a2 (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
/*******************************************************************************
 *  Copyright (c) 2009  Oracle. 
 *  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: 
 *  	Oracle - initial API and implementation
 *******************************************************************************/
package org.eclipse.jpt.common.ui.internal.jface;

import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IFolder;
import org.eclipse.core.resources.IResource;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IPath;
import org.eclipse.jface.viewers.Viewer;
import org.eclipse.jface.viewers.ViewerFilter;
import org.eclipse.jpt.common.ui.JptCommonUiPlugin;

/**
 * This filter will deny showing any file that are not JAR files or folders
 * that don't contain any JAR files in its sub-hierarchy. 
 */
public class ArchiveFileViewerFilter 
	extends ViewerFilter 
{
	private static final String[] archiveExtensions= { "jar", "zip" }; //$NON-NLS-1$ //$NON-NLS-2$ 
	
	
	public ArchiveFileViewerFilter() {
		super();
	}
	
	
	@Override
	public boolean select(
			Viewer viewer, Object parentElement, Object element) {
		if (element instanceof IFile) {
			return isArchivePath(((IFile)element).getFullPath());
		}
		else if (element instanceof IFolder) {
			IFolder folder = (IFolder) element;
			try {
				for (IResource each : folder.members()) {
					if (select(viewer, folder, each)) {
						return true;
					}
				}
			}
			catch (CoreException ce) {
				// just skip this one, then
				JptCommonUiPlugin.log(ce);
			}
		}
		return false;
	}
	
	public static boolean isArchivePath(IPath path) {
		String ext= path.getFileExtension();
		if (ext != null && ext.length() != 0) {
			for (int i= 0; i < archiveExtensions.length; i++) {
				if (ext.equalsIgnoreCase(archiveExtensions[i])) {
					return true;
				}
			}
		}
		return false;
	}		
}

Back to the top