]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.utils.ui/src/org/simantics/utils/ui/widgets/FileOrDirectorySelectionWidget.java
Fixed a bug related to the extent of variable frames (see test case)
[simantics/platform.git] / bundles / org.simantics.utils.ui / src / org / simantics / utils / ui / widgets / FileOrDirectorySelectionWidget.java
1 package org.simantics.utils.ui.widgets;
2
3 import java.io.File;
4 import java.util.ArrayList;
5 import java.util.List;
6
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;
19
20 public abstract class FileOrDirectorySelectionWidget extends Composite{
21         
22         Text fileText;
23         String filename[];
24         
25         private List<FileSelectionListener> listeners = new ArrayList<FileSelectionListener>();
26         
27         public FileOrDirectorySelectionWidget(Composite parent, String name,int style) {
28                 super(parent, style);
29                 this.setLayout(new GridLayout(1,false));
30                 GridDataFactory.fillDefaults().grab(true, false).align(SWT.FILL, SWT.TOP).applyTo(this);
31                 
32                 Group fileGroup = new Group(this, SWT.NONE);
33                 GridDataFactory.fillDefaults().grab(true, false).align(SWT.FILL, SWT.TOP).applyTo(fileGroup);
34                 
35                 fileGroup.setText(name);
36                 fileGroup.setLayout(new GridLayout(2, false));
37                 
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);
43                 
44                 Button browseButton = new Button(fileGroup, SWT.PUSH);
45                 browseButton.setText("Browse");
46                 data = new GridData();
47                 browseButton.setLayoutData(data);
48                 
49                 
50                                 
51                 browseButton.addSelectionListener(new SelectionAdapter() {
52                         @Override
53                         public void widgetSelected(SelectionEvent e) {
54                                 String[] name = openDialog();
55                                 if (name != null) {
56                                         setFilename(name);
57                                 }
58                         }
59                 });
60                 
61                 fileText.addModifyListener(new ModifyListener() {
62                         
63                         @Override
64                         public void modifyText(ModifyEvent e) {
65                                 String file = fileText.getText();
66                                 setFilename(file.split(","));
67                                 
68                         }
69                 });
70         }
71         
72         protected abstract String[] openDialog();
73         
74         protected abstract boolean isValid(File file);
75         
76         protected void setFilename(String[] filename) {
77                 String text = "";
78                 for (String s : filename) {
79                         text += s + ",";
80                 }
81                 if (text.length() > 2)
82                         text = text.substring(0, text.length() - 1);
83                 
84                 if (!text.equals(fileText.getText()))
85                         fileText.setText(text);
86                 
87                 boolean accept = true;
88                 for (String s : filename){
89                         File file = new File(s);
90                         if (!isValid(file)) {
91                                 accept = false;
92                                 break;
93                         }
94                 }
95                 if (accept)
96                         this.filename = filename;
97                 else
98                         this.filename = null;
99                 
100                 for (FileSelectionListener l : listeners) {
101                         l.fileSelected(this, this.filename);
102                 }
103         }
104         
105         public String[] getFilename() {
106                 return filename;
107         }
108         
109         public void addListener(FileSelectionListener listener) {
110                 this.listeners.add(listener);
111         }
112         
113         public void removeListener(FileSelectionListener listener) {
114                 this.listeners.remove(listener);
115         }
116         
117
118 }