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.internal.ide;
011:
012: import com.ibm.icu.text.Collator;
013: import java.util.Arrays;
014: import java.util.Comparator;
015: import java.util.Locale;
016:
017: import org.eclipse.jface.viewers.DoubleClickEvent;
018: import org.eclipse.jface.viewers.IDoubleClickListener;
019: import org.eclipse.jface.viewers.ISelectionChangedListener;
020: import org.eclipse.jface.viewers.IStructuredSelection;
021: import org.eclipse.jface.viewers.LabelProvider;
022: import org.eclipse.jface.viewers.ListViewer;
023: import org.eclipse.jface.viewers.SelectionChangedEvent;
024: import org.eclipse.jface.viewers.StructuredSelection;
025: import org.eclipse.swt.SWT;
026: import org.eclipse.swt.layout.GridData;
027: import org.eclipse.swt.widgets.Composite;
028: import org.eclipse.swt.widgets.Control;
029: import org.eclipse.swt.widgets.Shell;
030: import org.eclipse.ui.PlatformUI;
031: import org.eclipse.ui.dialogs.SelectionDialog;
032: import org.eclipse.ui.internal.ide.dialogs.SimpleListContentProvider;
033:
034: /**
035: * Dialog to allow the user to select a feature from a list.
036: */
037: public class FeatureSelectionDialog extends SelectionDialog {
038: /**
039: * List width in characters.
040: */
041: private final static int LIST_WIDTH = 60;
042:
043: /**
044: * List height in characters.
045: */
046: private final static int LIST_HEIGHT = 10;
047:
048: /**
049: * The feature about infos.
050: */
051: private AboutInfo[] features;
052:
053: /**
054: * List to display the resolutions.
055: */
056: private ListViewer listViewer;
057:
058: /**
059: * The help context id
060: */
061: private String helpContextId;
062:
063: /**
064: * Creates an instance of this dialog to display
065: * the given features.
066: * <p>
067: * There must be at least one feature.
068: * </p>
069: *
070: * @param shell the parent shell
071: * @param features the features to display
072: * @param primaryFeatureId the id of the primary feature or null if none
073: * @param shellTitle shell title
074: * @param shellMessage shell message
075: * @param helpContextId help context id
076: */
077: public FeatureSelectionDialog(Shell shell, AboutInfo[] features,
078: String primaryFeatureId, String shellTitle,
079: String shellMessage, String helpContextId) {
080:
081: super (shell);
082: if (features == null || features.length == 0) {
083: throw new IllegalArgumentException();
084: }
085: this .features = features;
086: this .helpContextId = helpContextId;
087: setTitle(shellTitle);
088: setMessage(shellMessage);
089:
090: // Sort ascending
091: Arrays.sort(features, new Comparator() {
092: Collator coll = Collator.getInstance(Locale.getDefault());
093:
094: public int compare(Object a, Object b) {
095: AboutInfo i1, i2;
096: String name1, name2;
097: i1 = (AboutInfo) a;
098: name1 = i1.getFeatureLabel();
099: i2 = (AboutInfo) b;
100: name2 = i2.getFeatureLabel();
101: if (name1 == null) {
102: name1 = ""; //$NON-NLS-1$
103: }
104: if (name2 == null) {
105: name2 = ""; //$NON-NLS-1$
106: }
107: return coll.compare(name1, name2);
108: }
109: });
110:
111: // Find primary feature
112: for (int i = 0; i < features.length; i++) {
113: if (features[i].getFeatureId().equals(primaryFeatureId)) {
114: setInitialSelections(new Object[] { features[i] });
115: return;
116: }
117: }
118:
119: // set a safe default
120: setInitialSelections(new Object[0]);
121: }
122:
123: /* (non-Javadoc)
124: * Method declared on Window.
125: */
126: protected void configureShell(Shell newShell) {
127: super .configureShell(newShell);
128: PlatformUI.getWorkbench().getHelpSystem().setHelp(newShell,
129: helpContextId);
130: }
131:
132: /* (non-Javadoc)
133: * Method declared on Dialog.
134: */
135: protected Control createDialogArea(Composite parent) {
136: Composite composite = (Composite) super
137: .createDialogArea(parent);
138:
139: // Create label
140: createMessageArea(composite);
141: // Create list viewer
142: listViewer = new ListViewer(composite, SWT.SINGLE
143: | SWT.H_SCROLL | SWT.V_SCROLL | SWT.BORDER);
144: GridData data = new GridData(GridData.FILL_BOTH);
145: data.heightHint = convertHeightInCharsToPixels(LIST_HEIGHT);
146: data.widthHint = convertWidthInCharsToPixels(LIST_WIDTH);
147: listViewer.getList().setLayoutData(data);
148: listViewer.getList().setFont(parent.getFont());
149: // Set the label provider
150: listViewer.setLabelProvider(new LabelProvider() {
151: public String getText(Object element) {
152: // Return the features's label.
153: return element == null ? "" : ((AboutInfo) element).getFeatureLabel(); //$NON-NLS-1$
154: }
155: });
156:
157: // Set the content provider
158: SimpleListContentProvider cp = new SimpleListContentProvider();
159: cp.setElements(features);
160: listViewer.setContentProvider(cp);
161: listViewer.setInput(new Object());
162: // it is ignored but must be non-null
163:
164: // Set the initial selection
165: listViewer.setSelection(new StructuredSelection(
166: getInitialElementSelections()), true);
167:
168: // Add a selection change listener
169: listViewer
170: .addSelectionChangedListener(new ISelectionChangedListener() {
171: public void selectionChanged(
172: SelectionChangedEvent event) {
173: // Update OK button enablement
174: getOkButton().setEnabled(
175: !event.getSelection().isEmpty());
176: }
177: });
178:
179: // Add double-click listener
180: listViewer.addDoubleClickListener(new IDoubleClickListener() {
181: public void doubleClick(DoubleClickEvent event) {
182: okPressed();
183: }
184: });
185: return composite;
186: }
187:
188: /* (non-Javadoc)
189: * Method declared on Dialog.
190: */
191: protected void okPressed() {
192: IStructuredSelection selection = (IStructuredSelection) listViewer
193: .getSelection();
194: setResult(selection.toList());
195: super.okPressed();
196: }
197: }
|