]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.utils.ui/src/org/simantics/utils/ui/widgets/FileOrDirectorySelectionWidget.java
Fixed multiple issues causing dangling references to discarded queries
[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,true);
57                                 }
58                         }
59                 });
60                 
61                 fileText.addModifyListener(new ModifyListener() {
62                         
63                         @Override
64                         public void modifyText(ModifyEvent e) {
65                                 String file = fileText.getText();
66                                 String files[] = file.split(",");
67                                 for (int i = 0; i < files.length; i++) {
68                                         files[i] = files[i].trim();
69                                 }
70                                 setFilename(files, false);
71                                 
72                         }
73                 });
74         }
75         
76         protected abstract String[] openDialog();
77         
78         protected abstract boolean isValid(File file);
79         
80         protected void setFilename(String[] filename) {
81                 setFilename(filename, true);
82         }
83         
84         protected void setFilename(String[] filename, boolean update) {
85                 String text = "";
86                 if (filename.length < 6) {
87                         for (String s : filename) {
88                                 text += s + ",";
89                         }
90                         if (text.length() > 2)
91                                 text = text.substring(0, text.length() - 1);
92                 } else {
93                         text = filename[0] + " and " + (filename.length -1) + " other files.";
94                 }
95                 
96                 if (update && !text.equals(fileText.getText()))
97                         fileText.setText(text);
98                 
99                 boolean accept = true;
100                 for (String s : filename){
101                         File file = new File(s);
102                         if (!isValid(file)) {
103                                 accept = false;
104                                 break;
105                         }
106                 }
107                 if (accept)
108                         this.filename = filename;
109                 else
110                         this.filename = null;
111                 
112                 for (FileSelectionListener l : listeners) {
113                         l.fileSelected(this, this.filename);
114                 }
115         }
116         
117         public String[] getFilename() {
118                 return filename;
119         }
120         
121         public void addListener(FileSelectionListener listener) {
122                 this.listeners.add(listener);
123         }
124         
125         public void removeListener(FileSelectionListener listener) {
126                 this.listeners.remove(listener);
127         }
128         
129
130 }