KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > search > internal > ui > util > ListDialog


1 /*******************************************************************************
2  * Copyright (c) 2000, 2006 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.search.internal.ui.util;
12
13 import java.util.List JavaDoc;
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 /**
36  * Dialog that shows a list of items with icon and label.
37  */

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 JavaDoc fInput;
45     private TableViewer fViewer;
46     private boolean fCreateCancelButton= true;
47     
48     public ListDialog(Shell parent, Object JavaDoc input, String JavaDoc title, String JavaDoc 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     /*
62      * Overrides method from Dialog
63      */

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     /*
75      * Overrides method from Dialog
76      */

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 JavaDoc 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     /*
104      * Overrides method from Dialog
105      */

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     /*
114      * Overrides method from Dialog
115      */

116     protected void okPressed() {
117         // Build a list of selected children.
118
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