1 11 package org.eclipse.team.internal.ccvs.ui; 12 13 import java.util.ArrayList ; 14 import java.util.Iterator ; 15 import java.util.List ; 16 17 import org.eclipse.jface.dialogs.Dialog; 18 import org.eclipse.jface.viewers.CheckStateChangedEvent; 19 import org.eclipse.jface.viewers.CheckboxTableViewer; 20 import org.eclipse.jface.viewers.ICheckStateListener; 21 import org.eclipse.jface.viewers.ILabelProvider; 22 import org.eclipse.jface.viewers.IStructuredContentProvider; 23 import org.eclipse.swt.SWT; 24 import org.eclipse.swt.events.SelectionAdapter; 25 import org.eclipse.swt.events.SelectionEvent; 26 import org.eclipse.swt.events.SelectionListener; 27 import org.eclipse.swt.layout.GridData; 28 import org.eclipse.swt.layout.GridLayout; 29 import org.eclipse.swt.widgets.Button; 30 import org.eclipse.swt.widgets.Composite; 31 import org.eclipse.team.internal.ui.dialogs.DialogArea; 32 33 37 public class ListSelectionArea extends DialogArea { 38 private Object inputElement; 39 private IStructuredContentProvider contentProvider; 40 private ILabelProvider labelProvider; 41 private String message; 42 private List initialSelections; 43 44 private CheckboxTableViewer listViewer; 46 47 private Object [] previousCheckedElements; 48 49 public static final String LIST_SELECTION = "ListSelection"; 51 56 public ListSelectionArea( 57 Object input, 58 IStructuredContentProvider contentProvider, 59 ILabelProvider labelProvider, 60 String message) { 61 this.inputElement = input; 62 this.contentProvider = contentProvider; 63 this.labelProvider = labelProvider; 64 this.message = message; 65 this.initialSelections = new ArrayList (); 66 } 67 68 71 public void createArea(Composite parent) { 72 73 Dialog.applyDialogFont(parent); 74 75 final Composite composite = createComposite(parent, 1, true); 76 77 initializeDialogUnits(composite); 78 79 if (message != null) 80 createWrappingLabel(composite, message, 1); 81 82 listViewer = CheckboxTableViewer.newCheckList(composite, SWT.BORDER); 83 GridData data = new GridData(SWT.FILL, SWT.FILL, true, true); 84 data.heightHint = 0; data.widthHint = 0; 86 listViewer.getTable().setLayoutData(data); 87 88 listViewer.setLabelProvider(labelProvider); 89 listViewer.setContentProvider(contentProvider); 90 91 listViewer.addCheckStateListener(new ICheckStateListener() { 92 public void checkStateChanged(CheckStateChangedEvent event) { 93 Object [] checkedElements = getViewer().getCheckedElements(); 94 firePropertyChangeChange(LIST_SELECTION, previousCheckedElements, checkedElements); 95 previousCheckedElements = checkedElements; 96 } 97 }); 98 99 addSelectionButtons(composite); 100 101 initializeViewer(); 102 103 if (!getInitialElementSelections().isEmpty()) 105 checkInitialSelections(); 106 } 107 108 111 private void initializeViewer() { 112 listViewer.setInput(inputElement); 113 } 114 115 119 private void checkInitialSelections() { 120 Iterator itemsToCheck = getInitialElementSelections().iterator(); 121 122 while (itemsToCheck.hasNext()) 123 listViewer.setChecked(itemsToCheck.next(),true); 124 } 125 126 130 private void addSelectionButtons(Composite composite) { 131 Composite buttonComposite = new Composite(composite, SWT.RIGHT); 132 buttonComposite.setLayout(new GridLayout(2, false)); 133 buttonComposite.setData(new GridData(SWT.END, SWT.BEGINNING, true, false)); 134 135 Button selectButton = createButton(buttonComposite, CVSUIMessages.ListSelectionArea_selectAll, GridData.HORIZONTAL_ALIGN_FILL); 136 137 SelectionListener listener = new SelectionAdapter() { 138 public void widgetSelected(SelectionEvent e) { 139 listViewer.setAllChecked(true); 140 } 141 }; 142 selectButton.addSelectionListener(listener); 143 144 145 Button deselectButton = createButton(buttonComposite, CVSUIMessages.ListSelectionArea_deselectAll, GridData.HORIZONTAL_ALIGN_FILL); 146 147 listener = new SelectionAdapter() { 148 public void widgetSelected(SelectionEvent e) { 149 listViewer.setAllChecked(false); 150 151 } 152 }; 153 deselectButton.addSelectionListener(listener); 154 } 155 156 160 protected List getInitialElementSelections(){ 161 return initialSelections; 162 } 163 164 168 public CheckboxTableViewer getViewer() { 169 return listViewer; 170 } 171 172 } 173 | Popular Tags |