~ubuntu-branches/debian/sid/eclipse-cdt/sid

« back to all changes in this revision

Viewing changes to core/org.eclipse.cdt.ui/src/org/eclipse/cdt/ui/wizards/CDTMainWizardPage.java

  • Committer: Package Import Robot
  • Author(s): Jakub Adam
  • Date: 2013-10-03 20:30:16 UTC
  • mfrom: (1.1.11)
  • Revision ID: package-import@ubuntu.com-20131003203016-d4ug6l0xgosasumq
Tags: 8.2.1-1
* New upstream release.
* Updated autotools documentation sources.

Show diffs side-by-side

added added

removed removed

Lines of Context:
10
10
 *     IBM Corporation
11
11
 *******************************************************************************/
12
12
package org.eclipse.cdt.ui.wizards;
13
 
        import java.net.URI;
 
13
 
 
14
import java.net.URI;
14
15
import java.util.ArrayList;
15
16
import java.util.Iterator;
16
17
import java.util.List;
53
54
 
54
55
import org.eclipse.cdt.internal.ui.newui.Messages;
55
56
 
56
 
        public class CDTMainWizardPage extends WizardNewProjectCreationPage implements IWizardItemsListListener {
57
 
                private static final Image IMG_CATEGORY = CDTSharedImages.getImage(CDTSharedImages.IMG_OBJS_SEARCHFOLDER);
58
 
                private static final Image IMG_ITEM = CDTSharedImages.getImage(CDTSharedImages.IMG_OBJS_VARIABLE);
59
 
 
60
 
                public static final String PAGE_ID = "org.eclipse.cdt.managedbuilder.ui.wizard.NewModelProjectWizardPage"; //$NON-NLS-1$
61
 
 
62
 
                private static final String EXTENSION_POINT_ID = "org.eclipse.cdt.ui.CDTWizard"; //$NON-NLS-1$
63
 
                private static final String ELEMENT_NAME = "wizard"; //$NON-NLS-1$
64
 
                private static final String CLASS_NAME = "class"; //$NON-NLS-1$
65
 
                public static final String DESC = "EntryDescriptor"; //$NON-NLS-1$
66
 
 
67
 
            // widgets
68
 
            private Tree tree;
69
 
            private Composite right;
70
 
            private Button show_sup;
71
 
            private Label right_label;
72
 
 
73
 
            public CWizardHandler h_selected = null;
74
 
                private Label categorySelectedLabel;
75
 
 
76
 
            /**
77
 
             * Creates a new project creation wizard page.
78
 
             *
79
 
             * @param pageName the name of this page
80
 
             */
81
 
            public CDTMainWizardPage(String pageName) {
82
 
                super(pageName);
83
 
                setPageComplete(false);
84
 
            }
85
 
 
86
 
            /** (non-Javadoc)
87
 
             * Method declared on IDialogPage.
88
 
             */
89
 
            @Override
90
 
                public void createControl(Composite parent) {
91
 
                super.createControl(parent);
92
 
 
93
 
                createDynamicGroup((Composite)getControl());
94
 
                        switchTo(updateData(tree, right, show_sup, CDTMainWizardPage.this, getWizard()),
95
 
                                        getDescriptor(tree));
96
 
 
97
 
                        setPageComplete(validatePage());
98
 
                setErrorMessage(null);
99
 
                setMessage(null);
100
 
            }
101
 
 
102
 
            private void createDynamicGroup(Composite parent) {
103
 
                Composite c = new Composite(parent, SWT.NONE);
104
 
                c.setLayoutData(new GridData(GridData.FILL_BOTH));
105
 
                c.setLayout(new GridLayout(2, true));
106
 
 
107
 
                Label l1 = new Label(c, SWT.NONE);
108
 
                l1.setText(Messages.CMainWizardPage_0);
109
 
                l1.setFont(parent.getFont());
110
 
                l1.setLayoutData(new GridData(GridData.BEGINNING));
111
 
 
112
 
                right_label = new Label(c, SWT.NONE);
113
 
                right_label.setFont(parent.getFont());
114
 
                right_label.setLayoutData(new GridData(GridData.BEGINNING));
115
 
 
116
 
                tree = new Tree(c, SWT.SINGLE | SWT.BORDER);
117
 
                tree.setLayoutData(new GridData(GridData.FILL_BOTH));
118
 
                tree.addSelectionListener(new SelectionAdapter() {
119
 
                                @Override
120
 
                                public void widgetSelected(SelectionEvent e) {
121
 
                                        TreeItem[] tis = tree.getSelection();
122
 
                                        if (tis == null || tis.length == 0) return;
123
 
                                        switchTo((CWizardHandler)tis[0].getData(), (EntryDescriptor)tis[0].getData(DESC));
124
 
                                        setPageComplete(validatePage());
125
 
                                }});
126
 
                tree.getAccessible().addAccessibleListener(
127
 
                                         new AccessibleAdapter() {
128
 
                                 @Override
129
 
                                                public void getName(AccessibleEvent e) {
130
 
                                         for (int i = 0; i < tree.getItemCount(); i++) {
131
 
                                                 if (tree.getItem(i).getText().equals(e.result))
132
 
                                                         return;
133
 
                                         }
134
 
                                 e.result = Messages.CMainWizardPage_0;
135
 
                                 }
136
 
                             }
137
 
                                 );
138
 
                right = new Composite(c, SWT.NONE);
139
 
                right.setLayoutData(new GridData(GridData.FILL_BOTH));
140
 
                right.setLayout(new PageLayout());
141
 
 
142
 
                show_sup = new Button(c, SWT.CHECK);
143
 
                show_sup.setText(Messages.CMainWizardPage_1);
144
 
                GridData gd = new GridData(GridData.FILL_HORIZONTAL);
145
 
                gd.horizontalSpan = 2;
146
 
                show_sup.setLayoutData(gd);
147
 
                show_sup.addSelectionListener(new SelectionAdapter() {
148
 
                                @Override
149
 
                                public void widgetSelected(SelectionEvent e) {
150
 
                                        if (h_selected != null)
151
 
                                                h_selected.setSupportedOnly(show_sup.getSelection());
152
 
                                        switchTo(updateData(tree, right, show_sup, CDTMainWizardPage.this, getWizard()),
153
 
                                                        getDescriptor(tree));
154
 
                                }} );
155
 
 
156
 
                // restore settings from preferences
157
 
                        show_sup.setSelection(!CDTPrefUtil.getBool(CDTPrefUtil.KEY_NOSUPP));
158
 
            }
159
 
 
160
 
            @Override
161
 
                public IWizardPage getNextPage() {
162
 
                        return (h_selected == null) ? null : h_selected.getSpecificPage();
163
 
            }
164
 
 
165
 
            public URI getProjectLocation() {
166
 
                return useDefaults() ? null : getLocationURI();
167
 
            }
168
 
 
169
 
            /**
170
 
             * Returns whether this page's controls currently all contain valid
171
 
             * values.
172
 
             *
173
 
             * @return <code>true</code> if all controls are valid, and
174
 
             *   <code>false</code> if at least one is invalid
175
 
             */
176
 
            @Override
177
 
                protected boolean validatePage() {
178
 
                setMessage(null);
179
 
                if (!super.validatePage())
180
 
                        return false;
181
 
 
182
 
                if (getProjectName().indexOf('#') >= 0) {
183
 
                    setErrorMessage(Messages.CDTMainWizardPage_0);
184
 
                    return false;
185
 
                }
186
 
 
187
 
                boolean bad = true; // should we treat existing project as error
188
 
 
189
 
                IProject handle = getProjectHandle();
190
 
                if (handle.exists()) {
191
 
                        if (getWizard() instanceof IWizardWithMemory) {
192
 
                                IWizardWithMemory w = (IWizardWithMemory)getWizard();
193
 
                                if (w.getLastProjectName() != null && w.getLastProjectName().equals(getProjectName()))
194
 
                                        bad = false;
195
 
                        }
196
 
                        if (bad) {
197
 
                                setErrorMessage(Messages.CMainWizardPage_10);
198
 
                            return false;
199
 
                        }
200
 
                }
201
 
 
202
 
                if (bad) { // skip this check if project already created
203
 
                        try {
204
 
                                IFileStore fs;
205
 
                                URI p = getProjectLocation();
206
 
                                if (p == null) {
207
 
                                        fs = EFS.getStore(ResourcesPlugin.getWorkspace().getRoot().getLocationURI());
208
 
                                        fs = fs.getChild(getProjectName());
209
 
                                } else
210
 
                                        fs = EFS.getStore(p);
211
 
                                IFileInfo f = fs.fetchInfo();
212
 
                                if (f.exists()) {
213
 
                                        if (f.isDirectory()) {
214
 
                                                if (f.getAttribute(EFS.ATTRIBUTE_READ_ONLY)) {
215
 
                                                        setErrorMessage(Messages.CMainWizardPage_DirReadOnlyError);
216
 
                                                        return false;
217
 
                                                }
218
 
                                                else
219
 
                                                        setMessage(Messages.CMainWizardPage_7, IMessageProvider.WARNING);
220
 
                                        } else {
221
 
                                                setErrorMessage(Messages.CMainWizardPage_6);
222
 
                                                return false;
223
 
                                        }
224
 
                                }
225
 
                        } catch (CoreException e) {
226
 
                                CUIPlugin.log(e.getStatus());
227
 
                        }
228
 
                }
229
 
 
230
 
                if (!useDefaults()) {
231
 
                    IStatus locationStatus = ResourcesPlugin.getWorkspace().validateProjectLocationURI(handle,
232
 
                                getLocationURI());
233
 
                    if (!locationStatus.isOK()) {
234
 
                        setErrorMessage(locationStatus.getMessage());
235
 
                        return false;
236
 
                    }
237
 
                }
238
 
 
239
 
                if (tree.getItemCount() == 0) {
240
 
                        setErrorMessage(Messages.CMainWizardPage_3);
241
 
                        return false;
242
 
                }
243
 
 
244
 
                // it is not an error, but we cannot continue
245
 
                if (h_selected == null) {
246
 
                    setErrorMessage(null);
247
 
                        return false;
248
 
                }
249
 
 
250
 
                String s = h_selected.getErrorMessage();
251
 
                        if (s != null) {
252
 
                        setErrorMessage(s);
253
 
                        return false;
254
 
                }
255
 
 
256
 
            setErrorMessage(null);
257
 
                return true;
258
 
            }
259
 
 
260
 
            /**
261
 
             *
262
 
             * @param tree
263
 
             * @param right
264
 
             * @param show_sup
265
 
             * @param ls
266
 
             * @param wizard
267
 
             * @return : selected Wizard Handler.
268
 
             */
269
 
                public static CWizardHandler updateData(Tree tree, Composite right, Button show_sup, IWizardItemsListListener ls, IWizard wizard) {
270
 
                        // remember selected item
271
 
                        TreeItem[] selection = tree.getSelection();
272
 
                        TreeItem selectedItem = selection.length>0 ? selection[0] : null;
273
 
                        String savedLabel = selectedItem!=null ? selectedItem.getText() : null;
274
 
                        String savedParentLabel = getParentText(selectedItem);
275
 
 
276
 
                        tree.removeAll();
277
 
                        IExtensionPoint extensionPoint =
278
 
                                    Platform.getExtensionRegistry().getExtensionPoint(EXTENSION_POINT_ID);
279
 
                        if (extensionPoint == null) return null;
280
 
                        IExtension[] extensions = extensionPoint.getExtensions();
281
 
                        if (extensions == null) return null;
282
 
 
283
 
                        List<EntryDescriptor> items = new ArrayList<EntryDescriptor>();
284
 
                        for (int i = 0; i < extensions.length; ++i)     {
285
 
                                IConfigurationElement[] elements = extensions[i].getConfigurationElements();
286
 
                                for (IConfigurationElement element : elements) {
287
 
                                        if (element.getName().equals(ELEMENT_NAME)) {
288
 
                                                CNewWizard w = null;
289
 
                                                try {
290
 
                                                        w = (CNewWizard) element.createExecutableExtension(CLASS_NAME);
291
 
                                                } catch (CoreException e) {
292
 
                                                        System.out.println(Messages.CMainWizardPage_5 + e.getLocalizedMessage());
293
 
                                                        return null;
 
57
public class CDTMainWizardPage extends WizardNewProjectCreationPage implements IWizardItemsListListener {
 
58
        public static final String PAGE_ID = "org.eclipse.cdt.managedbuilder.ui.wizard.NewModelProjectWizardPage"; //$NON-NLS-1$
 
59
 
 
60
        private static final String EXTENSION_POINT_ID = "org.eclipse.cdt.ui.CDTWizard"; //$NON-NLS-1$
 
61
        private static final String ELEMENT_NAME = "wizard"; //$NON-NLS-1$
 
62
        private static final String CLASS_NAME = "class"; //$NON-NLS-1$
 
63
        public static final String DESC = "EntryDescriptor"; //$NON-NLS-1$
 
64
 
 
65
        // Widgets
 
66
        private Tree tree;
 
67
        private Composite right;
 
68
        private Button showSup;
 
69
        private Label rightLabel;
 
70
 
 
71
        public CWizardHandler h_selected;
 
72
        private Label categorySelectedLabel;
 
73
 
 
74
        /**
 
75
         * Creates a new project creation wizard page.
 
76
         *
 
77
         * @param pageName the name of this page
 
78
         */
 
79
        public CDTMainWizardPage(String pageName) {
 
80
                super(pageName);
 
81
                setPageComplete(false);
 
82
        }
 
83
 
 
84
        @Override
 
85
        public void createControl(Composite parent) {
 
86
                super.createControl(parent);
 
87
 
 
88
                createDynamicGroup((Composite)getControl());
 
89
                switchTo(updateData(tree, right, showSup, CDTMainWizardPage.this, getWizard()),
 
90
                                getDescriptor(tree));
 
91
 
 
92
                setPageComplete(validatePage());
 
93
                setErrorMessage(null);
 
94
                setMessage(null);
 
95
        }
 
96
 
 
97
        private void createDynamicGroup(Composite parent) {
 
98
                Composite c = new Composite(parent, SWT.NONE);
 
99
                c.setLayoutData(new GridData(GridData.FILL_BOTH));
 
100
                c.setLayout(new GridLayout(2, true));
 
101
 
 
102
                Label l1 = new Label(c, SWT.NONE);
 
103
                l1.setText(Messages.CMainWizardPage_0);
 
104
                l1.setFont(parent.getFont());
 
105
                l1.setLayoutData(new GridData(GridData.BEGINNING));
 
106
 
 
107
                rightLabel = new Label(c, SWT.NONE);
 
108
                rightLabel.setFont(parent.getFont());
 
109
                rightLabel.setLayoutData(new GridData(GridData.BEGINNING));
 
110
 
 
111
                tree = new Tree(c, SWT.SINGLE | SWT.BORDER);
 
112
                tree.setLayoutData(new GridData(GridData.FILL_BOTH));
 
113
                tree.addSelectionListener(new SelectionAdapter() {
 
114
                        @Override
 
115
                        public void widgetSelected(SelectionEvent e) {
 
116
                                TreeItem[] tis = tree.getSelection();
 
117
                                if (tis == null || tis.length == 0) return;
 
118
                                switchTo((CWizardHandler)tis[0].getData(), (EntryDescriptor)tis[0].getData(DESC));
 
119
                                setPageComplete(validatePage());
 
120
                        }});
 
121
                tree.getAccessible().addAccessibleListener(new AccessibleAdapter() {
 
122
                        @Override
 
123
                        public void getName(AccessibleEvent e) {
 
124
                                for (int i = 0; i < tree.getItemCount(); i++) {
 
125
                                        if (tree.getItem(i).getText().equals(e.result))
 
126
                                                return;
 
127
                                }
 
128
                                e.result = Messages.CMainWizardPage_0;
 
129
                        }});
 
130
                right = new Composite(c, SWT.NONE);
 
131
                right.setLayoutData(new GridData(GridData.FILL_BOTH));
 
132
                right.setLayout(new PageLayout());
 
133
 
 
134
                showSup = new Button(c, SWT.CHECK);
 
135
                showSup.setText(Messages.CMainWizardPage_1);
 
136
                GridData gd = new GridData(GridData.FILL_HORIZONTAL);
 
137
                gd.horizontalSpan = 2;
 
138
                showSup.setLayoutData(gd);
 
139
                showSup.addSelectionListener(new SelectionAdapter() {
 
140
                        @Override
 
141
                        public void widgetSelected(SelectionEvent e) {
 
142
                                if (h_selected != null)
 
143
                                        h_selected.setSupportedOnly(showSup.getSelection());
 
144
                                switchTo(updateData(tree, right, showSup, CDTMainWizardPage.this, getWizard()),
 
145
                                                getDescriptor(tree));
 
146
                        }});
 
147
 
 
148
                // restore settings from preferences
 
149
                showSup.setSelection(!CDTPrefUtil.getBool(CDTPrefUtil.KEY_NOSUPP));
 
150
        }
 
151
 
 
152
        @Override
 
153
        public IWizardPage getNextPage() {
 
154
                return (h_selected == null) ? null : h_selected.getSpecificPage();
 
155
        }
 
156
 
 
157
        public URI getProjectLocation() {
 
158
                return useDefaults() ? null : getLocationURI();
 
159
        }
 
160
 
 
161
        /**
 
162
         * Returns whether this page's controls currently all contain valid
 
163
         * values.
 
164
         *
 
165
         * @return <code>true</code> if all controls are valid, and
 
166
         *   <code>false</code> if at least one is invalid
 
167
         */
 
168
        @Override
 
169
        protected boolean validatePage() {
 
170
                setMessage(null);
 
171
                if (!super.validatePage())
 
172
                        return false;
 
173
 
 
174
                if (getProjectName().indexOf('#') >= 0) {
 
175
                        setErrorMessage(Messages.CDTMainWizardPage_0);
 
176
                        return false;
 
177
                }
 
178
 
 
179
                boolean bad = true; // should we treat existing project as error
 
180
 
 
181
                IProject handle = getProjectHandle();
 
182
                if (handle.exists()) {
 
183
                        if (getWizard() instanceof IWizardWithMemory) {
 
184
                                IWizardWithMemory w = (IWizardWithMemory)getWizard();
 
185
                                if (w.getLastProjectName() != null && w.getLastProjectName().equals(getProjectName()))
 
186
                                        bad = false;
 
187
                        }
 
188
                        if (bad) {
 
189
                                setErrorMessage(Messages.CMainWizardPage_10);
 
190
                                return false;
 
191
                        }
 
192
                }
 
193
 
 
194
                if (bad) { // Skip this check if project already created
 
195
                        try {
 
196
                                IFileStore fs;
 
197
                                URI p = getProjectLocation();
 
198
                                if (p == null) {
 
199
                                        fs = EFS.getStore(ResourcesPlugin.getWorkspace().getRoot().getLocationURI());
 
200
                                        fs = fs.getChild(getProjectName());
 
201
                                } else
 
202
                                        fs = EFS.getStore(p);
 
203
                                IFileInfo f = fs.fetchInfo();
 
204
                                if (f.exists()) {
 
205
                                        if (f.isDirectory()) {
 
206
                                                if (f.getAttribute(EFS.ATTRIBUTE_READ_ONLY)) {
 
207
                                                        setErrorMessage(Messages.CMainWizardPage_DirReadOnlyError);
 
208
                                                        return false;
294
209
                                                }
295
 
                                                if (w == null) return null;
296
 
                                                w.setDependentControl(right, ls);
297
 
                                                for (EntryDescriptor ed : w.createItems(show_sup.getSelection(), wizard))
298
 
                                                        items.add(ed);
299
 
                                        }
300
 
                                }
301
 
                        }
302
 
                        // If there is a EntryDescriptor which is default for category, make sure it
303
 
                        // is in the front of the list.
304
 
                        for (int i = 0; i < items.size(); ++i)
305
 
                        {
306
 
                                EntryDescriptor ed = items.get(i);
307
 
                                if (ed.isDefaultForCategory())
308
 
                                {
309
 
                                        items.remove(i);
310
 
                                        items.add(0, ed);
311
 
                                        break;
312
 
                                }
313
 
                        }
314
 
 
315
 
                        // bug # 211935 : allow items filtering.
316
 
                        if (ls != null) // NULL means call from prefs
317
 
                                items = ls.filterItems(items);
318
 
                        addItemsToTree(tree, items);
319
 
 
320
 
                        if (tree.getItemCount() > 0) {
321
 
                                TreeItem target = null;
322
 
                                // try to search item which was selected before
323
 
                                if (savedLabel != null) {
324
 
                                        target = findItem(tree, savedLabel, savedParentLabel);
325
 
                                }
326
 
                                if (target == null) {
327
 
                                        // Default selection associated with "org.eclipse.cdt.build.core.buildArtefactType.exe" project type
328
 
                                        target = findItem(tree, Messages.CDTMainWizardPage_DefaultProjectType, Messages.CDTMainWizardPage_DefaultProjectCategory);
329
 
                                        if (target == null) {
330
 
                                                CUIPlugin.log(new Status(IStatus.WARNING, CUIPlugin.PLUGIN_ID, "Default project not found in New C/C++ Project Wizard")); //$NON-NLS-1$
331
 
                                        }
332
 
                                }
333
 
                                if (target == null) {
334
 
                                        target = tree.getItem(0);
335
 
                                        if (target.getItemCount() != 0)
336
 
                                                target = target.getItem(0);
337
 
                                }
338
 
                                tree.setSelection(target);
339
 
                                return (CWizardHandler)target.getData();
340
 
                        }
341
 
                        return null;
342
 
                }
343
 
 
344
 
                private static String getParentText(TreeItem item) {
345
 
                        if (item==null || item.getParentItem()==null)
346
 
                                return ""; //$NON-NLS-1$
347
 
                        return item.getParentItem().getText();
348
 
                }
349
 
 
350
 
                private static TreeItem findItem(Tree tree, String label, String parentLabel) {
351
 
                        for (TreeItem item : tree.getItems()) {
352
 
                                TreeItem foundItem = findTreeItem(item, label, parentLabel);
353
 
                                if (foundItem!=null)
354
 
                                        return foundItem;
355
 
                        }
356
 
                        return null;
357
 
                }
358
 
 
359
 
                private static TreeItem findTreeItem(TreeItem item, String label, String parentLabel) {
360
 
                        if (item.getText().equals(label) && getParentText(item).equals(parentLabel))
361
 
                                return item;
362
 
 
363
 
                        for (TreeItem child : item.getItems()) {
364
 
                                TreeItem foundItem = findTreeItem(child, label, parentLabel);
365
 
                                if (foundItem!=null)
366
 
                                        return foundItem;
367
 
                        }
368
 
                        return null;
369
 
                }
370
 
 
371
 
                private static void addItemsToTree(Tree tree, List<EntryDescriptor> items) {
 
210
                                                else
 
211
                                                        setMessage(Messages.CMainWizardPage_7, IMessageProvider.WARNING);
 
212
                                        } else {
 
213
                                                setErrorMessage(Messages.CMainWizardPage_6);
 
214
                                                return false;
 
215
                                        }
 
216
                                }
 
217
                        } catch (CoreException e) {
 
218
                                CUIPlugin.log(e.getStatus());
 
219
                        }
 
220
                }
 
221
 
 
222
                if (!useDefaults()) {
 
223
                        IStatus locationStatus =
 
224
                                        ResourcesPlugin.getWorkspace().validateProjectLocationURI(handle, getLocationURI());
 
225
                        if (!locationStatus.isOK()) {
 
226
                                setErrorMessage(locationStatus.getMessage());
 
227
                                return false;
 
228
                        }
 
229
                }
 
230
 
 
231
                if (tree.getItemCount() == 0) {
 
232
                        setErrorMessage(Messages.CMainWizardPage_3);
 
233
                        return false;
 
234
                }
 
235
 
 
236
                // it is not an error, but we cannot continue
 
237
                if (h_selected == null) {
 
238
                        setErrorMessage(null);
 
239
                        return false;
 
240
                }
 
241
 
 
242
                String s = h_selected.getErrorMessage();
 
243
                if (s != null) {
 
244
                        setErrorMessage(s);
 
245
                        return false;
 
246
                }
 
247
 
 
248
                setErrorMessage(null);
 
249
                return true;
 
250
        }
 
251
 
 
252
        /**
 
253
         *
 
254
         * @param tree
 
255
         * @param right
 
256
         * @param show_sup
 
257
         * @param ls
 
258
         * @param wizard
 
259
         * @return : selected Wizard Handler.
 
260
         */
 
261
        public static CWizardHandler updateData(Tree tree, Composite right, Button show_sup,
 
262
                        IWizardItemsListListener ls, IWizard wizard) {
 
263
                // Remember selected item
 
264
                TreeItem[] selection = tree.getSelection();
 
265
                TreeItem selectedItem = selection.length > 0 ? selection[0] : null;
 
266
                String savedLabel = selectedItem != null ? selectedItem.getText() : null;
 
267
                String savedParentLabel = getParentText(selectedItem);
 
268
 
 
269
                tree.removeAll();
 
270
                IExtensionPoint extensionPoint =
 
271
                                Platform.getExtensionRegistry().getExtensionPoint(EXTENSION_POINT_ID);
 
272
                if (extensionPoint == null) return null;
 
273
                IExtension[] extensions = extensionPoint.getExtensions();
 
274
                if (extensions == null) return null;
 
275
 
 
276
                List<EntryDescriptor> items = new ArrayList<EntryDescriptor>();
 
277
                for (int i = 0; i < extensions.length; ++i)     {
 
278
                        IConfigurationElement[] elements = extensions[i].getConfigurationElements();
 
279
                        for (IConfigurationElement element : elements) {
 
280
                                if (element.getName().equals(ELEMENT_NAME)) {
 
281
                                        CNewWizard w = null;
 
282
                                        try {
 
283
                                                w = (CNewWizard) element.createExecutableExtension(CLASS_NAME);
 
284
                                        } catch (CoreException e) {
 
285
                                                System.out.println(Messages.CMainWizardPage_5 + e.getLocalizedMessage());
 
286
                                                return null;
 
287
                                        }
 
288
                                        if (w == null)
 
289
                                                return null;
 
290
                                        w.setDependentControl(right, ls);
 
291
                                        for (EntryDescriptor ed : w.createItems(show_sup.getSelection(), wizard))
 
292
                                                items.add(ed);
 
293
                                }
 
294
                        }
 
295
                }
 
296
                // If there is a EntryDescriptor which is default for category, make sure it
 
297
                // is in the front of the list.
 
298
                for (int i = 0; i < items.size(); ++i) {
 
299
                        EntryDescriptor ed = items.get(i);
 
300
                        if (ed.isDefaultForCategory()) {
 
301
                                items.remove(i);
 
302
                                items.add(0, ed);
 
303
                                break;
 
304
                        }
 
305
                }
 
306
 
 
307
                // bug # 211935 : allow items filtering.
 
308
                if (ls != null) // NULL means call from prefs
 
309
                        items = ls.filterItems(items);
 
310
                addItemsToTree(tree, items);
 
311
 
 
312
                if (tree.getItemCount() > 0) {
 
313
                        TreeItem target = null;
 
314
                        // Try to search item which was selected before
 
315
                        if (savedLabel != null) {
 
316
                                target = findItem(tree, savedLabel, savedParentLabel);
 
317
                        }
 
318
                        if (target == null) {
 
319
                                // Default selection associated with "org.eclipse.cdt.build.core.buildArtefactType.exe" project type
 
320
                                target = findItem(tree, Messages.CDTMainWizardPage_DefaultProjectType,
 
321
                                                Messages.CDTMainWizardPage_DefaultProjectCategory);
 
322
                                if (target == null) {
 
323
                                        CUIPlugin.log(new Status(IStatus.WARNING, CUIPlugin.PLUGIN_ID,
 
324
                                                        "Default project not found in New C/C++ Project Wizard")); //$NON-NLS-1$
 
325
                                }
 
326
                        }
 
327
                        if (target == null) {
 
328
                                target = tree.getItem(0);
 
329
                                if (target.getItemCount() != 0)
 
330
                                        target = target.getItem(0);
 
331
                        }
 
332
                        tree.setSelection(target);
 
333
                        return (CWizardHandler)target.getData();
 
334
                }
 
335
                return null;
 
336
        }
 
337
 
 
338
        private static String getParentText(TreeItem item) {
 
339
                if (item == null || item.getParentItem() == null)
 
340
                        return ""; //$NON-NLS-1$
 
341
                return item.getParentItem().getText();
 
342
        }
 
343
 
 
344
        private static TreeItem findItem(Tree tree, String label, String parentLabel) {
 
345
                for (TreeItem item : tree.getItems()) {
 
346
                        TreeItem foundItem = findTreeItem(item, label, parentLabel);
 
347
                        if (foundItem!=null)
 
348
                                return foundItem;
 
349
                }
 
350
                return null;
 
351
        }
 
352
 
 
353
        private static TreeItem findTreeItem(TreeItem item, String label, String parentLabel) {
 
354
                if (item.getText().equals(label) && getParentText(item).equals(parentLabel))
 
355
                        return item;
 
356
 
 
357
                for (TreeItem child : item.getItems()) {
 
358
                        TreeItem foundItem = findTreeItem(child, label, parentLabel);
 
359
                        if (foundItem!=null)
 
360
                                return foundItem;
 
361
                }
 
362
                return null;
 
363
        }
 
364
 
 
365
        private static void addItemsToTree(Tree tree, List<EntryDescriptor> items) {
372
366
                //  Sorting is disabled because of users requests
373
367
                //      Collections.sort(items, CDTListComparator.getInstance());
374
368
 
375
 
                        ArrayList<TreeItem> placedTreeItemsList = new ArrayList<TreeItem>(items.size());
376
 
                        ArrayList<EntryDescriptor> placedEntryDescriptorsList = new ArrayList<EntryDescriptor>(items.size());
377
 
                        for (EntryDescriptor wd : items) {
378
 
                                if (wd.getParentId() == null) {
379
 
                                        wd.setPath(wd.getId());
380
 
                                        TreeItem ti = new TreeItem(tree, SWT.NONE);
381
 
                                        ti.setText(TextProcessor.process(wd.getName()));
382
 
                                        ti.setData(wd.getHandler());
383
 
                                        ti.setData(DESC, wd);
384
 
                                        ti.setImage(calcImage(wd));
385
 
                                        placedTreeItemsList.add(ti);
386
 
                                        placedEntryDescriptorsList.add(wd);
387
 
                                }
 
369
                ArrayList<TreeItem> placedTreeItemsList = new ArrayList<TreeItem>(items.size());
 
370
                ArrayList<EntryDescriptor> placedEntryDescriptorsList = new ArrayList<EntryDescriptor>(items.size());
 
371
                for (EntryDescriptor wd : items) {
 
372
                        if (wd.getParentId() == null) {
 
373
                                wd.setPath(wd.getId());
 
374
                                TreeItem ti = new TreeItem(tree, SWT.NONE);
 
375
                                ti.setText(TextProcessor.process(wd.getName()));
 
376
                                ti.setData(wd.getHandler());
 
377
                                ti.setData(DESC, wd);
 
378
                                ti.setImage(calcImage(wd));
 
379
                                placedTreeItemsList.add(ti);
 
380
                                placedEntryDescriptorsList.add(wd);
388
381
                        }
389
 
                        while(true) {
390
 
                                boolean found = false;
391
 
                                Iterator<EntryDescriptor> it2 = items.iterator();
392
 
                                while (it2.hasNext()) {
393
 
                                        EntryDescriptor wd1 = it2.next();
394
 
                                        if (wd1.getParentId() == null) continue;
395
 
                                        for (int i = 0; i< placedEntryDescriptorsList.size(); i++) {
396
 
                                                EntryDescriptor wd2 = placedEntryDescriptorsList.get(i);
397
 
                                                if (wd2.getId().equals(wd1.getParentId())) {
398
 
                                                        found = true;
399
 
                                                        wd1.setParentId(null);
400
 
                                                        CWizardHandler h = wd2.getHandler();
401
 
                                                        /* If neither wd1 itself, nor its parent (wd2) have a handler
402
 
                                                         * associated with them, and the item is not a category,
403
 
                                                         * then skip it. If it's category, then it's possible that
404
 
                                                         * children will have a handler associated with them.
405
 
                                                         */
406
 
                                                        if (h == null && wd1.getHandler() == null && !wd1.isCategory())
 
382
                }
 
383
                while (true) {
 
384
                        boolean found = false;
 
385
                        Iterator<EntryDescriptor> it2 = items.iterator();
 
386
                        while (it2.hasNext()) {
 
387
                                EntryDescriptor wd1 = it2.next();
 
388
                                if (wd1.getParentId() == null) continue;
 
389
                                for (int i = 0; i < placedEntryDescriptorsList.size(); i++) {
 
390
                                        EntryDescriptor wd2 = placedEntryDescriptorsList.get(i);
 
391
                                        if (wd2.getId().equals(wd1.getParentId())) {
 
392
                                                found = true;
 
393
                                                wd1.setParentId(null);
 
394
                                                CWizardHandler h = wd2.getHandler();
 
395
                                                /* If neither wd1 itself, nor its parent (wd2) have a handler
 
396
                                                 * associated with them, and the item is not a category,
 
397
                                                 * then skip it. If it's category, then it's possible that
 
398
                                                 * children will have a handler associated with them.
 
399
                                                 */
 
400
                                                if (h == null && wd1.getHandler() == null && !wd1.isCategory())
 
401
                                                        break;
 
402
 
 
403
                                                wd1.setPath(wd2.getPath() + "/" + wd1.getId()); //$NON-NLS-1$
 
404
                                                wd1.setParent(wd2);
 
405
                                                if (h != null) {
 
406
                                                        if (wd1.getHandler() == null && !wd1.isCategory())
 
407
                                                                wd1.setHandler((CWizardHandler)h.clone());
 
408
                                                        if (!h.isApplicable(wd1))
407
409
                                                                break;
408
 
 
409
 
                                                        wd1.setPath(wd2.getPath() + "/" + wd1.getId()); //$NON-NLS-1$
410
 
                                                        wd1.setParent(wd2);
411
 
                                                        if (h != null) {
412
 
                                                                if (wd1.getHandler() == null && !wd1.isCategory())
413
 
                                                                        wd1.setHandler((CWizardHandler)h.clone());
414
 
                                                                if (!h.isApplicable(wd1))
415
 
                                                                        break;
416
 
                                                        }
417
 
 
418
 
                                                        TreeItem p = placedTreeItemsList.get(i);
419
 
                                                        TreeItem ti = new TreeItem(p, SWT.NONE);
420
 
                                                        ti.setText(wd1.getName());
421
 
                                                        ti.setData(wd1.getHandler());
422
 
                                                        ti.setData(DESC, wd1);
423
 
                                                        ti.setImage(calcImage(wd1));
424
 
                                                        placedTreeItemsList.add(ti);
425
 
                                                        placedEntryDescriptorsList.add(wd1);
426
 
                                                        break;
427
410
                                                }
428
 
                                        }
429
 
                                }
430
 
                                // repeat iterations until all items are placed.
431
 
                                if (!found) break;
432
 
                        }
433
 
                        // orphan elements (with not-existing parentId) are ignored
434
 
                }
435
 
 
436
 
                private void switchTo(CWizardHandler h, EntryDescriptor ed) {
437
 
                        if (h == null)
438
 
                                h = ed.getHandler();
439
 
                        if (ed.isCategory())
440
 
                                h = null;
441
 
                        try {
442
 
                                if (h != null)
443
 
                                        h.initialize(ed);
444
 
                        } catch (CoreException e) {
445
 
                                h = null;
446
 
                        }
447
 
                        if (h_selected != null)
448
 
                                h_selected.handleUnSelection();
449
 
                        h_selected = h;
450
 
                        if (h == null) {
451
 
                                if (ed.isCategory()) {
452
 
                                        if (categorySelectedLabel == null) {
453
 
                                                categorySelectedLabel = new Label(right, SWT.WRAP);
454
 
                                                categorySelectedLabel.setText(
455
 
                                                                Messages.CDTMainWizardPage_1);
456
 
                                                right.layout();
457
 
                                        }
458
 
                                        categorySelectedLabel.setVisible(true);
459
 
                                }
460
 
                                return;
461
 
                        }
462
 
                        right_label.setText(h_selected.getHeader());
463
 
                        if (categorySelectedLabel != null)
464
 
                                categorySelectedLabel.setVisible(false);
465
 
                        h_selected.handleSelection();
466
 
                        h_selected.setSupportedOnly(show_sup.getSelection());
467
 
                }
468
 
 
469
 
 
470
 
                public static EntryDescriptor getDescriptor(Tree _tree) {
471
 
                        TreeItem[] sel = _tree.getSelection();
472
 
                        if (sel == null || sel.length == 0)
473
 
                                return null;
474
 
                        return (EntryDescriptor)sel[0].getData(DESC);
475
 
                }
476
 
 
477
 
                @Override
478
 
                public void toolChainListChanged(int count) {
479
 
                        setPageComplete(validatePage());
480
 
                        getWizard().getContainer().updateButtons();
481
 
                }
482
 
 
483
 
                @Override
484
 
                public boolean isCurrent() { return isCurrentPage(); }
485
 
 
486
 
                private static Image calcImage(EntryDescriptor ed) {
487
 
                        if (ed.getImage() != null) return ed.getImage();
488
 
                        if (ed.isCategory()) return IMG_CATEGORY;
489
 
                        return IMG_ITEM;
490
 
                }
491
 
 
492
 
                @Override
493
 
                @SuppressWarnings({ "unchecked", "rawtypes" })
494
 
                public List filterItems(List items) {
495
 
                        return items;
496
 
                }
 
411
 
 
412
                                                TreeItem p = placedTreeItemsList.get(i);
 
413
                                                TreeItem ti = new TreeItem(p, SWT.NONE);
 
414
                                                ti.setText(wd1.getName());
 
415
                                                ti.setData(wd1.getHandler());
 
416
                                                ti.setData(DESC, wd1);
 
417
                                                ti.setImage(calcImage(wd1));
 
418
                                                placedTreeItemsList.add(ti);
 
419
                                                placedEntryDescriptorsList.add(wd1);
 
420
                                                break;
 
421
                                        }
 
422
                                }
 
423
                        }
 
424
                        // repeat iterations until all items are placed.
 
425
                        if (!found) break;
 
426
                }
 
427
                // orphan elements (with not-existing parentId) are ignored
 
428
        }
 
429
 
 
430
        private void switchTo(CWizardHandler h, EntryDescriptor ed) {
 
431
                if (h == null)
 
432
                        h = ed.getHandler();
 
433
                if (ed.isCategory())
 
434
                        h = null;
 
435
                try {
 
436
                        if (h != null)
 
437
                                h.initialize(ed);
 
438
                } catch (CoreException e) {
 
439
                        h = null;
 
440
                }
 
441
                if (h_selected != null)
 
442
                        h_selected.handleUnSelection();
 
443
                h_selected = h;
 
444
                if (h == null) {
 
445
                        if (ed.isCategory()) {
 
446
                                if (categorySelectedLabel == null) {
 
447
                                        categorySelectedLabel = new Label(right, SWT.WRAP);
 
448
                                        categorySelectedLabel.setText(Messages.CDTMainWizardPage_1);
 
449
                                        right.layout();
 
450
                                }
 
451
                                categorySelectedLabel.setVisible(true);
 
452
                        }
 
453
                        return;
 
454
                }
 
455
                rightLabel.setText(h_selected.getHeader());
 
456
                if (categorySelectedLabel != null)
 
457
                        categorySelectedLabel.setVisible(false);
 
458
                h_selected.handleSelection();
 
459
                h_selected.setSupportedOnly(showSup.getSelection());
 
460
        }
 
461
 
 
462
        public static EntryDescriptor getDescriptor(Tree tree) {
 
463
                TreeItem[] sel = tree.getSelection();
 
464
                if (sel == null || sel.length == 0)
 
465
                        return null;
 
466
                return (EntryDescriptor) sel[0].getData(DESC);
 
467
        }
 
468
 
 
469
        @Override
 
470
        public void toolChainListChanged(int count) {
 
471
                setPageComplete(validatePage());
 
472
                getWizard().getContainer().updateButtons();
 
473
        }
 
474
 
 
475
        @Override
 
476
        public boolean isCurrent() {
 
477
                return isCurrentPage();
 
478
        }
 
479
 
 
480
        private static Image calcImage(EntryDescriptor ed) {
 
481
                Image image = ed.getImage();
 
482
                if (image != null)
 
483
                        return image;
 
484
                if (ed.isCategory())
 
485
                        return CDTSharedImages.getImage(CDTSharedImages.IMG_OBJS_SEARCHFOLDER);
 
486
                return CDTSharedImages.getImage(CDTSharedImages.IMG_OBJS_VARIABLE);
 
487
        }
 
488
 
 
489
        @Override
 
490
        @SuppressWarnings({ "unchecked", "rawtypes" })
 
491
        public List filterItems(List items) {
 
492
                return items;
 
493
        }
497
494
}
498