Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: bfa5146a56eb5967772d420743d4c111c0e333fa (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
/*******************************************************************************
 * Copyright (c) 2004, 2012 QNX Software Systems and others.
 *
 * This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License 2.0
 * which accompanies this distribution, and is available at
 * https://www.eclipse.org/legal/epl-2.0/
 *
 * SPDX-License-Identifier: EPL-2.0
 *
 * Contributors:
 *     QNX Software Systems - initial API and implementation
 *******************************************************************************/
package org.eclipse.cdt.internal.ui.wizards.classwizard;

import java.util.ArrayList;

import org.eclipse.cdt.core.model.CoreModel;
import org.eclipse.cdt.core.model.ICContainer;
import org.eclipse.cdt.core.model.ICElement;
import org.eclipse.cdt.core.model.ICElementVisitor;
import org.eclipse.cdt.core.model.ICModel;
import org.eclipse.cdt.core.model.ICProject;
import org.eclipse.cdt.core.model.ITranslationUnit;
import org.eclipse.cdt.internal.corext.util.CModelUtil;
import org.eclipse.cdt.internal.ui.dialogs.StatusInfo;
import org.eclipse.cdt.internal.ui.dialogs.StatusUtil;
import org.eclipse.cdt.internal.ui.dialogs.TypedViewerFilter;
import org.eclipse.cdt.internal.ui.wizards.dialogfields.DialogField;
import org.eclipse.cdt.internal.ui.wizards.dialogfields.IDialogFieldListener;
import org.eclipse.cdt.internal.ui.wizards.dialogfields.LayoutUtil;
import org.eclipse.cdt.internal.ui.wizards.dialogfields.StringDialogField;
import org.eclipse.cdt.ui.CElementContentProvider;
import org.eclipse.cdt.ui.CElementLabelProvider;
import org.eclipse.cdt.ui.CElementSorter;
import org.eclipse.cdt.utils.PathUtil;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.IResource;
import org.eclipse.core.resources.IWorkspaceRoot;
import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IPath;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Path;
import org.eclipse.jface.dialogs.IDialogConstants;
import org.eclipse.jface.viewers.DoubleClickEvent;
import org.eclipse.jface.viewers.IDoubleClickListener;
import org.eclipse.jface.viewers.ILabelProvider;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.ISelectionChangedListener;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.jface.viewers.ITreeContentProvider;
import org.eclipse.jface.viewers.SelectionChangedEvent;
import org.eclipse.jface.viewers.StructuredSelection;
import org.eclipse.jface.viewers.TreeViewer;
import org.eclipse.jface.viewers.Viewer;
import org.eclipse.osgi.util.NLS;
import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.BusyIndicator;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Tree;
import org.eclipse.ui.dialogs.SelectionStatusDialog;

public class SourceFileSelectionDialog extends SelectionStatusDialog {

	TreeViewer fViewer;
	private final ITreeContentProvider fContentProvider = new CElementContentProvider();
	private final ILabelProvider fLabelProvider = new CElementLabelProvider(CElementLabelProvider.SHOW_DEFAULT);
	IStatus fCurrStatus = new StatusInfo();
	IStatus fFolderNameStatus = new StatusInfo();
	IStatus fFileNameStatus = new StatusInfo();
	ICElement fInput;
	private int fWidth = 60;
	private int fHeight = 18;
	StringDialogField fFolderNameDialogField;
	StringDialogField fFileNameDialogField;
	private IWorkspaceRoot fWorkspaceRoot;
	private final FieldsAdapter fFieldsAdapter = new FieldsAdapter();

	private ICElement fCurrentFolder = null;
	private String fCurrentFileString = null;
	String fInitialFolderName = null;
	String fInitialFileName = null;

	private final class FieldsAdapter extends SelectionAdapter
			implements ISelectionChangedListener, IDoubleClickListener, IDialogFieldListener {

		// -- SelectionAdapter --
		@Override
		public void widgetDefaultSelected(SelectionEvent e) {
			doStatusUpdate();
			if (fCurrStatus.isOK())
				buttonPressed(IDialogConstants.OK_ID);
		}

		// -- ISelectionChangedListener --
		@Override
		public void selectionChanged(SelectionChangedEvent event) {
			setResult(((IStructuredSelection) event.getSelection()).toList());
			ISelection sel = event.getSelection();
			if (sel instanceof IStructuredSelection) {
				Object obj = ((IStructuredSelection) sel).getFirstElement();
				if (obj instanceof ICElement) {
					ICElement elem = (ICElement) obj;
					IPath path = elem.getPath();
					String fileName = fFileNameDialogField.getText();
					String folderName = fFolderNameDialogField.getText();
					if (elem instanceof ICContainer || elem instanceof ICProject) {
						folderName = path.toString();
					} else {
						folderName = path.removeLastSegments(1).toString();
						fileName = path.lastSegment();
					}
					setPathFields(folderName, fileName);
				}
			}
			doStatusUpdate();
		}

		// -- IDoubleClickListener --
		@Override
		public void doubleClick(DoubleClickEvent event) {
			doStatusUpdate();

			ISelection selection = event.getSelection();
			if (selection instanceof IStructuredSelection) {
				Object item = ((IStructuredSelection) selection).getFirstElement();
				if (fCurrStatus.getSeverity() != IStatus.ERROR) {
					if (item instanceof ITranslationUnit) {
						setResult(((IStructuredSelection) selection).toList());
						close();
						return;
					}
				}
				if (fViewer.getExpandedState(item))
					fViewer.collapseToLevel(item, 1);
				else
					fViewer.expandToLevel(item, 1);
			}
		}

		// -- IDialogFieldListener --
		@Override
		public void dialogFieldChanged(DialogField field) {
			if (field == fFolderNameDialogField) {
				fFolderNameStatus = folderNameChanged();
				fFileNameStatus = fileNameChanged();
			} else if (field == fFileNameDialogField) {
				fFileNameStatus = fileNameChanged();
			}
			doStatusUpdate();
		}
	}

	static final Class<?>[] FILTER_TYPES = new Class[] { ICModel.class, ICProject.class, ICContainer.class,
			ITranslationUnit.class };

	private final class Filter extends TypedViewerFilter {

		private Filter() {
			super(FILTER_TYPES);
		}

		@Override
		public boolean select(Viewer viewer, Object parent, Object obj) {
			if (obj instanceof ICElement) {
				ICElement elem = (ICElement) obj;
				if (!(fInput instanceof ICModel)) {
					return elem.getCProject().equals(fInput.getCProject());
				}
				return true;
			}
			return super.select(viewer, parent, obj);
		}
	}

	/**
	 * Constructs an instance of <code>ElementTreeSelectionDialog</code>.
	 *
	 * @param parent
	 *            The parent shell for the dialog
	 */
	public SourceFileSelectionDialog(Shell parent) {
		super(parent);

		fWorkspaceRoot = ResourcesPlugin.getWorkspace().getRoot();
		fInput = CoreModel.create(fWorkspaceRoot);

		fFolderNameDialogField = new StringDialogField();
		fFolderNameDialogField.setDialogFieldListener(fFieldsAdapter);
		fFolderNameDialogField.setLabelText(NewClassWizardMessages.SourceFileSelectionDialog_folderName_label);

		fFileNameDialogField = new StringDialogField();
		fFileNameDialogField.setDialogFieldListener(fFieldsAdapter);
		fFileNameDialogField.setLabelText(NewClassWizardMessages.SourceFileSelectionDialog_fileName_label);

		setResult(new ArrayList<>(0));
		setStatusLineAboveButtons(true);

		int shellStyle = getShellStyle();
		setShellStyle(shellStyle | SWT.MAX | SWT.RESIZE);
	}

	/**
	 * Sets the tree input.
	 *
	 * @param input
	 *            the tree input.
	 */
	public void setInput(ICElement input) {
		fInput = input;
	}

	protected void doStatusUpdate() {
		// status of all used components
		IStatus[] status = new IStatus[] { fFolderNameStatus, fFileNameStatus };

		// the mode severe status will be displayed and the ok button enabled/disabled.
		updateStatus(status);
	}

	/**
	 * Updates the status line and the ok button according to the given status
	 *
	 * @param status status to apply
	 */
	@Override
	protected void updateStatus(IStatus status) {
		fCurrStatus = status;
		super.updateStatus(status);
	}

	/**
	 * Updates the status line and the ok button according to the status evaluate from
	 * an array of status. The most severe error is taken.  In case that two status with
	 * the same severity exists, the status with lower index is taken.
	 *
	 * @param status the array of status
	 */
	protected void updateStatus(IStatus[] status) {
		updateStatus(StatusUtil.getMostSevere(status));
	}

	IStatus folderNameChanged() {
		StatusInfo status = new StatusInfo();

		fCurrentFolder = null;
		String str = fFolderNameDialogField.getText();
		if (str.length() == 0) {
			status.setError(NewClassWizardMessages.SourceFileSelectionDialog_error_EnterFolderName);
			return status;
		}

		IPath path = new Path(str);
		IResource res = fWorkspaceRoot.findMember(path);
		if (res != null && res.exists()) {
			int resType = res.getType();
			if (resType == IResource.PROJECT || resType == IResource.FOLDER) {
				IProject proj = res.getProject();
				if (!proj.isOpen()) {
					status.setError(NLS.bind(NewClassWizardMessages.SourceFileSelectionDialog_error_NotAFolder, str));
					return status;
				}
				ICElement e = CoreModel.getDefault().create(res.getFullPath());
				fCurrentFolder = CModelUtil.getSourceFolder(e);
				if (fCurrentFolder == null) {
					status.setError(
							NLS.bind(NewClassWizardMessages.SourceFileSelectionDialog_error_NotASourceFolder, str));
					return status;
				}
				if (!CoreModel.hasCCNature(proj) && !CoreModel.hasCNature(proj)) {
					if (resType == IResource.PROJECT) {
						status.setError(NewClassWizardMessages.SourceFileSelectionDialog_warning_NotACProject);
						return status;
					}
					status.setWarning(NewClassWizardMessages.SourceFileSelectionDialog_warning_NotInACProject);
				}
			} else {
				status.setError(NLS.bind(NewClassWizardMessages.SourceFileSelectionDialog_error_NotAFolder, str));
				return status;
			}
		} else {
			status.setError(NLS.bind(NewClassWizardMessages.SourceFileSelectionDialog_error_FolderDoesNotExist, str));
			return status;
		}
		return status;
	}

	IStatus fileNameChanged() {
		StatusInfo status = new StatusInfo();

		fCurrentFileString = null;
		ICElement existingFile = null;
		String str = fFileNameDialogField.getText();
		if (str.length() == 0) {
			status.setError(NewClassWizardMessages.SourceFileSelectionDialog_error_EnterFileName);
			return status;
		}

		if (fCurrentFolder != null) {
			IPath folderPath = fCurrentFolder.getPath();
			IPath path = new Path(str);
			IResource res = fWorkspaceRoot.findMember(folderPath.append(path));
			if (res == null)
				res = fWorkspaceRoot.findMember(path);
			if (res != null && res.exists()) {
				int resType = res.getType();
				if (resType == IResource.FILE) {
					IProject proj = res.getProject();
					if (!proj.isOpen()) {
						status.setError(NLS.bind(NewClassWizardMessages.SourceFileSelectionDialog_error_NotAFile, str));
						return status;
					}
					ICElement e = CoreModel.getDefault().create(res.getFullPath());
					if (e instanceof ITranslationUnit) {
						existingFile = e;
					}
					if (existingFile == null) {
						status.setError(
								NLS.bind(NewClassWizardMessages.SourceFileSelectionDialog_error_NotASourceFile, str));
						return status;
					}
					if (!CoreModel.hasCCNature(proj) && !CoreModel.hasCNature(proj)) {
						status.setWarning(NewClassWizardMessages.SourceFileSelectionDialog_warning_NotInACProject);
					}
				} else {
					status.setError(NLS.bind(NewClassWizardMessages.SourceFileSelectionDialog_error_NotAFile, str));
					return status;
				}
			}
		}
		if (existingFile != null) {
			status.setWarning(NLS.bind(NewClassWizardMessages.SourceFileSelectionDialog_warning_SourceFileExists, str));
		}
		fCurrentFileString = str;
		return status;
	}

	@Override
	public int open() {
		super.open();
		return getReturnCode();
	}

	/**
	 * Handles cancel button pressed event.
	 */
	@Override
	protected void cancelPressed() {
		setResult(null);
		super.cancelPressed();
	}

	/*
	 * @see SelectionStatusDialog#computeResult()
	 */
	@Override
	protected void computeResult() {
		setResult(((IStructuredSelection) fViewer.getSelection()).toList());
	}

	@Override
	public void create() {
		BusyIndicator.showWhile(null, new Runnable() {
			@Override
			public void run() {
				superCreate();
				fViewer.setSelection(new StructuredSelection(getInitialElementSelections()), true);
				setPathFields(fInitialFolderName, fInitialFileName);
				fFileNameDialogField.setFocus();
				doStatusUpdate();
			}
		});
	}

	void superCreate() {
		super.create();
	}

	/*
	 * @see Dialog#createDialogArea(Composite)
	 */
	@Override
	protected Control createDialogArea(Composite parent) {
		Composite composite = (Composite) super.createDialogArea(parent);
		// Label messageLabel = createMessageArea(composite);
		int nColumns = 4;
		fFolderNameDialogField.doFillIntoGrid(composite, nColumns - 1);
		DialogField.createEmptySpace(composite);
		LayoutUtil.setWidthHint(fFolderNameDialogField.getTextControl(null), getMaxFieldWidth());

		TreeViewer treeViewer = createTreeViewer(composite);

		GridData data = new GridData(GridData.FILL_BOTH);
		data.widthHint = convertWidthInCharsToPixels(fWidth);
		data.heightHint = convertHeightInCharsToPixels(fHeight);

		Tree treeWidget = treeViewer.getTree();
		treeWidget.setLayoutData(data);
		treeWidget.setFont(parent.getFont());

		fFileNameDialogField.doFillIntoGrid(composite, nColumns - 1);
		DialogField.createEmptySpace(composite);
		LayoutUtil.setWidthHint(fFileNameDialogField.getTextControl(null), getMaxFieldWidth());

		return composite;
	}

	/**
	 * Returns the recommended maximum width for text fields (in pixels). This
	 * method requires that createContent has been called before this method is
	 * call. Subclasses may override to change the maximum width for text
	 * fields.
	 *
	 * @return the recommended maximum width for text fields.
	 */
	protected int getMaxFieldWidth() {
		return convertWidthInCharsToPixels(60);
	}

	/**
	 * Creates the tree viewer.
	 *
	 * @param parent
	 *            the parent composite
	 * @return the tree viewer
	 */
	protected TreeViewer createTreeViewer(Composite parent) {
		int style = (SWT.BORDER | SWT.SINGLE);

		fViewer = new TreeViewer(new Tree(parent, style));
		fViewer.setContentProvider(fContentProvider);
		fViewer.setLabelProvider(fLabelProvider);
		fViewer.addSelectionChangedListener(fFieldsAdapter);

		fViewer.setSorter(new CElementSorter());
		fViewer.addFilter(new Filter());

		Tree tree = fViewer.getTree();
		tree.addSelectionListener(fFieldsAdapter);
		fViewer.addDoubleClickListener(fFieldsAdapter);

		fViewer.setInput(fInput.getCModel());

		return fViewer;
	}

	/**
	 * Returns the tree viewer.
	 *
	 * @return the tree viewer
	 */
	protected TreeViewer getTreeViewer() {
		return fViewer;
	}

	/**
	 * @see org.eclipse.jface.window.Window#handleShellCloseEvent()
	 */
	@Override
	protected void handleShellCloseEvent() {
		super.handleShellCloseEvent();

		//Handle the closing of the shell by selecting the close icon
		if (getReturnCode() == CANCEL)
			setResult(null);
	}

	public String getFolderName() {
		if (fCurrentFolder != null) {
			return fCurrentFolder.getPath().toString();
		}
		return null;
	}

	public String getFileName() {
		return fCurrentFileString;
	}

	public IPath getFilePath() {
		IPath path = null;
		if (fCurrentFolder != null) {
			path = fCurrentFolder.getPath();
			if (fCurrentFileString != null)
				path = path.append(fCurrentFileString);
		} else if (fCurrentFileString != null) {
			path = new Path(fCurrentFileString);
		}
		return path;
	}

	void setPathFields(String folderName, String fileName) {
		fFolderNameDialogField.setTextWithoutUpdate(folderName != null ? folderName : ""); //$NON-NLS-1$
		fFileNameDialogField.setTextWithoutUpdate(fileName != null ? fileName : ""); //$NON-NLS-1$
		fFolderNameStatus = folderNameChanged();
		fFileNameStatus = fileNameChanged();
	}

	/**
	 * Sets the initial selection. Convenience method.
	 */
	public void setInitialSelection(String folderName, String fileName) {
		fInitialFileName = (fileName != null && fileName.length() > 0) ? fileName : null;
		fInitialFolderName = null;
		if (folderName != null && folderName.length() > 0) {
			// find a folder that actually exists
			IPath initialFolderPath = new Path(folderName);
			final IPath folderPath = PathUtil.getValidEnclosingFolder(initialFolderPath);
			if (folderPath != null) {
				fInitialFolderName = folderPath.toString();
				if (fInput != null) {
					final ICElement[] foundElem = { /*base_folder*/ null, /*exact_folder*/ null, /*exact_file*/ null };
					try {
						fInput.accept(new ICElementVisitor() {
							@Override
							public boolean visit(ICElement elem) {
								IPath path = elem.getPath();
								if (path.isPrefixOf(folderPath)) {
									if (foundElem[0] == null
											|| path.segmentCount() > foundElem[0].getPath().segmentCount()) {
										foundElem[0] = elem; /*base_folder*/
									}
									if (path.equals(folderPath)) {
										foundElem[1] = elem; /*exact_folder*/
										if (fInitialFileName == null)
											return false; // no need to search children
									} else if (fInitialFileName != null
											&& elem.getElementName().equals(fInitialFileName)) {
										foundElem[2] = elem; /*exact_file*/
										return false; // no need to search children
									}
									return true;
								}
								return false;
							}
						});

						ICElement selectedElement = foundElem[2]; /*exact_file*/
						if (selectedElement == null)
							selectedElement = foundElem[1]; /*exact_folder*/
						if (selectedElement == null)
							selectedElement = foundElem[0]; /*base_folder*/

						if (selectedElement != null) {
							setInitialSelections(new Object[] { selectedElement });
						}
					} catch (CoreException e) {
					}
				}
			}
		}
	}
}

Back to the top