Skip to main content
summaryrefslogtreecommitdiffstats
blob: f8cd8e8928efebf68851ce7fb5a13864ce037a4d (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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
/*******************************************************************************
 * Copyright (c) 2000, 2015 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.search.internal.ui.util;

import java.util.Arrays;
import java.util.Comparator;
import java.util.HashSet;
import java.util.Set;
import java.util.StringTokenizer;

import org.eclipse.swt.events.DisposeEvent;
import org.eclipse.swt.events.DisposeListener;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Combo;
import org.eclipse.swt.widgets.Widget;

import org.eclipse.jface.window.Window;

import org.eclipse.ui.dialogs.TypeFilteringDialog;

import org.eclipse.search.internal.ui.SearchMessages;

public class FileTypeEditor extends SelectionAdapter implements DisposeListener {

	private Combo fTextField;
	private Button fBrowseButton;

	private final static String TYPE_DELIMITER= SearchMessages.FileTypeEditor_typeDelimiter;
	public final static String FILE_PATTERN_NEGATOR= "!"; //$NON-NLS-1$

	private static final Comparator<String> FILE_TYPES_COMPARATOR= new Comparator<String>() {
		@Override
		public int compare(String fp1, String fp2) {
			boolean isNegative1= fp1.startsWith(FILE_PATTERN_NEGATOR);
			boolean isNegative2= fp2.startsWith(FILE_PATTERN_NEGATOR);
			if (isNegative1 != isNegative2) {
				return isNegative1 ? 1 : -1;
			}
			return fp1.compareTo(fp2);
		}
	};

	public FileTypeEditor(Combo textField, Button browseButton) {
		fTextField= textField;
		fBrowseButton= browseButton;

		fTextField.addDisposeListener(this);
		fBrowseButton.addDisposeListener(this);
		fBrowseButton.addSelectionListener(this);
	}

	@Override
	public void widgetDisposed(DisposeEvent event) {
		Widget widget= event.widget;
		if (widget == fTextField)
			fTextField= null;
		else if (widget	== fBrowseButton)
			fBrowseButton= null;
	}

	@Override
	public void widgetSelected(SelectionEvent event) {
		if (event.widget == fBrowseButton)
			handleBrowseButton();
	}

	public String[] getFileTypes() {
		Set<String> result= new HashSet<>();
		StringTokenizer tokenizer= new StringTokenizer(fTextField.getText(), TYPE_DELIMITER);

		while (tokenizer.hasMoreTokens()) {
			String currentExtension= tokenizer.nextToken().trim();
			result.add(currentExtension);
		}
		return result.toArray(new String[result.size()]);
	}

	public void setFileTypes(String[] types) {
		fTextField.setText(typesToString(types));
	}

	protected void handleBrowseButton() {
		TypeFilteringDialog dialog= new TypeFilteringDialog(fTextField.getShell(), Arrays.asList(getFileTypes()));
		if (dialog.open() == Window.OK) {
			Object[] result= dialog.getResult();
			HashSet<String> patterns= new HashSet<>();
			boolean starIncluded= false;
			for (int i= 0; i < result.length; i++) {
				String curr= result[i].toString();
				if (curr.equals("*")) { //$NON-NLS-1$
					starIncluded= true;
				} else {
					patterns.add("*." + curr); //$NON-NLS-1$
				}
			}
			if (patterns.isEmpty() && starIncluded) { // remove star when other file extensions active
				patterns.add("*"); //$NON-NLS-1$
			}
			String[] filePatterns= patterns.toArray(new String[patterns.size()]);
			Arrays.sort(filePatterns);
			setFileTypes(filePatterns);
		}
	}

	public static String typesToString(String[] types) {
		Arrays.sort(types, FILE_TYPES_COMPARATOR);
		StringBuffer result= new StringBuffer();
		for (int i= 0; i < types.length; i++) {
			if (i > 0) {
				result.append(TYPE_DELIMITER);
				result.append(" "); //$NON-NLS-1$
			}
			result.append(types[i]);
		}
		return result.toString();
	}
}

Back to the top