1 | package de.uka.ipd.sdq.sensorframework.dialogs.dataset; |
2 | |
3 | |
4 | import java.net.URI; |
5 | |
6 | import org.eclipse.core.filesystem.EFS; |
7 | import org.eclipse.core.filesystem.IFileInfo; |
8 | import org.eclipse.core.filesystem.IFileStore; |
9 | import org.eclipse.core.filesystem.URIUtil; |
10 | import org.eclipse.core.resources.IResource; |
11 | import org.eclipse.core.resources.ResourcesPlugin; |
12 | import org.eclipse.core.runtime.CoreException; |
13 | import org.eclipse.core.runtime.IPath; |
14 | import org.eclipse.core.runtime.Path; |
15 | import org.eclipse.core.runtime.Preferences; |
16 | import org.eclipse.jface.viewers.DoubleClickEvent; |
17 | import org.eclipse.jface.viewers.IDoubleClickListener; |
18 | import org.eclipse.jface.viewers.ISelection; |
19 | import org.eclipse.jface.viewers.ISelectionChangedListener; |
20 | import org.eclipse.jface.viewers.IStructuredSelection; |
21 | import org.eclipse.jface.viewers.SelectionChangedEvent; |
22 | import org.eclipse.jface.viewers.TreeViewer; |
23 | import org.eclipse.jface.viewers.ViewerComparator; |
24 | import org.eclipse.jface.wizard.WizardPage; |
25 | import org.eclipse.swt.SWT; |
26 | import org.eclipse.swt.events.SelectionAdapter; |
27 | import org.eclipse.swt.events.SelectionEvent; |
28 | import org.eclipse.swt.graphics.Point; |
29 | import org.eclipse.swt.layout.GridData; |
30 | import org.eclipse.swt.layout.GridLayout; |
31 | import org.eclipse.swt.widgets.Button; |
32 | import org.eclipse.swt.widgets.Composite; |
33 | import org.eclipse.swt.widgets.Event; |
34 | import org.eclipse.swt.widgets.Label; |
35 | import org.eclipse.swt.widgets.Listener; |
36 | import org.eclipse.swt.widgets.Shell; |
37 | import org.eclipse.swt.widgets.Text; |
38 | import org.eclipse.ui.internal.ide.IDEWorkbenchMessages; |
39 | import org.eclipse.ui.internal.ide.dialogs.CreateLinkedResourceGroup; |
40 | import org.eclipse.ui.model.WorkbenchContentProvider; |
41 | import org.eclipse.ui.model.WorkbenchLabelProvider; |
42 | import org.eclipse.ui.part.DrillDownComposite; |
43 | |
44 | /** |
45 | * Load the existing data source wizard page. If the initial resource selection |
46 | * contains exactly one file or directory resource then it will be used as the |
47 | * default data source. |
48 | * |
49 | * @param pageName |
50 | * the name of the page |
51 | * @param type |
52 | * specifies the type of resource to link to. IResource.FILE or |
53 | * IResource.FOLDER |
54 | * |
55 | * @author Roman Andrej |
56 | */ |
57 | public class WizardDatasourceLoadPage extends WizardPage implements Listener { |
58 | |
59 | /** string constants */ |
60 | private static final String WIZARD_TITEL_FOLDER = "Choose the data directory"; |
61 | private static final String WIZARD_MSG_FOLDER = "Choose the folder resource."; |
62 | private static final String WIZARD_TITEL_FILE = "Choose the data file"; |
63 | private static final String WIZARD_MSG_FILE = "Choose the file resource."; |
64 | |
65 | private static final int SIZING_CONTAINER_GROUP_HEIGHT = 300; |
66 | private static final int SIZING_CONTAINER_GROUP_WIDTH = 250; |
67 | |
68 | private int type; |
69 | |
70 | private Button advancedButton; |
71 | |
72 | private CreateLinkedResourceGroup linkedResourceGroup; |
73 | |
74 | private Composite linkedResourceParent; |
75 | |
76 | private Composite linkedResourceComposite; |
77 | |
78 | |
79 | /** |
80 | * Creates a wizard of load data source |
81 | */ |
82 | protected WizardDatasourceLoadPage(String pageName, int type) { |
83 | super(pageName); |
84 | this.type = type; |
85 | } |
86 | |
87 | /** |
88 | * Height of the "advanced" linked resource group. Set when the advanced |
89 | * group is first made visible. |
90 | */ |
91 | private int linkedResourceGroupHeight = -1; |
92 | |
93 | // handle on parts |
94 | private Text containerNameField; |
95 | |
96 | // Last selection made by user |
97 | //private String selectedResourcePath = ""; |
98 | |
99 | // Last selection made by user |
100 | private IResource selectedResource; |
101 | |
102 | private IFileStore fileStore = null; |
103 | |
104 | private TreeViewer treeViewer; |
105 | |
106 | /* (non-Javadoc) |
107 | * @see org.eclipse.jface.dialogs.IDialogPage#createControl(org.eclipse.swt.widgets.Composite) |
108 | */ |
109 | public void createControl(Composite parent) { |
110 | initializeDialogUnits(parent); |
111 | // top level group |
112 | Composite composite = new Composite(parent, SWT.NONE); |
113 | composite.setFont(parent.getFont()); |
114 | composite.setLayout(new GridLayout()); |
115 | composite.setLayoutData(new GridData(GridData.VERTICAL_ALIGN_FILL |
116 | | GridData.HORIZONTAL_ALIGN_FILL)); |
117 | |
118 | Label label = new Label(composite, SWT.WRAP); |
119 | label.setText("Enter or select the " + getResourceType() + ":"); |
120 | label.setFont(this.getFont()); |
121 | |
122 | containerNameField = new Text(composite, SWT.SINGLE | SWT.BORDER); |
123 | containerNameField |
124 | .setLayoutData(new GridData(GridData.FILL_HORIZONTAL)); |
125 | //containerNameField.addListener(SWT.Modify, this); |
126 | containerNameField.setFont(this.getFont()); |
127 | |
128 | createTreeViewer(composite); |
129 | createAdvancedControls(composite); |
130 | // Show description on opening |
131 | setErrorMessage(null); |
132 | setDialogStrings(); |
133 | setControl(composite); |
134 | |
135 | this.setPageComplete(false); |
136 | } |
137 | |
138 | |
139 | /** The method set a wizard title and message */ |
140 | private void setDialogStrings() { |
141 | setMessage(null); |
142 | if (type == IResource.FILE) { |
143 | setMessage(WIZARD_MSG_FILE); |
144 | setTitle(WIZARD_TITEL_FILE); |
145 | } |
146 | |
147 | if (type == IResource.FOLDER) { |
148 | setMessage(WIZARD_MSG_FOLDER); |
149 | setTitle(WIZARD_TITEL_FOLDER); |
150 | } |
151 | } |
152 | |
153 | /** |
154 | * Creates the widget for advanced options. |
155 | * |
156 | * @param parent |
157 | * the parent composite |
158 | */ |
159 | protected void createAdvancedControls(Composite parent) { |
160 | Preferences preferences = ResourcesPlugin.getPlugin() |
161 | .getPluginPreferences(); |
162 | |
163 | if (preferences.getBoolean(ResourcesPlugin.PREF_DISABLE_LINKING) == false) { |
164 | linkedResourceParent = new Composite(parent, SWT.NONE); |
165 | linkedResourceParent.setFont(parent.getFont()); |
166 | linkedResourceParent.setLayoutData(new GridData( |
167 | GridData.FILL_HORIZONTAL)); |
168 | GridLayout layout = new GridLayout(); |
169 | layout.marginHeight = 0; |
170 | layout.marginWidth = 0; |
171 | linkedResourceParent.setLayout(layout); |
172 | |
173 | advancedButton = new Button(linkedResourceParent, SWT.PUSH); |
174 | advancedButton.setFont(linkedResourceParent.getFont()); |
175 | |
176 | advancedButton.setText(IDEWorkbenchMessages.showAdvanced); |
177 | GridData data = setButtonLayoutData(advancedButton); |
178 | data.horizontalAlignment = GridData.BEGINNING; |
179 | advancedButton.setLayoutData(data); |
180 | advancedButton.addSelectionListener(new SelectionAdapter() { |
181 | public void widgetSelected(SelectionEvent e) { |
182 | handleAdvancedButtonSelect(); |
183 | } |
184 | }); |
185 | } |
186 | |
187 | linkedResourceGroup = new CreateLinkedResourceGroup(type, |
188 | this, new CreateLinkedResourceGroup.IStringValue() { |
189 | |
190 | public String getValue() { |
191 | return containerNameField.getText(); |
192 | } |
193 | |
194 | public void setValue(String string) { |
195 | containerNameField.setText(string); |
196 | } |
197 | |
198 | @Override |
199 | public IResource getResource() { |
200 | // TODO Auto-generated method stub |
201 | return null; |
202 | } |
203 | }); |
204 | } |
205 | |
206 | /** |
207 | * Returns a new drill down viewer for this dialog. |
208 | * |
209 | * @param heightHint |
210 | * height hint for the drill down composite |
211 | */ |
212 | protected void createTreeViewer(Composite parent) { |
213 | // Create drill down. |
214 | DrillDownComposite drillDown = new DrillDownComposite(parent, |
215 | SWT.BORDER); |
216 | GridData spec = new GridData(SWT.FILL, SWT.FILL, true, true); |
217 | spec.heightHint = SIZING_CONTAINER_GROUP_HEIGHT; |
218 | spec.widthHint = SIZING_CONTAINER_GROUP_WIDTH; |
219 | drillDown.setLayoutData(spec); |
220 | |
221 | // Create tree viewer inside drill down. |
222 | treeViewer = new TreeViewer(drillDown, SWT.NONE); |
223 | drillDown.setChildTree(treeViewer); |
224 | treeViewer.setContentProvider(new WorkbenchContentProvider()); |
225 | treeViewer.setLabelProvider(WorkbenchLabelProvider |
226 | .getDecoratingWorkbenchLabelProvider()); |
227 | treeViewer.setComparator(new ViewerComparator()); |
228 | treeViewer.setUseHashlookup(true); |
229 | treeViewer.addSelectionChangedListener(new ISelectionChangedListener() { |
230 | public void selectionChanged(SelectionChangedEvent event) { |
231 | IStructuredSelection selection = (IStructuredSelection) event |
232 | .getSelection(); |
233 | Object object = selection.getFirstElement(); |
234 | |
235 | if (object instanceof IResource) { |
236 | selectedResource = (IResource) object; |
237 | setPageComplete(validitaResource(selectedResource)); |
238 | } |
239 | } |
240 | }); |
241 | treeViewer.addDoubleClickListener(new IDoubleClickListener() { |
242 | public void doubleClick(DoubleClickEvent event) { |
243 | ISelection selection = event.getSelection(); |
244 | if (selection instanceof IStructuredSelection) { |
245 | Object item = ((IStructuredSelection) selection) |
246 | .getFirstElement(); |
247 | if (item == null) { |
248 | return; |
249 | } |
250 | if (treeViewer.getExpandedState(item)) { |
251 | treeViewer.collapseToLevel(item, 1); |
252 | } else { |
253 | treeViewer.expandToLevel(item, 1); |
254 | } |
255 | } |
256 | } |
257 | }); |
258 | |
259 | // This has to be done after the viewer has been laid out |
260 | treeViewer.setInput(ResourcesPlugin.getWorkspace()); |
261 | } |
262 | |
263 | /** |
264 | * The container selection has changed in the tree view. Update the |
265 | * container name field value and notify all listeners. |
266 | * |
267 | * @param text |
268 | * The container that changed |
269 | */ |
270 | public void containerSelectionChanged(String text) { |
271 | |
272 | if (text.startsWith("/")) { |
273 | text = text.replaceFirst("/", ""); |
274 | } |
275 | |
276 | containerNameField.setText(text); |
277 | containerNameField.setToolTipText(text); |
278 | } |
279 | |
280 | /** |
281 | * Validate in LinkedResourceGroup selected file |
282 | */ |
283 | private boolean validitaLinkedResource() { |
284 | if ((linkedResourceGroup != null) |
285 | && (linkedResourceGroup.getLinkTargetURI() != null)) { |
286 | URI uri = linkedResourceGroup.getLinkTargetURI(); |
287 | |
288 | /** |
289 | * This variable represents a value. That is back supplied, if |
290 | * linkedResourceGroup.getLinkTargetURI() is empty. |
291 | */ |
292 | URI defaultURI = URIUtil.toURI(""); |
293 | |
294 | if (!uri.toString().equals(defaultURI.toString())) { |
295 | |
296 | try { |
297 | fileStore = EFS.getStore(uri); |
298 | } catch (CoreException e) { |
299 | return false; |
300 | } |
301 | return true; |
302 | } |
303 | } |
304 | return false; |
305 | } |
306 | |
307 | /** |
308 | * Validate in viewer selected resource |
309 | * |
310 | * @param resource - |
311 | * viewer selection |
312 | */ |
313 | private boolean validitaResource(IResource resource) { |
314 | |
315 | if (resource == null) { |
316 | return false; |
317 | } |
318 | |
319 | IFileInfo fileInfo = null; |
320 | |
321 | try { |
322 | fileStore = EFS.getStore(resource.getLocationURI()); |
323 | } catch (CoreException e1) { |
324 | return false; |
325 | } |
326 | |
327 | fileInfo = fileStore.fetchInfo(); |
328 | |
329 | if (type == IResource.FILE) { |
330 | return !fileInfo.isDirectory(); |
331 | } |
332 | |
333 | if (type == IResource.FOLDER) { |
334 | return fileInfo.isDirectory(); |
335 | } |
336 | |
337 | return false; |
338 | } |
339 | |
340 | /** |
341 | * Shows/hides the advanced option widgets. |
342 | */ |
343 | protected void handleAdvancedButtonSelect() { |
344 | Shell shell = getShell(); |
345 | Point shellSize = shell.getSize(); |
346 | Composite composite = (Composite) getControl(); |
347 | |
348 | if (linkedResourceComposite != null) { |
349 | linkedResourceComposite.dispose(); |
350 | linkedResourceComposite = null; |
351 | composite.layout(); |
352 | shell.setSize(shellSize.x, shellSize.y - linkedResourceGroupHeight); |
353 | advancedButton.setText(IDEWorkbenchMessages.showAdvanced); |
354 | } else { |
355 | linkedResourceComposite = linkedResourceGroup |
356 | .createContents(linkedResourceParent); |
357 | if (linkedResourceGroupHeight == -1) { |
358 | Point groupSize = linkedResourceComposite.computeSize( |
359 | SWT.DEFAULT, SWT.DEFAULT, true); |
360 | linkedResourceGroupHeight = groupSize.y; |
361 | } |
362 | shell.setSize(shellSize.x, shellSize.y + linkedResourceGroupHeight); |
363 | composite.layout(); |
364 | advancedButton.setText(IDEWorkbenchMessages.hideAdvanced); |
365 | } |
366 | } |
367 | |
368 | /** |
369 | * Returns the currently selected file name. Null if the field is empty. |
370 | * |
371 | * @return IPath |
372 | */ |
373 | public IPath getFileFullPath() { |
374 | if (fileStore == null) { |
375 | return null; |
376 | } |
377 | |
378 | IPath path = new Path(fileStore.toString()); |
379 | // The user may not have made this absolute so do it for them |
380 | path.makeAbsolute(); |
381 | |
382 | // TODO: StB: Commented out as this is not working under linux and I have doubts that it works correctly under windows!! |
383 | if (false && path.getDevice() == null) { |
384 | IPath rootPath = ResourcesPlugin.getWorkspace().getRoot() |
385 | .getLocation(); |
386 | return rootPath.append(path); |
387 | } |
388 | |
389 | return path; |
390 | } |
391 | |
392 | /** |
393 | * The WizardDatasourceFileLoadPage implementation of this Listener method |
394 | * handles all events and enablements for controls on this page. Subclasses |
395 | * may extend. |
396 | */ |
397 | public void handleEvent(Event event) { |
398 | if (linkedResourceComposite != null |
399 | && !linkedResourceComposite.isDisposed()) { |
400 | setPageComplete(validitaLinkedResource()); |
401 | }else { |
402 | setPageComplete(validitaResource(selectedResource)); |
403 | } |
404 | } |
405 | |
406 | private String getResourceType(){ |
407 | if (type == IResource.FILE ) |
408 | return "file"; |
409 | if (type == IResource.FOLDER ) |
410 | return "folder"; |
411 | return "<undefined>"; |
412 | } |
413 | |
414 | /* (non-Javadoc) |
415 | * @see org.eclipse.jface.wizard.WizardPage#setPageComplete(boolean) |
416 | */ |
417 | @Override |
418 | public void setPageComplete(boolean complete) { |
419 | super.setPageComplete(complete); |
420 | setErrorMessage("No resource selected!"); |
421 | |
422 | if (complete) { |
423 | setErrorMessage(null); |
424 | containerSelectionChanged(fileStore.toString()); |
425 | } |
426 | } |
427 | } |