KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ui > internal > ide > FeatureSelectionDialog


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
12 package org.eclipse.ui.internal.ide;
13
14 import com.ibm.icu.text.Collator;
15 import java.util.Arrays JavaDoc;
16 import java.util.Comparator JavaDoc;
17 import java.util.Locale JavaDoc;
18
19 import org.eclipse.jface.viewers.DoubleClickEvent;
20 import org.eclipse.jface.viewers.IDoubleClickListener;
21 import org.eclipse.jface.viewers.ISelectionChangedListener;
22 import org.eclipse.jface.viewers.IStructuredSelection;
23 import org.eclipse.jface.viewers.LabelProvider;
24 import org.eclipse.jface.viewers.ListViewer;
25 import org.eclipse.jface.viewers.SelectionChangedEvent;
26 import org.eclipse.jface.viewers.StructuredSelection;
27 import org.eclipse.swt.SWT;
28 import org.eclipse.swt.layout.GridData;
29 import org.eclipse.swt.widgets.Composite;
30 import org.eclipse.swt.widgets.Control;
31 import org.eclipse.swt.widgets.Shell;
32 import org.eclipse.ui.PlatformUI;
33 import org.eclipse.ui.dialogs.SelectionDialog;
34 import org.eclipse.ui.internal.ide.dialogs.SimpleListContentProvider;
35
36 /**
37  * Dialog to allow the user to select a feature from a list.
38  */

39 public class FeatureSelectionDialog extends SelectionDialog {
40     /**
41      * List width in characters.
42      */

43     private final static int LIST_WIDTH = 60;
44
45     /**
46      * List height in characters.
47      */

48     private final static int LIST_HEIGHT = 10;
49
50     /**
51      * The feature about infos.
52      */

53     private AboutInfo[] features;
54
55     /**
56      * List to display the resolutions.
57      */

58     private ListViewer listViewer;
59
60     /**
61      * The help context id
62      */

63     private String JavaDoc helpContextId;
64
65     /**
66      * Creates an instance of this dialog to display
67      * the given features.
68      * <p>
69      * There must be at least one feature.
70      * </p>
71      *
72      * @param shell the parent shell
73      * @param features the features to display
74      * @param primaryFeatureId the id of the primary feature or null if none
75      * @param shellTitle shell title
76      * @param shellMessage shell message
77      * @param helpContextId help context id
78      */

79     public FeatureSelectionDialog(Shell shell, AboutInfo[] features,
80             String JavaDoc primaryFeatureId, String JavaDoc shellTitle, String JavaDoc shellMessage,
81             String JavaDoc helpContextId) {
82
83         super(shell);
84         if (features == null || features.length == 0) {
85             throw new IllegalArgumentException JavaDoc();
86         }
87         this.features = features;
88         this.helpContextId = helpContextId;
89         setTitle(shellTitle);
90         setMessage(shellMessage);
91
92         // Sort ascending
93
Arrays.sort(features, new Comparator JavaDoc() {
94             Collator coll = Collator.getInstance(Locale.getDefault());
95
96             public int compare(Object JavaDoc a, Object JavaDoc b) {
97                 AboutInfo i1, i2;
98                 String JavaDoc name1, name2;
99                 i1 = (AboutInfo) a;
100                 name1 = i1.getFeatureLabel();
101                 i2 = (AboutInfo) b;
102                 name2 = i2.getFeatureLabel();
103                 if (name1 == null) {
104                     name1 = ""; //$NON-NLS-1$
105
}
106                 if (name2 == null) {
107                     name2 = ""; //$NON-NLS-1$
108
}
109                 return coll.compare(name1, name2);
110             }
111         });
112
113         // Find primary feature
114
for (int i = 0; i < features.length; i++) {
115             if (features[i].getFeatureId().equals(primaryFeatureId)) {
116                 setInitialSelections(new Object JavaDoc[] { features[i] });
117                 return;
118             }
119         }
120
121         // set a safe default
122
setInitialSelections(new Object JavaDoc[0]);
123     }
124
125     /* (non-Javadoc)
126      * Method declared on Window.
127      */

128     protected void configureShell(Shell newShell) {
129         super.configureShell(newShell);
130         PlatformUI.getWorkbench().getHelpSystem().setHelp(newShell,
131                 helpContextId);
132     }
133
134     /* (non-Javadoc)
135      * Method declared on Dialog.
136      */

137     protected Control createDialogArea(Composite parent) {
138         Composite composite = (Composite) super.createDialogArea(parent);
139
140         // Create label
141
createMessageArea(composite);
142         // Create list viewer
143
listViewer = new ListViewer(composite, SWT.SINGLE | SWT.H_SCROLL
144                 | SWT.V_SCROLL | SWT.BORDER);
145         GridData data = new GridData(GridData.FILL_BOTH);
146         data.heightHint = convertHeightInCharsToPixels(LIST_HEIGHT);
147         data.widthHint = convertWidthInCharsToPixels(LIST_WIDTH);
148         listViewer.getList().setLayoutData(data);
149         listViewer.getList().setFont(parent.getFont());
150         // Set the label provider
151
listViewer.setLabelProvider(new LabelProvider() {
152             public String JavaDoc getText(Object JavaDoc element) {
153                 // Return the features's label.
154
return element == null ? "" : ((AboutInfo) element).getFeatureLabel(); //$NON-NLS-1$
155
}
156         });
157
158         // Set the content provider
159
SimpleListContentProvider cp = new SimpleListContentProvider();
160         cp.setElements(features);
161         listViewer.setContentProvider(cp);
162         listViewer.setInput(new Object JavaDoc());
163         // it is ignored but must be non-null
164

165         // Set the initial selection
166
listViewer.setSelection(new StructuredSelection(
167                 getInitialElementSelections()), true);
168
169         // Add a selection change listener
170
listViewer.addSelectionChangedListener(new ISelectionChangedListener() {
171             public void selectionChanged(SelectionChangedEvent event) {
172                 // Update OK button enablement
173
getOkButton().setEnabled(!event.getSelection().isEmpty());
174             }
175         });
176
177         // Add double-click listener
178
listViewer.addDoubleClickListener(new IDoubleClickListener() {
179             public void doubleClick(DoubleClickEvent event) {
180                 okPressed();
181             }
182         });
183         return composite;
184     }
185
186     /* (non-Javadoc)
187      * Method declared on Dialog.
188      */

189     protected void okPressed() {
190         IStructuredSelection selection = (IStructuredSelection) listViewer
191                 .getSelection();
192         setResult(selection.toList());
193         super.okPressed();
194     }
195 }
196
Popular Tags