1 | /** |
2 | * |
3 | */ |
4 | package de.uka.ipd.sdq.cip.runtime.dialogs; |
5 | |
6 | import java.io.File; |
7 | |
8 | import org.eclipse.core.resources.IResource; |
9 | import org.eclipse.core.resources.ResourcesPlugin; |
10 | import org.eclipse.emf.common.util.URI; |
11 | import org.eclipse.swt.SWT; |
12 | import org.eclipse.swt.events.SelectionEvent; |
13 | import org.eclipse.swt.events.SelectionListener; |
14 | import org.eclipse.swt.layout.GridData; |
15 | import org.eclipse.swt.layout.GridLayout; |
16 | import org.eclipse.swt.widgets.Button; |
17 | import org.eclipse.swt.widgets.Composite; |
18 | import org.eclipse.swt.widgets.FileDialog; |
19 | import org.eclipse.swt.widgets.Label; |
20 | import org.eclipse.swt.widgets.Text; |
21 | import org.eclipse.ui.dialogs.FilteredResourcesSelectionDialog; |
22 | |
23 | class FileSelectionWidget extends Composite{ |
24 | |
25 | private Label label; |
26 | private Text textfield; |
27 | private Button workspaceButton; |
28 | private Button filesystemButton; |
29 | |
30 | private String[] fileExtensions; |
31 | |
32 | public FileSelectionWidget(Composite parent, int style) { |
33 | super(parent, style); |
34 | GridLayout layout = new GridLayout(3,false); |
35 | setLayout(layout); |
36 | |
37 | label = new Label(this, SWT.LEFT); |
38 | label.setText("DUMMY"); |
39 | GridData gd = new GridData(); |
40 | gd.grabExcessHorizontalSpace = true; |
41 | gd.horizontalAlignment = SWT.FILL; |
42 | label.setLayoutData(gd); |
43 | |
44 | workspaceButton = createButton(this, WORKSPACE); |
45 | gd = new GridData(); |
46 | workspaceButton.setLayoutData(gd); |
47 | |
48 | filesystemButton = createButton(this, FILESYSTEM); |
49 | gd = new GridData(); |
50 | filesystemButton.setLayoutData(gd); |
51 | |
52 | textfield = new Text(this,SWT.BORDER); |
53 | gd = new GridData(); |
54 | gd.horizontalSpan = layout.numColumns; |
55 | gd.grabExcessHorizontalSpace = true; |
56 | gd.horizontalAlignment = SWT.FILL; |
57 | gd.verticalAlignment = SWT.TOP; |
58 | textfield.setLayoutData(gd); |
59 | |
60 | } |
61 | |
62 | public void setCaption(String caption) { |
63 | label.setText(caption); |
64 | } |
65 | public void setFileExtensions(String[] fileExtensions) { |
66 | this.fileExtensions = fileExtensions; |
67 | } |
68 | |
69 | public void setText(String text) { |
70 | textfield.setText(text); |
71 | } |
72 | |
73 | public String getText() { |
74 | return textfield.getText(); |
75 | } |
76 | |
77 | private final static int FILESYSTEM = 2; |
78 | private final static int WORKSPACE = 1; |
79 | |
80 | private Button createButton(Composite parent, int style) { |
81 | Button button = new Button(parent,SWT.PUSH); |
82 | if((style & WORKSPACE) == WORKSPACE) { |
83 | button.setText("Browse &Workspace..."); |
84 | } |
85 | else { |
86 | button.setText("Browse &File System..."); |
87 | |
88 | } |
89 | button.addSelectionListener(new ButtonSelectionListener(this,style)); |
90 | return button; |
91 | } |
92 | |
93 | public boolean browseWorkspace() { |
94 | /*ResourceSelectionDialog resourceSelectionDialog = new ResourceSelectionDialog( |
95 | getShell(), |
96 | ResourcesPlugin.getWorkspace().getRoot(), |
97 | "File Selection");*/ |
98 | |
99 | FilteredResourcesSelectionDialog resourceSelectionDialog = new FilteredResourcesSelectionDialog(getShell(), false, ResourcesPlugin.getWorkspace().getRoot(),IResource.DEPTH_INFINITE | IResource.FILE); |
100 | resourceSelectionDialog.open(); |
101 | Object [] result = resourceSelectionDialog.getResult(); |
102 | if (result != null) |
103 | { |
104 | StringBuffer text = new StringBuffer (); |
105 | int length = result.length; |
106 | for (int i = 0; i < length; ++i) |
107 | { |
108 | IResource resource = (IResource)result[i]; |
109 | //if (isValidWorkspaceResource(resource)) |
110 | { |
111 | text.append(URI.createURI(URI.createPlatformResourceURI(resource.getFullPath().toString(),true).toString(), true)); |
112 | text.append(" "); |
113 | } |
114 | } |
115 | textfield.setText(text.toString().trim()); |
116 | return true; |
117 | } |
118 | return false; |
119 | } |
120 | |
121 | private boolean browseFileSystem() { |
122 | FileDialog fileDialog = new FileDialog(getShell(), SWT.OPEN | SWT.MULTI ); |
123 | fileDialog.setFilterExtensions(fileExtensions); |
124 | |
125 | if (textfield.getText() != null) |
126 | { |
127 | fileDialog.setFileName(textfield.getText()); |
128 | } |
129 | |
130 | if (fileDialog.open() != null && fileDialog.getFileNames().length > 0) |
131 | { |
132 | String [] fileNames = fileDialog.getFileNames(); |
133 | StringBuffer text = new StringBuffer (); |
134 | for (int i = 0; i < fileNames.length; ++i) |
135 | { |
136 | String filePath = fileDialog.getFilterPath() + File.separator + fileNames[i]; |
137 | text.append(URI.createFileURI(filePath).toString()); |
138 | text.append(" "); |
139 | } |
140 | textfield.setText(text.toString().trim()); |
141 | |
142 | return true; |
143 | } |
144 | return false; |
145 | } |
146 | |
147 | private class ButtonSelectionListener implements SelectionListener { |
148 | FileSelectionWidget widget; |
149 | int style; |
150 | |
151 | public ButtonSelectionListener(FileSelectionWidget fileSelectionWidget, int style) { |
152 | widget = fileSelectionWidget; |
153 | this.style = style; |
154 | } |
155 | |
156 | @Override |
157 | public void widgetDefaultSelected(SelectionEvent e) { |
158 | |
159 | } |
160 | |
161 | @Override |
162 | public void widgetSelected(SelectionEvent e) { |
163 | if((style & WORKSPACE) == WORKSPACE) { |
164 | widget.browseWorkspace(); |
165 | } |
166 | else { |
167 | widget.browseFileSystem(); |
168 | |
169 | } |
170 | } |
171 | |
172 | } |
173 | } |