1 11 package org.eclipse.search.internal.ui.util; 12 13 import java.util.List ; 14 15 import org.eclipse.swt.SWT; 16 import org.eclipse.swt.events.MouseAdapter; 17 import org.eclipse.swt.events.MouseEvent; 18 import org.eclipse.swt.layout.GridData; 19 import org.eclipse.swt.widgets.Composite; 20 import org.eclipse.swt.widgets.Control; 21 import org.eclipse.swt.widgets.Label; 22 import org.eclipse.swt.widgets.Shell; 23 import org.eclipse.swt.widgets.Table; 24 25 import org.eclipse.jface.dialogs.IDialogConstants; 26 import org.eclipse.jface.viewers.ILabelProvider; 27 import org.eclipse.jface.viewers.ISelection; 28 import org.eclipse.jface.viewers.IStructuredContentProvider; 29 import org.eclipse.jface.viewers.IStructuredSelection; 30 import org.eclipse.jface.viewers.StructuredSelection; 31 import org.eclipse.jface.viewers.TableViewer; 32 33 import org.eclipse.ui.dialogs.SelectionDialog; 34 35 38 public class ListDialog extends SelectionDialog { 39 40 private static final int WIDTH_IN_CHARACTERS= 55; 41 42 private IStructuredContentProvider fContentProvider; 43 private ILabelProvider fLabelProvider; 44 private Object fInput; 45 private TableViewer fViewer; 46 private boolean fCreateCancelButton= true; 47 48 public ListDialog(Shell parent, Object input, String title, String message, IStructuredContentProvider sp, ILabelProvider lp) { 49 super(parent); 50 setTitle(title); 51 setMessage(message); 52 fInput= input; 53 fContentProvider= sp; 54 fLabelProvider= lp; 55 } 56 57 public void setCreateCancelButton(boolean value) { 58 fCreateCancelButton= value; 59 } 60 61 64 protected Label createMessageArea(Composite composite) { 65 Label label = new Label(composite,SWT.WRAP); 66 label.setText(getMessage()); 67 GridData gd= new GridData(GridData.FILL_BOTH); 68 gd.widthHint= convertWidthInCharsToPixels(WIDTH_IN_CHARACTERS); 69 label.setLayoutData(gd); 70 applyDialogFont(label); 71 return label; 72 } 73 74 77 protected Control createDialogArea(Composite container) { 78 Composite parent= (Composite) super.createDialogArea(container); 79 createMessageArea(parent); 80 fViewer= new TableViewer(parent, SWT.SINGLE | SWT.H_SCROLL | SWT.V_SCROLL | SWT.BORDER); 81 fViewer.setContentProvider(fContentProvider); 82 83 final Table table= fViewer.getTable(); 84 table.addMouseListener(new MouseAdapter() { 85 public void mouseDoubleClick(MouseEvent e) { 86 if (fCreateCancelButton) 87 okPressed(); 88 } 89 }); 90 fViewer.setLabelProvider(fLabelProvider); 91 fViewer.setInput(fInput); 92 List initialSelection= getInitialElementSelections(); 93 if (initialSelection != null) 94 fViewer.setSelection(new StructuredSelection(initialSelection)); 95 GridData gd= new GridData(GridData.FILL_BOTH); 96 gd.heightHint= convertHeightInCharsToPixels(15); 97 gd.widthHint= convertWidthInCharsToPixels(WIDTH_IN_CHARACTERS); 98 table.setLayoutData(gd); 99 applyDialogFont(table); 100 return table; 101 } 102 103 106 protected void createButtonsForButtonBar(Composite parent) { 107 if (! fCreateCancelButton) 108 createButton(parent, IDialogConstants.OK_ID, IDialogConstants.OK_LABEL, true); 109 else 110 super.createButtonsForButtonBar(parent); 111 } 112 113 116 protected void okPressed() { 117 ISelection selection= fViewer.getSelection(); 119 if (selection instanceof IStructuredSelection) 120 setResult(((IStructuredSelection)fViewer.getSelection()).toList()); 121 super.okPressed(); 122 } 123 } 124 125 126 | Popular Tags |