38
38
public class ResourceSelectorWidget {
40
public enum ResourceType { FILE, DIRECTORY }
42
private static String BROWSE_LABEL = ResourceSelectorWidgetMessages.browseLabelText;
44
private ResourceType resourceType;
45
private Group mainComp;
46
private String sectionLabelText;
47
protected Label sectionLabel;
48
private Label uriLabel;
49
private Text uriField;
50
private Button browseButton;
51
private FileSystemSelectionArea fileSystemSelectionArea;
54
* Return the path on the URI field.
56
* @return the path or the field's text if the path is invalid
58
public String getPathFromURIField() {
61
fieldURI = new URI(uriField.getText());
62
} catch (URISyntaxException e) {
63
return uriField.getText();
65
String path = fieldURI.getPath();
66
return path != null ? path : uriField.getText();
70
* Open an appropriate directory browser
72
private void handleURIBrowseButtonPressed() {
74
String selectedResource = null;
75
String path = getURIText().getText();
76
FileSystemElement fileSystem = fileSystemSelectionArea.getSelectedFileSystem();
78
IRemoteResourceSelectorProxy resourceSelector = fileSystem.getSelectorProxy();
79
if (resourceSelector != null) {
80
switch (resourceType) {
82
URI uri = resourceSelector.selectFile(fileSystem.getScheme(), path, ResourceSelectorWidgetMessages.ResourceSelectorWidget_select + sectionLabelText, browseButton.getShell());
84
selectedResource = uri.toString();
89
URI uri = resourceSelector.selectDirectory(fileSystem.getScheme(), path, ResourceSelectorWidgetMessages.ResourceSelectorWidget_select + sectionLabelText, browseButton.getShell());
91
selectedResource = uri.toString();
96
ProfileLaunchPlugin.log(IStatus.ERROR, ResourceSelectorWidgetMessages.ResourceSelectorWidget_unrecognized_resourceType);
100
ProfileLaunchPlugin.log(IStatus.ERROR, ResourceSelectorWidgetMessages.ResourceSelectorWidget_getSelectorProxy_returned_null);
103
if (selectedResource != null) {
104
updateURIField(selectedResource);
111
* Update the filesystem selector, if possible
115
private void updateFilesystemSelector(String newPath) {
117
URI selectedURI = new URI(newPath);
118
String scheme = selectedURI.getScheme();
120
if (scheme == null) {
121
fileSystemSelectionArea.setSelectedFileSystem("local"); //$NON-NLS-1$
123
fileSystemSelectionArea.setSelectedFileSystem(scheme);
125
} catch (CoreException e) {
126
// Probably an unrecognized scheme. Don't change the setting of
127
// the filesystem selector.
129
} catch (URISyntaxException e) {
130
// This error can be ignored because we just won't set the filesystem selector
136
* Update the URI field based on the selected path.
138
* @param selectedPath
140
private void updateURIField(String selectedPath) {
141
uriField.setText(TextProcessor.process(selectedPath));
142
updateFilesystemSelector(selectedPath);
146
* Create the file system selection area.
150
private void createFileSystemSelection(Composite composite) {
151
fileSystemSelectionArea = new FileSystemSelectionArea();
152
fileSystemSelectionArea.createContents(composite);
156
* Create the area for user entry.
159
* @param defaultEnabled
161
private void createUserEntryArea(Composite composite, String uriLabelText, boolean defaultEnabled) {
163
GridLayout layout = new GridLayout();
164
layout.numColumns = 2;
165
layout.marginHeight = 0;
166
layout.marginWidth = 0;
167
composite.setLayout(layout);
169
uriLabel = new Label(composite, SWT.NONE);
170
if (uriLabelText != null) {
171
uriLabel.setText(uriLabelText);
173
uriLabel.setText(ResourceSelectorWidgetMessages.uriLabelText);
176
// project location entry field
177
uriField = new Text(composite, SWT.BORDER);
178
GridData data = new GridData(GridData.FILL_HORIZONTAL);
180
data.horizontalSpan = 1;
181
uriField.setLayoutData(data);
183
// create a blank space to align the filesystem selector with the path box.
184
new Label(composite, SWT.NONE);
186
Composite browserComp = new Composite(composite, SWT.NONE);
187
FillLayout browserLayout = new FillLayout(SWT.HORIZONTAL);
188
browserComp.setLayout(browserLayout);
190
createFileSystemSelection(browserComp);
193
browseButton = new Button(browserComp, SWT.PUSH);
194
browseButton.setText(BROWSE_LABEL);
195
browseButton.addSelectionListener(new SelectionAdapter() {
197
public void widgetSelected(SelectionEvent event) {
198
handleURIBrowseButtonPressed();
202
uriField.addModifyListener(new ModifyListener() {
206
* @see org.eclipse.swt.events.ModifyListener#modifyText(org.eclipse.swt.events.ModifyEvent)
209
public void modifyText(ModifyEvent e) {
210
updateFilesystemSelector(uriField.getText());
215
public ResourceSelectorWidget(Composite parent, ResourceType resourceType, int colSpan, String sectionLabelText, String uriLabelText) {
216
this.resourceType = resourceType;
217
this.sectionLabelText = sectionLabelText;
218
mainComp = new Group(parent, SWT.NONE);
219
GridLayout mainLayout = new GridLayout();
220
mainLayout.numColumns = 5;
221
mainLayout.marginHeight = 0;
222
mainLayout.marginWidth = 0;
223
mainComp.setLayout(mainLayout);
224
GridData gd = new GridData(GridData.FILL_HORIZONTAL);
225
gd.horizontalSpan = colSpan;
226
mainComp.setLayoutData(gd);
227
mainComp.setText(sectionLabelText);
228
createUserEntryArea(mainComp, uriLabelText, true);
231
public void setEnabled(boolean enabled) {
232
if (mainComp != null) {
233
mainComp.setEnabled(enabled);
235
if (sectionLabel != null) {
236
sectionLabel.setEnabled(enabled);
238
if (uriLabel != null) {
239
uriLabel.setEnabled(enabled);
241
if (browseButton != null) {
242
browseButton.setEnabled(enabled);
244
if (uriField != null) {
245
uriField.setEnabled(enabled);
247
if (fileSystemSelectionArea != null) {
248
fileSystemSelectionArea.setEnabled(enabled);
252
public Text getURIText() {
40
public enum ResourceType { FILE, DIRECTORY }
42
private static String BROWSE_LABEL = ResourceSelectorWidgetMessages.browseLabelText;
44
private ResourceType resourceType;
45
private Group mainComp;
46
private String sectionLabelText;
47
private Label uriLabel;
48
private Text uriField;
49
private Button browseButton;
50
private FileSystemSelectionArea fileSystemSelectionArea;
53
* Open an appropriate directory browser
55
private void handleURIBrowseButtonPressed() {
57
String selectedResource = null;
58
String path = getURIText().getText();
59
FileSystemElement fileSystem = fileSystemSelectionArea.getSelectedFileSystem();
61
IRemoteResourceSelectorProxy resourceSelector = fileSystem.getSelectorProxy();
62
if (resourceSelector != null) {
63
switch (resourceType) {
65
URI uri = resourceSelector.selectFile(fileSystem.getScheme(), path, ResourceSelectorWidgetMessages.ResourceSelectorWidget_select + sectionLabelText, browseButton.getShell());
67
selectedResource = uri.toString();
72
URI uri = resourceSelector.selectDirectory(fileSystem.getScheme(), path, ResourceSelectorWidgetMessages.ResourceSelectorWidget_select + sectionLabelText, browseButton.getShell());
74
selectedResource = uri.toString();
79
ProfileLaunchPlugin.log(IStatus.ERROR, ResourceSelectorWidgetMessages.ResourceSelectorWidget_unrecognized_resourceType);
83
ProfileLaunchPlugin.log(IStatus.ERROR, ResourceSelectorWidgetMessages.ResourceSelectorWidget_getSelectorProxy_returned_null);
86
if (selectedResource != null) {
87
updateURIField(selectedResource);
94
* Update the filesystem selector, if possible
98
private void updateFilesystemSelector(String newPath) {
100
URI selectedURI = new URI(newPath);
101
String scheme = selectedURI.getScheme();
103
if (scheme == null) {
104
fileSystemSelectionArea.setSelectedFileSystem("local"); //$NON-NLS-1$
106
fileSystemSelectionArea.setSelectedFileSystem(scheme);
108
} catch (CoreException e) {
109
// Probably an unrecognized scheme. Don't change the setting of
110
// the filesystem selector.
112
} catch (URISyntaxException e) {
113
// This error can be ignored because we just won't set the filesystem selector
119
* Update the URI field based on the selected path.
121
* @param selectedPath
123
private void updateURIField(String selectedPath) {
124
uriField.setText(TextProcessor.process(selectedPath));
125
updateFilesystemSelector(selectedPath);
129
* Create the file system selection area.
133
private void createFileSystemSelection(Composite composite) {
134
fileSystemSelectionArea = new FileSystemSelectionArea();
135
fileSystemSelectionArea.createContents(composite);
139
* Create the area for user entry.
142
* @param uriLabelText
144
private void createUserEntryArea(Composite composite, String uriLabelText) {
146
GridLayout layout = new GridLayout();
147
layout.numColumns = 2;
148
layout.marginHeight = 0;
149
layout.marginWidth = 0;
150
composite.setLayout(layout);
152
uriLabel = new Label(composite, SWT.NONE);
153
if (uriLabelText != null) {
154
uriLabel.setText(uriLabelText);
156
uriLabel.setText(ResourceSelectorWidgetMessages.uriLabelText);
159
// project location entry field
160
uriField = new Text(composite, SWT.BORDER);
161
GridData data = new GridData(GridData.FILL_HORIZONTAL);
163
data.horizontalSpan = 1;
164
uriField.setLayoutData(data);
166
// create a blank space to align the filesystem selector with the path box.
167
new Label(composite, SWT.NONE);
169
Composite browserComp = new Composite(composite, SWT.NONE);
170
FillLayout browserLayout = new FillLayout(SWT.HORIZONTAL);
171
browserComp.setLayout(browserLayout);
173
createFileSystemSelection(browserComp);
176
browseButton = new Button(browserComp, SWT.PUSH);
177
browseButton.setText(BROWSE_LABEL);
178
browseButton.addSelectionListener(new SelectionAdapter() {
180
public void widgetSelected(SelectionEvent event) {
181
handleURIBrowseButtonPressed();
185
uriField.addModifyListener(new ModifyListener() {
187
public void modifyText(ModifyEvent e) {
188
updateFilesystemSelector(uriField.getText());
193
public ResourceSelectorWidget(Composite parent, ResourceType resourceType, int colSpan, String sectionLabelText, String uriLabelText) {
194
this.resourceType = resourceType;
195
this.sectionLabelText = sectionLabelText;
196
mainComp = new Group(parent, SWT.NONE);
197
GridLayout mainLayout = new GridLayout();
198
mainLayout.numColumns = 5;
199
mainLayout.marginHeight = 0;
200
mainLayout.marginWidth = 0;
201
mainComp.setLayout(mainLayout);
202
GridData gd = new GridData(GridData.FILL_HORIZONTAL);
203
gd.horizontalSpan = colSpan;
204
mainComp.setLayoutData(gd);
205
mainComp.setText(sectionLabelText);
206
createUserEntryArea(mainComp, uriLabelText);
209
public void setEnabled(boolean enabled) {
210
if (mainComp != null) {
211
mainComp.setEnabled(enabled);
213
if (uriLabel != null) {
214
uriLabel.setEnabled(enabled);
216
if (browseButton != null) {
217
browseButton.setEnabled(enabled);
219
if (uriField != null) {
220
uriField.setEnabled(enabled);
222
if (fileSystemSelectionArea != null) {
223
fileSystemSelectionArea.setEnabled(enabled);
227
public Text getURIText() {