KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ui > dialogs > 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.ui.dialogs;
12
13 import java.util.List JavaDoc;
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 /**
31  * A dialog that prompts for one element out of a list of elements. Uses
32  * <code>IStructuredContentProvider</code> to provide the elements and
33  * <code>ILabelProvider</code> to provide their labels.
34  *
35  * @since 2.1
36  */

37 public class ListDialog extends SelectionDialog {
38     private IStructuredContentProvider fContentProvider;
39
40     private ILabelProvider fLabelProvider;
41
42     private Object JavaDoc 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     /**
53      * Create a new instance of the receiver with parent shell of parent.
54      * @param parent
55      */

56     public ListDialog(Shell parent) {
57         super(parent);
58     }
59
60     /**
61      * @param input The input for the list.
62      */

63     public void setInput(Object JavaDoc input) {
64         fInput = input;
65     }
66
67     /**
68      * @param sp The content provider for the list.
69      */

70     public void setContentProvider(IStructuredContentProvider sp) {
71         fContentProvider = sp;
72     }
73
74     /**
75      * @param lp The labelProvider for the list.
76      */

77     public void setLabelProvider(ILabelProvider lp) {
78         fLabelProvider = lp;
79     }
80
81     /**
82      *@param addCancelButton if <code>true</code> there will be a cancel
83      * button.
84      */

85     public void setAddCancelButton(boolean addCancelButton) {
86         fAddCancelButton = addCancelButton;
87     }
88
89     /**
90      * @return the TableViewer for the receiver.
91      */

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 JavaDoc 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     /**
134      * Return the style flags for the table viewer.
135      * @return int
136      */

137     protected int getTableStyle() {
138         return SWT.SINGLE | SWT.H_SCROLL | SWT.V_SCROLL | SWT.BORDER;
139     }
140
141     /*
142      * Overrides method from Dialog
143      */

144     protected void okPressed() {
145         // Build a list of selected children.
146
IStructuredSelection selection = (IStructuredSelection) fTableViewer
147                 .getSelection();
148         setResult(selection.toList());
149         super.okPressed();
150     }
151
152     /**
153      * Returns the initial height of the dialog in number of characters.
154      *
155      * @return the initial height of the dialog in number of characters
156      */

157     public int getHeightInChars() {
158         return heightInChars;
159     }
160
161     /**
162      * Returns the initial width of the dialog in number of characters.
163      *
164      * @return the initial width of the dialog in number of characters
165      */

166     public int getWidthInChars() {
167         return widthInChars;
168     }
169
170     /**
171      * Sets the initial height of the dialog in number of characters.
172      *
173      * @param heightInChars
174      * the initialheight of the dialog in number of characters
175      */

176     public void setHeightInChars(int heightInChars) {
177         this.heightInChars = heightInChars;
178     }
179
180     /**
181      * Sets the initial width of the dialog in number of characters.
182      *
183      * @param widthInChars
184      * the initial width of the dialog in number of characters
185      */

186     public void setWidthInChars(int widthInChars) {
187         this.widthInChars = widthInChars;
188     }
189 }
190
Popular Tags