1 11 package org.eclipse.jdt.internal.ui.refactoring; 12 13 import org.eclipse.core.runtime.Assert; 14 15 import org.eclipse.swt.SWT; 16 import org.eclipse.swt.events.SelectionAdapter; 17 import org.eclipse.swt.events.SelectionEvent; 18 import org.eclipse.swt.layout.GridData; 19 import org.eclipse.swt.layout.GridLayout; 20 import org.eclipse.swt.widgets.Combo; 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.Dialog; 27 28 29 class ComboSelectionDialog extends Dialog{ 30 31 private String fSelection= null; 32 private final String fShellTitle; 33 private final String fLabelText; 34 private final String [] fAllowedStrings; 35 private final int fInitialSelectionIndex; 36 public ComboSelectionDialog(Shell parentShell, String shellTitle, String labelText, String [] comboStrings, int initialSelectionIndex) { 37 super(parentShell); 38 Assert.isNotNull(shellTitle); 39 Assert.isNotNull(labelText); 40 Assert.isTrue(comboStrings.length > 0); 41 Assert.isTrue(initialSelectionIndex >= 0 && initialSelectionIndex < comboStrings.length); 42 fShellTitle= shellTitle; 43 fLabelText= labelText; 44 fAllowedStrings= comboStrings; 45 fInitialSelectionIndex= initialSelectionIndex; 46 } 47 48 String getSelectedString(){ 49 return fSelection; 50 } 51 52 55 protected Control createDialogArea(Composite parent) { 56 getShell().setText(fShellTitle); 57 58 Composite composite = (Composite)super.createDialogArea(parent); 59 Composite innerComposite = new Composite(composite, SWT.NONE); 60 innerComposite.setLayoutData(new GridData()); 61 GridLayout gl= new GridLayout(); 62 gl.numColumns= 2; 63 innerComposite.setLayout(gl); 64 65 Label label= new Label(innerComposite, SWT.NONE); 66 label.setText(fLabelText); 67 label.setLayoutData(new GridData()); 68 69 final Combo combo= new Combo(innerComposite, SWT.READ_ONLY); 70 for (int i = 0; i < fAllowedStrings.length; i++) { 71 combo.add(fAllowedStrings[i]); 72 } 73 combo.select(fInitialSelectionIndex); 74 fSelection= combo.getItem(combo.getSelectionIndex()); 75 GridData gd= new GridData(); 76 gd.widthHint= convertWidthInCharsToPixels(getMaxStringLength()); 77 combo.setLayoutData(gd); 78 combo.addSelectionListener(new SelectionAdapter(){ 79 public void widgetSelected(SelectionEvent e) { 80 fSelection= combo.getItem(combo.getSelectionIndex()); 81 } 82 }); 83 applyDialogFont(composite); 84 return composite; 85 } 86 87 private int getMaxStringLength() { 88 int max= 0; 89 for (int i= 0; i < fAllowedStrings.length; i++) { 90 max= Math.max(max, fAllowedStrings[i].length()); 91 } 92 return max; 93 } 94 } 95 | Popular Tags |