KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ui > dialogs > TwoPaneElementSelector


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  * Sebastian Davids <sdavids@gmx.de> - Fix for bug 19346 - Dialog
11  * font should be activated and used by other components.
12  *******************************************************************************/

13 package org.eclipse.ui.dialogs;
14
15 import java.util.Arrays JavaDoc;
16 import java.util.List JavaDoc;
17
18 import org.eclipse.jface.dialogs.IDialogConstants;
19 import org.eclipse.jface.viewers.ILabelProvider;
20 import org.eclipse.swt.SWT;
21 import org.eclipse.swt.events.DisposeEvent;
22 import org.eclipse.swt.events.DisposeListener;
23 import org.eclipse.swt.layout.GridData;
24 import org.eclipse.swt.widgets.Composite;
25 import org.eclipse.swt.widgets.Control;
26 import org.eclipse.swt.widgets.Event;
27 import org.eclipse.swt.widgets.Label;
28 import org.eclipse.swt.widgets.Listener;
29 import org.eclipse.swt.widgets.Shell;
30 import org.eclipse.swt.widgets.Table;
31 import org.eclipse.swt.widgets.TableItem;
32
33 /**
34  * A list selection dialog with two panes. Duplicated entries will be folded
35  * together and are displayed in the lower pane (qualifier).
36  *
37  * @since 2.0
38  */

39 public class TwoPaneElementSelector extends AbstractElementListSelectionDialog {
40     private String JavaDoc fUpperListLabel;
41
42     private String JavaDoc fLowerListLabel;
43
44     private ILabelProvider fQualifierRenderer;
45
46     private Object JavaDoc[] fElements = new Object JavaDoc[0];
47
48     private Table fLowerList;
49
50     private Object JavaDoc[] fQualifierElements;
51
52     /**
53      * Creates the two pane element selector.
54      *
55      * @param parent
56      * the parent shell.
57      * @param elementRenderer
58      * the element renderer.
59      * @param qualifierRenderer
60      * the qualifier renderer.
61      */

62     public TwoPaneElementSelector(Shell parent, ILabelProvider elementRenderer,
63             ILabelProvider qualifierRenderer) {
64         super(parent, elementRenderer);
65         setSize(50, 15);
66         setAllowDuplicates(false);
67         fQualifierRenderer = qualifierRenderer;
68     }
69
70     /**
71      * Sets the upper list label. If the label is <code>null</code> (default),
72      * no label is created.
73      *
74      * @param label
75      */

76     public void setUpperListLabel(String JavaDoc label) {
77         fUpperListLabel = label;
78     }
79
80     /**
81      * Sets the lower list label.
82      *
83      * @param label
84      * String or <code>null</code>. If the label is
85      * <code>null</code> (default), no label is created.
86      */

87     public void setLowerListLabel(String JavaDoc label) {
88         fLowerListLabel = label;
89     }
90
91     /**
92      * Sets the elements to be displayed.
93      *
94      * @param elements
95      * the elements to be displayed.
96      */

97     public void setElements(Object JavaDoc[] elements) {
98         fElements = elements;
99     }
100
101     /*
102      * @see Dialog#createDialogArea(Composite)
103      */

104     public Control createDialogArea(Composite parent) {
105         Composite contents = (Composite) super.createDialogArea(parent);
106         createMessageArea(contents);
107         createFilterText(contents);
108         createLabel(contents, fUpperListLabel);
109         createFilteredList(contents);
110         createLabel(contents, fLowerListLabel);
111         createLowerList(contents);
112         setListElements(fElements);
113         List JavaDoc initialSelections = getInitialElementSelections();
114         if (!initialSelections.isEmpty()) {
115             Object JavaDoc element = initialSelections.get(0);
116             setSelection(new Object JavaDoc[] { element });
117             setLowerSelectedElement(element);
118         }
119         return contents;
120     }
121
122     /**
123      * Creates a label if name was not <code>null</code>.
124      *
125      * @param parent
126      * the parent composite.
127      * @param name
128      * the name of the label.
129      * @return returns a label if a name was given, <code>null</code>
130      * otherwise.
131      */

132     protected Label createLabel(Composite parent, String JavaDoc name) {
133         if (name == null) {
134             return null;
135         }
136         Label label = new Label(parent, SWT.NONE);
137         label.setText(name);
138         label.setFont(parent.getFont());
139         return label;
140     }
141
142     /**
143      * Creates the list widget and sets layout data.
144      *
145      * @param parent
146      * the parent composite.
147      * @return returns the list table widget.
148      */

149     protected Table createLowerList(Composite parent) {
150         Table list = new Table(parent, SWT.BORDER | SWT.V_SCROLL | SWT.H_SCROLL);
151         list.addListener(SWT.Selection, new Listener() {
152             public void handleEvent(Event evt) {
153                 handleLowerSelectionChanged();
154             }
155         });
156         list.addListener(SWT.MouseDoubleClick, new Listener() {
157             public void handleEvent(Event evt) {
158                 handleDefaultSelected();
159             }
160         });
161         list.addDisposeListener(new DisposeListener() {
162             public void widgetDisposed(DisposeEvent e) {
163                 fQualifierRenderer.dispose();
164             }
165         });
166         GridData data = new GridData();
167         data.widthHint = convertWidthInCharsToPixels(50);
168         data.heightHint = convertHeightInCharsToPixels(5);
169         data.grabExcessVerticalSpace = true;
170         data.grabExcessHorizontalSpace = true;
171         data.horizontalAlignment = GridData.FILL;
172         data.verticalAlignment = GridData.FILL;
173         list.setLayoutData(data);
174         list.setFont(parent.getFont());
175         fLowerList = list;
176         return list;
177     }
178
179     /**
180      * @see SelectionStatusDialog#computeResult()
181      */

182     protected void computeResult() {
183         Object JavaDoc[] results = new Object JavaDoc[] { getLowerSelectedElement() };
184         setResult(Arrays.asList(results));
185     }
186
187     /**
188      * @see AbstractElementListSelectionDialog#handleDefaultSelected()
189      */

190     protected void handleDefaultSelected() {
191         if (validateCurrentSelection() && (getLowerSelectedElement() != null)) {
192             buttonPressed(IDialogConstants.OK_ID);
193         }
194     }
195
196     /**
197      * @see AbstractElementListSelectionDialog#handleSelectionChanged()
198      */

199     protected void handleSelectionChanged() {
200         handleUpperSelectionChanged();
201     }
202
203     private void handleUpperSelectionChanged() {
204         int index = getSelectionIndex();
205         fLowerList.removeAll();
206         if (index >= 0) {
207             fQualifierElements = getFoldedElements(index);
208             if (fQualifierElements == null) {
209                 updateLowerListWidget(new Object JavaDoc[] {});
210             } else {
211                 updateLowerListWidget(fQualifierElements);
212             }
213         }
214         validateCurrentSelection();
215     }
216
217     private void handleLowerSelectionChanged() {
218         validateCurrentSelection();
219     }
220
221     /**
222      * Selects an element in the lower pane.
223      * @param element
224      */

225     protected void setLowerSelectedElement(Object JavaDoc element) {
226         if (fQualifierElements == null) {
227             return;
228         }
229         // find matching index
230
int i;
231         for (i = 0; i != fQualifierElements.length; i++) {
232             if (fQualifierElements[i].equals(element)) {
233                 break;
234             }
235         }
236         // set selection
237
if (i != fQualifierElements.length) {
238             fLowerList.setSelection(i);
239         }
240     }
241
242     /**
243      * Returns the selected element from the lower pane.
244      * @return Object
245      */

246     protected Object JavaDoc getLowerSelectedElement() {
247         int index = fLowerList.getSelectionIndex();
248         if (index >= 0) {
249             return fQualifierElements[index];
250         }
251         return null;
252     }
253
254     private void updateLowerListWidget(Object JavaDoc[] elements) {
255         int length = elements.length;
256         String JavaDoc[] qualifiers = new String JavaDoc[length];
257         for (int i = 0; i != length; i++){
258             String JavaDoc text = fQualifierRenderer.getText(elements[i]);
259             if(text == null) {
260                 text = ""; //$NON-NLS-1$
261
}
262             qualifiers[i] = text;
263         }
264         TwoArrayQuickSorter sorter = new TwoArrayQuickSorter(isCaseIgnored());
265         sorter.sort(qualifiers, elements);
266         for (int i = 0; i != length; i++) {
267             TableItem item = new TableItem(fLowerList, SWT.NONE);
268             item.setText(qualifiers[i]);
269             item.setImage(fQualifierRenderer.getImage(elements[i]));
270         }
271         if (fLowerList.getItemCount() > 0) {
272             fLowerList.setSelection(0);
273         }
274     }
275
276     /*
277      * @see AbstractElementListSelectionDialog#handleEmptyList()
278      */

279     protected void handleEmptyList() {
280         super.handleEmptyList();
281         fLowerList.setEnabled(false);
282     }
283 }
284
Popular Tags