1 11 package org.eclipse.ant.internal.ui.launchConfigurations; 12 13 14 import java.util.ArrayList ; 15 import java.util.Arrays ; 16 import java.util.Iterator ; 17 import java.util.List ; 18 import org.eclipse.ant.internal.ui.IAntUIHelpContextIds; 19 import org.eclipse.ant.internal.ui.model.AntModelContentProvider; 20 import org.eclipse.ant.internal.ui.model.AntModelLabelProvider; 21 import org.eclipse.jface.dialogs.Dialog; 22 import org.eclipse.jface.viewers.ISelectionChangedListener; 23 import org.eclipse.jface.viewers.IStructuredSelection; 24 import org.eclipse.jface.viewers.SelectionChangedEvent; 25 import org.eclipse.jface.viewers.TableViewer; 26 import org.eclipse.swt.SWT; 27 import org.eclipse.swt.events.SelectionAdapter; 28 import org.eclipse.swt.events.SelectionEvent; 29 import org.eclipse.swt.layout.GridData; 30 import org.eclipse.swt.layout.GridLayout; 31 import org.eclipse.swt.widgets.Button; 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 import org.eclipse.swt.widgets.Table; 37 import org.eclipse.ui.PlatformUI; 38 39 42 public class TargetOrderDialog extends Dialog implements ISelectionChangedListener { 43 44 private Button fUp; 45 private Button fDown; 46 private TableViewer fViewer; 47 private Object [] fTargets; 48 49 54 public TargetOrderDialog(Shell parentShell, Object [] targets) { 55 super(parentShell); 56 fTargets = targets; 57 } 58 59 62 protected Control createDialogArea(Composite parent) { 63 getShell().setText(AntLaunchConfigurationMessages.TargetOrderDialog_Order_Targets_1); 64 65 Composite comp = (Composite)super.createDialogArea(parent); 66 ((GridLayout)comp.getLayout()).numColumns= 2; 67 Label label = new Label(comp, SWT.NONE); 68 label.setText(AntLaunchConfigurationMessages.TargetOrderDialog__Specify_target_execution_order__2); 69 label.setFont(comp.getFont()); 70 GridData gd = new GridData(GridData.FILL_HORIZONTAL); 71 gd.horizontalSpan = 2; 72 label.setLayoutData(gd); 73 74 createTargetList(comp); 75 76 createButtons(comp); 77 78 updateButtons(); 79 80 return comp; 81 } 82 83 88 private void createButtons(Composite parent) { 89 Composite comp = new Composite(parent, SWT.NONE); 90 GridLayout layout = new GridLayout(); 91 layout.marginHeight= 0; 92 layout.marginWidth= 0; 93 GridData gd = new GridData(GridData.HORIZONTAL_ALIGN_FILL); 94 gd.verticalAlignment = GridData.BEGINNING; 95 comp.setLayout(layout); 96 comp.setLayoutData(gd); 97 98 fUp = new Button(comp, SWT.PUSH); 99 fUp.setFont(parent.getFont()); 100 fUp.setText(AntLaunchConfigurationMessages.TargetOrderDialog__Up_3); 101 setButtonLayoutData(fUp); 102 fUp.addSelectionListener(new SelectionAdapter() { 103 public void widgetSelected(SelectionEvent e) { 104 handleUpPressed(); 105 } 106 }); 107 108 fDown = new Button(comp, SWT.PUSH); 109 fDown.setFont(parent.getFont()); 110 fDown.setText(AntLaunchConfigurationMessages.TargetOrderDialog__Down_4); 111 setButtonLayoutData(fDown); 112 fDown.addSelectionListener(new SelectionAdapter() { 113 public void widgetSelected(SelectionEvent e) { 114 handleDownPressed(); 115 } 116 }); 117 } 118 119 private void handleDownPressed() { 120 List targets = getOrderedSelection(); 121 if (targets.isEmpty()) { 122 return; 123 } 124 List list= new ArrayList (Arrays.asList(fTargets)); 125 int bottom = list.size() - 1; 126 int index = 0; 127 for (int i = targets.size() - 1; i >= 0; i--) { 128 Object target = targets.get(i); 129 index = list.indexOf(target); 130 if (index < bottom) { 131 bottom = index + 1; 132 Object temp = list.get(bottom); 133 list.set(bottom, target); 134 list.set(index, temp); 135 } 136 bottom = index; 137 } 138 setEntries(list); 139 } 140 141 private void handleUpPressed() { 142 List targets = getOrderedSelection(); 143 if (targets.isEmpty()) { 144 return; 145 } 146 int top = 0; 147 int index = 0; 148 List list= new ArrayList (Arrays.asList(fTargets)); 149 Iterator entries = targets.iterator(); 150 while (entries.hasNext()) { 151 Object target = entries.next(); 152 index = list.indexOf(target); 153 if (index > top) { 154 top = index - 1; 155 Object temp = list.get(top); 156 list.set(top, target); 157 list.set(index, temp); 158 } 159 top = index; 160 } 161 setEntries(list); 162 } 163 164 167 private void setEntries(List list) { 168 fTargets= list.toArray(); 169 fViewer.setInput(fTargets); 170 fViewer.setSelection(fViewer.getSelection()); 172 } 173 174 180 private List getOrderedSelection() { 181 List targets = new ArrayList (); 182 List selection = ((IStructuredSelection)fViewer.getSelection()).toList(); 183 Object [] entries = fTargets; 184 for (int i = 0; i < entries.length; i++) { 185 Object target = entries[i]; 186 if (selection.contains(target)) { 187 targets.add(target); 188 } 189 } 190 return targets; 191 } 192 193 198 private void createTargetList(Composite comp) { 199 fViewer = new TableViewer(comp, SWT.MULTI | SWT.FULL_SELECTION | SWT.BORDER); 200 fViewer.setLabelProvider(new AntModelLabelProvider()); 201 202 fViewer.setContentProvider(new AntModelContentProvider()); 203 fViewer.setInput(fTargets); 204 fViewer.addSelectionChangedListener(this); 205 Table table = fViewer.getTable(); 206 GridData gd = new GridData(GridData.FILL_BOTH); 207 gd.heightHint = 200; 208 gd.widthHint = 250; 209 table.setLayoutData(gd); 210 table.setFont(comp.getFont()); 211 } 212 213 216 public Object [] getTargets() { 217 return fTargets; 218 } 219 220 225 public void selectionChanged(SelectionChangedEvent event) { 226 updateButtons(); 227 } 228 229 private void updateButtons() { 230 int[] selections = fViewer.getTable().getSelectionIndices(); 231 int last = fTargets.length - 1; 232 boolean up = true && selections.length > 0; 233 boolean down = true && selections.length > 0; 234 for (int i = 0; i < selections.length; i++) { 235 if (selections[i] == 0) { 236 up = false; 237 } 238 if (selections[i] == last) { 239 down = false; 240 } 241 } 242 fUp.setEnabled(up); 243 fDown.setEnabled(down); 244 } 245 246 249 protected void configureShell(Shell shell) { 250 super.configureShell(shell); 251 PlatformUI.getWorkbench().getHelpSystem().setHelp(shell, IAntUIHelpContextIds.TARGET_ORDER_DIALOG); 252 } 253 } 254 | Popular Tags |