1 13 package org.eclipse.ui.dialogs; 14 15 import java.util.ArrayList ; 16 import java.util.Iterator ; 17 18 import org.eclipse.jface.dialogs.Dialog; 19 import org.eclipse.jface.dialogs.IDialogConstants; 20 import org.eclipse.jface.viewers.CheckboxTableViewer; 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.swt.widgets.Control; 32 import org.eclipse.swt.widgets.Shell; 33 import org.eclipse.ui.PlatformUI; 34 import org.eclipse.ui.internal.IWorkbenchHelpContextIds; 35 import org.eclipse.ui.internal.WorkbenchMessages; 36 37 61 public class ListSelectionDialog extends SelectionDialog { 62 private Object inputElement; 64 65 private ILabelProvider labelProvider; 67 68 private IStructuredContentProvider contentProvider; 69 70 CheckboxTableViewer listViewer; 72 73 private final static int SIZING_SELECTION_WIDGET_HEIGHT = 250; 75 76 private final static int SIZING_SELECTION_WIDGET_WIDTH = 300; 77 78 88 public ListSelectionDialog(Shell parentShell, Object input, 89 IStructuredContentProvider contentProvider, 90 ILabelProvider labelProvider, String message) { 91 super(parentShell); 92 setTitle(WorkbenchMessages.ListSelection_title); 93 inputElement = input; 94 this.contentProvider = contentProvider; 95 this.labelProvider = labelProvider; 96 if (message != null) { 97 setMessage(message); 98 } else { 99 setMessage(WorkbenchMessages.ListSelection_message); 100 } 101 } 102 103 107 private void addSelectionButtons(Composite composite) { 108 Composite buttonComposite = new Composite(composite, SWT.NONE); 109 GridLayout layout = new GridLayout(); 110 layout.numColumns = 0; 111 layout.marginWidth = 0; 112 layout.horizontalSpacing = convertHorizontalDLUsToPixels(IDialogConstants.HORIZONTAL_SPACING); 113 buttonComposite.setLayout(layout); 114 buttonComposite.setLayoutData(new GridData(SWT.END, SWT.TOP, true, false)); 115 116 Button selectButton = createButton(buttonComposite, 117 IDialogConstants.SELECT_ALL_ID, SELECT_ALL_TITLE, false); 118 119 SelectionListener listener = new SelectionAdapter() { 120 public void widgetSelected(SelectionEvent e) { 121 listViewer.setAllChecked(true); 122 } 123 }; 124 selectButton.addSelectionListener(listener); 125 126 Button deselectButton = createButton(buttonComposite, 127 IDialogConstants.DESELECT_ALL_ID, DESELECT_ALL_TITLE, false); 128 129 listener = new SelectionAdapter() { 130 public void widgetSelected(SelectionEvent e) { 131 listViewer.setAllChecked(false); 132 } 133 }; 134 deselectButton.addSelectionListener(listener); 135 } 136 137 141 private void checkInitialSelections() { 142 Iterator itemsToCheck = getInitialElementSelections().iterator(); 143 144 while (itemsToCheck.hasNext()) { 145 listViewer.setChecked(itemsToCheck.next(), true); 146 } 147 } 148 149 153 protected void configureShell(Shell shell) { 154 super.configureShell(shell); 155 PlatformUI.getWorkbench().getHelpSystem().setHelp(shell, 156 IWorkbenchHelpContextIds.LIST_SELECTION_DIALOG); 157 } 158 159 162 protected Control createDialogArea(Composite parent) { 163 Composite composite = (Composite) super.createDialogArea(parent); 165 166 initializeDialogUnits(composite); 167 168 createMessageArea(composite); 169 170 listViewer = CheckboxTableViewer.newCheckList(composite, SWT.BORDER); 171 GridData data = new GridData(GridData.FILL_BOTH); 172 data.heightHint = SIZING_SELECTION_WIDGET_HEIGHT; 173 data.widthHint = SIZING_SELECTION_WIDGET_WIDTH; 174 listViewer.getTable().setLayoutData(data); 175 176 listViewer.setLabelProvider(labelProvider); 177 listViewer.setContentProvider(contentProvider); 178 179 addSelectionButtons(composite); 180 181 initializeViewer(); 182 183 if (!getInitialElementSelections().isEmpty()) { 185 checkInitialSelections(); 186 } 187 188 Dialog.applyDialogFont(composite); 189 190 return composite; 191 } 192 193 198 protected CheckboxTableViewer getViewer() { 199 return listViewer; 200 } 201 202 205 private void initializeViewer() { 206 listViewer.setInput(inputElement); 207 } 208 209 214 protected void okPressed() { 215 216 Object [] children = contentProvider.getElements(inputElement); 218 219 if (children != null) { 221 ArrayList list = new ArrayList (); 222 for (int i = 0; i < children.length; ++i) { 223 Object element = children[i]; 224 if (listViewer.getChecked(element)) { 225 list.add(element); 226 } 227 } 228 setResult(list); 229 } 230 231 super.okPressed(); 232 } 233 } 234 | Popular Tags |