Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 48ff3a9a46ce935b2e1907af0cb5af5a619324e1 (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
/*******************************************************************************
 * Copyright (c) 2011, 2012 IBM Corporation 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:
 *     IBM Corporation - initial API and implementation
 *******************************************************************************/

package org.eclipse.cdt.internal.ui.resources;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedHashMap;

import org.eclipse.cdt.core.resources.ExclusionInstance;
import org.eclipse.cdt.core.resources.ExclusionType;
import org.eclipse.cdt.core.resources.RefreshExclusion;
import org.eclipse.cdt.internal.core.resources.ResourceExclusion;
import org.eclipse.cdt.ui.resources.Messages;
import org.eclipse.cdt.ui.resources.RefreshExclusionContributor;
import org.eclipse.core.resources.IContainer;
import org.eclipse.core.resources.IFolder;
import org.eclipse.core.resources.IResource;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IPath;
import org.eclipse.jface.viewers.ITreeContentProvider;
import org.eclipse.jface.viewers.Viewer;
import org.eclipse.jface.window.Window;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Group;
import org.eclipse.swt.widgets.List;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.ui.dialogs.CheckedTreeSelectionDialog;
import org.eclipse.ui.model.WorkbenchLabelProvider;

/**
 * <strong>EXPERIMENTAL</strong>. This class or interface has been added as part of a work in progress. There
 * is no guarantee that this API will work or that it will remain the same. Please do not use this API without
 * consulting with the CDT team.
 *
 * @author vkong
 * @since 5.3
 *
 */
public class ResourceExclusionContributor extends RefreshExclusionContributor {

	@Override
	public RefreshExclusion createExclusion() {
		ResourceExclusion newExclusion = new ResourceExclusion();
		newExclusion.setContributorId(getID());

		// TODO change this for Phase 2
		newExclusion.setExclusionType(ExclusionType.FOLDER);
		return newExclusion;
	}

	@Override
	public void createProperiesUI(Composite parent, final RefreshExclusion exclusion) {
		final Shell shell = parent.getShell();

		Group g = new Group(parent, SWT.NONE);
		g.setText(Messages.RefreshPolicyExceptionDialog_exceptionTypeResources);
		g.setLayout(new GridLayout(1, false));
		GridData gridData = new GridData(GridData.FILL_HORIZONTAL | GridData.FILL_VERTICAL);
		gridData.verticalAlignment = GridData.FILL;
		gridData.horizontalSpan = 2;
		gridData.verticalSpan = 2;
		g.setLayoutData(gridData);

		final List exceptionsList = new List(g, SWT.NONE);
		gridData = new GridData();
		gridData.verticalAlignment = GridData.FILL;
		gridData.verticalSpan = 2;
		gridData.grabExcessHorizontalSpace = true;
		gridData.grabExcessVerticalSpace = true;
		gridData.minimumHeight = 250;
		gridData.minimumWidth = 275;
		exceptionsList.setLayoutData(gridData);

		final HashMap<String, ExclusionInstance> exclusionInstanceStrings = new LinkedHashMap<>();
		final HashMap<String, Object> exclusionInstanceResources = new LinkedHashMap<>();

		java.util.List<ExclusionInstance> exclusionInstances = exclusion.getExclusionInstances();

		// populate exclusion instance list
		if (exclusionInstances != null) {
			Iterator<ExclusionInstance> iterator = exclusionInstances.iterator();
			while (iterator.hasNext()) {
				ExclusionInstance exclusionInstance = iterator.next();
				String name = exclusionInstance.getDisplayString();
				exceptionsList.add(name);
				exclusionInstanceStrings.put(name, exclusionInstance);
				exclusionInstanceResources.put(name, exclusionInstance.getResource());
			}
		}

		Composite buttonComp = new Composite(parent, SWT.NONE);
		buttonComp.setLayout(new GridLayout(1, false));
		gridData = new GridData(GridData.FILL_HORIZONTAL | GridData.FILL_VERTICAL);
		gridData.minimumWidth = 100;
		buttonComp.setLayoutData(gridData);

		Button addButton = new Button(buttonComp, SWT.NONE);
		gridData = new GridData();
		gridData.horizontalAlignment = GridData.FILL;
		gridData.grabExcessHorizontalSpace = true;
		addButton.setLayoutData(gridData);
		addButton.setText(Messages.RefreshPolicyExceptionDialog_addButtonLabel);

		addButton.addSelectionListener(new SelectionAdapter() {

			/*
			 * (non-Javadoc)
			 *
			 * @see
			 * org.eclipse.swt.events.SelectionAdapter#widgetSelected(org.eclipse.swt.events.SelectionEvent)
			 */
			@Override
			public void widgetSelected(SelectionEvent e) {
				CheckedTreeSelectionDialog dialog = new CheckedTreeSelectionDialog(shell,
						WorkbenchLabelProvider.getDecoratingWorkbenchLabelProvider(), new ITreeContentProvider() {

							@Override
							public void dispose() {
							}

							@Override
							public Object[] getChildren(Object parentElement) {
								if (parentElement instanceof IContainer) {
									IContainer container = (IContainer) parentElement;
									if (container.isAccessible()) {
										try {
											java.util.List<IResource> children = new ArrayList<>();
											IResource[] members = container.members();
											for (int i = 0; i < members.length; i++) {
												if (members[i].getType() == IResource.FOLDER) {
													children.add(members[i]);
												}
											}
											return children.toArray();
										} catch (CoreException e) {
											// this should never happen because we call #isAccessible before
											// invoking #members
										}
									}
								}
								return new Object[0];
							}

							@Override
							public Object[] getElements(Object inputElement) {
								return getChildren(inputElement);
							}

							@Override
							public Object getParent(Object element) {
								if (element instanceof IResource) {
									return ((IResource) element).getParent();
								}
								return null;
							}

							@Override
							public boolean hasChildren(Object element) {
								return getChildren(element).length > 0;
							}

							@Override
							public void inputChanged(Viewer viewer, Object oldInput, Object newInput) {
							}
						});

				dialog.setInput(getResourceRoot(exclusion));

				if (exclusionInstanceResources.values().size() > 0) {
					dialog.setInitialElementSelections(Arrays.asList(exclusionInstanceResources.values().toArray()));
				}
				dialog.setMessage(Messages.RefreshPolicyExceptionDialog_SelectResourceDialogMessage);
				dialog.setTitle(Messages.RefreshPolicyExceptionDialog_SelectResourceDialogTitle);

				if (dialog.open() == Window.OK) {
					Object[] selection = dialog.getResult();
					exceptionsList.removeAll();
					exclusionInstanceResources.clear();
					final HashMap<String, ExclusionInstance> oldExclusionInstanceStrings = new LinkedHashMap<>(
							exclusionInstanceStrings);
					exclusionInstanceStrings.clear();

					for (int i = 0; i < selection.length; i++) {
						Object selected = selection[i];
						if (selected instanceof IFolder) {
							IPath path = ((IFolder) selected).getFullPath();
							IPath relativePath = path.makeRelativeTo(getResourceRoot(exclusion).getFullPath());

							exceptionsList.add(relativePath.toString());
							ExclusionInstance instance = oldExclusionInstanceStrings.get(relativePath.toString());
							if (instance == null) {
								instance = new ExclusionInstance();
								instance.setExclusionType(ExclusionType.FOLDER);
								instance.setParentExclusion(exclusion);
								instance.setResource((IResource) selected);
								instance.setDisplayString(relativePath.toString());
								exclusion.addExclusionInstance(instance);
							} else {
								oldExclusionInstanceStrings.remove(relativePath.toString());
							}

							exclusionInstanceStrings.put(instance.getDisplayString(), instance);
							exclusionInstanceResources.put(instance.getDisplayString(), selected);
						}
					}
					// remove deprecated exclusion instances
					oldExclusionInstanceStrings.keySet();
					Iterator<String> iterator = oldExclusionInstanceStrings.keySet().iterator();
					while (iterator.hasNext()) {
						String toRemove = iterator.next();
						ExclusionInstance instanceToRemove = oldExclusionInstanceStrings.get(toRemove);
						exclusion.removeExclusionInstance(instanceToRemove);
						iterator.remove();
					}
				}
			}

		});

		Button deleteButton = new Button(buttonComp, SWT.NONE);
		gridData = new GridData();
		gridData.horizontalAlignment = GridData.FILL;
		gridData.grabExcessHorizontalSpace = true;
		deleteButton.setLayoutData(gridData);
		deleteButton.setText(Messages.RefreshPolicyExceptionDialog_deleteButtonLabel);

		deleteButton.addSelectionListener(new SelectionAdapter() {

			/*
			 * (non-Javadoc)
			 *
			 * @see
			 * org.eclipse.swt.events.SelectionAdapter#widgetSelected(org.eclipse.swt.events.SelectionEvent)
			 */
			@Override
			public void widgetSelected(SelectionEvent e) {
				String[] selected = exceptionsList.getSelection();
				if (selected.length < 1)
					return;

				for (int i = 0; i < selected.length; i++) {
					String folderToRemove = selected[i];
					ExclusionInstance instanceToRemove = exclusionInstanceStrings.get(folderToRemove);

					// Iterator<Object> iterator = selectedFolders.iterator();

					// while (iterator.hasNext()) {
					// IPath path = ((IFolder)iterator.next()).getFullPath();
					// IPath relativePath = path.makeRelativeTo(getResourceRoot(exclusionRoot).getFullPath());
					// if (relativePath.toString().compareTo(folderToRemove) == 0) {
					// iterator.remove();
					// break;
					// }
					// }
					exclusion.removeExclusionInstance(instanceToRemove);
					exclusionInstanceStrings.remove(folderToRemove);
					exclusionInstanceResources.remove(folderToRemove);
				}
				exceptionsList.remove(exceptionsList.getSelectionIndices());
			}

		});
	}

	private IResource getResourceRoot(RefreshExclusion exclusion) {
		if (exclusion.getParentExclusion() != null) {
			return getResourceRoot(exclusion.getParentExclusion());
		}
		return exclusion.getParentResource();
	}
}

Back to the top