~ubuntu-branches/ubuntu/wily/eclipse-linuxtools/wily

« back to all changes in this revision

Viewing changes to profiling/org.eclipse.linuxtools.profiling.launch/src/org/eclipse/linuxtools/profiling/launch/ui/ResourceSelectorWidget.java

  • Committer: Package Import Robot
  • Author(s): Jakub Adam, Jakub Adam, tony mancill
  • Date: 2014-10-11 11:44:05 UTC
  • mfrom: (1.2.4)
  • Revision ID: package-import@ubuntu.com-20141011114405-yazjvxfzzhmi5sgj
Tags: 3.1.0-1
[ Jakub Adam ]
* New upstream release (Closes: #761524).
* Refreshed d/patches.
* Don't build removed feature org.eclipse.linuxtools.tools.launch
  - merged into org.eclipse.linuxtools.profiling.
* Use javac target 1.7.
* Build new feature org.eclipse.linuxtools.dataviewers.feature
  - required by Valgrind integration.
* Build-depend on eclipse-remote-services-api and eclipse-cdt-autotools.
* Bump Standards-Version to 3.9.6.
* Override incompatible-java-bytecode-format - linuxtools needs Java 7.
* Remove unused codeless-jar override.

[ tony mancill ]
* Tweak short package description to make lintian happy.

Show diffs side-by-side

added added

removed removed

Lines of Context:
37
37
 */
38
38
public class ResourceSelectorWidget {
39
39
 
40
 
        public enum ResourceType { FILE, DIRECTORY }
41
 
 
42
 
        private static String BROWSE_LABEL = ResourceSelectorWidgetMessages.browseLabelText;
43
 
 
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;
52
 
 
53
 
        /**
54
 
         * Return the path on the URI field.
55
 
         *
56
 
         * @return the path or the field's text if the path is invalid
57
 
         */
58
 
        public String getPathFromURIField() {
59
 
                URI fieldURI;
60
 
                try {
61
 
                        fieldURI = new URI(uriField.getText());
62
 
                } catch (URISyntaxException e) {
63
 
                        return uriField.getText();
64
 
                }
65
 
                String path = fieldURI.getPath();
66
 
                return path != null ? path : uriField.getText();
67
 
        }
68
 
 
69
 
        /**
70
 
         * Open an appropriate directory browser
71
 
         */
72
 
        private void handleURIBrowseButtonPressed() {
73
 
 
74
 
                String selectedResource = null;
75
 
                String path = getURIText().getText();
76
 
                FileSystemElement fileSystem = fileSystemSelectionArea.getSelectedFileSystem();
77
 
 
78
 
                IRemoteResourceSelectorProxy resourceSelector = fileSystem.getSelectorProxy();
79
 
                if (resourceSelector != null) {
80
 
                        switch (resourceType) {
81
 
                        case FILE: {
82
 
                                URI uri = resourceSelector.selectFile(fileSystem.getScheme(), path, ResourceSelectorWidgetMessages.ResourceSelectorWidget_select + sectionLabelText, browseButton.getShell());
83
 
                                if (uri != null) {
84
 
                                        selectedResource = uri.toString();
85
 
                                }
86
 
                                break;
87
 
                        }
88
 
                        case DIRECTORY: {
89
 
                                URI uri = resourceSelector.selectDirectory(fileSystem.getScheme(), path, ResourceSelectorWidgetMessages.ResourceSelectorWidget_select + sectionLabelText, browseButton.getShell());
90
 
                                if (uri != null) {
91
 
                                        selectedResource = uri.toString();
92
 
                                }
93
 
                                break;
94
 
                        }
95
 
                        default:
96
 
                                ProfileLaunchPlugin.log(IStatus.ERROR, ResourceSelectorWidgetMessages.ResourceSelectorWidget_unrecognized_resourceType);
97
 
                                return;
98
 
                        }
99
 
                } else {
100
 
                        ProfileLaunchPlugin.log(IStatus.ERROR, ResourceSelectorWidgetMessages.ResourceSelectorWidget_getSelectorProxy_returned_null);
101
 
                }
102
 
 
103
 
                if (selectedResource != null) {
104
 
                        updateURIField(selectedResource);
105
 
                }
106
 
        }
107
 
 
108
 
 
109
 
 
110
 
        /**
111
 
         * Update the filesystem selector, if possible
112
 
         *
113
 
         * @param newPath
114
 
         */
115
 
        private void updateFilesystemSelector(String newPath) {
116
 
        try {
117
 
                        URI selectedURI  = new URI(newPath);
118
 
                        String scheme = selectedURI.getScheme();
119
 
                        try {
120
 
                                if (scheme == null) {
121
 
                                        fileSystemSelectionArea.setSelectedFileSystem("local"); //$NON-NLS-1$
122
 
                                } else {
123
 
                                        fileSystemSelectionArea.setSelectedFileSystem(scheme);
124
 
                                }
125
 
                        } catch (CoreException e) {
126
 
                                // Probably an unrecognized scheme.  Don't change the setting of
127
 
                                // the filesystem selector.
128
 
                        }
129
 
                } catch (URISyntaxException e) {
130
 
                        // This error can be ignored because we just won't set the filesystem selector
131
 
                        // to a anything
132
 
                }
133
 
        }
134
 
 
135
 
        /**
136
 
         * Update the URI field based on the selected path.
137
 
         *
138
 
         * @param selectedPath
139
 
         */
140
 
        private void updateURIField(String selectedPath) {
141
 
                uriField.setText(TextProcessor.process(selectedPath));
142
 
                updateFilesystemSelector(selectedPath);
143
 
        }
144
 
 
145
 
        /**
146
 
         * Create the file system selection area.
147
 
         *
148
 
         * @param composite
149
 
         */
150
 
        private void createFileSystemSelection(Composite composite) {
151
 
                fileSystemSelectionArea = new FileSystemSelectionArea();
152
 
                fileSystemSelectionArea.createContents(composite);
153
 
        }
154
 
 
155
 
        /**
156
 
         * Create the area for user entry.
157
 
         *
158
 
         * @param composite
159
 
         * @param defaultEnabled
160
 
         */
161
 
        private void createUserEntryArea(Composite composite, String uriLabelText, boolean defaultEnabled) {
162
 
                // location label
163
 
                GridLayout layout = new GridLayout();
164
 
                layout.numColumns = 2;
165
 
                layout.marginHeight = 0;
166
 
                layout.marginWidth = 0;
167
 
                composite.setLayout(layout);
168
 
 
169
 
                uriLabel = new Label(composite, SWT.NONE);
170
 
                if (uriLabelText != null) {
171
 
                        uriLabel.setText(uriLabelText);
172
 
                } else {
173
 
                        uriLabel.setText(ResourceSelectorWidgetMessages.uriLabelText);
174
 
                }
175
 
 
176
 
                // project location entry field
177
 
                uriField = new Text(composite, SWT.BORDER);
178
 
                GridData data = new GridData(GridData.FILL_HORIZONTAL);
179
 
 
180
 
                data.horizontalSpan = 1;
181
 
                uriField.setLayoutData(data);
182
 
 
183
 
                // create a blank space to align the filesystem selector with the path box.
184
 
                new Label(composite, SWT.NONE);
185
 
 
186
 
                Composite browserComp = new Composite(composite, SWT.NONE);
187
 
                FillLayout browserLayout = new FillLayout(SWT.HORIZONTAL);
188
 
                browserComp.setLayout(browserLayout);
189
 
 
190
 
                createFileSystemSelection(browserComp);
191
 
 
192
 
                // browse button
193
 
                browseButton = new Button(browserComp, SWT.PUSH);
194
 
                browseButton.setText(BROWSE_LABEL);
195
 
                browseButton.addSelectionListener(new SelectionAdapter() {
196
 
                        @Override
197
 
                        public void widgetSelected(SelectionEvent event) {
198
 
                                handleURIBrowseButtonPressed();
199
 
                        }
200
 
                });
201
 
 
202
 
                uriField.addModifyListener(new ModifyListener() {
203
 
                        /*
204
 
                         * (non-Javadoc)
205
 
                         *
206
 
                         * @see org.eclipse.swt.events.ModifyListener#modifyText(org.eclipse.swt.events.ModifyEvent)
207
 
                         */
208
 
                        @Override
209
 
                        public void modifyText(ModifyEvent e) {
210
 
                                updateFilesystemSelector(uriField.getText());
211
 
                        }
212
 
                });
213
 
        }
214
 
 
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);
229
 
        }
230
 
 
231
 
        public void setEnabled(boolean enabled) {
232
 
                if (mainComp != null) {
233
 
                        mainComp.setEnabled(enabled);
234
 
                }
235
 
                if (sectionLabel != null) {
236
 
                        sectionLabel.setEnabled(enabled);
237
 
                }
238
 
                if (uriLabel != null) {
239
 
                        uriLabel.setEnabled(enabled);
240
 
                }
241
 
                if (browseButton != null) {
242
 
                        browseButton.setEnabled(enabled);
243
 
                }
244
 
                if (uriField != null) {
245
 
                        uriField.setEnabled(enabled);
246
 
                }
247
 
                if (fileSystemSelectionArea != null) {
248
 
                        fileSystemSelectionArea.setEnabled(enabled);
249
 
                }
250
 
        }
251
 
 
252
 
        public Text getURIText() {
253
 
                return uriField;
254
 
        }
 
40
    public enum ResourceType { FILE, DIRECTORY }
 
41
 
 
42
    private static String BROWSE_LABEL = ResourceSelectorWidgetMessages.browseLabelText;
 
43
 
 
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;
 
51
 
 
52
    /**
 
53
     * Open an appropriate directory browser
 
54
     */
 
55
    private void handleURIBrowseButtonPressed() {
 
56
 
 
57
        String selectedResource = null;
 
58
        String path = getURIText().getText();
 
59
        FileSystemElement fileSystem = fileSystemSelectionArea.getSelectedFileSystem();
 
60
 
 
61
        IRemoteResourceSelectorProxy resourceSelector = fileSystem.getSelectorProxy();
 
62
        if (resourceSelector != null) {
 
63
            switch (resourceType) {
 
64
            case FILE: {
 
65
                URI uri = resourceSelector.selectFile(fileSystem.getScheme(), path, ResourceSelectorWidgetMessages.ResourceSelectorWidget_select + sectionLabelText, browseButton.getShell());
 
66
                if (uri != null) {
 
67
                    selectedResource = uri.toString();
 
68
                }
 
69
                break;
 
70
            }
 
71
            case DIRECTORY: {
 
72
                URI uri = resourceSelector.selectDirectory(fileSystem.getScheme(), path, ResourceSelectorWidgetMessages.ResourceSelectorWidget_select + sectionLabelText, browseButton.getShell());
 
73
                if (uri != null) {
 
74
                    selectedResource = uri.toString();
 
75
                }
 
76
                break;
 
77
            }
 
78
            default:
 
79
                ProfileLaunchPlugin.log(IStatus.ERROR, ResourceSelectorWidgetMessages.ResourceSelectorWidget_unrecognized_resourceType);
 
80
                return;
 
81
            }
 
82
        } else {
 
83
            ProfileLaunchPlugin.log(IStatus.ERROR, ResourceSelectorWidgetMessages.ResourceSelectorWidget_getSelectorProxy_returned_null);
 
84
        }
 
85
 
 
86
        if (selectedResource != null) {
 
87
            updateURIField(selectedResource);
 
88
        }
 
89
    }
 
90
 
 
91
 
 
92
 
 
93
    /**
 
94
     * Update the filesystem selector, if possible
 
95
     *
 
96
     * @param newPath
 
97
     */
 
98
    private void updateFilesystemSelector(String newPath) {
 
99
    try {
 
100
            URI selectedURI  = new URI(newPath);
 
101
            String scheme = selectedURI.getScheme();
 
102
            try {
 
103
                if (scheme == null) {
 
104
                    fileSystemSelectionArea.setSelectedFileSystem("local"); //$NON-NLS-1$
 
105
                } else {
 
106
                    fileSystemSelectionArea.setSelectedFileSystem(scheme);
 
107
                }
 
108
            } catch (CoreException e) {
 
109
                // Probably an unrecognized scheme.  Don't change the setting of
 
110
                // the filesystem selector.
 
111
            }
 
112
        } catch (URISyntaxException e) {
 
113
            // This error can be ignored because we just won't set the filesystem selector
 
114
            // to a anything
 
115
        }
 
116
    }
 
117
 
 
118
    /**
 
119
     * Update the URI field based on the selected path.
 
120
     *
 
121
     * @param selectedPath
 
122
     */
 
123
    private void updateURIField(String selectedPath) {
 
124
        uriField.setText(TextProcessor.process(selectedPath));
 
125
        updateFilesystemSelector(selectedPath);
 
126
    }
 
127
 
 
128
    /**
 
129
     * Create the file system selection area.
 
130
     *
 
131
     * @param composite
 
132
     */
 
133
    private void createFileSystemSelection(Composite composite) {
 
134
        fileSystemSelectionArea = new FileSystemSelectionArea();
 
135
        fileSystemSelectionArea.createContents(composite);
 
136
    }
 
137
 
 
138
    /**
 
139
     * Create the area for user entry.
 
140
     *
 
141
     * @param composite
 
142
     * @param uriLabelText
 
143
     */
 
144
    private void createUserEntryArea(Composite composite, String uriLabelText) {
 
145
        // location label
 
146
        GridLayout layout = new GridLayout();
 
147
        layout.numColumns = 2;
 
148
        layout.marginHeight = 0;
 
149
        layout.marginWidth = 0;
 
150
        composite.setLayout(layout);
 
151
 
 
152
        uriLabel = new Label(composite, SWT.NONE);
 
153
        if (uriLabelText != null) {
 
154
            uriLabel.setText(uriLabelText);
 
155
        } else {
 
156
            uriLabel.setText(ResourceSelectorWidgetMessages.uriLabelText);
 
157
        }
 
158
 
 
159
        // project location entry field
 
160
        uriField = new Text(composite, SWT.BORDER);
 
161
        GridData data = new GridData(GridData.FILL_HORIZONTAL);
 
162
 
 
163
        data.horizontalSpan = 1;
 
164
        uriField.setLayoutData(data);
 
165
 
 
166
        // create a blank space to align the filesystem selector with the path box.
 
167
        new Label(composite, SWT.NONE);
 
168
 
 
169
        Composite browserComp = new Composite(composite, SWT.NONE);
 
170
        FillLayout browserLayout = new FillLayout(SWT.HORIZONTAL);
 
171
        browserComp.setLayout(browserLayout);
 
172
 
 
173
        createFileSystemSelection(browserComp);
 
174
 
 
175
        // browse button
 
176
        browseButton = new Button(browserComp, SWT.PUSH);
 
177
        browseButton.setText(BROWSE_LABEL);
 
178
        browseButton.addSelectionListener(new SelectionAdapter() {
 
179
            @Override
 
180
            public void widgetSelected(SelectionEvent event) {
 
181
                handleURIBrowseButtonPressed();
 
182
            }
 
183
        });
 
184
 
 
185
        uriField.addModifyListener(new ModifyListener() {
 
186
            @Override
 
187
            public void modifyText(ModifyEvent e) {
 
188
                updateFilesystemSelector(uriField.getText());
 
189
            }
 
190
        });
 
191
    }
 
192
 
 
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);
 
207
    }
 
208
 
 
209
    public void setEnabled(boolean enabled) {
 
210
        if (mainComp != null) {
 
211
            mainComp.setEnabled(enabled);
 
212
        }
 
213
        if (uriLabel != null) {
 
214
            uriLabel.setEnabled(enabled);
 
215
        }
 
216
        if (browseButton != null) {
 
217
            browseButton.setEnabled(enabled);
 
218
        }
 
219
        if (uriField != null) {
 
220
            uriField.setEnabled(enabled);
 
221
        }
 
222
        if (fileSystemSelectionArea != null) {
 
223
            fileSystemSelectionArea.setEnabled(enabled);
 
224
        }
 
225
    }
 
226
 
 
227
    public Text getURIText() {
 
228
        return uriField;
 
229
    }
255
230
}