upload tizen1.0 source
[sdk/ide/product.git] / org.eclipse.cdt.ui / src / org / eclipse / cdt / ui / newui / ExPatternEntryDialog.java
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
7  *
8  * Contributors:
9  *     QNX Software Systems - initial API and implementation
10  *******************************************************************************/
11 package org.eclipse.cdt.ui.newui;
12
13 import java.util.List;
14
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;
41
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;
51
52 /**
53  * @noextend This class is not intended to be subclassed by clients.
54  */
55 public class ExPatternEntryDialog extends StatusDialog {
56         private StringButtonDialogField fExclusionPatternDialog;
57         private StatusInfo fExclusionPatternStatus;
58
59         private IContainer fCurrSourceFolder;
60         private String fExclusionPattern;
61         private List<String> fExistingPatterns;
62
63         public ExPatternEntryDialog(Shell parent, String patternToEdit, List<String> existingPatterns, IProject proj, IPath path) {
64                 super(parent);
65                 fExistingPatterns = existingPatterns;
66                 if (patternToEdit == null) {
67                         setTitle(CPathEntryMessages.ExclusionPatternEntryDialog_add_title); 
68                 } else {
69                         setTitle(CPathEntryMessages.ExclusionPatternEntryDialog_edit_title); 
70                         fExistingPatterns.remove(patternToEdit);
71                 }
72
73                 IWorkspaceRoot root = proj.getWorkspace().getRoot();
74                 IResource res = root.findMember(path);
75                 if (res instanceof IContainer) {
76                         fCurrSourceFolder = (IContainer) res;
77                 }
78
79                 fExclusionPatternStatus = new StatusInfo();
80
81                 String label = NLS.bind(CPathEntryMessages.ExclusionPatternEntryDialog_pattern_label, 
82                                 path.makeRelative().toString());
83
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);
90
91                 if (patternToEdit == null) {
92                         fExclusionPatternDialog.setText(""); //$NON-NLS-1$
93                 } else {
94                         fExclusionPatternDialog.setText(patternToEdit.toString());
95                 }
96
97                 setHelpAvailable(false);
98         }
99
100         @Override
101         protected Control createDialogArea(Composite parent) {
102                 Composite composite = (Composite) super.createDialogArea(parent);
103
104                 int widthHint = convertWidthInCharsToPixels(60);
105
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);
112
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);
119
120                 fExclusionPatternDialog.doFillIntoGrid(inner, 3);
121
122                 LayoutUtil.setWidthHint(fExclusionPatternDialog.getLabelControl(null), widthHint);
123                 LayoutUtil.setHorizontalSpan(fExclusionPatternDialog.getLabelControl(null), 2);
124
125                 LayoutUtil.setWidthHint(fExclusionPatternDialog.getTextControl(null), widthHint);
126                 LayoutUtil.setHorizontalGrabbing(fExclusionPatternDialog.getTextControl(null), true);
127
128                 fExclusionPatternDialog.postSetFocusOnDialogField(parent.getDisplay());
129                 applyDialogFont(composite);
130                 return composite;
131         }
132
133         // -------- ExclusionPatternAdapter --------
134
135         private class ExPatternAdapter implements IDialogFieldListener, IStringButtonAdapter {
136
137                 // -------- IDialogFieldListener
138
139                 public void dialogFieldChanged(DialogField field) {
140                         doStatusLineUpdate();
141                 }
142
143                 public void changeControlPressed(DialogField field) {
144                         doChangeControlPressed();
145                 }
146         }
147
148         protected void doChangeControlPressed() {
149                 IPath pattern = chooseExclusionPattern();
150                 if (pattern != null) {
151                         fExclusionPatternDialog.setText(pattern.toString());
152                 }
153         }
154
155         protected void doStatusLineUpdate() {
156                 checkIfPatternValid();
157                 updateStatus(fExclusionPatternStatus);
158         }
159
160         protected void checkIfPatternValid() {
161                 String pattern = fExclusionPatternDialog.getText().trim();
162                 if (pattern.length() == 0) {
163                         fExclusionPatternStatus.setError(CPathEntryMessages.ExclusionPatternEntryDialog_error_empty); 
164                         return;
165                 }
166                 IPath path = new Path(pattern);
167                 if (path.isAbsolute() || path.getDevice() != null) {
168                         fExclusionPatternStatus.setError(CPathEntryMessages.ExclusionPatternEntryDialog_error_notrelative); 
169                         return;
170                 }
171                 if (fExistingPatterns.contains(pattern)) {
172                         fExclusionPatternStatus.setError(CPathEntryMessages.ExclusionPatternEntryDialog_error_exists); 
173                         return;
174                 }
175
176                 fExclusionPattern = pattern;
177                 fExclusionPatternStatus.setOK();
178         }
179
180         public String getExclusionPattern() {
181                 return fExclusionPattern;
182         }
183
184         /*
185          * @see org.eclipse.jface.window.Window#configureShell(Shell)
186          */
187         @Override
188         protected void configureShell(Shell newShell) {
189                 super.configureShell(newShell);
190                 //              WorkbenchHelp.setHelp(newShell,
191                 // ICHelpContextIds.EXCLUSION_PATTERN_DIALOG);
192         }
193
194         // ---------- util method ------------
195
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);
200
201                 ILabelProvider lp = new WorkbenchLabelProvider();
202                 ITreeContentProvider cp = new WorkbenchContentProvider();
203
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));
210                         if (elem != null) {
211                                 initialElement = elem;
212                         }
213                         if (elem instanceof IContainer) {
214                                 curr = (IContainer) elem;
215                         } else {
216                                 break;
217                         }
218                 }
219
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));
228
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();
234                         }
235                         return path;
236                 }
237                 return null;
238         }
239 }