1 11 package org.eclipse.jdt.internal.ui.wizards.dialogfields; 12 13 import java.util.ArrayList ; 14 import java.util.Collection ; 15 import java.util.List ; 16 17 import org.eclipse.core.runtime.Assert; 18 19 import org.eclipse.swt.SWT; 20 import org.eclipse.swt.widgets.Composite; 21 import org.eclipse.swt.widgets.Control; 22 import org.eclipse.swt.widgets.Table; 23 24 import org.eclipse.jface.viewers.CheckStateChangedEvent; 25 import org.eclipse.jface.viewers.CheckboxTableViewer; 26 import org.eclipse.jface.viewers.ICheckStateListener; 27 import org.eclipse.jface.viewers.ILabelProvider; 28 import org.eclipse.jface.viewers.ISelection; 29 import org.eclipse.jface.viewers.TableViewer; 30 31 36 public class CheckedListDialogField extends ListDialogField { 37 38 private int fCheckAllButtonIndex; 39 private int fUncheckAllButtonIndex; 40 41 private List fCheckedElements; 42 private List fGrayedElements; 43 44 public CheckedListDialogField(IListAdapter adapter, String [] customButtonLabels, ILabelProvider lprovider) { 45 super(adapter, customButtonLabels, lprovider); 46 fCheckedElements= new ArrayList (); 47 fGrayedElements= new ArrayList (); 48 49 fCheckAllButtonIndex= -1; 50 fUncheckAllButtonIndex= -1; 51 } 52 53 58 public void setCheckAllButtonIndex(int checkButtonIndex) { 59 Assert.isTrue(checkButtonIndex < fButtonLabels.length); 60 fCheckAllButtonIndex= checkButtonIndex; 61 } 62 63 68 public void setUncheckAllButtonIndex(int uncheckButtonIndex) { 69 Assert.isTrue(uncheckButtonIndex < fButtonLabels.length); 70 fUncheckAllButtonIndex= uncheckButtonIndex; 71 } 72 73 74 77 protected TableViewer createTableViewer(Composite parent) { 78 Table table= new Table(parent, SWT.CHECK + getListStyle()); 79 table.setFont(parent.getFont()); 80 CheckboxTableViewer tableViewer= new CheckboxTableViewer(table); 81 tableViewer.addCheckStateListener(new ICheckStateListener() { 82 public void checkStateChanged(CheckStateChangedEvent e) { 83 doCheckStateChanged(e); 84 } 85 }); 86 return tableViewer; 87 } 88 89 90 93 public Control getListControl(Composite parent) { 94 Control control= super.getListControl(parent); 95 if (parent != null) { 96 ((CheckboxTableViewer)fTable).setCheckedElements(fCheckedElements.toArray()); 97 ((CheckboxTableViewer)fTable).setGrayedElements(fGrayedElements.toArray()); 98 } 99 return control; 100 } 101 102 106 public void dialogFieldChanged() { 107 for (int i= fCheckedElements.size() -1; i >= 0; i--) { 108 if (!fElements.contains(fCheckedElements.get(i))) { 109 fCheckedElements.remove(i); 110 } 111 } 112 super.dialogFieldChanged(); 113 } 114 115 private void checkStateChanged() { 116 super.dialogFieldChanged(); 118 } 119 120 123 public List getCheckedElements() { 124 if (isOkToUse(fTableControl)) { 125 Object [] checked= ((CheckboxTableViewer) fTable).getCheckedElements(); 127 ArrayList res= new ArrayList (checked.length); 128 for (int i= 0; i < checked.length; i++) { 129 res.add(checked[i]); 130 } 131 return res; 132 } 133 134 135 return new ArrayList (fCheckedElements); 136 } 137 138 141 public int getCheckedSize() { 142 return fCheckedElements.size(); 143 } 144 145 148 public boolean isChecked(Object obj) { 149 if (isOkToUse(fTableControl)) { 150 return ((CheckboxTableViewer) fTable).getChecked(obj); 151 } 152 153 return fCheckedElements.contains(obj); 154 } 155 156 public boolean isGrayed(Object obj) { 157 if (isOkToUse(fTableControl)) { 158 return ((CheckboxTableViewer) fTable).getGrayed(obj); 159 } 160 161 return fGrayedElements.contains(obj); 162 } 163 164 167 public void setCheckedElements(Collection list) { 168 fCheckedElements= new ArrayList (list); 169 if (isOkToUse(fTableControl)) { 170 ((CheckboxTableViewer)fTable).setCheckedElements(list.toArray()); 171 } 172 checkStateChanged(); 173 } 174 175 178 public void setChecked(Object object, boolean state) { 179 setCheckedWithoutUpdate(object, state); 180 checkStateChanged(); 181 } 182 183 186 public void setCheckedWithoutUpdate(Object object, boolean state) { 187 if (state) { 188 if (!fCheckedElements.contains(object)) { 189 fCheckedElements.add(object); 190 } 191 } else { 192 fCheckedElements.remove(object); 193 } 194 if (isOkToUse(fTableControl)) { 195 ((CheckboxTableViewer)fTable).setChecked(object, state); 196 } 197 } 198 199 public void setGrayedWithoutUpdate(Object object, boolean state) { 200 if (state) { 201 if (!fGrayedElements.contains(object)) { 202 fGrayedElements.add(object); 203 } 204 } else { 205 fGrayedElements.remove(object); 206 } 207 if (isOkToUse(fTableControl)) { 208 ((CheckboxTableViewer)fTable).setGrayed(object, state); 209 } 210 } 211 212 215 public void checkAll(boolean state) { 216 if (state) { 217 fCheckedElements= getElements(); 218 } else { 219 fCheckedElements.clear(); 220 } 221 if (isOkToUse(fTableControl)) { 222 ((CheckboxTableViewer)fTable).setAllChecked(state); 223 } 224 checkStateChanged(); 225 } 226 227 228 private void doCheckStateChanged(CheckStateChangedEvent e) { 229 if (e.getChecked()) { 230 fCheckedElements.add(e.getElement()); 231 } else { 232 fCheckedElements.remove(e.getElement()); 233 } 234 checkStateChanged(); 235 } 236 237 240 public void replaceElement(Object oldElement, Object newElement) throws IllegalArgumentException { 241 boolean wasChecked= isChecked(oldElement); 242 super.replaceElement(oldElement, newElement); 243 setChecked(newElement, wasChecked); 244 } 245 246 248 251 protected boolean getManagedButtonState(ISelection sel, int index) { 252 if (index == fCheckAllButtonIndex) { 253 return !fElements.isEmpty(); 254 } else if (index == fUncheckAllButtonIndex) { 255 return !fElements.isEmpty(); 256 } 257 return super.getManagedButtonState(sel, index); 258 } 259 260 263 protected boolean managedButtonPressed(int index) { 264 if (index == fCheckAllButtonIndex) { 265 checkAll(true); 266 } else if (index == fUncheckAllButtonIndex) { 267 checkAll(false); 268 } else { 269 return super.managedButtonPressed(index); 270 } 271 return true; 272 } 273 274 public void refresh() { 275 super.refresh(); 276 if (isOkToUse(fTableControl)) { 277 ((CheckboxTableViewer)fTable).setCheckedElements(fCheckedElements.toArray()); 278 ((CheckboxTableViewer)fTable).setGrayedElements(fGrayedElements.toArray()); 279 } 280 } 281 282 283 284 285 286 } 287 | Popular Tags |