Limit the amount of file names shown in the text widget.
[simantics/platform.git] / bundles / org.simantics.utils.ui / src / org / simantics / utils / ui / widgets / FileSelectionWidget.java
1 package org.simantics.utils.ui.widgets;
2
3 import java.io.File;
4
5 import org.eclipse.swt.SWT;
6 import org.eclipse.swt.widgets.Composite;
7 import org.eclipse.swt.widgets.FileDialog;
8
9 public class FileSelectionWidget extends FileOrDirectorySelectionWidget {
10
11         
12         String[] filterExtensions = new String[]{"*.*"};
13         String[] filterNames = new String[]{"All files"};
14         int style = SWT.OPEN;
15         
16         public FileSelectionWidget(Composite parent, String name, int style) {
17                 super(parent, name, style&~(SWT.OPEN|SWT.CLOSE));
18                 this.style = style & (SWT.OPEN|SWT.CLOSE|SWT.MULTI);
19         }
20
21         @Override
22         protected String[] openDialog() {
23                 FileDialog dialog = new FileDialog(getShell(),style);
24                 dialog.setFilterExtensions(getFilterExtensions());
25                 dialog.setFilterNames(getFilterNames());
26                 if (filename != null && filename.length == 1)
27                         dialog.setFileName(filename[0]);
28                 String filename = dialog.open();
29                 if (filename == null)
30                         return null;
31                 String path = dialog.getFilterPath();
32                 String names[] = dialog.getFileNames();
33                 String fullpath[] = new String[names.length];
34                 for (int i = 0; i < names.length; i++) {
35                         fullpath[i] = path + File.separator + names[i];
36                 }
37                 return fullpath;
38         }
39
40         @Override
41         protected boolean isValid(File file) {
42                 boolean accept = file.exists() && file.canRead();
43                 return accept;
44         }
45         
46         public String[] getFilterExtensions() {
47                 return filterExtensions;
48         }
49         
50         public String[] getFilterNames() {
51                 return filterNames;
52         }
53         
54         public void setFilterExtensions(String[] filterExtensions) {
55                 this.filterExtensions = filterExtensions;
56         }
57         
58         public void setFilterNames(String[] filterNames) {
59                 this.filterNames = filterNames;
60         }
61         
62
63 }