Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: efda781116ac76da04b4f0e63b7956e1b7a2f966 (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
/*******************************************************************************
 * Copyright (c) 2000, 2004 IBM Corporation and others.
 * All rights reserved. This program and the accompanying materials 
 * are made available under the terms of the Common Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/cpl-v10.html
 * 
 * Contributors:
 *     IBM Corporation - initial API and implementation
 *******************************************************************************/
package org.eclipse.search.internal.ui.util;

import java.util.Arrays;
import java.util.HashSet;
import java.util.Iterator;
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.events.SelectionListener;
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.IEditorRegistry;

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

public class FileTypeEditor extends SelectionAdapter implements DisposeListener, SelectionListener {
	
	private Combo fTextField;
	private Button fBrowseButton;

	final static String TYPE_DELIMITER= SearchMessages.getString("FileTypeEditor.typeDelimiter"); //$NON-NLS-1$

	public FileTypeEditor(IEditorRegistry registry, Combo textField, Button browseButton) {
		fTextField= textField;
		fBrowseButton= browseButton;
		
		fTextField.addDisposeListener(this);
		fBrowseButton.addDisposeListener(this);
		fBrowseButton.addSelectionListener(this);
	}
	
	public void widgetDisposed(DisposeEvent event) {
		Widget widget= event.widget;
		if (widget == fTextField) 
			fTextField= null;
		else if (widget	== fBrowseButton)
			fBrowseButton= null;
	}
	
	public void widgetSelected(SelectionEvent event) {
		if (event.widget == fBrowseButton)
			handleBrowseButton();
	}
		
	public void widgetDoubleSelected(SelectionEvent event) {
	}
	/**
	 *	Answer a collection of the currently-specified resource types
	 *
	 *	@return java.util.Vector
	 */
	public Set getFileTypes() {
		Set result= new HashSet();
			StringTokenizer tokenizer= new StringTokenizer(fTextField.getText(), TYPE_DELIMITER);

			while (tokenizer.hasMoreTokens()) {
				String currentExtension= tokenizer.nextToken().trim();
					result.add(currentExtension);
			}
		return result;
	}
	/**
	 *	Populate self's import types field based upon the passed types collection
	 *
	 *	@param types java.util.Vector
	 */
	public void setFileTypes(Set types) {
		fTextField.setText(typesToString(types));
	}
	protected void handleBrowseButton() {
		TypeFilteringDialog dialog= new TypeFilteringDialog(fTextField.getShell(), getFileTypes());
		if (dialog.open() == Window.OK) {
			setFileTypes(new HashSet(Arrays.asList(dialog.getResult())));
		}
	}

	public static String typesToString(Set types) {
		StringBuffer result= new StringBuffer();
		Iterator typesIter= types.iterator();
		boolean first= true;
		while (typesIter.hasNext()) {
			if (!first) {
				result.append(TYPE_DELIMITER);
				result.append(" "); //$NON-NLS-1$
			} else
				first= false;
			result.append(typesIter.next());
		}
		return result.toString();
	}
}

Back to the top