001: /*******************************************************************************
002: * Copyright (c) 2000, 2006 IBM Corporation and others.
003: * All rights reserved. This program and the accompanying materials
004: * are made available under the terms of the Eclipse Public License v1.0
005: * which accompanies this distribution, and is available at
006: * http://www.eclipse.org/legal/epl-v10.html
007: *
008: * Contributors:
009: * IBM Corporation - initial API and implementation
010: * Sebastian Davids <sdavids@gmx.de> - Fix for bug 19346 - Dialog
011: * font should be activated and used by other components.
012: *******************************************************************************/package org.eclipse.ui.dialogs;
013:
014: import java.util.Arrays;
015: import java.util.List;
016:
017: import org.eclipse.jface.dialogs.IDialogConstants;
018: import org.eclipse.jface.viewers.ILabelProvider;
019: import org.eclipse.swt.SWT;
020: import org.eclipse.swt.events.DisposeEvent;
021: import org.eclipse.swt.events.DisposeListener;
022: import org.eclipse.swt.layout.GridData;
023: import org.eclipse.swt.widgets.Composite;
024: import org.eclipse.swt.widgets.Control;
025: import org.eclipse.swt.widgets.Event;
026: import org.eclipse.swt.widgets.Label;
027: import org.eclipse.swt.widgets.Listener;
028: import org.eclipse.swt.widgets.Shell;
029: import org.eclipse.swt.widgets.Table;
030: import org.eclipse.swt.widgets.TableItem;
031:
032: /**
033: * A list selection dialog with two panes. Duplicated entries will be folded
034: * together and are displayed in the lower pane (qualifier).
035: *
036: * @since 2.0
037: */
038: public class TwoPaneElementSelector extends
039: AbstractElementListSelectionDialog {
040: private String fUpperListLabel;
041:
042: private String fLowerListLabel;
043:
044: private ILabelProvider fQualifierRenderer;
045:
046: private Object[] fElements = new Object[0];
047:
048: private Table fLowerList;
049:
050: private Object[] fQualifierElements;
051:
052: /**
053: * Creates the two pane element selector.
054: *
055: * @param parent
056: * the parent shell.
057: * @param elementRenderer
058: * the element renderer.
059: * @param qualifierRenderer
060: * the qualifier renderer.
061: */
062: public TwoPaneElementSelector(Shell parent,
063: ILabelProvider elementRenderer,
064: ILabelProvider qualifierRenderer) {
065: super (parent, elementRenderer);
066: setSize(50, 15);
067: setAllowDuplicates(false);
068: fQualifierRenderer = qualifierRenderer;
069: }
070:
071: /**
072: * Sets the upper list label. If the label is <code>null</code> (default),
073: * no label is created.
074: *
075: * @param label
076: */
077: public void setUpperListLabel(String label) {
078: fUpperListLabel = label;
079: }
080:
081: /**
082: * Sets the lower list label.
083: *
084: * @param label
085: * String or <code>null</code>. If the label is
086: * <code>null</code> (default), no label is created.
087: */
088: public void setLowerListLabel(String label) {
089: fLowerListLabel = label;
090: }
091:
092: /**
093: * Sets the elements to be displayed.
094: *
095: * @param elements
096: * the elements to be displayed.
097: */
098: public void setElements(Object[] elements) {
099: fElements = elements;
100: }
101:
102: /*
103: * @see Dialog#createDialogArea(Composite)
104: */
105: public Control createDialogArea(Composite parent) {
106: Composite contents = (Composite) super .createDialogArea(parent);
107: createMessageArea(contents);
108: createFilterText(contents);
109: createLabel(contents, fUpperListLabel);
110: createFilteredList(contents);
111: createLabel(contents, fLowerListLabel);
112: createLowerList(contents);
113: setListElements(fElements);
114: List initialSelections = getInitialElementSelections();
115: if (!initialSelections.isEmpty()) {
116: Object element = initialSelections.get(0);
117: setSelection(new Object[] { element });
118: setLowerSelectedElement(element);
119: }
120: return contents;
121: }
122:
123: /**
124: * Creates a label if name was not <code>null</code>.
125: *
126: * @param parent
127: * the parent composite.
128: * @param name
129: * the name of the label.
130: * @return returns a label if a name was given, <code>null</code>
131: * otherwise.
132: */
133: protected Label createLabel(Composite parent, String name) {
134: if (name == null) {
135: return null;
136: }
137: Label label = new Label(parent, SWT.NONE);
138: label.setText(name);
139: label.setFont(parent.getFont());
140: return label;
141: }
142:
143: /**
144: * Creates the list widget and sets layout data.
145: *
146: * @param parent
147: * the parent composite.
148: * @return returns the list table widget.
149: */
150: protected Table createLowerList(Composite parent) {
151: Table list = new Table(parent, SWT.BORDER | SWT.V_SCROLL
152: | SWT.H_SCROLL);
153: list.addListener(SWT.Selection, new Listener() {
154: public void handleEvent(Event evt) {
155: handleLowerSelectionChanged();
156: }
157: });
158: list.addListener(SWT.MouseDoubleClick, new Listener() {
159: public void handleEvent(Event evt) {
160: handleDefaultSelected();
161: }
162: });
163: list.addDisposeListener(new DisposeListener() {
164: public void widgetDisposed(DisposeEvent e) {
165: fQualifierRenderer.dispose();
166: }
167: });
168: GridData data = new GridData();
169: data.widthHint = convertWidthInCharsToPixels(50);
170: data.heightHint = convertHeightInCharsToPixels(5);
171: data.grabExcessVerticalSpace = true;
172: data.grabExcessHorizontalSpace = true;
173: data.horizontalAlignment = GridData.FILL;
174: data.verticalAlignment = GridData.FILL;
175: list.setLayoutData(data);
176: list.setFont(parent.getFont());
177: fLowerList = list;
178: return list;
179: }
180:
181: /**
182: * @see SelectionStatusDialog#computeResult()
183: */
184: protected void computeResult() {
185: Object[] results = new Object[] { getLowerSelectedElement() };
186: setResult(Arrays.asList(results));
187: }
188:
189: /**
190: * @see AbstractElementListSelectionDialog#handleDefaultSelected()
191: */
192: protected void handleDefaultSelected() {
193: if (validateCurrentSelection()
194: && (getLowerSelectedElement() != null)) {
195: buttonPressed(IDialogConstants.OK_ID);
196: }
197: }
198:
199: /**
200: * @see AbstractElementListSelectionDialog#handleSelectionChanged()
201: */
202: protected void handleSelectionChanged() {
203: handleUpperSelectionChanged();
204: }
205:
206: private void handleUpperSelectionChanged() {
207: int index = getSelectionIndex();
208: fLowerList.removeAll();
209: if (index >= 0) {
210: fQualifierElements = getFoldedElements(index);
211: if (fQualifierElements == null) {
212: updateLowerListWidget(new Object[] {});
213: } else {
214: updateLowerListWidget(fQualifierElements);
215: }
216: }
217: validateCurrentSelection();
218: }
219:
220: private void handleLowerSelectionChanged() {
221: validateCurrentSelection();
222: }
223:
224: /**
225: * Selects an element in the lower pane.
226: * @param element
227: */
228: protected void setLowerSelectedElement(Object element) {
229: if (fQualifierElements == null) {
230: return;
231: }
232: // find matching index
233: int i;
234: for (i = 0; i != fQualifierElements.length; i++) {
235: if (fQualifierElements[i].equals(element)) {
236: break;
237: }
238: }
239: // set selection
240: if (i != fQualifierElements.length) {
241: fLowerList.setSelection(i);
242: }
243: }
244:
245: /**
246: * Returns the selected element from the lower pane.
247: * @return Object
248: */
249: protected Object getLowerSelectedElement() {
250: int index = fLowerList.getSelectionIndex();
251: if (index >= 0) {
252: return fQualifierElements[index];
253: }
254: return null;
255: }
256:
257: private void updateLowerListWidget(Object[] elements) {
258: int length = elements.length;
259: String[] qualifiers = new String[length];
260: for (int i = 0; i != length; i++) {
261: String text = fQualifierRenderer.getText(elements[i]);
262: if (text == null) {
263: text = ""; //$NON-NLS-1$
264: }
265: qualifiers[i] = text;
266: }
267: TwoArrayQuickSorter sorter = new TwoArrayQuickSorter(
268: isCaseIgnored());
269: sorter.sort(qualifiers, elements);
270: for (int i = 0; i != length; i++) {
271: TableItem item = new TableItem(fLowerList, SWT.NONE);
272: item.setText(qualifiers[i]);
273: item.setImage(fQualifierRenderer.getImage(elements[i]));
274: }
275: if (fLowerList.getItemCount() > 0) {
276: fLowerList.setSelection(0);
277: }
278: }
279:
280: /*
281: * @see AbstractElementListSelectionDialog#handleEmptyList()
282: */
283: protected void handleEmptyList() {
284: super .handleEmptyList();
285: fLowerList.setEnabled(false);
286: }
287: }
|