Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: b46f6f005e85969b7d208ec74ff67d0c01bc224e (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
/*****************************************************************************
 * Copyright (c) 2008 CEA LIST.
 *
 *    
 * 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:
 *  Chokri Mraidha (CEA LIST) Chokri.Mraidha@cea.fr - Initial API and implementation
 *  Patrick Tessier (CEA LIST) Patrick.Tessier@cea.fr - modification
 *
 *****************************************************************************/
package org.eclipse.papyrus.uml.profile.ui.dialogs;

import java.util.List;

import org.eclipse.core.resources.IContainer;
import org.eclipse.core.resources.IFile;
import org.eclipse.jface.viewers.Viewer;
import org.eclipse.jface.viewers.ViewerFilter;


public class FileSelectionFilter extends ViewerFilter {

	/** Authorized file extensions */
	List<String> filetypes;

	/**
	 * If newFileTypes is null, any extension is accepted
	 * 
	 * @param newFiletypes
	 *        authorized extensions for the file viewer filter
	 */
	public FileSelectionFilter(List<String> newFiletypes) {
		super();
		filetypes = newFiletypes;
	}

	@Override
	public boolean select(Viewer arg0, Object arg1, Object arg2) {

		if((arg2 instanceof IContainer) || (arg2 instanceof IFile)) {

			// Filter files
			if(arg2 instanceof IFile) {
				IFile file = (IFile)arg2;
				String file_ext = file.getFileExtension();

				if((filetypes != null) && (!filetypes.contains(file_ext))) {
					return false;
				}
			}

			// Filter folders
			if(arg2 instanceof IContainer) {
				IContainer container = (IContainer)arg2;
				String name = container.getName();

				// Mask hidden folder 
				if(name.startsWith(".")) {
					return false;
				}
			}

			return true;
		}
		return false;
	}

}

Back to the top