1 13 14 package org.eclipse.ui.views.markers.internal; 15 16 import java.util.ArrayList ; 17 import java.util.Arrays ; 18 import java.util.Comparator ; 19 20 import org.eclipse.jface.dialogs.Dialog; 21 import org.eclipse.jface.dialogs.IDialogConstants; 22 import org.eclipse.jface.dialogs.TrayDialog; 23 import org.eclipse.jface.window.IShellProvider; 24 import org.eclipse.osgi.util.NLS; 25 import org.eclipse.swt.SWT; 26 import org.eclipse.swt.events.SelectionAdapter; 27 import org.eclipse.swt.events.SelectionEvent; 28 import org.eclipse.swt.layout.GridData; 29 import org.eclipse.swt.layout.GridLayout; 30 import org.eclipse.swt.widgets.Button; 31 import org.eclipse.swt.widgets.Combo; 32 import org.eclipse.swt.widgets.Composite; 33 import org.eclipse.swt.widgets.Control; 34 import org.eclipse.swt.widgets.Label; 35 import org.eclipse.swt.widgets.Shell; 36 37 41 public class TableSortDialog extends TrayDialog { 42 43 private TableComparator sorter; 44 45 private Combo[] priorityCombos; 46 47 private String [] propertyText; 48 49 private IField[] properties; 50 51 private Button[] ascendingButtons; 52 53 private Button[] descendingButtons; 54 55 private boolean dirty; 56 57 private final Comparator columnComparator = new Comparator () { 58 public int compare(Object arg0, Object arg1) { 59 int index0 = -1; 60 int index1 = -1; 61 for (int i = 0; i < propertyText.length; i++) { 62 if (propertyText[i].equals(arg0)) { 63 index0 = i; 64 } 65 if (propertyText[i].equals(arg1)) { 66 index1 = i; 67 } 68 } 69 return index0 - index1; 70 } 71 }; 72 73 78 public TableSortDialog(IShellProvider parentShell, TableComparator sorter) { 79 super(parentShell); 80 this.sorter = sorter; 81 dirty = false; 82 } 83 84 87 protected void configureShell(Shell newShell) { 88 super.configureShell(newShell); 89 newShell.setText(MarkerMessages.sortDialog_title); 90 } 91 92 95 protected Control createDialogArea(Composite parent) { 96 Composite composite = (Composite) super.createDialogArea(parent); 97 if (sorter == null) { 98 return composite; 99 } 100 101 initializeDialogUnits(composite); 102 103 createPrioritiesArea(composite); 104 createRestoreDefaultsButton(composite); 105 createSeparatorLine(composite); 106 107 Dialog.applyDialogFont(composite); 108 109 return composite; 110 } 111 112 116 private void createPrioritiesArea(Composite parent) { 117 Composite prioritiesArea = new Composite(parent, SWT.NULL); 118 prioritiesArea.setLayout(new GridLayout(3, false)); 119 120 int[] priorities = sorter.getPriorities(); 121 122 ascendingButtons = new Button[priorities.length]; 123 descendingButtons = new Button[priorities.length]; 124 priorityCombos = new Combo[Math.min(priorities.length, 125 TableComparator.MAX_DEPTH)]; 126 initPriotityText(); 127 128 Label sortByLabel = new Label(prioritiesArea, SWT.NULL); 129 sortByLabel.setText(MarkerMessages.sortDialog_label); 130 GridData data = new GridData(); 131 data.horizontalSpan = 3; 132 sortByLabel.setLayoutData(data); 133 134 for (int i = 0; i < priorityCombos.length; i++) { 135 final int index = i; 136 Label numberLabel = new Label(prioritiesArea, SWT.NULL); 137 numberLabel 138 .setText(NLS 139 .bind(MarkerMessages.sortDialog_columnLabel,new Integer (i + 1))); 140 141 priorityCombos[i] = new Combo(prioritiesArea, SWT.READ_ONLY); 142 priorityCombos[i].setLayoutData(new GridData( 143 GridData.FILL_HORIZONTAL)); 144 145 Composite directionGroup = new Composite(prioritiesArea, SWT.NONE); 146 directionGroup.setLayout(new GridLayout(2, false)); 147 148 ascendingButtons[i] = new Button(directionGroup, SWT.RADIO); 149 ascendingButtons[i].setText(getAscendingText(i)); 150 ascendingButtons[i].addSelectionListener(new SelectionAdapter() { 151 public void widgetSelected(SelectionEvent e) { 152 markDirty(); 153 } 154 }); 155 descendingButtons[i] = new Button(directionGroup, SWT.RADIO); 156 descendingButtons[i].setText(getDescendingText(i)); 157 descendingButtons[i].addSelectionListener(new SelectionAdapter() { 158 public void widgetSelected(SelectionEvent e) { 159 markDirty(); 160 } 161 }); 162 163 if (i < priorityCombos.length - 1) { 164 priorityCombos[i].addSelectionListener(new SelectionAdapter() { 165 public void widgetSelected(SelectionEvent e) { 166 int oldSelectionDirection = TableComparator.ASCENDING; 167 if (descendingButtons[index].getSelection()) { 168 oldSelectionDirection = TableComparator.DESCENDING; 169 } 170 ArrayList oldSelectionList = new ArrayList (Arrays 171 .asList(priorityCombos[index].getItems())); 172 oldSelectionList.removeAll(Arrays 173 .asList(priorityCombos[index + 1].getItems())); 174 if (oldSelectionList.size() != 1) { 175 return; 176 } 177 String oldSelection = (String ) oldSelectionList.get(0); 178 String newSelection = priorityCombos[index] 179 .getItem(priorityCombos[index] 180 .getSelectionIndex()); 181 if (oldSelection.equals(newSelection)) { 182 return; 183 } 184 for (int j = index + 1; j < priorityCombos.length; j++) { 185 int newSelectionIndex = priorityCombos[j] 186 .indexOf(newSelection); 187 if (priorityCombos[j].getSelectionIndex() == newSelectionIndex) { 189 priorityCombos[j].remove(newSelection); 190 int insertionPoint = -1 191 - Arrays.binarySearch(priorityCombos[j] 192 .getItems(), oldSelection, 193 columnComparator); 194 if (insertionPoint >= 0 195 && insertionPoint <= priorityCombos[j] 196 .getItemCount()) { 197 priorityCombos[j].add(oldSelection, 198 insertionPoint); 199 } else { 200 priorityCombos[j].add(oldSelection); 201 } 202 priorityCombos[j].select(priorityCombos[j] 203 .indexOf(oldSelection)); 204 ascendingButtons[index] 205 .setSelection(ascendingButtons[j] 206 .getSelection()); 207 descendingButtons[index] 208 .setSelection(descendingButtons[j] 209 .getSelection()); 210 ascendingButtons[j] 211 .setSelection(oldSelectionDirection == TableComparator.ASCENDING); 212 descendingButtons[j] 213 .setSelection(oldSelectionDirection == TableComparator.DESCENDING); 214 } 215 else if (newSelectionIndex >= 0) { 217 priorityCombos[j].remove(newSelection); 218 int insertionPoint = -1 219 - Arrays.binarySearch(priorityCombos[j] 220 .getItems(), oldSelection, 221 columnComparator); 222 if (insertionPoint >= 0 223 && insertionPoint <= priorityCombos[j] 224 .getItemCount()) { 225 priorityCombos[j].add(oldSelection, 226 insertionPoint); 227 } else { 228 priorityCombos[j].add(oldSelection); 229 } 230 } 231 } 232 markDirty(); 233 } 234 }); 235 } else { 236 priorityCombos[i].addSelectionListener(new SelectionAdapter() { 237 public void widgetSelected(SelectionEvent e) { 238 markDirty(); 239 } 240 }); 241 } 242 } 243 updateUIFromSorter(); 244 } 245 246 252 private String getDescendingText(int index) { 253 switch (index) { 254 case 1: 255 return MarkerMessages.sortDirectionDescending_text2; 256 case 2: 257 return MarkerMessages.sortDirectionDescending_text3; 258 case 3: 259 return MarkerMessages.sortDirectionDescending_text4; 260 default: 261 return MarkerMessages.sortDirectionDescending_text; 262 } 263 264 } 265 266 272 private String getAscendingText(int index) { 273 switch (index) { 274 case 1: 275 return MarkerMessages.sortDirectionAscending_text2; 276 case 2: 277 return MarkerMessages.sortDirectionAscending_text3; 278 case 3: 279 return MarkerMessages.sortDirectionAscending_text4; 280 default: 281 return MarkerMessages.sortDirectionAscending_text; 282 } 283 284 } 285 286 290 private void createRestoreDefaultsButton(Composite parent) { 291 Button defaultsButton = new Button(parent, SWT.PUSH); 292 defaultsButton.setText(MarkerMessages.restoreDefaults_text); 293 setButtonSize(defaultsButton, new GridData( 294 GridData.HORIZONTAL_ALIGN_END | GridData.FILL_HORIZONTAL)); 295 defaultsButton.addSelectionListener(new SelectionAdapter() { 296 public void widgetSelected(SelectionEvent e) { 297 restoreDefaults(); 298 markDirty(); 299 } 300 }); 301 } 302 303 private void createSeparatorLine(Composite parent) { 304 Label separator = new Label(parent, SWT.SEPARATOR | SWT.HORIZONTAL); 305 separator.setLayoutData(new GridData(GridData.FILL_HORIZONTAL 306 | GridData.VERTICAL_ALIGN_CENTER)); 307 } 310 311 private void restoreDefaults() { 312 updateUI(sorter.getDefaultPriorities(), sorter.getDefaultDirections()); 313 } 314 315 private void updateUIFromSorter() { 316 updateUI(sorter.getPriorities(), sorter.getDirections()); 317 } 318 319 private void updateUI(int[] priorities, int[] directions) { 320 ArrayList availablePriorities = new ArrayList (Arrays 321 .asList(propertyText)); 322 323 for (int i = 0; i < priorityCombos.length; i++) { 324 priorityCombos[i].removeAll(); 325 for (int j = 0; j < availablePriorities.size(); j++) { 326 priorityCombos[i].add((String ) availablePriorities.get(j)); 327 } 328 priorityCombos[i].select(priorityCombos[i] 329 .indexOf(propertyText[priorities[i]])); 330 availablePriorities.remove(propertyText[priorities[i]]); 331 332 ascendingButtons[i] 333 .setSelection(directions[priorities[i]] == TableComparator.ASCENDING); 334 descendingButtons[i] 335 .setSelection(directions[priorities[i]] == TableComparator.DESCENDING); 336 } 337 } 338 339 protected void okPressed() { 340 if (isDirty()) { 341 for (int i = priorityCombos.length - 1; i >= 0; i--) { 342 String column = priorityCombos[i].getItem(priorityCombos[i] 343 .getSelectionIndex()); 344 int index = -1; 345 for (int j = 0; j < propertyText.length && index == -1; j++) { 346 if (propertyText[j].equals(column)) { 347 index = j; 348 } 349 } 350 if (index == -1) { 351 sorter.resetState(); 352 return; 353 } 354 sorter.setTopPriority(properties[index]); 355 int direction = TableComparator.ASCENDING; 356 if (descendingButtons[i].getSelection()) { 357 direction = TableComparator.DESCENDING; 358 } 359 sorter.setTopPriorityDirection(direction); 360 } 361 } 362 super.okPressed(); 363 } 364 365 368 public boolean isDirty() { 369 return dirty; 370 } 371 372 375 public void markDirty() { 376 dirty = true; 377 } 378 379 384 private void setButtonSize(Button button, GridData buttonData) { 385 button.setFont(button.getParent().getFont()); 386 int widthHint = convertHorizontalDLUsToPixels(IDialogConstants.BUTTON_WIDTH); 387 buttonData.widthHint = Math.max(widthHint, button.computeSize( 388 SWT.DEFAULT, SWT.DEFAULT, true).x); 389 button.setLayoutData(buttonData); 390 } 391 392 private void initPriotityText() { 393 IField[] unorderedProperties = sorter.getFields(); 394 properties = new IField[unorderedProperties.length]; 395 System.arraycopy(unorderedProperties, 0, properties, 0, 396 properties.length); 397 propertyText = new String [properties.length]; 398 for (int i = 0; i < properties.length; i++) { 399 propertyText[i] = properties[i].getDescription(); 400 } 401 } 402 403 407 public TableComparator getSorter() { 408 return sorter; 409 } 410 } 411 | Popular Tags |