]>
Commit | Line | Data |
---|---|---|
1b2798f6 EK |
1 | /******************************************************************************* |
2 | * Copyright (c) 2000, 2011 IBM Corporation 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 | * IBM Corporation - initial API and implementation | |
10 | *******************************************************************************/ | |
11 | package org.eclipse.jdt.internal.ui.wizards.buildpaths; | |
12 | ||
13 | import java.util.ArrayList; | |
14 | import java.util.HashMap; | |
15 | import java.util.List; | |
16 | ||
17 | import org.eclipse.swt.SWT; | |
18 | import org.eclipse.swt.graphics.GC; | |
19 | import org.eclipse.swt.layout.GridData; | |
20 | import org.eclipse.swt.layout.GridLayout; | |
21 | import org.eclipse.swt.widgets.Composite; | |
22 | import org.eclipse.swt.widgets.Control; | |
23 | import org.eclipse.swt.widgets.Label; | |
24 | import org.eclipse.swt.widgets.Shell; | |
25 | ||
26 | import org.eclipse.jface.dialogs.MessageDialog; | |
27 | import org.eclipse.jface.dialogs.StatusDialog; | |
28 | import org.eclipse.jface.viewers.ColumnLayoutData; | |
29 | import org.eclipse.jface.viewers.ColumnPixelData; | |
30 | import org.eclipse.jface.viewers.ColumnWeightData; | |
31 | ||
32 | import org.eclipse.ui.PlatformUI; | |
33 | import org.eclipse.ui.forms.events.HyperlinkAdapter; | |
34 | import org.eclipse.ui.forms.events.HyperlinkEvent; | |
35 | import org.eclipse.ui.forms.widgets.FormText; | |
36 | import org.eclipse.ui.forms.widgets.FormToolkit; | |
37 | import org.eclipse.ui.preferences.IWorkbenchPreferenceContainer; | |
38 | ||
39 | import org.eclipse.jdt.core.IAccessRule; | |
40 | import org.eclipse.jdt.core.IClasspathEntry; | |
41 | import org.eclipse.jdt.core.IJavaProject; | |
42 | import org.eclipse.jdt.core.JavaCore; | |
43 | ||
44 | import org.eclipse.jdt.internal.corext.util.Messages; | |
45 | ||
46 | import org.eclipse.jdt.internal.ui.IJavaHelpContextIds; | |
47 | import org.eclipse.jdt.internal.ui.JavaPlugin; | |
48 | import org.eclipse.jdt.internal.ui.preferences.ProblemSeveritiesPreferencePage; | |
49 | import org.eclipse.jdt.internal.ui.wizards.NewWizardMessages; | |
50 | import org.eclipse.jdt.internal.ui.wizards.dialogfields.DialogField; | |
51 | import org.eclipse.jdt.internal.ui.wizards.dialogfields.IDialogFieldListener; | |
52 | import org.eclipse.jdt.internal.ui.wizards.dialogfields.IListAdapter; | |
53 | import org.eclipse.jdt.internal.ui.wizards.dialogfields.ListDialogField; | |
54 | import org.eclipse.jdt.internal.ui.wizards.dialogfields.SelectionButtonDialogField; | |
55 | ||
56 | public class AccessRulesDialog extends StatusDialog { | |
57 | ||
58 | public static final int SWITCH_PAGE= 10; | |
59 | ||
60 | public final ListDialogField<IAccessRule> fAccessRulesList; | |
61 | public final SelectionButtonDialogField fCombineRulesCheckbox; | |
62 | public final CPListElement fCurrElement; | |
63 | ||
64 | private final IJavaProject fProject; | |
65 | private final boolean fParentCanSwitchPage; | |
66 | ||
67 | public static final int IDX_ADD= 0; | |
68 | public static final int IDX_EDIT= 1; | |
69 | private static final int IDX_UP= 3; | |
70 | private static final int IDX_DOWN= 4; | |
71 | private static final int IDX_REMOVE= 6; | |
72 | ||
73 | ||
74 | public AccessRulesDialog(Shell parent, CPListElement entryToEdit, IJavaProject project, boolean parentCanSwitchPage) { | |
75 | super(parent); | |
76 | ||
77 | fCurrElement= entryToEdit; | |
78 | fProject= project; // can be null | |
79 | ||
80 | setTitle(NewWizardMessages.AccessRulesDialog_title); | |
81 | ||
82 | fAccessRulesList= createListContents(entryToEdit); | |
83 | ||
84 | fCombineRulesCheckbox= new SelectionButtonDialogField(SWT.CHECK); | |
85 | fCombineRulesCheckbox.generated_8271091329551797055(entryToEdit); | |
86 | ||
87 | fParentCanSwitchPage= parentCanSwitchPage; | |
88 | } | |
89 | ||
90 | /* | |
91 | * @see org.eclipse.jface.dialogs.Dialog#isResizable() | |
92 | * @since 3.4 | |
93 | */ | |
94 | @Override | |
95 | protected boolean isResizable() { | |
96 | return true; | |
97 | } | |
98 | ||
99 | private ListDialogField<IAccessRule> createListContents(CPListElement entryToEdit) { | |
100 | String label= NewWizardMessages.AccessRulesDialog_rules_label; | |
101 | String[] buttonLabels= new String[] { | |
102 | NewWizardMessages.AccessRulesDialog_rules_add, | |
103 | NewWizardMessages.AccessRulesDialog_rules_edit, | |
104 | null, | |
105 | NewWizardMessages.AccessRulesDialog_rules_up, | |
106 | NewWizardMessages.AccessRulesDialog_rules_down, | |
107 | null, | |
108 | NewWizardMessages.AccessRulesDialog_rules_remove | |
109 | }; | |
110 | ||
111 | TypeRestrictionAdapter adapter= new TypeRestrictionAdapter(); | |
112 | AccessRulesLabelProvider labelProvider= new AccessRulesLabelProvider(); | |
113 | ||
114 | ListDialogField<IAccessRule> patternList= new ListDialogField<IAccessRule>(adapter, buttonLabels, labelProvider); | |
115 | patternList.setDialogFieldListener(adapter); | |
116 | ||
117 | patternList.setLabelText(label); | |
118 | patternList.setRemoveButtonIndex(IDX_REMOVE); | |
119 | patternList.setUpButtonIndex(IDX_UP); | |
120 | patternList.setDownButtonIndex(IDX_DOWN); | |
121 | patternList.enableButton(IDX_EDIT, false); | |
122 | ||
123 | IAccessRule[] rules= (IAccessRule[]) entryToEdit.getAttribute(CPListElement.ACCESSRULES); | |
124 | ArrayList<IAccessRule> elements= new ArrayList<IAccessRule>(rules.length); | |
125 | for (int i= 0; i < rules.length; i++) { | |
126 | elements.add(rules[i]); | |
127 | } | |
128 | patternList.setElements(elements); | |
129 | return patternList.generated_4997583003568925344(); | |
130 | } | |
131 | ||
132 | @Override | |
133 | protected Control createDialogArea(Composite parent) { | |
134 | Composite composite= (Composite) super.createDialogArea(parent); | |
135 | ||
136 | int maxLabelSize= 0; | |
137 | GC gc= new GC(composite); | |
138 | try { | |
139 | maxLabelSize= gc.textExtent(AccessRulesLabelProvider.getResolutionLabel(IAccessRule.K_ACCESSIBLE)).x; | |
140 | int len2= gc.textExtent(AccessRulesLabelProvider.getResolutionLabel(IAccessRule.K_DISCOURAGED)).x; | |
141 | if (len2 > maxLabelSize) { | |
142 | maxLabelSize= len2; | |
143 | } | |
144 | int len3= gc.textExtent(AccessRulesLabelProvider.getResolutionLabel(IAccessRule.K_NON_ACCESSIBLE)).x; | |
145 | if (len3 > maxLabelSize) { | |
146 | maxLabelSize= len3; | |
147 | } | |
148 | } finally { | |
149 | gc.dispose(); | |
150 | } | |
151 | ||
152 | ColumnLayoutData[] columnDta= new ColumnLayoutData[] { | |
153 | new ColumnPixelData(maxLabelSize + 40), | |
154 | new ColumnWeightData(1), | |
155 | }; | |
156 | fAccessRulesList.setTableColumns(new ListDialogField.ColumnsDescription(columnDta, null, false)); | |
157 | ||
158 | ||
159 | Composite inner= new Composite(composite, SWT.NONE); | |
160 | inner.setFont(composite.getFont()); | |
161 | ||
162 | GridLayout layout= new GridLayout(); | |
163 | layout.marginHeight= 0; | |
164 | layout.marginWidth= 0; | |
165 | layout.numColumns= 2; | |
166 | inner.setLayout(layout); | |
167 | inner.setLayoutData(new GridData(GridData.FILL_BOTH)); | |
168 | ||
169 | Label description= new Label(inner, SWT.WRAP); | |
170 | ||
171 | description.setText(getDescriptionString()); | |
172 | ||
173 | GridData data= new GridData(SWT.FILL, SWT.CENTER, false, false, 2, 1); | |
174 | data.widthHint= convertWidthInCharsToPixels(70); | |
175 | fAccessRulesList.generated_5730539116729323404(this, inner, description, data); | |
176 | ||
177 | if (fProject != null) { | |
178 | String forbiddenSeverity= fProject.getOption(JavaCore.COMPILER_PB_FORBIDDEN_REFERENCE, true); | |
179 | String discouragedSeverity= fProject.getOption(JavaCore.COMPILER_PB_DISCOURAGED_REFERENCE, true); | |
180 | String[] args= { getLocalizedString(discouragedSeverity), getLocalizedString(forbiddenSeverity) }; | |
181 | ||
182 | FormToolkit toolkit= new FormToolkit(parent.getDisplay()); | |
183 | toolkit.setBackground(null); | |
184 | try { | |
185 | FormText text = toolkit.createFormText(composite, true); | |
186 | text.setFont(inner.getFont()); | |
187 | if (fParentCanSwitchPage) { | |
188 | // with link | |
189 | text.setText(Messages.format(NewWizardMessages.AccessRulesDialog_severity_info_with_link, args), true, false); | |
190 | text.addHyperlinkListener(new HyperlinkAdapter() { | |
191 | @Override | |
192 | public void linkActivated(HyperlinkEvent e) { | |
193 | doErrorWarningLinkPressed(); | |
194 | } | |
195 | }); | |
196 | } else { | |
197 | // no link | |
198 | text.setText(Messages.format(NewWizardMessages.AccessRulesDialog_severity_info_no_link, args), true, false); | |
199 | } | |
200 | data= new GridData(SWT.FILL, SWT.CENTER, false, false, 2, 1); | |
201 | data.widthHint= convertWidthInCharsToPixels(70); | |
202 | text.setLayoutData(data); | |
203 | } catch (IllegalArgumentException e) { | |
204 | JavaPlugin.log(e); // invalid string | |
205 | } finally { | |
206 | toolkit.dispose(); | |
207 | } | |
208 | } | |
209 | applyDialogFont(composite); | |
210 | return composite; | |
211 | } | |
212 | ||
213 | final void doErrorWarningLinkPressed() { | |
214 | if (fParentCanSwitchPage && MessageDialog.openQuestion(getShell(), NewWizardMessages.AccessRulesDialog_switch_dialog_title, NewWizardMessages.AccessRulesDialog_switch_dialog_message)) { | |
215 | setReturnCode(SWITCH_PAGE); | |
216 | close(); | |
217 | } | |
218 | } | |
219 | ||
220 | private String getLocalizedString(String severity) { | |
221 | if (JavaCore.ERROR.equals(severity)) { | |
222 | return NewWizardMessages.AccessRulesDialog_severity_error; | |
223 | } else if (JavaCore.WARNING.equals(severity)) { | |
224 | return NewWizardMessages.AccessRulesDialog_severity_warning; | |
225 | } else { | |
226 | return NewWizardMessages.AccessRulesDialog_severity_ignore; | |
227 | } | |
228 | } | |
229 | ||
230 | private String getDescriptionString() { | |
231 | return fCurrElement.generated_8456234895900901071(); | |
232 | } | |
233 | ||
234 | protected void doCustomButtonPressed(ListDialogField<IAccessRule> field, int index) { | |
235 | field.generated_7276088159970757159(this, index); | |
236 | } | |
237 | ||
238 | protected void doDoubleClicked(ListDialogField<IAccessRule> field) { | |
239 | editEntry(field); | |
240 | } | |
241 | ||
242 | protected void doSelectionChanged(ListDialogField<IAccessRule> field) { | |
243 | field.generated_4866860090493217720(this); | |
244 | } | |
245 | ||
246 | public boolean canEdit(List<IAccessRule> selected) { | |
247 | return selected.size() == 1; | |
248 | } | |
249 | ||
250 | public void editEntry(ListDialogField<IAccessRule> field) { | |
251 | ||
252 | List<IAccessRule> selElements= field.getSelectedElements(); | |
253 | if (selElements.size() != 1) { | |
254 | return; | |
255 | } | |
256 | IAccessRule rule= selElements.get(0); | |
257 | AccessRuleEntryDialog dialog= new AccessRuleEntryDialog(getShell(), rule, fCurrElement); | |
258 | dialog.generated_5991918521968413620(field, rule); | |
259 | } | |
260 | ||
261 | public void addEntry(ListDialogField<IAccessRule> field) { | |
262 | AccessRuleEntryDialog dialog= new AccessRuleEntryDialog(getShell(), null, fCurrElement); | |
263 | dialog.generated_2988736205975303634(field); | |
264 | } | |
265 | ||
266 | ||
267 | ||
268 | ||
269 | ||
270 | // -------- TypeRestrictionAdapter -------- | |
271 | ||
272 | private class TypeRestrictionAdapter implements IListAdapter<IAccessRule>, IDialogFieldListener { | |
273 | /** | |
274 | * @see org.eclipse.jdt.internal.ui.wizards.dialogfields.IListAdapter#customButtonPressed(org.eclipse.jdt.internal.ui.wizards.dialogfields.ListDialogField, int) | |
275 | */ | |
276 | public void customButtonPressed(ListDialogField<IAccessRule> field, int index) { | |
277 | doCustomButtonPressed(field, index); | |
278 | } | |
279 | ||
280 | /** | |
281 | * @see org.eclipse.jdt.internal.ui.wizards.dialogfields.IListAdapter#selectionChanged(org.eclipse.jdt.internal.ui.wizards.dialogfields.ListDialogField) | |
282 | */ | |
283 | public void selectionChanged(ListDialogField<IAccessRule> field) { | |
284 | doSelectionChanged(field); | |
285 | } | |
286 | /** | |
287 | * @see org.eclipse.jdt.internal.ui.wizards.dialogfields.IListAdapter#doubleClicked(org.eclipse.jdt.internal.ui.wizards.dialogfields.ListDialogField) | |
288 | */ | |
289 | public void doubleClicked(ListDialogField<IAccessRule> field) { | |
290 | doDoubleClicked(field); | |
291 | } | |
292 | ||
293 | /** | |
294 | * @see org.eclipse.jdt.internal.ui.wizards.dialogfields.IDialogFieldListener#dialogFieldChanged(org.eclipse.jdt.internal.ui.wizards.dialogfields.DialogField) | |
295 | */ | |
296 | public void dialogFieldChanged(DialogField field) { | |
297 | } | |
298 | ||
299 | } | |
300 | ||
301 | protected void doStatusLineUpdate() { | |
302 | } | |
303 | ||
304 | protected void checkIfPatternValid() { | |
305 | } | |
306 | ||
307 | public IAccessRule[] getAccessRules() { | |
308 | List<IAccessRule> elements= fAccessRulesList.getElements(); | |
309 | return elements.toArray(new IAccessRule[elements.size()]); | |
310 | } | |
311 | ||
312 | public boolean doCombineAccessRules() { | |
313 | return fCombineRulesCheckbox.isSelected(); | |
314 | } | |
315 | ||
316 | /* | |
317 | * @see org.eclipse.jface.window.Window#configureShell(Shell) | |
318 | */ | |
319 | @Override | |
320 | protected void configureShell(Shell newShell) { | |
321 | super.configureShell(newShell); | |
322 | String helpContextId; | |
323 | if (fCurrElement.getEntryKind() == IClasspathEntry.CPE_PROJECT) | |
324 | helpContextId= IJavaHelpContextIds.ACCESS_RULES_DIALOG_COMBINE_RULES; | |
325 | else | |
326 | helpContextId= IJavaHelpContextIds.ACCESS_RULES_DIALOG; | |
327 | PlatformUI.getWorkbench().getHelpSystem().setHelp(newShell, helpContextId); | |
328 | } | |
329 | ||
330 | public void performPageSwitch(IWorkbenchPreferenceContainer pageContainer) { | |
331 | HashMap<String, String> data= new HashMap<String, String>(); | |
332 | data.put(ProblemSeveritiesPreferencePage.DATA_SELECT_OPTION_KEY, JavaCore.COMPILER_PB_FORBIDDEN_REFERENCE); | |
333 | data.put(ProblemSeveritiesPreferencePage.DATA_SELECT_OPTION_QUALIFIER, JavaCore.PLUGIN_ID); | |
334 | pageContainer.openPage(ProblemSeveritiesPreferencePage.PROP_ID, data); | |
335 | } | |
336 | } |