01: /*******************************************************************************
02: * Copyright (c) 2000, 2005 IBM Corporation and others.
03: * All rights reserved. This program and the accompanying materials
04: * are made available under the terms of the Eclipse Public License v1.0
05: * which accompanies this distribution, and is available at
06: * http://www.eclipse.org/legal/epl-v10.html
07: *
08: * Contributors:
09: * IBM Corporation - initial API and implementation
10: *******************************************************************************/package org.eclipse.jdt.internal.ui.refactoring;
11:
12: import java.util.ArrayList;
13: import java.util.List;
14:
15: import org.eclipse.jdt.core.dom.Modifier;
16:
17: import org.eclipse.swt.SWT;
18: import org.eclipse.swt.events.SelectionAdapter;
19: import org.eclipse.swt.events.SelectionEvent;
20: import org.eclipse.swt.layout.GridData;
21: import org.eclipse.swt.layout.GridLayout;
22: import org.eclipse.swt.widgets.Button;
23: import org.eclipse.swt.widgets.Composite;
24: import org.eclipse.swt.widgets.Group;
25:
26: public class VisibilityControlUtil {
27: private VisibilityControlUtil() {
28: }
29:
30: public static Composite createVisibilityControl(Composite parent,
31: final IVisibilityChangeListener visibilityChangeListener,
32: int[] availableVisibilities, int correctVisibility) {
33: List allowedVisibilities = convertToIntegerList(availableVisibilities);
34: if (allowedVisibilities.size() == 1)
35: return null;
36:
37: Group group = new Group(parent, SWT.NONE);
38: group
39: .setText(RefactoringMessages.VisibilityControlUtil_Access_modifier);
40: GridData gd = new GridData(GridData.FILL_HORIZONTAL);
41: group.setLayoutData(gd);
42: GridLayout layout = new GridLayout();
43: layout.makeColumnsEqualWidth = true;
44: layout.numColumns = 4;
45: group.setLayout(layout);
46:
47: String[] labels = new String[] {
48: "&public", //$NON-NLS-1$
49: "pro&tected", //$NON-NLS-1$
50: RefactoringMessages.VisibilityControlUtil_defa_ult_4,
51: "pri&vate" //$NON-NLS-1$
52: };
53: Integer[] data = new Integer[] { new Integer(Modifier.PUBLIC),
54: new Integer(Modifier.PROTECTED),
55: new Integer(Modifier.NONE),
56: new Integer(Modifier.PRIVATE) };
57: Integer initialVisibility = new Integer(correctVisibility);
58: for (int i = 0; i < labels.length; i++) {
59: Button radio = new Button(group, SWT.RADIO);
60: Integer visibilityCode = data[i];
61: radio.setText(labels[i]);
62: radio.setData(visibilityCode);
63: radio
64: .setSelection(visibilityCode
65: .equals(initialVisibility));
66: radio.setEnabled(allowedVisibilities
67: .contains(visibilityCode));
68: radio.addSelectionListener(new SelectionAdapter() {
69: public void widgetSelected(SelectionEvent event) {
70: visibilityChangeListener
71: .visibilityChanged(((Integer) event.widget
72: .getData()).intValue());
73: }
74: });
75: }
76: group.setLayoutData((new GridData(GridData.FILL_HORIZONTAL)));
77: return group;
78: }
79:
80: private static List convertToIntegerList(int[] array) {
81: List result = new ArrayList(array.length);
82: for (int i = 0; i < array.length; i++) {
83: result.add(new Integer(array[i]));
84: }
85: return result;
86: }
87: }
|