1 package com.stateofflow.eclipse.metrics.export.wizard;
2
3 import java.io.File;
4
5 import org.eclipse.core.runtime.CoreException;
6 import org.eclipse.jface.preference.StringFieldEditor;
7 import org.eclipse.jface.util.IPropertyChangeListener;
8 import org.eclipse.swt.SWT;
9 import org.eclipse.swt.events.SelectionAdapter;
10 import org.eclipse.swt.events.SelectionEvent;
11 import org.eclipse.swt.widgets.DirectoryDialog;
12 import org.eclipse.swt.widgets.Shell;
13
14 import com.stateofflow.eclipse.metrics.swt.SWTBuilder;
15
16 class ExportDirectoryAgent implements Agent {
17 private final StringFieldEditor exportDirectoryText;
18
19 public ExportDirectoryAgent(final SWTBuilder builder, final IPropertyChangeListener changeListener, final Shell shell) {
20 exportDirectoryText = builder.createStringFieldEditor("Export Directory:", changeListener);
21 builder.addPushButton("Browse...", new SelectionAdapter() {
22 public void widgetSelected(final SelectionEvent e) {
23 handleBrowseExportLocation(shell);
24 }
25 });
26
27 }
28
29 private String getPath() {
30 return exportDirectoryText.getStringValue();
31 }
32
33 private void handleBrowseExportLocation(final Shell shell) {
34 final DirectoryDialog dialog = new DirectoryDialog(shell, SWT.SINGLE | SWT.OPEN);
35 final String path = dialog.open();
36 if (path != null) {
37 exportDirectoryText.setStringValue(path);
38 }
39 }
40
41 public void initialise(final ProjectProperties properties) throws CoreException {
42 final String exportDir = properties.getString(MetricsExportWizard.DIRECTORY_QUALIFIED_NAME);
43 if (exportDir != null) {
44 exportDirectoryText.setStringValue(exportDir);
45 } else {
46 exportDirectoryText.setStringValue("");
47 }
48 }
49
50 public void updateStatus(final StatusUpdateable updateable) {
51 if (!isDirectorySpecified()) {
52 updateable.updateStatus(false, "Export Directory must be specified");
53 return;
54 }
55 updateable.updateStatus(doesPathExist(), "Export Directory must exist");
56 updateable.updateStatus(isPathADirectory(), "Specified Export Directory is not a directory");
57 updateable.updateStatus(isPathWritable(), "Export Directory is not writable");
58 }
59
60 private boolean isDirectorySpecified() {
61 return getPath().length() != 0;
62 }
63
64 private boolean doesPathExist() {
65 return getDirectory().exists();
66 }
67
68 private boolean isPathADirectory() {
69 return getDirectory().isDirectory();
70 }
71
72 private boolean isPathWritable() {
73 return getDirectory().canWrite();
74 }
75
76 public File getDirectory() {
77 return new File(getPath());
78 }
79 }
80