KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > internal > ui > wizards > buildpaths > AccessRulesDialog


1 /*******************************************************************************
2  * Copyright (c) 2000, 2005 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 JavaDoc;
14 import java.util.HashMap JavaDoc;
15 import java.util.List JavaDoc;
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 import org.eclipse.jface.window.Window;
32
33 import org.eclipse.ui.PlatformUI;
34 import org.eclipse.ui.preferences.IWorkbenchPreferenceContainer;
35
36 import org.eclipse.ui.forms.events.HyperlinkAdapter;
37 import org.eclipse.ui.forms.events.HyperlinkEvent;
38 import org.eclipse.ui.forms.widgets.FormText;
39 import org.eclipse.ui.forms.widgets.FormToolkit;
40
41 import org.eclipse.jdt.core.IAccessRule;
42 import org.eclipse.jdt.core.IClasspathEntry;
43 import org.eclipse.jdt.core.IJavaProject;
44 import org.eclipse.jdt.core.JavaCore;
45 import org.eclipse.jdt.core.JavaModelException;
46
47 import org.eclipse.jdt.internal.corext.util.Messages;
48
49 import org.eclipse.jdt.ui.JavaElementLabels;
50
51 import org.eclipse.jdt.internal.ui.IJavaHelpContextIds;
52 import org.eclipse.jdt.internal.ui.JavaPlugin;
53 import org.eclipse.jdt.internal.ui.preferences.ProblemSeveritiesPreferencePage;
54 import org.eclipse.jdt.internal.ui.wizards.NewWizardMessages;
55 import org.eclipse.jdt.internal.ui.wizards.dialogfields.DialogField;
56 import org.eclipse.jdt.internal.ui.wizards.dialogfields.IDialogFieldListener;
57 import org.eclipse.jdt.internal.ui.wizards.dialogfields.IListAdapter;
58 import org.eclipse.jdt.internal.ui.wizards.dialogfields.LayoutUtil;
59 import org.eclipse.jdt.internal.ui.wizards.dialogfields.ListDialogField;
60 import org.eclipse.jdt.internal.ui.wizards.dialogfields.SelectionButtonDialogField;
61
62 public class AccessRulesDialog extends StatusDialog {
63     
64     public static final int SWITCH_PAGE= 10;
65     
66     private final ListDialogField fAccessRulesList;
67     private final SelectionButtonDialogField fCombineRulesCheckbox;
68     private final CPListElement fCurrElement;
69     
70     private final IJavaProject fProject;
71     private final boolean fParentCanSwitchPage;
72     
73     private static final int IDX_ADD= 0;
74     private static final int IDX_EDIT= 1;
75     private static final int IDX_UP= 3;
76     private static final int IDX_DOWN= 4;
77     private static final int IDX_REMOVE= 6;
78
79     
80     public AccessRulesDialog(Shell parent, CPListElement entryToEdit, IJavaProject project, boolean parentCanSwitchPage) {
81         super(parent);
82         setShellStyle(getShellStyle() | SWT.RESIZE);
83         
84         fCurrElement= entryToEdit;
85         fProject= project; // can be null
86

87         setTitle(NewWizardMessages.AccessRulesDialog_title);
88         
89         fAccessRulesList= createListContents(entryToEdit);
90         
91         fCombineRulesCheckbox= new SelectionButtonDialogField(SWT.CHECK);
92         fCombineRulesCheckbox.setLabelText(NewWizardMessages.AccessRulesDialog_combine_label);
93         fCombineRulesCheckbox.setSelection(Boolean.TRUE.equals(entryToEdit.getAttribute(CPListElement.COMBINE_ACCESSRULES)));
94         
95         fParentCanSwitchPage= parentCanSwitchPage;
96     }
97     
98     
99     private ListDialogField createListContents(CPListElement entryToEdit) {
100         String JavaDoc label= NewWizardMessages.AccessRulesDialog_rules_label;
101         String JavaDoc[] buttonLabels= new String JavaDoc[] {
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 patternList= new ListDialogField(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 JavaDoc elements= new ArrayList JavaDoc(rules.length);
125         for (int i= 0; i < rules.length; i++) {
126             elements.add(rules[i]);
127         }
128         patternList.setElements(elements);
129         patternList.selectFirstElement();
130         return patternList;
131     }
132
133
134     protected Control createDialogArea(Composite parent) {
135         Composite composite= (Composite) super.createDialogArea(parent);
136                 
137         int maxLabelSize= 0;
138         GC gc= new GC(composite);
139         try {
140             maxLabelSize= gc.textExtent(AccessRulesLabelProvider.getResolutionLabel(IAccessRule.K_ACCESSIBLE)).x;
141             int len2= gc.textExtent(AccessRulesLabelProvider.getResolutionLabel(IAccessRule.K_DISCOURAGED)).x;
142             if (len2 > maxLabelSize) {
143                 maxLabelSize= len2;
144             }
145             int len3= gc.textExtent(AccessRulesLabelProvider.getResolutionLabel(IAccessRule.K_NON_ACCESSIBLE)).x;
146             if (len3 > maxLabelSize) {
147                 maxLabelSize= len3;
148             }
149         } finally {
150             gc.dispose();
151         }
152         
153         ColumnLayoutData[] columnDta= new ColumnLayoutData[] {
154                 new ColumnPixelData(maxLabelSize + 40),
155                 new ColumnWeightData(1),
156         };
157         fAccessRulesList.setTableColumns(new ListDialogField.ColumnsDescription(columnDta, null, false));
158         
159
160         Composite inner= new Composite(composite, SWT.NONE);
161         inner.setFont(composite.getFont());
162         
163         GridLayout layout= new GridLayout();
164         layout.marginHeight= 0;
165         layout.marginWidth= 0;
166         layout.numColumns= 2;
167         inner.setLayout(layout);
168         inner.setLayoutData(new GridData(GridData.FILL_BOTH));
169         
170         Label description= new Label(inner, SWT.WRAP);
171
172         description.setText(getDescriptionString());
173         
174         GridData data= new GridData(SWT.FILL, SWT.CENTER, false, false, 2, 1);
175         data.widthHint= convertWidthInCharsToPixels(70);
176         description.setLayoutData(data);
177         
178         fAccessRulesList.doFillIntoGrid(inner, 3);
179                 
180         LayoutUtil.setHorizontalSpan(fAccessRulesList.getLabelControl(null), 2);
181         
182         data= (GridData) fAccessRulesList.getListControl(null).getLayoutData();
183         data.grabExcessHorizontalSpace= true;
184         data.heightHint= SWT.DEFAULT;
185         
186         if (fCurrElement.getEntryKind() == IClasspathEntry.CPE_PROJECT) {
187             fCombineRulesCheckbox.doFillIntoGrid(inner, 2);
188         }
189         
190         if (fProject != null) {
191             String JavaDoc forbiddenSeverity= fProject.getOption(JavaCore.COMPILER_PB_FORBIDDEN_REFERENCE, true);
192             String JavaDoc discouragedSeverity= fProject.getOption(JavaCore.COMPILER_PB_DISCOURAGED_REFERENCE, true);
193             String JavaDoc[] args= { getLocalizedString(discouragedSeverity), getLocalizedString(forbiddenSeverity) };
194             
195             FormToolkit toolkit= new FormToolkit(parent.getDisplay());
196             toolkit.setBackground(null);
197             try {
198                 FormText text = toolkit.createFormText(composite, true);
199                 text.setFont(inner.getFont());
200                 if (fParentCanSwitchPage) {
201                     // with link
202
text.setText(Messages.format(NewWizardMessages.AccessRulesDialog_severity_info_with_link, args), true, false);
203                     text.addHyperlinkListener(new HyperlinkAdapter() {
204                         public void linkActivated(HyperlinkEvent e) {
205                             doErrorWarningLinkPressed();
206                         }
207                     });
208                 } else {
209                     // no link
210
text.setText(Messages.format(NewWizardMessages.AccessRulesDialog_severity_info_no_link, args), true, false);
211                 }
212                 data= new GridData(SWT.FILL, SWT.CENTER, false, false, 2, 1);
213                 data.widthHint= convertWidthInCharsToPixels(70);
214                 text.setLayoutData(data);
215             } catch (IllegalArgumentException JavaDoc e) {
216                 JavaPlugin.log(e); // invalid string
217
} finally {
218                 toolkit.dispose();
219             }
220         }
221         applyDialogFont(composite);
222         return composite;
223     }
224
225     final void doErrorWarningLinkPressed() {
226         if (fParentCanSwitchPage && MessageDialog.openQuestion(getShell(), NewWizardMessages.AccessRulesDialog_switch_dialog_title, NewWizardMessages.AccessRulesDialog_switch_dialog_message)) {
227             setReturnCode(SWITCH_PAGE);
228             close();
229         }
230     }
231
232     private String JavaDoc getLocalizedString(String JavaDoc severity) {
233         if (JavaCore.ERROR.equals(severity)) {
234             return NewWizardMessages.AccessRulesDialog_severity_error;
235         } else if (JavaCore.WARNING.equals(severity)) {
236             return NewWizardMessages.AccessRulesDialog_severity_warning;
237         } else {
238             return NewWizardMessages.AccessRulesDialog_severity_ignore;
239         }
240     }
241     
242     private String JavaDoc getDescriptionString() {
243         String JavaDoc desc;
244         String JavaDoc name= fCurrElement.getPath().lastSegment();
245         switch (fCurrElement.getEntryKind()) {
246             case IClasspathEntry.CPE_CONTAINER:
247                 try {
248                     name= JavaElementLabels.getContainerEntryLabel(fCurrElement.getPath(), fCurrElement.getJavaProject());
249                 } catch (JavaModelException e) {
250                 }
251                 desc= NewWizardMessages.AccessRulesDialog_container_description;
252                 break;
253             case IClasspathEntry.CPE_PROJECT:
254                 desc= NewWizardMessages.AccessRulesDialog_project_description;
255                 break;
256             default:
257                 desc= NewWizardMessages.AccessRulesDialog_description;
258         }
259         
260         return Messages.format(desc, name);
261     }
262
263
264     protected void doCustomButtonPressed(ListDialogField field, int index) {
265         if (index == IDX_ADD) {
266             addEntry(field);
267         } else if (index == IDX_EDIT) {
268             editEntry(field);
269         }
270     }
271     
272     protected void doDoubleClicked(ListDialogField field) {
273         editEntry(field);
274     }
275     
276     protected void doSelectionChanged(ListDialogField field) {
277         List JavaDoc selected= field.getSelectedElements();
278         field.enableButton(IDX_EDIT, canEdit(selected));
279     }
280     
281     private boolean canEdit(List JavaDoc selected) {
282         return selected.size() == 1;
283     }
284     
285     private void editEntry(ListDialogField field) {
286         
287         List JavaDoc selElements= field.getSelectedElements();
288         if (selElements.size() != 1) {
289             return;
290         }
291         IAccessRule rule= (IAccessRule) selElements.get(0);
292         AccessRuleEntryDialog dialog= new AccessRuleEntryDialog(getShell(), rule, fCurrElement);
293         if (dialog.open() == Window.OK) {
294             field.replaceElement(rule, dialog.getRule());
295         }
296     }
297
298     private void addEntry(ListDialogField field) {
299         AccessRuleEntryDialog dialog= new AccessRuleEntryDialog(getShell(), null, fCurrElement);
300         if (dialog.open() == Window.OK) {
301             field.addElement(dialog.getRule());
302         }
303     }
304     
305     
306         
307     // -------- TypeRestrictionAdapter --------
308

309     private class TypeRestrictionAdapter implements IListAdapter, IDialogFieldListener {
310         /**
311          * @see org.eclipse.jdt.internal.ui.wizards.dialogfields.IListAdapter#customButtonPressed(org.eclipse.jdt.internal.ui.wizards.dialogfields.ListDialogField, int)
312          */

313         public void customButtonPressed(ListDialogField field, int index) {
314             doCustomButtonPressed(field, index);
315         }
316
317         /**
318          * @see org.eclipse.jdt.internal.ui.wizards.dialogfields.IListAdapter#selectionChanged(org.eclipse.jdt.internal.ui.wizards.dialogfields.ListDialogField)
319          */

320         public void selectionChanged(ListDialogField field) {
321             doSelectionChanged(field);
322         }
323         /**
324          * @see org.eclipse.jdt.internal.ui.wizards.dialogfields.IListAdapter#doubleClicked(org.eclipse.jdt.internal.ui.wizards.dialogfields.ListDialogField)
325          */

326         public void doubleClicked(ListDialogField field) {
327             doDoubleClicked(field);
328         }
329
330         /**
331          * @see org.eclipse.jdt.internal.ui.wizards.dialogfields.IDialogFieldListener#dialogFieldChanged(org.eclipse.jdt.internal.ui.wizards.dialogfields.DialogField)
332          */

333         public void dialogFieldChanged(DialogField field) {
334         }
335         
336     }
337     
338     protected void doStatusLineUpdate() {
339     }
340     
341     protected void checkIfPatternValid() {
342     }
343     
344     public IAccessRule[] getAccessRules() {
345         List JavaDoc elements= fAccessRulesList.getElements();
346         return (IAccessRule[]) elements.toArray(new IAccessRule[elements.size()]);
347     }
348     
349     public boolean doCombineAccessRules() {
350         return fCombineRulesCheckbox.isSelected();
351     }
352     
353     /*
354      * @see org.eclipse.jface.window.Window#configureShell(Shell)
355      */

356     protected void configureShell(Shell newShell) {
357         super.configureShell(newShell);
358         PlatformUI.getWorkbench().getHelpSystem().setHelp(newShell, IJavaHelpContextIds.ACCESS_RULES_DIALOG);
359     }
360
361
362     public void performPageSwitch(IWorkbenchPreferenceContainer pageContainer) {
363         HashMap JavaDoc data= new HashMap JavaDoc();
364         data.put(ProblemSeveritiesPreferencePage.DATA_SELECT_OPTION_KEY, JavaCore.COMPILER_PB_FORBIDDEN_REFERENCE);
365         data.put(ProblemSeveritiesPreferencePage.DATA_SELECT_OPTION_QUALIFIER, JavaCore.PLUGIN_ID);
366         pageContainer.openPage(ProblemSeveritiesPreferencePage.PROP_ID, data);
367     }
368 }
369
Popular Tags