1 13 package org.eclipse.ui.dialogs; 14 15 import java.util.Arrays ; 16 import java.util.List ; 17 18 import org.eclipse.jface.dialogs.IDialogConstants; 19 import org.eclipse.jface.viewers.ILabelProvider; 20 import org.eclipse.swt.SWT; 21 import org.eclipse.swt.events.DisposeEvent; 22 import org.eclipse.swt.events.DisposeListener; 23 import org.eclipse.swt.layout.GridData; 24 import org.eclipse.swt.widgets.Composite; 25 import org.eclipse.swt.widgets.Control; 26 import org.eclipse.swt.widgets.Event; 27 import org.eclipse.swt.widgets.Label; 28 import org.eclipse.swt.widgets.Listener; 29 import org.eclipse.swt.widgets.Shell; 30 import org.eclipse.swt.widgets.Table; 31 import org.eclipse.swt.widgets.TableItem; 32 33 39 public class TwoPaneElementSelector extends AbstractElementListSelectionDialog { 40 private String fUpperListLabel; 41 42 private String fLowerListLabel; 43 44 private ILabelProvider fQualifierRenderer; 45 46 private Object [] fElements = new Object [0]; 47 48 private Table fLowerList; 49 50 private Object [] fQualifierElements; 51 52 62 public TwoPaneElementSelector(Shell parent, ILabelProvider elementRenderer, 63 ILabelProvider qualifierRenderer) { 64 super(parent, elementRenderer); 65 setSize(50, 15); 66 setAllowDuplicates(false); 67 fQualifierRenderer = qualifierRenderer; 68 } 69 70 76 public void setUpperListLabel(String label) { 77 fUpperListLabel = label; 78 } 79 80 87 public void setLowerListLabel(String label) { 88 fLowerListLabel = label; 89 } 90 91 97 public void setElements(Object [] elements) { 98 fElements = elements; 99 } 100 101 104 public Control createDialogArea(Composite parent) { 105 Composite contents = (Composite) super.createDialogArea(parent); 106 createMessageArea(contents); 107 createFilterText(contents); 108 createLabel(contents, fUpperListLabel); 109 createFilteredList(contents); 110 createLabel(contents, fLowerListLabel); 111 createLowerList(contents); 112 setListElements(fElements); 113 List initialSelections = getInitialElementSelections(); 114 if (!initialSelections.isEmpty()) { 115 Object element = initialSelections.get(0); 116 setSelection(new Object [] { element }); 117 setLowerSelectedElement(element); 118 } 119 return contents; 120 } 121 122 132 protected Label createLabel(Composite parent, String name) { 133 if (name == null) { 134 return null; 135 } 136 Label label = new Label(parent, SWT.NONE); 137 label.setText(name); 138 label.setFont(parent.getFont()); 139 return label; 140 } 141 142 149 protected Table createLowerList(Composite parent) { 150 Table list = new Table(parent, SWT.BORDER | SWT.V_SCROLL | SWT.H_SCROLL); 151 list.addListener(SWT.Selection, new Listener() { 152 public void handleEvent(Event evt) { 153 handleLowerSelectionChanged(); 154 } 155 }); 156 list.addListener(SWT.MouseDoubleClick, new Listener() { 157 public void handleEvent(Event evt) { 158 handleDefaultSelected(); 159 } 160 }); 161 list.addDisposeListener(new DisposeListener() { 162 public void widgetDisposed(DisposeEvent e) { 163 fQualifierRenderer.dispose(); 164 } 165 }); 166 GridData data = new GridData(); 167 data.widthHint = convertWidthInCharsToPixels(50); 168 data.heightHint = convertHeightInCharsToPixels(5); 169 data.grabExcessVerticalSpace = true; 170 data.grabExcessHorizontalSpace = true; 171 data.horizontalAlignment = GridData.FILL; 172 data.verticalAlignment = GridData.FILL; 173 list.setLayoutData(data); 174 list.setFont(parent.getFont()); 175 fLowerList = list; 176 return list; 177 } 178 179 182 protected void computeResult() { 183 Object [] results = new Object [] { getLowerSelectedElement() }; 184 setResult(Arrays.asList(results)); 185 } 186 187 190 protected void handleDefaultSelected() { 191 if (validateCurrentSelection() && (getLowerSelectedElement() != null)) { 192 buttonPressed(IDialogConstants.OK_ID); 193 } 194 } 195 196 199 protected void handleSelectionChanged() { 200 handleUpperSelectionChanged(); 201 } 202 203 private void handleUpperSelectionChanged() { 204 int index = getSelectionIndex(); 205 fLowerList.removeAll(); 206 if (index >= 0) { 207 fQualifierElements = getFoldedElements(index); 208 if (fQualifierElements == null) { 209 updateLowerListWidget(new Object [] {}); 210 } else { 211 updateLowerListWidget(fQualifierElements); 212 } 213 } 214 validateCurrentSelection(); 215 } 216 217 private void handleLowerSelectionChanged() { 218 validateCurrentSelection(); 219 } 220 221 225 protected void setLowerSelectedElement(Object element) { 226 if (fQualifierElements == null) { 227 return; 228 } 229 int i; 231 for (i = 0; i != fQualifierElements.length; i++) { 232 if (fQualifierElements[i].equals(element)) { 233 break; 234 } 235 } 236 if (i != fQualifierElements.length) { 238 fLowerList.setSelection(i); 239 } 240 } 241 242 246 protected Object getLowerSelectedElement() { 247 int index = fLowerList.getSelectionIndex(); 248 if (index >= 0) { 249 return fQualifierElements[index]; 250 } 251 return null; 252 } 253 254 private void updateLowerListWidget(Object [] elements) { 255 int length = elements.length; 256 String [] qualifiers = new String [length]; 257 for (int i = 0; i != length; i++){ 258 String text = fQualifierRenderer.getText(elements[i]); 259 if(text == null) { 260 text = ""; } 262 qualifiers[i] = text; 263 } 264 TwoArrayQuickSorter sorter = new TwoArrayQuickSorter(isCaseIgnored()); 265 sorter.sort(qualifiers, elements); 266 for (int i = 0; i != length; i++) { 267 TableItem item = new TableItem(fLowerList, SWT.NONE); 268 item.setText(qualifiers[i]); 269 item.setImage(fQualifierRenderer.getImage(elements[i])); 270 } 271 if (fLowerList.getItemCount() > 0) { 272 fLowerList.setSelection(0); 273 } 274 } 275 276 279 protected void handleEmptyList() { 280 super.handleEmptyList(); 281 fLowerList.setEnabled(false); 282 } 283 } 284 | Popular Tags |