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: *******************************************************************************/package org.eclipse.ui.dialogs;
011:
012: import java.util.List;
013:
014: import org.eclipse.jface.dialogs.IDialogConstants;
015: import org.eclipse.jface.viewers.DoubleClickEvent;
016: import org.eclipse.jface.viewers.IDoubleClickListener;
017: import org.eclipse.jface.viewers.ILabelProvider;
018: import org.eclipse.jface.viewers.IStructuredContentProvider;
019: import org.eclipse.jface.viewers.IStructuredSelection;
020: import org.eclipse.jface.viewers.StructuredSelection;
021: import org.eclipse.jface.viewers.TableViewer;
022: import org.eclipse.swt.SWT;
023: import org.eclipse.swt.layout.GridData;
024: import org.eclipse.swt.widgets.Composite;
025: import org.eclipse.swt.widgets.Control;
026: import org.eclipse.swt.widgets.Shell;
027: import org.eclipse.swt.widgets.Table;
028:
029: /**
030: * A dialog that prompts for one element out of a list of elements. Uses
031: * <code>IStructuredContentProvider</code> to provide the elements and
032: * <code>ILabelProvider</code> to provide their labels.
033: *
034: * @since 2.1
035: */
036: public class ListDialog extends SelectionDialog {
037: private IStructuredContentProvider fContentProvider;
038:
039: private ILabelProvider fLabelProvider;
040:
041: private Object fInput;
042:
043: private TableViewer fTableViewer;
044:
045: private boolean fAddCancelButton = true;
046:
047: private int widthInChars = 55;
048:
049: private int heightInChars = 15;
050:
051: /**
052: * Create a new instance of the receiver with parent shell of parent.
053: * @param parent
054: */
055: public ListDialog(Shell parent) {
056: super (parent);
057: }
058:
059: /**
060: * @param input The input for the list.
061: */
062: public void setInput(Object input) {
063: fInput = input;
064: }
065:
066: /**
067: * @param sp The content provider for the list.
068: */
069: public void setContentProvider(IStructuredContentProvider sp) {
070: fContentProvider = sp;
071: }
072:
073: /**
074: * @param lp The labelProvider for the list.
075: */
076: public void setLabelProvider(ILabelProvider lp) {
077: fLabelProvider = lp;
078: }
079:
080: /**
081: *@param addCancelButton if <code>true</code> there will be a cancel
082: * button.
083: */
084: public void setAddCancelButton(boolean addCancelButton) {
085: fAddCancelButton = addCancelButton;
086: }
087:
088: /**
089: * @return the TableViewer for the receiver.
090: */
091: public TableViewer getTableViewer() {
092: return fTableViewer;
093: }
094:
095: protected void createButtonsForButtonBar(Composite parent) {
096: if (!fAddCancelButton) {
097: createButton(parent, IDialogConstants.OK_ID,
098: IDialogConstants.OK_LABEL, true);
099: } else {
100: super .createButtonsForButtonBar(parent);
101: }
102: }
103:
104: protected Control createDialogArea(Composite container) {
105: Composite parent = (Composite) super
106: .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 initialSelection = getInitialElementSelections();
120: if (initialSelection != null) {
121: fTableViewer.setSelection(new StructuredSelection(
122: 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: }
|