1 /*******************************************************************************
2 * Copyright (c) 2004, 2009 QNX Software Systems and others.
3 * All rights reserved. This program and the accompanying materials
4 * are made available under the terms of the Eclipse Public License v1.0
5 * which accompanies this distribution, and is available at
6 * http://www.eclipse.org/legal/epl-v10.html
9 * QNX Software Systems - initial API and implementation
10 *******************************************************************************/
11 package org.eclipse.cdt.ui.newui;
13 import java.util.List;
15 import org.eclipse.core.resources.IContainer;
16 import org.eclipse.core.resources.IFile;
17 import org.eclipse.core.resources.IFolder;
18 import org.eclipse.core.resources.IProject;
19 import org.eclipse.core.resources.IResource;
20 import org.eclipse.core.resources.IWorkspaceRoot;
21 import org.eclipse.core.runtime.IPath;
22 import org.eclipse.core.runtime.Path;
23 import org.eclipse.jface.dialogs.StatusDialog;
24 import org.eclipse.jface.viewers.ILabelProvider;
25 import org.eclipse.jface.viewers.ITreeContentProvider;
26 import org.eclipse.jface.viewers.ViewerFilter;
27 import org.eclipse.jface.window.Window;
28 import org.eclipse.osgi.util.NLS;
29 import org.eclipse.swt.SWT;
30 import org.eclipse.swt.layout.GridData;
31 import org.eclipse.swt.layout.GridLayout;
32 import org.eclipse.swt.widgets.Composite;
33 import org.eclipse.swt.widgets.Control;
34 import org.eclipse.swt.widgets.Label;
35 import org.eclipse.swt.widgets.Shell;
36 import org.eclipse.ui.dialogs.ElementTreeSelectionDialog;
37 import org.eclipse.ui.dialogs.ISelectionStatusValidator;
38 import org.eclipse.ui.model.WorkbenchContentProvider;
39 import org.eclipse.ui.model.WorkbenchLabelProvider;
40 import org.eclipse.ui.views.navigator.ResourceComparator;
42 import org.eclipse.cdt.internal.ui.dialogs.StatusInfo;
43 import org.eclipse.cdt.internal.ui.dialogs.TypedElementSelectionValidator;
44 import org.eclipse.cdt.internal.ui.dialogs.TypedViewerFilter;
45 import org.eclipse.cdt.internal.ui.dialogs.cpaths.CPathEntryMessages;
46 import org.eclipse.cdt.internal.ui.wizards.dialogfields.DialogField;
47 import org.eclipse.cdt.internal.ui.wizards.dialogfields.IDialogFieldListener;
48 import org.eclipse.cdt.internal.ui.wizards.dialogfields.IStringButtonAdapter;
49 import org.eclipse.cdt.internal.ui.wizards.dialogfields.LayoutUtil;
50 import org.eclipse.cdt.internal.ui.wizards.dialogfields.StringButtonDialogField;
53 * @noextend This class is not intended to be subclassed by clients.
55 public class ExPatternEntryDialog extends StatusDialog {
56 private StringButtonDialogField fExclusionPatternDialog;
57 private StatusInfo fExclusionPatternStatus;
59 private IContainer fCurrSourceFolder;
60 private String fExclusionPattern;
61 private List<String> fExistingPatterns;
63 public ExPatternEntryDialog(Shell parent, String patternToEdit, List<String> existingPatterns, IProject proj, IPath path) {
65 fExistingPatterns = existingPatterns;
66 if (patternToEdit == null) {
67 setTitle(CPathEntryMessages.ExclusionPatternEntryDialog_add_title);
69 setTitle(CPathEntryMessages.ExclusionPatternEntryDialog_edit_title);
70 fExistingPatterns.remove(patternToEdit);
73 IWorkspaceRoot root = proj.getWorkspace().getRoot();
74 IResource res = root.findMember(path);
75 if (res instanceof IContainer) {
76 fCurrSourceFolder = (IContainer) res;
79 fExclusionPatternStatus = new StatusInfo();
81 String label = NLS.bind(CPathEntryMessages.ExclusionPatternEntryDialog_pattern_label,
82 path.makeRelative().toString());
84 ExPatternAdapter adapter = new ExPatternAdapter();
85 fExclusionPatternDialog = new StringButtonDialogField(adapter);
86 fExclusionPatternDialog.setLabelText(label);
87 fExclusionPatternDialog.setButtonLabel(CPathEntryMessages.ExclusionPatternEntryDialog_pattern_button);
88 fExclusionPatternDialog.setDialogFieldListener(adapter);
89 fExclusionPatternDialog.enableButton(fCurrSourceFolder != null);
91 if (patternToEdit == null) {
92 fExclusionPatternDialog.setText(""); //$NON-NLS-1$
94 fExclusionPatternDialog.setText(patternToEdit.toString());
97 setHelpAvailable(false);
101 protected Control createDialogArea(Composite parent) {
102 Composite composite = (Composite) super.createDialogArea(parent);
104 int widthHint = convertWidthInCharsToPixels(60);
106 Composite inner = new Composite(composite, SWT.NONE);
107 GridLayout layout = new GridLayout();
108 layout.marginHeight = 0;
109 layout.marginWidth = 0;
110 layout.numColumns = 2;
111 inner.setLayout(layout);
113 Label description = new Label(inner, SWT.WRAP);
114 description.setText(CPathEntryMessages.ExclusionPatternEntryDialog_description);
115 GridData gd = new GridData();
116 gd.horizontalSpan = 2;
117 gd.widthHint = convertWidthInCharsToPixels(80);
118 description.setLayoutData(gd);
120 fExclusionPatternDialog.doFillIntoGrid(inner, 3);
122 LayoutUtil.setWidthHint(fExclusionPatternDialog.getLabelControl(null), widthHint);
123 LayoutUtil.setHorizontalSpan(fExclusionPatternDialog.getLabelControl(null), 2);
125 LayoutUtil.setWidthHint(fExclusionPatternDialog.getTextControl(null), widthHint);
126 LayoutUtil.setHorizontalGrabbing(fExclusionPatternDialog.getTextControl(null), true);
128 fExclusionPatternDialog.postSetFocusOnDialogField(parent.getDisplay());
129 applyDialogFont(composite);
133 // -------- ExclusionPatternAdapter --------
135 private class ExPatternAdapter implements IDialogFieldListener, IStringButtonAdapter {
137 // -------- IDialogFieldListener
139 public void dialogFieldChanged(DialogField field) {
140 doStatusLineUpdate();
143 public void changeControlPressed(DialogField field) {
144 doChangeControlPressed();
148 protected void doChangeControlPressed() {
149 IPath pattern = chooseExclusionPattern();
150 if (pattern != null) {
151 fExclusionPatternDialog.setText(pattern.toString());
155 protected void doStatusLineUpdate() {
156 checkIfPatternValid();
157 updateStatus(fExclusionPatternStatus);
160 protected void checkIfPatternValid() {
161 String pattern = fExclusionPatternDialog.getText().trim();
162 if (pattern.length() == 0) {
163 fExclusionPatternStatus.setError(CPathEntryMessages.ExclusionPatternEntryDialog_error_empty);
166 IPath path = new Path(pattern);
167 if (path.isAbsolute() || path.getDevice() != null) {
168 fExclusionPatternStatus.setError(CPathEntryMessages.ExclusionPatternEntryDialog_error_notrelative);
171 if (fExistingPatterns.contains(pattern)) {
172 fExclusionPatternStatus.setError(CPathEntryMessages.ExclusionPatternEntryDialog_error_exists);
176 fExclusionPattern = pattern;
177 fExclusionPatternStatus.setOK();
180 public String getExclusionPattern() {
181 return fExclusionPattern;
185 * @see org.eclipse.jface.window.Window#configureShell(Shell)
188 protected void configureShell(Shell newShell) {
189 super.configureShell(newShell);
190 // WorkbenchHelp.setHelp(newShell,
191 // ICHelpContextIds.EXCLUSION_PATTERN_DIALOG);
194 // ---------- util method ------------
196 private IPath chooseExclusionPattern() {
197 Class<?>[] acceptedClasses = new Class<?>[] { IFolder.class, IFile.class};
198 ISelectionStatusValidator validator = new TypedElementSelectionValidator(acceptedClasses, false);
199 ViewerFilter filter = new TypedViewerFilter(acceptedClasses);
201 ILabelProvider lp = new WorkbenchLabelProvider();
202 ITreeContentProvider cp = new WorkbenchContentProvider();
204 IPath initialPath = new Path(fExclusionPatternDialog.getText());
205 IResource initialElement = null;
206 IContainer curr = fCurrSourceFolder;
207 int nSegments = initialPath.segmentCount();
208 for (int i = 0; i < nSegments; i++) {
209 IResource elem = curr.findMember(initialPath.segment(i));
211 initialElement = elem;
213 if (elem instanceof IContainer) {
214 curr = (IContainer) elem;
220 ElementTreeSelectionDialog dialog = new ElementTreeSelectionDialog(getShell(), lp, cp);
221 dialog.setTitle(CPathEntryMessages.ExclusionPatternEntryDialog_ChooseExclusionPattern_title);
222 dialog.setValidator(validator);
223 dialog.setMessage(CPathEntryMessages.ExclusionPatternEntryDialog_ChooseExclusionPattern_description);
224 dialog.addFilter(filter);
225 dialog.setInput(fCurrSourceFolder);
226 dialog.setInitialSelection(initialElement);
227 dialog.setComparator(new ResourceComparator(ResourceComparator.NAME));
229 if (dialog.open() == Window.OK) {
230 IResource res = (IResource) dialog.getFirstResult();
231 IPath path = res.getFullPath().removeFirstSegments(fCurrSourceFolder.getFullPath().segmentCount()).makeRelative();
232 if (res instanceof IContainer) {
233 return path.addTrailingSeparator();