1 package org.simantics.utils.ui.widgets;
4 import java.util.ArrayList;
7 import org.eclipse.jface.layout.GridDataFactory;
8 import org.eclipse.swt.SWT;
9 import org.eclipse.swt.events.ModifyEvent;
10 import org.eclipse.swt.events.ModifyListener;
11 import org.eclipse.swt.events.SelectionAdapter;
12 import org.eclipse.swt.events.SelectionEvent;
13 import org.eclipse.swt.layout.GridData;
14 import org.eclipse.swt.layout.GridLayout;
15 import org.eclipse.swt.widgets.Button;
16 import org.eclipse.swt.widgets.Composite;
17 import org.eclipse.swt.widgets.Group;
18 import org.eclipse.swt.widgets.Text;
20 public abstract class FileOrDirectorySelectionWidget extends Composite{
25 private List<FileSelectionListener> listeners = new ArrayList<FileSelectionListener>();
27 public FileOrDirectorySelectionWidget(Composite parent, String name,int style) {
29 this.setLayout(new GridLayout(1,false));
30 GridDataFactory.fillDefaults().grab(true, false).align(SWT.FILL, SWT.TOP).applyTo(this);
32 Group fileGroup = new Group(this, SWT.NONE);
33 GridDataFactory.fillDefaults().grab(true, false).align(SWT.FILL, SWT.TOP).applyTo(fileGroup);
35 fileGroup.setText(name);
36 fileGroup.setLayout(new GridLayout(2, false));
38 fileText = new Text(fileGroup, SWT.SINGLE|SWT.BORDER);
39 GridData data = new GridData();
40 data.grabExcessHorizontalSpace = true;
41 data.horizontalAlignment = SWT.FILL;
42 fileText.setLayoutData(data);
44 Button browseButton = new Button(fileGroup, SWT.PUSH);
45 browseButton.setText("Browse");
46 data = new GridData();
47 browseButton.setLayoutData(data);
51 browseButton.addSelectionListener(new SelectionAdapter() {
53 public void widgetSelected(SelectionEvent e) {
54 String[] name = openDialog();
61 fileText.addModifyListener(new ModifyListener() {
64 public void modifyText(ModifyEvent e) {
65 String file = fileText.getText();
66 setFilename(file.split(","));
72 protected abstract String[] openDialog();
74 protected abstract boolean isValid(File file);
76 protected void setFilename(String[] filename) {
78 for (String s : filename) {
81 if (text.length() > 2)
82 text = text.substring(0, text.length() - 1);
84 if (!text.equals(fileText.getText()))
85 fileText.setText(text);
87 boolean accept = true;
88 for (String s : filename){
89 File file = new File(s);
96 this.filename = filename;
100 for (FileSelectionListener l : listeners) {
101 l.fileSelected(this, this.filename);
105 public String[] getFilename() {
109 public void addListener(FileSelectionListener listener) {
110 this.listeners.add(listener);
113 public void removeListener(FileSelectionListener listener) {
114 this.listeners.remove(listener);