-
Notifications
You must be signed in to change notification settings - Fork 56
/
ApiErrorsWarningsPreferencePage.java
217 lines (196 loc) · 7.33 KB
/
ApiErrorsWarningsPreferencePage.java
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
/*******************************************************************************
* Copyright (c) 2007, 2018 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.pde.api.tools.ui.internal.preferences;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.jdt.core.IJavaProject;
import org.eclipse.jdt.core.JavaCore;
import org.eclipse.jdt.core.JavaModelException;
import org.eclipse.jface.dialogs.IDialogConstants;
import org.eclipse.jface.preference.PreferencePage;
import org.eclipse.pde.api.tools.internal.provisional.ApiPlugin;
import org.eclipse.pde.api.tools.ui.internal.ApiUIPlugin;
import org.eclipse.pde.api.tools.ui.internal.IApiToolsConstants;
import org.eclipse.pde.api.tools.ui.internal.IApiToolsHelpContextIds;
import org.eclipse.pde.api.tools.ui.internal.SWTFactory;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionListener;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Link;
import org.eclipse.ui.IWorkbench;
import org.eclipse.ui.IWorkbenchPreferencePage;
import org.eclipse.ui.PlatformUI;
import org.eclipse.ui.dialogs.PreferencesUtil;
import org.eclipse.ui.preferences.IWorkbenchPreferenceContainer;
/**
* Class provides the general API tool preference page
*
* @since 1.0.0
*/
public class ApiErrorsWarningsPreferencePage extends PreferencePage implements IWorkbenchPreferencePage {
public static final String DATA_SELECT_OPTION_KEY = "select_option_key"; //$NON-NLS-1$
public static final String DATA_SELECT_OPTION_QUALIFIER = "select_option_qualifier"; //$NON-NLS-1$
/**
* Key for a Boolean value defining if 'use project specific settings' should be
* enabled or not.
*/
public static final String USE_PROJECT_SPECIFIC_OPTIONS = "use_project_specific_key"; //$NON-NLS-1$
public static final String ID = ApiUIPlugin.PLUGIN_ID + ".apitools.errorwarnings.prefpage"; //$NON-NLS-1$
/**
* Id of a setting in the data map applied when the page is opened. Value
* must be a Boolean object. If true, the customize project settings link
* will be hidden.
*/
public static final String NO_LINK = "PropertyAndPreferencePage.nolink"; //$NON-NLS-1$
/**
* Id of a setting in the data map applied when the page is opened. Value
* must be an Integer object with a value match the id of a tab on the page
* See constants
* {@link ApiErrorsWarningsConfigurationBlock#API_USE_SCANS_PAGE_ID},
* {@link ApiErrorsWarningsConfigurationBlock#COMPATIBILITY_PAGE_ID},
* {@link ApiErrorsWarningsConfigurationBlock#VERSION_MANAGEMENT_PAGE_ID},
* {@link ApiErrorsWarningsConfigurationBlock#API_COMPONENT_RESOLUTION_PAGE_ID}
* and {@link ApiErrorsWarningsConfigurationBlock#API_USE_SCANS_PAGE_ID} If
* an id is provided, the preference page will open with the specified tab
* visible
*/
public static final String INITIAL_TAB = "PropertyAndPreferencePage.initialTab"; //$NON-NLS-1$
/**
* The main configuration block for the page
*/
ApiErrorsWarningsConfigurationBlock block = null;
private Link link = null;
/**
* Since {@link #applyData(Object)} can be called before createContents,
* store the data
*/
private Map<String, Object> fPageData = null;
/**
* Constructor
*/
public ApiErrorsWarningsPreferencePage() {
super(PreferenceMessages.ApiErrorsWarningsPreferencePage_0);
}
@Override
protected Control createContents(Composite parent) {
Composite comp = SWTFactory.createComposite(parent, 1, 1, GridData.FILL_BOTH, 0, 0);
link = new Link(comp, SWT.NONE);
link.setLayoutData(new GridData(GridData.END, GridData.CENTER, true, false));
link.setFont(comp.getFont());
link.setText(PreferenceMessages.ApiErrorsWarningsPreferencePage_1);
link.addSelectionListener(SelectionListener.widgetSelectedAdapter(e -> {
Set<IJavaProject> set = new HashSet<>();
try {
IJavaProject[] projects = JavaCore.create(ResourcesPlugin.getWorkspace().getRoot()).getJavaProjects();
IProject project = null;
for (IJavaProject p : projects) {
project = p.getProject();
try {
if (project.hasNature(ApiPlugin.NATURE_ID) && block.hasProjectSpecificSettings(project)) {
set.add(p);
}
} catch (CoreException ce) {
// do nothing ignore the project
}
}
} catch (JavaModelException jme) {
// ignore
}
ProjectSelectionDialog psd = new ProjectSelectionDialog(getShell(), set);
if (psd.open() == IDialogConstants.OK_ID) {
Map<String, Boolean> data = new HashMap<>();
data.put(NO_LINK, Boolean.TRUE);
PreferencesUtil.createPropertyDialogOn(getShell(), ((IJavaProject) psd.getFirstResult()).getProject(),
IApiToolsConstants.ID_ERRORS_WARNINGS_PROP_PAGE,
new String[] { IApiToolsConstants.ID_ERRORS_WARNINGS_PROP_PAGE }, data).open();
}
}));
block = new ApiErrorsWarningsConfigurationBlock(null, (IWorkbenchPreferenceContainer) getContainer());
block.createControl(comp);
// Initialize with data map in case applyData was called before
// createContents
applyData(fPageData);
PlatformUI.getWorkbench().getHelpSystem().setHelp(parent, IApiToolsHelpContextIds.APITOOLS_ERROR_WARNING_PREF_PAGE);
return comp;
}
@Override
public void dispose() {
if (block != null) {
block.dispose();
}
super.dispose();
}
@Override
public void init(IWorkbench workbench) {
}
@Override
public boolean performCancel() {
block.performCancel();
return super.performCancel();
}
@Override
public boolean performOk() {
block.performOK();
return super.performOk();
}
@Override
protected void performApply() {
block.performApply();
super.performApply();
}
@Override
protected void performDefaults() {
block.performDefaults();
super.performDefaults();
}
@SuppressWarnings("unchecked")
@Override
public void applyData(Object data) {
if (data instanceof Map) {
fPageData = (Map<String, Object>) data;
if (link != null && fPageData.containsKey(NO_LINK)) {
link.setVisible(!Boolean.TRUE.equals(fPageData.get(NO_LINK)));
}
if (block != null && fPageData.containsKey(INITIAL_TAB)) {
Integer tabIndex = (Integer) fPageData.get(INITIAL_TAB);
if (tabIndex != null) {
try {
block.selectTab(tabIndex.intValue());
} catch (NumberFormatException e) {
// Page was called with bad data, just ignore
}
}
}
if (block == null) {
return;
}
Integer tab = (Integer) fPageData.get(ApiErrorsWarningsPreferencePage.INITIAL_TAB);
if (tab != null) {
block.selectTab(tab.intValue());
}
Object key = fPageData.get(ApiErrorsWarningsPreferencePage.DATA_SELECT_OPTION_KEY);
Object qualifier = fPageData.get(ApiErrorsWarningsPreferencePage.DATA_SELECT_OPTION_QUALIFIER);
if (key instanceof String && qualifier instanceof String) {
block.selectOption((String) key, (String) qualifier);
}
}
}
}