1 11 package org.eclipse.jdt.internal.junit.util; 12 13 import java.util.ArrayList ; 14 import java.util.Arrays ; 15 import java.util.List ; 16 17 import org.eclipse.core.runtime.IStatus; 18 19 import org.eclipse.swt.SWT; 20 import org.eclipse.swt.custom.BusyIndicator; 21 import org.eclipse.swt.events.SelectionAdapter; 22 import org.eclipse.swt.events.SelectionEvent; 23 import org.eclipse.swt.events.SelectionListener; 24 import org.eclipse.swt.layout.GridData; 25 import org.eclipse.swt.layout.GridLayout; 26 import org.eclipse.swt.widgets.Button; 27 import org.eclipse.swt.widgets.Composite; 28 import org.eclipse.swt.widgets.Control; 29 import org.eclipse.swt.widgets.Label; 30 import org.eclipse.swt.widgets.Shell; 31 import org.eclipse.swt.widgets.Table; 32 33 import org.eclipse.jface.dialogs.IDialogConstants; 34 import org.eclipse.jface.viewers.CheckStateChangedEvent; 35 import org.eclipse.jface.viewers.CheckboxTableViewer; 36 import org.eclipse.jface.viewers.ICheckStateListener; 37 import org.eclipse.jface.viewers.ILabelProvider; 38 import org.eclipse.jface.viewers.IStructuredContentProvider; 39 import org.eclipse.jface.viewers.ViewerFilter; 40 41 import org.eclipse.ui.dialogs.ISelectionStatusValidator; 42 import org.eclipse.ui.dialogs.SelectionStatusDialog; 43 44 import org.eclipse.jdt.internal.junit.wizards.WizardMessages; 45 46 49 public class CheckedTableSelectionDialog extends SelectionStatusDialog { 50 51 private CheckboxTableViewer fViewer; 52 53 private ILabelProvider fLabelProvider; 54 private IStructuredContentProvider fContentProvider; 55 56 private ISelectionStatusValidator fValidator= null; 57 private String fEmptyListMessage= WizardMessages.CheckedTableSelectionDialog_emptyListMessage; 58 59 private IStatus fCurrStatus= new JUnitStatus(); 60 private List fFilters; 61 private Object fInput; 62 private boolean fIsEmpty; 63 64 private int fWidth= 40; 65 private int fHeight= 18; 66 67 72 public CheckedTableSelectionDialog(Shell parent, ILabelProvider labelProvider, 73 IStructuredContentProvider contentProvider) { 74 super(parent); 75 76 fLabelProvider= labelProvider; 77 fContentProvider= contentProvider; 78 79 setResult(new ArrayList (0)); 80 setStatusLineAboveButtons(true); 81 } 82 83 88 public void setInitialSelection(Object selection) { 89 setInitialSelections(new Object [] {selection}); 90 } 91 92 96 public void setEmptyListMessage(String message) { 97 fEmptyListMessage= message; 98 } 99 100 104 public void addFilter(ViewerFilter filter) { 105 if (fFilters == null) 106 fFilters= new ArrayList (4); 107 108 fFilters.add(filter); 109 } 110 111 116 public void setValidator(ISelectionStatusValidator validator) { 117 fValidator= validator; 118 } 119 120 124 public void setInput(Object input) { 125 fInput= input; 126 } 127 128 133 public void setSize(int width, int height) { 134 fWidth= width; 135 fHeight= height; 136 } 137 138 protected void updateOKStatus() { 139 if (!fIsEmpty) { 140 if (fValidator != null) { 141 fCurrStatus= fValidator.validate(fViewer.getCheckedElements()); 142 updateStatus(fCurrStatus); 143 } else if (!fCurrStatus.isOK()) { 144 fCurrStatus= new JUnitStatus(); 145 } 146 } else { 147 fCurrStatus= new JUnitStatus(IStatus.ERROR, fEmptyListMessage); 148 } 149 updateStatus(fCurrStatus); 150 } 151 152 155 public int open() { 156 fIsEmpty= evaluateIfTableEmpty(fInput); 157 BusyIndicator.showWhile(null, new Runnable () { 158 public void run() { 159 access$superOpen(); 160 } 161 }); 162 return getReturnCode(); 163 } 164 165 private void access$superOpen() { 166 super.open(); 167 } 168 169 172 protected void cancelPressed() { 173 setResult(null); 174 super.cancelPressed(); 175 } 176 177 180 protected void computeResult() { 181 setResult(Arrays.asList(fViewer.getCheckedElements())); 182 } 183 184 187 public void create() { 188 super.create(); 189 190 List initialSelections= getInitialElementSelections(); 191 if (initialSelections.size() > 0) { 192 fViewer.setCheckedElements(initialSelections.toArray()); 193 } 194 195 updateOKStatus(); 196 } 197 198 201 protected Control createDialogArea(Composite parent) { 202 Composite composite= (Composite) super.createDialogArea(parent); 203 204 Label messageLabel= createMessageArea(composite); 205 Control treeWidget= createTableViewer(composite); 206 Control buttonComposite= createSelectionButtons(composite); 207 208 GridData data= new GridData(GridData.FILL_BOTH); 209 data.widthHint= convertWidthInCharsToPixels(fWidth); 210 data.heightHint= convertHeightInCharsToPixels(fHeight); 211 treeWidget.setLayoutData(data); 212 213 if (fIsEmpty) { 214 messageLabel.setEnabled(false); 215 treeWidget.setEnabled(false); 216 buttonComposite.setEnabled(false); 217 } 218 applyDialogFont(composite); 219 return composite; 220 } 221 222 private Table createTableViewer(Composite parent) { 223 fViewer= CheckboxTableViewer.newCheckList(parent, SWT.BORDER); 224 225 fViewer.setContentProvider(fContentProvider); 226 fViewer.setLabelProvider(fLabelProvider); 227 fViewer.addCheckStateListener(new ICheckStateListener() { 228 public void checkStateChanged(CheckStateChangedEvent event) { 229 updateOKStatus(); 230 } 231 }); 232 233 if (fFilters != null) { 234 for (int i= 0; i != fFilters.size(); i++) 235 fViewer.addFilter((ViewerFilter) fFilters.get(i)); 236 } 237 238 fViewer.setInput(fInput); 239 return fViewer.getTable(); 240 } 241 242 246 private Composite createSelectionButtons(Composite composite) { 247 Composite buttonComposite= new Composite(composite, SWT.RIGHT); 248 GridLayout layout= new GridLayout(); 249 layout.numColumns= 2; 250 buttonComposite.setLayout(layout); 251 GridData data= new GridData(GridData.HORIZONTAL_ALIGN_END | GridData.GRAB_HORIZONTAL); 252 data.grabExcessHorizontalSpace= true; 253 composite.setData(data); 254 255 Button selectButton= createButton(buttonComposite, IDialogConstants.SELECT_ALL_ID, WizardMessages.CheckedTableSelectionDialog_selectAll, false); 256 257 SelectionListener listener= new SelectionAdapter() { 258 public void widgetSelected(SelectionEvent e) { 259 fViewer.setCheckedElements(fContentProvider.getElements(fInput)); 260 updateOKStatus(); 261 } 262 }; 263 selectButton.addSelectionListener(listener); 264 265 Button deselectButton= createButton(buttonComposite, IDialogConstants.DESELECT_ALL_ID, WizardMessages.CheckedTableSelectionDialog_deselectAll, false); 266 267 listener= new SelectionAdapter() { 268 public void widgetSelected(SelectionEvent e) { 269 fViewer.setCheckedElements(new Object [0]); 270 updateOKStatus(); 271 } 272 }; 273 deselectButton.addSelectionListener(listener); 274 return buttonComposite; 275 } 276 277 private boolean evaluateIfTableEmpty(Object input) { 278 Object [] elements= fContentProvider.getElements(input); 279 if (elements.length > 0) { 280 if (fFilters != null) { 281 for (int i= 0; i < fFilters.size(); i++) { 282 ViewerFilter curr= (ViewerFilter)fFilters.get(i); 283 elements= curr.filter(fViewer, input, elements); 284 } 285 } 286 } 287 return elements.length == 0; 288 } 289 } 290 | Popular Tags |