1 11 12 package org.eclipse.ui.internal.ide; 13 14 import com.ibm.icu.text.Collator; 15 import java.util.Arrays ; 16 import java.util.Comparator ; 17 import java.util.Locale ; 18 19 import org.eclipse.jface.viewers.DoubleClickEvent; 20 import org.eclipse.jface.viewers.IDoubleClickListener; 21 import org.eclipse.jface.viewers.ISelectionChangedListener; 22 import org.eclipse.jface.viewers.IStructuredSelection; 23 import org.eclipse.jface.viewers.LabelProvider; 24 import org.eclipse.jface.viewers.ListViewer; 25 import org.eclipse.jface.viewers.SelectionChangedEvent; 26 import org.eclipse.jface.viewers.StructuredSelection; 27 import org.eclipse.swt.SWT; 28 import org.eclipse.swt.layout.GridData; 29 import org.eclipse.swt.widgets.Composite; 30 import org.eclipse.swt.widgets.Control; 31 import org.eclipse.swt.widgets.Shell; 32 import org.eclipse.ui.PlatformUI; 33 import org.eclipse.ui.dialogs.SelectionDialog; 34 import org.eclipse.ui.internal.ide.dialogs.SimpleListContentProvider; 35 36 39 public class FeatureSelectionDialog extends SelectionDialog { 40 43 private final static int LIST_WIDTH = 60; 44 45 48 private final static int LIST_HEIGHT = 10; 49 50 53 private AboutInfo[] features; 54 55 58 private ListViewer listViewer; 59 60 63 private String helpContextId; 64 65 79 public FeatureSelectionDialog(Shell shell, AboutInfo[] features, 80 String primaryFeatureId, String shellTitle, String shellMessage, 81 String helpContextId) { 82 83 super(shell); 84 if (features == null || features.length == 0) { 85 throw new IllegalArgumentException (); 86 } 87 this.features = features; 88 this.helpContextId = helpContextId; 89 setTitle(shellTitle); 90 setMessage(shellMessage); 91 92 Arrays.sort(features, new Comparator () { 94 Collator coll = Collator.getInstance(Locale.getDefault()); 95 96 public int compare(Object a, Object b) { 97 AboutInfo i1, i2; 98 String name1, name2; 99 i1 = (AboutInfo) a; 100 name1 = i1.getFeatureLabel(); 101 i2 = (AboutInfo) b; 102 name2 = i2.getFeatureLabel(); 103 if (name1 == null) { 104 name1 = ""; } 106 if (name2 == null) { 107 name2 = ""; } 109 return coll.compare(name1, name2); 110 } 111 }); 112 113 for (int i = 0; i < features.length; i++) { 115 if (features[i].getFeatureId().equals(primaryFeatureId)) { 116 setInitialSelections(new Object [] { features[i] }); 117 return; 118 } 119 } 120 121 setInitialSelections(new Object [0]); 123 } 124 125 128 protected void configureShell(Shell newShell) { 129 super.configureShell(newShell); 130 PlatformUI.getWorkbench().getHelpSystem().setHelp(newShell, 131 helpContextId); 132 } 133 134 137 protected Control createDialogArea(Composite parent) { 138 Composite composite = (Composite) super.createDialogArea(parent); 139 140 createMessageArea(composite); 142 listViewer = new ListViewer(composite, SWT.SINGLE | SWT.H_SCROLL 144 | SWT.V_SCROLL | SWT.BORDER); 145 GridData data = new GridData(GridData.FILL_BOTH); 146 data.heightHint = convertHeightInCharsToPixels(LIST_HEIGHT); 147 data.widthHint = convertWidthInCharsToPixels(LIST_WIDTH); 148 listViewer.getList().setLayoutData(data); 149 listViewer.getList().setFont(parent.getFont()); 150 listViewer.setLabelProvider(new LabelProvider() { 152 public String getText(Object element) { 153 return element == null ? "" : ((AboutInfo) element).getFeatureLabel(); } 156 }); 157 158 SimpleListContentProvider cp = new SimpleListContentProvider(); 160 cp.setElements(features); 161 listViewer.setContentProvider(cp); 162 listViewer.setInput(new Object ()); 163 165 listViewer.setSelection(new StructuredSelection( 167 getInitialElementSelections()), true); 168 169 listViewer.addSelectionChangedListener(new ISelectionChangedListener() { 171 public void selectionChanged(SelectionChangedEvent event) { 172 getOkButton().setEnabled(!event.getSelection().isEmpty()); 174 } 175 }); 176 177 listViewer.addDoubleClickListener(new IDoubleClickListener() { 179 public void doubleClick(DoubleClickEvent event) { 180 okPressed(); 181 } 182 }); 183 return composite; 184 } 185 186 189 protected void okPressed() { 190 IStructuredSelection selection = (IStructuredSelection) listViewer 191 .getSelection(); 192 setResult(selection.toList()); 193 super.okPressed(); 194 } 195 } 196 | Popular Tags |