1 /*******************************************************************************
2  * Copyright (c) 2003, 2019 IBM Corporation and others.
3  *
4  * This program and the accompanying materials
5  * are made available under the terms of the Eclipse Public License 2.0
6  * which accompanies this distribution, and is available at
7  * https://www.eclipse.org/legal/epl-2.0/
8  *
9  * SPDX-License-Identifier: EPL-2.0
10  *
11  * Contributors:
12  * IBM Corporation - initial API and implementation
13  *******************************************************************************/
14 package org.eclipse.ui.internal.navigator.filters;
15 
16 import java.util.ArrayList;
17 import java.util.List;
18 import java.util.Set;
19 
20 import org.eclipse.jface.dialogs.TrayDialog;
21 import org.eclipse.jface.resource.ColorRegistry;
22 import org.eclipse.jface.resource.JFaceResources;
23 import org.eclipse.jface.viewers.ISelectionChangedListener;
24 import org.eclipse.swt.SWT;
25 import org.eclipse.swt.custom.CTabFolder;
26 import org.eclipse.swt.custom.CTabItem;
27 import org.eclipse.swt.events.SelectionAdapter;
28 import org.eclipse.swt.events.SelectionEvent;
29 import org.eclipse.swt.graphics.Color;
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.TableItem;
36 import org.eclipse.ui.PlatformUI;
37 import org.eclipse.ui.internal.navigator.CommonNavigatorMessages;
38 import org.eclipse.ui.internal.navigator.NavigatorPlugin;
39 import org.eclipse.ui.navigator.CommonViewer;
40 import org.eclipse.ui.navigator.ICommonFilterDescriptor;
41 import org.eclipse.ui.navigator.INavigatorContentDescriptor;
42 import org.eclipse.ui.navigator.INavigatorContentService;
43 import org.eclipse.ui.navigator.INavigatorViewerDescriptor;
44 
45 /**
46  *
47  * @since 3.2
48  *
49  */
50 public class CommonFilterSelectionDialog extends TrayDialog {
51 
52 	private static final String FILTER_ICON = "icons/full/elcl16/filter_ps.png"; //$NON-NLS-1$
53 	private static final String CONTENT_ICON = "icons/full/elcl16/content.png"; //$NON-NLS-1$
54 
55 	private static final int TAB_WIDTH_IN_DLUS = 300;
56 
57 	private static final int TAB_HEIGHT_IN_DLUS = 150;
58 
59 	private final CommonViewer commonViewer;
60 
61 	private final INavigatorContentService contentService;
62 
63 	private CTabFolder customizationsTabFolder;
64 
65 	private CommonFiltersTab commonFiltersTab;
66 
67 	private ContentExtensionsTab contentExtensionsTab;
68 
69 	private Label descriptionText;
70 
71 	private ISelectionChangedListener updateDescriptionSelectionListener;
72 
73 	private String helpContext;
74 
75 	private UserFiltersTab userFiltersTab;
76 
77 	/**
78 	 * Public only for tests.
79 	 *
80 	 * @param aCommonViewer
81 	 */
CommonFilterSelectionDialog(CommonViewer aCommonViewer)82 	public CommonFilterSelectionDialog(CommonViewer aCommonViewer) {
83 		super(aCommonViewer.getControl().getShell());
84 		setShellStyle(SWT.RESIZE | getShellStyle());
85 
86 		commonViewer = aCommonViewer;
87 		contentService = commonViewer.getNavigatorContentService();
88 
89 		INavigatorViewerDescriptor viewerDescriptor = contentService.getViewerDescriptor();
90 		helpContext = viewerDescriptor
91 				.getStringConfigProperty(INavigatorViewerDescriptor.PROP_CUSTOMIZE_VIEW_DIALOG_HELP_CONTEXT);
92 
93 		if (helpContext != null) {
94 			PlatformUI.getWorkbench().getHelpSystem().setHelp(
95 					aCommonViewer.getControl().getShell(), helpContext);
96 		}
97 	}
98 
99 	@Override
isHelpAvailable()100 	public boolean isHelpAvailable() {
101 		return helpContext != null;
102 	}
103 
104 	@Override
createDialogArea(Composite parent)105 	protected Control createDialogArea(Composite parent) {
106 
107 		getShell()
108 				.setText(
109 						CommonNavigatorMessages.CommonFilterSelectionDialog_Available_customization_);
110 
111 
112 		Composite superComposite = (Composite) super.createDialogArea(parent);
113 
114 		createCustomizationsTabFolder(superComposite);
115 
116 		commonFiltersTab = new CommonFiltersTab(customizationsTabFolder,
117 				contentService);
118 		createTabItem(
119 				customizationsTabFolder,
120 				CommonNavigatorMessages.CommonFilterSelectionDialog_Available_Filters,
121 				commonFiltersTab, FILTER_ICON);
122 
123 		if (contentService.getViewerDescriptor()
124 				.isVisibleContentExtension("org.eclipse.ui.navigator.resourceContent")) { //$NON-NLS-1$
125 			this.userFiltersTab = new UserFiltersTab(customizationsTabFolder, this.commonViewer);
126 			createTabItem(customizationsTabFolder,
127 					CommonNavigatorMessages.CommonFilterSelectionDialog_User_Resource_Filters, userFiltersTab,
128 					FILTER_ICON);
129 		}
130 
131 		boolean hideExtensionsTab = contentService.getViewerDescriptor()
132 				.getBooleanConfigProperty(
133 						INavigatorViewerDescriptor.PROP_HIDE_AVAILABLE_EXT_TAB);
134 
135 		if (!hideExtensionsTab) {
136 			contentExtensionsTab = new ContentExtensionsTab(
137 					customizationsTabFolder, contentService);
138 
139 			createTabItem(
140 					customizationsTabFolder,
141 					CommonNavigatorMessages.CommonFilterSelectionDialog_Available_Content,
142 					contentExtensionsTab, CONTENT_ICON);
143 
144 		}
145 
146 		createDescriptionText(superComposite);
147 
148 		if (commonFiltersTab != null) {
149 			commonFiltersTab.addSelectionChangedListener(getSelectionListener());
150 		}
151 
152 		if (contentExtensionsTab != null) {
153 			contentExtensionsTab
154 					.addSelectionChangedListener(getSelectionListener());
155 		}
156 
157 		commonFiltersTab.setInitialFocus();
158 
159 		return customizationsTabFolder;
160 	}
161 
createCustomizationsTabFolder(Composite superComposite)162 	private void createCustomizationsTabFolder(Composite superComposite) {
163 		customizationsTabFolder = new CTabFolder (superComposite, SWT.RESIZE | SWT.BORDER);
164 
165 		GridData gd = new GridData(SWT.FILL, SWT.FILL, true, true);
166 		gd.widthHint = convertHorizontalDLUsToPixels(TAB_WIDTH_IN_DLUS);
167 		gd.heightHint = convertVerticalDLUsToPixels(TAB_HEIGHT_IN_DLUS);
168 
169 		customizationsTabFolder.setLayout(new GridLayout());
170 		customizationsTabFolder.setLayoutData(gd);
171 
172 		customizationsTabFolder.setFont(superComposite.getFont());
173 
174 		customizationsTabFolder.addSelectionListener(new SelectionAdapter() {
175 
176 			@Override
177 			public void widgetSelected(SelectionEvent e) {
178 				if (descriptionText != null) {
179 					descriptionText.setText(""); //$NON-NLS-1$
180 				}
181 			}
182 
183 
184 		});
185 
186 		customize();
187 
188 	}
189 
customize()190 	private void customize() {
191 		ColorRegistry reg = JFaceResources.getColorRegistry();
192 		Color c1 = reg.get("org.eclipse.ui.workbench.ACTIVE_TAB_BG_START"), //$NON-NLS-1$
193 		c2 = reg.get("org.eclipse.ui.workbench.ACTIVE_TAB_BG_END"); //$NON-NLS-1$
194 		customizationsTabFolder.setSelectionBackground(new Color[] {c1, c2},	new int[] {100}, true);
195 		customizationsTabFolder.setSelectionForeground(reg.get("org.eclipse.ui.workbench.ACTIVE_TAB_TEXT_COLOR")); //$NON-NLS-1$
196 		customizationsTabFolder.setSimple(true);
197 	}
198 
createTabItem(CTabFolder aTabFolder, String label, Composite composite, String imageKey)199 	private CTabItem createTabItem(CTabFolder aTabFolder, String label,
200 			Composite composite, String imageKey) {
201 		CTabItem extensionsTabItem = new CTabItem(aTabFolder, SWT.BORDER);
202 		extensionsTabItem.setText(label);
203 		extensionsTabItem.setControl(composite);
204 		extensionsTabItem.setImage(NavigatorPlugin.getDefault().getImage(imageKey));
205 		return extensionsTabItem;
206 	}
207 
createDescriptionText(Composite composite)208 	private void createDescriptionText(Composite composite) {
209 
210 		descriptionText = new Label(composite, SWT.WRAP);
211 		descriptionText.setFont(composite.getFont());
212 		descriptionText.setBackground(composite.getBackground());
213 		GridData descriptionTextGridData = new GridData(GridData.FILL, GridData.BEGINNING, true, false);
214 		descriptionTextGridData.heightHint = convertHeightInCharsToPixels(3);
215 		descriptionText.setLayoutData(descriptionTextGridData);
216 	}
217 
getSelectionListener()218 	private ISelectionChangedListener getSelectionListener() {
219 		if (updateDescriptionSelectionListener == null) {
220 			updateDescriptionSelectionListener = new FilterDialogSelectionListener(
221 					descriptionText);
222 		}
223 		return updateDescriptionSelectionListener;
224 	}
225 
226 	@Override
okPressed()227 	protected void okPressed() {
228 
229 		if (contentExtensionsTab != null) {
230 			List<String> checkedExtensions = new ArrayList<>();
231 			TableItem[] tableItems = contentExtensionsTab.getTable().getItems();
232 			INavigatorContentDescriptor descriptor;
233 			for (TableItem tableItem : tableItems) {
234 				descriptor = (INavigatorContentDescriptor) tableItem
235 						.getData();
236 
237 				if (tableItem.getChecked()) {
238 					checkedExtensions.add(descriptor.getId());
239 				}
240 			}
241 			String[] contentExtensionIdsToActivate = checkedExtensions
242 					.toArray(new String[checkedExtensions.size()]);
243 			UpdateActiveExtensionsOperation updateExtensions = new UpdateActiveExtensionsOperation(
244 					commonViewer, contentExtensionIdsToActivate);
245 			updateExtensions.execute(null, null);
246 		}
247 
248 		List<String> filterIdsToActivate = new ArrayList<>();
249 		if (commonFiltersTab != null) {
250 			Set<ICommonFilterDescriptor> checkedFilters = commonFiltersTab.getCheckedItems();
251 			for (ICommonFilterDescriptor descriptor : checkedFilters) {
252 				filterIdsToActivate.add(descriptor.getId());
253 			}
254 		}
255 		if (this.userFiltersTab != null) {
256 			this.commonViewer.setData(NavigatorPlugin.RESOURCE_REGEXP_FILTER_DATA, this.userFiltersTab.getUserFilters());
257 			if (!this.userFiltersTab.getUserFilters().isEmpty()) {
258 				filterIdsToActivate.add(NavigatorPlugin.RESOURCE_REGEXP_FILTER_FILTER_ID);
259 			}
260 		}
261 		if (this.userFiltersTab != null || this.commonFiltersTab != null) {
262 			UpdateActiveFiltersOperation updateFilters = new UpdateActiveFiltersOperation(
263 					commonViewer, filterIdsToActivate.toArray(new String[filterIdsToActivate.size()]));
264 			updateFilters.execute(null, null);
265 		}
266 
267 		super.okPressed();
268 	}
269 
getFilterDescriptorChangeHistory()270 	protected ICommonFilterDescriptor[] getFilterDescriptorChangeHistory() {
271 		if (commonFiltersTab != null) {
272 			return commonFiltersTab.getFilterDescriptorChangeHistory();
273 		}
274 		return new ICommonFilterDescriptor[0];
275 	}
276 }
277