1 11 package org.eclipse.jdt.internal.ui.refactoring; 12 13 import java.util.ArrayList ; 14 import java.util.List ; 15 16 import org.eclipse.jdt.core.dom.Modifier; 17 18 import org.eclipse.swt.SWT; 19 import org.eclipse.swt.events.SelectionAdapter; 20 import org.eclipse.swt.events.SelectionEvent; 21 import org.eclipse.swt.layout.GridData; 22 import org.eclipse.swt.layout.GridLayout; 23 import org.eclipse.swt.widgets.Button; 24 import org.eclipse.swt.widgets.Composite; 25 import org.eclipse.swt.widgets.Group; 26 27 public class VisibilityControlUtil { 28 private VisibilityControlUtil(){} 29 30 public static Composite createVisibilityControl(Composite parent, final IVisibilityChangeListener visibilityChangeListener, int[] availableVisibilities, int correctVisibility) { 31 List allowedVisibilities= convertToIntegerList(availableVisibilities); 32 if (allowedVisibilities.size() == 1) 33 return null; 34 35 Group group= new Group(parent, SWT.NONE); 36 group.setText(RefactoringMessages.VisibilityControlUtil_Access_modifier); 37 GridData gd= new GridData(GridData.FILL_HORIZONTAL); 38 group.setLayoutData(gd); 39 GridLayout layout= new GridLayout(); 40 layout.makeColumnsEqualWidth= true; 41 layout.numColumns= 4; 42 group.setLayout(layout); 43 44 String [] labels= new String [] { 45 "&public", "pro&tected", RefactoringMessages.VisibilityControlUtil_defa_ult_4, 48 "pri&vate" }; 50 Integer [] data= new Integer [] { 51 new Integer (Modifier.PUBLIC), 52 new Integer (Modifier.PROTECTED), 53 new Integer (Modifier.NONE), 54 new Integer (Modifier.PRIVATE)}; 55 Integer initialVisibility= new Integer (correctVisibility); 56 for (int i= 0; i < labels.length; i++) { 57 Button radio= new Button(group, SWT.RADIO); 58 Integer visibilityCode= data[i]; 59 radio.setText(labels[i]); 60 radio.setData(visibilityCode); 61 radio.setSelection(visibilityCode.equals(initialVisibility)); 62 radio.setEnabled(allowedVisibilities.contains(visibilityCode)); 63 radio.addSelectionListener(new SelectionAdapter() { 64 public void widgetSelected(SelectionEvent event) { 65 visibilityChangeListener.visibilityChanged(((Integer )event.widget.getData()).intValue()); 66 } 67 }); 68 } 69 group.setLayoutData((new GridData(GridData.FILL_HORIZONTAL))); 70 return group; 71 } 72 73 private static List convertToIntegerList(int[] array) { 74 List result= new ArrayList (array.length); 75 for (int i= 0; i < array.length; i++) { 76 result.add(new Integer (array[i])); 77 } 78 return result; 79 } 80 } 81 | Popular Tags |