001: /*******************************************************************************
002: * Copyright (c) 2000, 2005 IBM Corporation and others.
003: * All rights reserved. This program and the accompanying materials
004: * are made available under the terms of the Eclipse Public License v1.0
005: * which accompanies this distribution, and is available at
006: * http://www.eclipse.org/legal/epl-v10.html
007: *
008: * Contributors:
009: * IBM Corporation - initial API and implementation
010: *******************************************************************************/package org.eclipse.jdt.internal.ui.wizards.buildpaths;
011:
012: import java.util.ArrayList;
013: import java.util.HashMap;
014: import java.util.List;
015:
016: import org.eclipse.swt.SWT;
017: import org.eclipse.swt.graphics.GC;
018: import org.eclipse.swt.layout.GridData;
019: import org.eclipse.swt.layout.GridLayout;
020: import org.eclipse.swt.widgets.Composite;
021: import org.eclipse.swt.widgets.Control;
022: import org.eclipse.swt.widgets.Label;
023: import org.eclipse.swt.widgets.Shell;
024:
025: import org.eclipse.jface.dialogs.MessageDialog;
026: import org.eclipse.jface.dialogs.StatusDialog;
027: import org.eclipse.jface.viewers.ColumnLayoutData;
028: import org.eclipse.jface.viewers.ColumnPixelData;
029: import org.eclipse.jface.viewers.ColumnWeightData;
030: import org.eclipse.jface.window.Window;
031:
032: import org.eclipse.ui.PlatformUI;
033: import org.eclipse.ui.preferences.IWorkbenchPreferenceContainer;
034:
035: import org.eclipse.ui.forms.events.HyperlinkAdapter;
036: import org.eclipse.ui.forms.events.HyperlinkEvent;
037: import org.eclipse.ui.forms.widgets.FormText;
038: import org.eclipse.ui.forms.widgets.FormToolkit;
039:
040: import org.eclipse.jdt.core.IAccessRule;
041: import org.eclipse.jdt.core.IClasspathEntry;
042: import org.eclipse.jdt.core.IJavaProject;
043: import org.eclipse.jdt.core.JavaCore;
044: import org.eclipse.jdt.core.JavaModelException;
045:
046: import org.eclipse.jdt.internal.corext.util.Messages;
047:
048: import org.eclipse.jdt.ui.JavaElementLabels;
049:
050: import org.eclipse.jdt.internal.ui.IJavaHelpContextIds;
051: import org.eclipse.jdt.internal.ui.JavaPlugin;
052: import org.eclipse.jdt.internal.ui.preferences.ProblemSeveritiesPreferencePage;
053: import org.eclipse.jdt.internal.ui.wizards.NewWizardMessages;
054: import org.eclipse.jdt.internal.ui.wizards.dialogfields.DialogField;
055: import org.eclipse.jdt.internal.ui.wizards.dialogfields.IDialogFieldListener;
056: import org.eclipse.jdt.internal.ui.wizards.dialogfields.IListAdapter;
057: import org.eclipse.jdt.internal.ui.wizards.dialogfields.LayoutUtil;
058: import org.eclipse.jdt.internal.ui.wizards.dialogfields.ListDialogField;
059: import org.eclipse.jdt.internal.ui.wizards.dialogfields.SelectionButtonDialogField;
060:
061: public class AccessRulesDialog extends StatusDialog {
062:
063: public static final int SWITCH_PAGE = 10;
064:
065: private final ListDialogField fAccessRulesList;
066: private final SelectionButtonDialogField fCombineRulesCheckbox;
067: private final CPListElement fCurrElement;
068:
069: private final IJavaProject fProject;
070: private final boolean fParentCanSwitchPage;
071:
072: private static final int IDX_ADD = 0;
073: private static final int IDX_EDIT = 1;
074: private static final int IDX_UP = 3;
075: private static final int IDX_DOWN = 4;
076: private static final int IDX_REMOVE = 6;
077:
078: public AccessRulesDialog(Shell parent, CPListElement entryToEdit,
079: IJavaProject project, boolean parentCanSwitchPage) {
080: super (parent);
081: setShellStyle(getShellStyle() | SWT.RESIZE);
082:
083: fCurrElement = entryToEdit;
084: fProject = project; // can be null
085:
086: setTitle(NewWizardMessages.AccessRulesDialog_title);
087:
088: fAccessRulesList = createListContents(entryToEdit);
089:
090: fCombineRulesCheckbox = new SelectionButtonDialogField(
091: SWT.CHECK);
092: fCombineRulesCheckbox
093: .setLabelText(NewWizardMessages.AccessRulesDialog_combine_label);
094: fCombineRulesCheckbox
095: .setSelection(Boolean.TRUE
096: .equals(entryToEdit
097: .getAttribute(CPListElement.COMBINE_ACCESSRULES)));
098:
099: fParentCanSwitchPage = parentCanSwitchPage;
100: }
101:
102: private ListDialogField createListContents(CPListElement entryToEdit) {
103: String label = NewWizardMessages.AccessRulesDialog_rules_label;
104: String[] buttonLabels = new String[] {
105: NewWizardMessages.AccessRulesDialog_rules_add,
106: NewWizardMessages.AccessRulesDialog_rules_edit, null,
107: NewWizardMessages.AccessRulesDialog_rules_up,
108: NewWizardMessages.AccessRulesDialog_rules_down, null,
109: NewWizardMessages.AccessRulesDialog_rules_remove };
110:
111: TypeRestrictionAdapter adapter = new TypeRestrictionAdapter();
112: AccessRulesLabelProvider labelProvider = new AccessRulesLabelProvider();
113:
114: ListDialogField patternList = new ListDialogField(adapter,
115: buttonLabels, labelProvider);
116: patternList.setDialogFieldListener(adapter);
117:
118: patternList.setLabelText(label);
119: patternList.setRemoveButtonIndex(IDX_REMOVE);
120: patternList.setUpButtonIndex(IDX_UP);
121: patternList.setDownButtonIndex(IDX_DOWN);
122: patternList.enableButton(IDX_EDIT, false);
123:
124: IAccessRule[] rules = (IAccessRule[]) entryToEdit
125: .getAttribute(CPListElement.ACCESSRULES);
126: ArrayList elements = new ArrayList(rules.length);
127: for (int i = 0; i < rules.length; i++) {
128: elements.add(rules[i]);
129: }
130: patternList.setElements(elements);
131: patternList.selectFirstElement();
132: return patternList;
133: }
134:
135: protected Control createDialogArea(Composite parent) {
136: Composite composite = (Composite) super
137: .createDialogArea(parent);
138:
139: int maxLabelSize = 0;
140: GC gc = new GC(composite);
141: try {
142: maxLabelSize = gc.textExtent(AccessRulesLabelProvider
143: .getResolutionLabel(IAccessRule.K_ACCESSIBLE)).x;
144: int len2 = gc.textExtent(AccessRulesLabelProvider
145: .getResolutionLabel(IAccessRule.K_DISCOURAGED)).x;
146: if (len2 > maxLabelSize) {
147: maxLabelSize = len2;
148: }
149: int len3 = gc.textExtent(AccessRulesLabelProvider
150: .getResolutionLabel(IAccessRule.K_NON_ACCESSIBLE)).x;
151: if (len3 > maxLabelSize) {
152: maxLabelSize = len3;
153: }
154: } finally {
155: gc.dispose();
156: }
157:
158: ColumnLayoutData[] columnDta = new ColumnLayoutData[] {
159: new ColumnPixelData(maxLabelSize + 40),
160: new ColumnWeightData(1), };
161: fAccessRulesList
162: .setTableColumns(new ListDialogField.ColumnsDescription(
163: columnDta, null, false));
164:
165: Composite inner = new Composite(composite, SWT.NONE);
166: inner.setFont(composite.getFont());
167:
168: GridLayout layout = new GridLayout();
169: layout.marginHeight = 0;
170: layout.marginWidth = 0;
171: layout.numColumns = 2;
172: inner.setLayout(layout);
173: inner.setLayoutData(new GridData(GridData.FILL_BOTH));
174:
175: Label description = new Label(inner, SWT.WRAP);
176:
177: description.setText(getDescriptionString());
178:
179: GridData data = new GridData(SWT.FILL, SWT.CENTER, false,
180: false, 2, 1);
181: data.widthHint = convertWidthInCharsToPixels(70);
182: description.setLayoutData(data);
183:
184: fAccessRulesList.doFillIntoGrid(inner, 3);
185:
186: LayoutUtil.setHorizontalSpan(fAccessRulesList
187: .getLabelControl(null), 2);
188:
189: data = (GridData) fAccessRulesList.getListControl(null)
190: .getLayoutData();
191: data.grabExcessHorizontalSpace = true;
192: data.heightHint = SWT.DEFAULT;
193:
194: if (fCurrElement.getEntryKind() == IClasspathEntry.CPE_PROJECT) {
195: fCombineRulesCheckbox.doFillIntoGrid(inner, 2);
196: }
197:
198: if (fProject != null) {
199: String forbiddenSeverity = fProject.getOption(
200: JavaCore.COMPILER_PB_FORBIDDEN_REFERENCE, true);
201: String discouragedSeverity = fProject.getOption(
202: JavaCore.COMPILER_PB_DISCOURAGED_REFERENCE, true);
203: String[] args = { getLocalizedString(discouragedSeverity),
204: getLocalizedString(forbiddenSeverity) };
205:
206: FormToolkit toolkit = new FormToolkit(parent.getDisplay());
207: toolkit.setBackground(null);
208: try {
209: FormText text = toolkit.createFormText(composite, true);
210: text.setFont(inner.getFont());
211: if (fParentCanSwitchPage) {
212: // with link
213: text
214: .setText(
215: Messages
216: .format(
217: NewWizardMessages.AccessRulesDialog_severity_info_with_link,
218: args), true, false);
219: text.addHyperlinkListener(new HyperlinkAdapter() {
220: public void linkActivated(HyperlinkEvent e) {
221: doErrorWarningLinkPressed();
222: }
223: });
224: } else {
225: // no link
226: text
227: .setText(
228: Messages
229: .format(
230: NewWizardMessages.AccessRulesDialog_severity_info_no_link,
231: args), true, false);
232: }
233: data = new GridData(SWT.FILL, SWT.CENTER, false, false,
234: 2, 1);
235: data.widthHint = convertWidthInCharsToPixels(70);
236: text.setLayoutData(data);
237: } catch (IllegalArgumentException e) {
238: JavaPlugin.log(e); // invalid string
239: } finally {
240: toolkit.dispose();
241: }
242: }
243: applyDialogFont(composite);
244: return composite;
245: }
246:
247: final void doErrorWarningLinkPressed() {
248: if (fParentCanSwitchPage
249: && MessageDialog
250: .openQuestion(
251: getShell(),
252: NewWizardMessages.AccessRulesDialog_switch_dialog_title,
253: NewWizardMessages.AccessRulesDialog_switch_dialog_message)) {
254: setReturnCode(SWITCH_PAGE);
255: close();
256: }
257: }
258:
259: private String getLocalizedString(String severity) {
260: if (JavaCore.ERROR.equals(severity)) {
261: return NewWizardMessages.AccessRulesDialog_severity_error;
262: } else if (JavaCore.WARNING.equals(severity)) {
263: return NewWizardMessages.AccessRulesDialog_severity_warning;
264: } else {
265: return NewWizardMessages.AccessRulesDialog_severity_ignore;
266: }
267: }
268:
269: private String getDescriptionString() {
270: String desc;
271: String name = fCurrElement.getPath().lastSegment();
272: switch (fCurrElement.getEntryKind()) {
273: case IClasspathEntry.CPE_CONTAINER:
274: try {
275: name = JavaElementLabels.getContainerEntryLabel(
276: fCurrElement.getPath(), fCurrElement
277: .getJavaProject());
278: } catch (JavaModelException e) {
279: }
280: desc = NewWizardMessages.AccessRulesDialog_container_description;
281: break;
282: case IClasspathEntry.CPE_PROJECT:
283: desc = NewWizardMessages.AccessRulesDialog_project_description;
284: break;
285: default:
286: desc = NewWizardMessages.AccessRulesDialog_description;
287: }
288:
289: return Messages.format(desc, name);
290: }
291:
292: protected void doCustomButtonPressed(ListDialogField field,
293: int index) {
294: if (index == IDX_ADD) {
295: addEntry(field);
296: } else if (index == IDX_EDIT) {
297: editEntry(field);
298: }
299: }
300:
301: protected void doDoubleClicked(ListDialogField field) {
302: editEntry(field);
303: }
304:
305: protected void doSelectionChanged(ListDialogField field) {
306: List selected = field.getSelectedElements();
307: field.enableButton(IDX_EDIT, canEdit(selected));
308: }
309:
310: private boolean canEdit(List selected) {
311: return selected.size() == 1;
312: }
313:
314: private void editEntry(ListDialogField field) {
315:
316: List selElements = field.getSelectedElements();
317: if (selElements.size() != 1) {
318: return;
319: }
320: IAccessRule rule = (IAccessRule) selElements.get(0);
321: AccessRuleEntryDialog dialog = new AccessRuleEntryDialog(
322: getShell(), rule, fCurrElement);
323: if (dialog.open() == Window.OK) {
324: field.replaceElement(rule, dialog.getRule());
325: }
326: }
327:
328: private void addEntry(ListDialogField field) {
329: AccessRuleEntryDialog dialog = new AccessRuleEntryDialog(
330: getShell(), null, fCurrElement);
331: if (dialog.open() == Window.OK) {
332: field.addElement(dialog.getRule());
333: }
334: }
335:
336: // -------- TypeRestrictionAdapter --------
337:
338: private class TypeRestrictionAdapter implements IListAdapter,
339: IDialogFieldListener {
340: /**
341: * @see org.eclipse.jdt.internal.ui.wizards.dialogfields.IListAdapter#customButtonPressed(org.eclipse.jdt.internal.ui.wizards.dialogfields.ListDialogField, int)
342: */
343: public void customButtonPressed(ListDialogField field, int index) {
344: doCustomButtonPressed(field, index);
345: }
346:
347: /**
348: * @see org.eclipse.jdt.internal.ui.wizards.dialogfields.IListAdapter#selectionChanged(org.eclipse.jdt.internal.ui.wizards.dialogfields.ListDialogField)
349: */
350: public void selectionChanged(ListDialogField field) {
351: doSelectionChanged(field);
352: }
353:
354: /**
355: * @see org.eclipse.jdt.internal.ui.wizards.dialogfields.IListAdapter#doubleClicked(org.eclipse.jdt.internal.ui.wizards.dialogfields.ListDialogField)
356: */
357: public void doubleClicked(ListDialogField field) {
358: doDoubleClicked(field);
359: }
360:
361: /**
362: * @see org.eclipse.jdt.internal.ui.wizards.dialogfields.IDialogFieldListener#dialogFieldChanged(org.eclipse.jdt.internal.ui.wizards.dialogfields.DialogField)
363: */
364: public void dialogFieldChanged(DialogField field) {
365: }
366:
367: }
368:
369: protected void doStatusLineUpdate() {
370: }
371:
372: protected void checkIfPatternValid() {
373: }
374:
375: public IAccessRule[] getAccessRules() {
376: List elements = fAccessRulesList.getElements();
377: return (IAccessRule[]) elements
378: .toArray(new IAccessRule[elements.size()]);
379: }
380:
381: public boolean doCombineAccessRules() {
382: return fCombineRulesCheckbox.isSelected();
383: }
384:
385: /*
386: * @see org.eclipse.jface.window.Window#configureShell(Shell)
387: */
388: protected void configureShell(Shell newShell) {
389: super .configureShell(newShell);
390: PlatformUI.getWorkbench().getHelpSystem().setHelp(newShell,
391: IJavaHelpContextIds.ACCESS_RULES_DIALOG);
392: }
393:
394: public void performPageSwitch(
395: IWorkbenchPreferenceContainer pageContainer) {
396: HashMap data = new HashMap();
397: data.put(
398: ProblemSeveritiesPreferencePage.DATA_SELECT_OPTION_KEY,
399: JavaCore.COMPILER_PB_FORBIDDEN_REFERENCE);
400: data
401: .put(
402: ProblemSeveritiesPreferencePage.DATA_SELECT_OPTION_QUALIFIER,
403: JavaCore.PLUGIN_ID);
404: pageContainer.openPage(ProblemSeveritiesPreferencePage.PROP_ID,
405: data);
406: }
407: }
|