1 11 package org.eclipse.ui.dialogs; 12 13 import java.util.List ; 14 15 import org.eclipse.jface.dialogs.IDialogConstants; 16 import org.eclipse.jface.viewers.DoubleClickEvent; 17 import org.eclipse.jface.viewers.IDoubleClickListener; 18 import org.eclipse.jface.viewers.ILabelProvider; 19 import org.eclipse.jface.viewers.IStructuredContentProvider; 20 import org.eclipse.jface.viewers.IStructuredSelection; 21 import org.eclipse.jface.viewers.StructuredSelection; 22 import org.eclipse.jface.viewers.TableViewer; 23 import org.eclipse.swt.SWT; 24 import org.eclipse.swt.layout.GridData; 25 import org.eclipse.swt.widgets.Composite; 26 import org.eclipse.swt.widgets.Control; 27 import org.eclipse.swt.widgets.Shell; 28 import org.eclipse.swt.widgets.Table; 29 30 37 public class ListDialog extends SelectionDialog { 38 private IStructuredContentProvider fContentProvider; 39 40 private ILabelProvider fLabelProvider; 41 42 private Object fInput; 43 44 private TableViewer fTableViewer; 45 46 private boolean fAddCancelButton = true; 47 48 private int widthInChars = 55; 49 50 private int heightInChars = 15; 51 52 56 public ListDialog(Shell parent) { 57 super(parent); 58 } 59 60 63 public void setInput(Object input) { 64 fInput = input; 65 } 66 67 70 public void setContentProvider(IStructuredContentProvider sp) { 71 fContentProvider = sp; 72 } 73 74 77 public void setLabelProvider(ILabelProvider lp) { 78 fLabelProvider = lp; 79 } 80 81 85 public void setAddCancelButton(boolean addCancelButton) { 86 fAddCancelButton = addCancelButton; 87 } 88 89 92 public TableViewer getTableViewer() { 93 return fTableViewer; 94 } 95 96 protected void createButtonsForButtonBar(Composite parent) { 97 if (!fAddCancelButton) { 98 createButton(parent, IDialogConstants.OK_ID, 99 IDialogConstants.OK_LABEL, true); 100 } else { 101 super.createButtonsForButtonBar(parent); 102 } 103 } 104 105 protected Control createDialogArea(Composite container) { 106 Composite parent = (Composite) super.createDialogArea(container); 107 createMessageArea(parent); 108 fTableViewer = new TableViewer(parent, getTableStyle()); 109 fTableViewer.setContentProvider(fContentProvider); 110 fTableViewer.setLabelProvider(fLabelProvider); 111 fTableViewer.setInput(fInput); 112 fTableViewer.addDoubleClickListener(new IDoubleClickListener() { 113 public void doubleClick(DoubleClickEvent event) { 114 if (fAddCancelButton) { 115 okPressed(); 116 } 117 } 118 }); 119 List initialSelection = getInitialElementSelections(); 120 if (initialSelection != null) { 121 fTableViewer 122 .setSelection(new StructuredSelection(initialSelection)); 123 } 124 GridData gd = new GridData(GridData.FILL_BOTH); 125 gd.heightHint = convertHeightInCharsToPixels(heightInChars); 126 gd.widthHint = convertWidthInCharsToPixels(widthInChars); 127 Table table = fTableViewer.getTable(); 128 table.setLayoutData(gd); 129 table.setFont(container.getFont()); 130 return parent; 131 } 132 133 137 protected int getTableStyle() { 138 return SWT.SINGLE | SWT.H_SCROLL | SWT.V_SCROLL | SWT.BORDER; 139 } 140 141 144 protected void okPressed() { 145 IStructuredSelection selection = (IStructuredSelection) fTableViewer 147 .getSelection(); 148 setResult(selection.toList()); 149 super.okPressed(); 150 } 151 152 157 public int getHeightInChars() { 158 return heightInChars; 159 } 160 161 166 public int getWidthInChars() { 167 return widthInChars; 168 } 169 170 176 public void setHeightInChars(int heightInChars) { 177 this.heightInChars = heightInChars; 178 } 179 180 186 public void setWidthInChars(int widthInChars) { 187 this.widthInChars = widthInChars; 188 } 189 } 190 | Popular Tags |