1 /*******************************************************************************
2 * Copyright (c) 2000, 2011 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
9 * IBM Corporation - initial API and implementation
10 *******************************************************************************/
11 package org.eclipse.jdt.internal.ui.dialogs;
13 import java.util.ArrayList;
14 import java.util.Comparator;
15 import java.util.List;
17 import org.eclipse.swt.SWT;
18 import org.eclipse.swt.layout.GridData;
19 import org.eclipse.swt.layout.GridLayout;
20 import org.eclipse.swt.widgets.Button;
21 import org.eclipse.swt.widgets.Composite;
22 import org.eclipse.swt.widgets.Control;
23 import org.eclipse.swt.widgets.Label;
24 import org.eclipse.swt.widgets.Shell;
26 import org.eclipse.core.runtime.Assert;
27 import org.eclipse.core.runtime.IStatus;
29 import org.eclipse.jface.dialogs.IDialogConstants;
30 import org.eclipse.jface.viewers.ILabelProvider;
31 import org.eclipse.jface.viewers.ISelection;
32 import org.eclipse.jface.window.Window;
34 import org.eclipse.jface.text.ITextSelection;
36 import org.eclipse.ui.dialogs.AbstractElementListSelectionDialog;
37 import org.eclipse.ui.dialogs.FilteredList;
39 import org.eclipse.jdt.core.search.TypeNameMatch;
41 import org.eclipse.jdt.internal.corext.util.Messages;
42 import org.eclipse.jdt.internal.corext.util.QualifiedTypeNameHistory;
44 import org.eclipse.jdt.ui.actions.OrganizeImportsAction;
46 import org.eclipse.jdt.internal.ui.JavaUIMessages;
47 import org.eclipse.jdt.internal.ui.actions.ActionMessages;
48 import org.eclipse.jdt.internal.ui.javaeditor.JavaEditor;
51 * A class to select elements out of a list of elements, organized on multiple
54 public class MultiElementListSelectionDialog extends AbstractElementListSelectionDialog {
57 private Object[] elements;
59 public boolean okState= false;
61 public Page(Object[] elements) {
62 this.elements= elements;
65 public void generated_340588322571221580(MultiElementListSelectionDialog multielementlistselectiondialog, String filter) {
67 filter= ""; //$NON-NLS-1$
71 public String generated_2110802331564376624(MultiElementListSelectionDialog multielementlistselectiondialog) {
72 String filter= filter;
79 private Page[] fPages;
80 private int fCurrentPage;
81 private int fNumberOfPages;
83 private Button fFinishButton;
84 private Button fBackButton;
85 private Button fNextButton;
87 private Label fPageInfoLabel;
88 private String fPageInfoMessage= JavaUIMessages.MultiElementListSelectionDialog_pageInfoMessage;
89 private Comparator<?> fComparator;
92 * Constructs a multi-page list selection dialog.
93 * @param parent The parent shell
94 * @param renderer the label renderer.
96 public MultiElementListSelectionDialog(Shell parent, ILabelProvider renderer) {
97 super(parent, renderer);
101 * Sets message shown in the right top corner. Use {0} and {1} as placeholders
102 * for the current and the total number of pages.
103 * @param message the message.
105 public void setPageInfoMessage(String message) {
106 fPageInfoMessage= message;
110 * Sets the elements to be displayed in the dialog.
111 * @param elements an array of pages holding arrays of elements
113 public void setElements(Object[][] elements) {
114 fNumberOfPages= elements.length;
115 fPages= new Page[fNumberOfPages];
116 for (int i= 0; i != fNumberOfPages; i++)
117 fPages[i]= new Page(elements[i]);
119 initializeResult(fNumberOfPages);
127 List<Object[]> selection= getInitialElementSelections();
128 if (selection == null || selection.size() != fNumberOfPages) {
129 setInitialSelections(new Object[fNumberOfPages]);
130 selection= getInitialElementSelections();
133 Assert.isTrue(selection.size() == fNumberOfPages);
139 * @see org.eclipse.jface.dialogs.Dialog#createDialogArea(Composite)
142 protected Control createDialogArea(Composite parent) {
143 Composite contents= (Composite) super.createDialogArea(parent);
145 createMessageArea(contents);
146 createFilterText(contents);
147 createFilteredList(contents);
152 applyDialogFont(contents);
157 * @see org.eclipse.jface.dialogs.Dialog#createButtonsForButtonBar(Composite)
160 protected void createButtonsForButtonBar(Composite parent) {
161 fBackButton= createButton(parent, IDialogConstants.BACK_ID, IDialogConstants.BACK_LABEL, false);
163 // XXX: Workaround for https://bugs.eclipse.org/bugs/show_bug.cgi?id=279425
164 boolean HAS_BUG_279425= true;
165 fNextButton= createButton(parent, IDialogConstants.NEXT_ID, IDialogConstants.NEXT_LABEL, !HAS_BUG_279425);
166 fFinishButton= createButton(parent, IDialogConstants.OK_ID, IDialogConstants.FINISH_LABEL, HAS_BUG_279425);
168 createButton(parent, IDialogConstants.CANCEL_ID, IDialogConstants.CANCEL_LABEL, false);
172 * XXX: Workaround for https://bugs.eclipse.org/bugs/show_bug.cgi?id=279425
173 * The whole method can be removed once that bug is fixed.
174 * @see org.eclipse.jface.dialogs.Dialog#initializeBounds()
178 protected void initializeBounds() {
179 super.initializeBounds();
180 fNextButton.getShell().setDefaultButton(fNextButton);
184 * @see org.eclipse.ui.dialogs.SelectionDialog#createMessageArea(Composite)
187 protected Label createMessageArea(Composite parent) {
188 Composite composite= new Composite(parent, SWT.NONE);
190 GridLayout layout= new GridLayout();
191 layout.marginHeight= 0;
192 layout.marginWidth= 0;
193 layout.horizontalSpacing= 5;
194 layout.numColumns= 2;
195 composite.setLayout(layout);
197 GridData data= new GridData(GridData.HORIZONTAL_ALIGN_FILL);
198 composite.setLayoutData(data);
200 Label messageLabel= super.createMessageArea(composite);
202 fPageInfoLabel= new Label(composite, SWT.NULL);
203 fPageInfoLabel.setText(getPageInfoMessage());
205 data= new GridData(GridData.HORIZONTAL_ALIGN_FILL);
206 data.horizontalAlignment= GridData.END;
207 fPageInfoLabel.setLayoutData(data);
208 applyDialogFont(messageLabel);
213 * @see org.eclipse.ui.dialogs.SelectionStatusDialog#computeResult()
216 protected void computeResult() {
217 setResult(fCurrentPage, getSelectedElements());
221 * @see org.eclipse.jface.dialogs.Dialog#buttonPressed(int)
224 protected void buttonPressed(int buttonId) {
225 if (buttonId == IDialogConstants.BACK_ID) {
227 } else if (buttonId == IDialogConstants.NEXT_ID) {
230 super.buttonPressed(buttonId);
235 * @see AbstractElementListSelectionDialog#handleDefaultSelected()
238 protected void handleDefaultSelected() {
239 if (validateCurrentSelection()) {
240 if (fCurrentPage == fNumberOfPages - 1) {
241 buttonPressed(IDialogConstants.OK_ID);
243 buttonPressed(IDialogConstants.NEXT_ID);
249 * @see AbstractElementListSelectionDialog#updateButtonsEnableState(IStatus)
252 protected void updateButtonsEnableState(IStatus status) {
253 boolean isOK= !status.matches(IStatus.ERROR);
254 fPages[fCurrentPage].okState= isOK;
256 boolean isAllOK= isOK;
257 for (int i= 0; i != fNumberOfPages; i++)
258 isAllOK = isAllOK && fPages[i].okState;
260 fFinishButton.setEnabled(isAllOK);
262 boolean nextButtonEnabled= isOK && (fCurrentPage < fNumberOfPages - 1);
264 fNextButton.setEnabled(nextButtonEnabled);
265 fBackButton.setEnabled(fCurrentPage != 0);
267 if (nextButtonEnabled) {
268 getShell().setDefaultButton(fNextButton);
269 } else if (isAllOK) {
270 getShell().setDefaultButton(fFinishButton);
274 private void turnPage(boolean toNextPage) {
275 Page page= fPages[fCurrentPage];
278 String filter= getFilter();
279 page.generated_340588322571221580(this, filter);
282 Object[] selectedElements= getSelectedElements();
283 List<Object[]> list= getInitialElementSelections();
284 list.set(fCurrentPage, selectedElements);
287 setResult(fCurrentPage, getSelectedElements());
290 if (fCurrentPage + 1 >= fNumberOfPages)
295 if (fCurrentPage - 1 < 0)
301 if (fPageInfoLabel != null && !fPageInfoLabel.isDisposed())
302 fPageInfoLabel.setText(getPageInfoMessage());
306 validateCurrentSelection();
309 private void setPageData() {
310 Page page= fPages[fCurrentPage];
313 setListElements(page.elements);
316 String filter= page.generated_2110802331564376624(this); //$NON-NLS-1$
319 // 3. select elements
320 Object[] selectedElements= (Object[]) getInitialElementSelections().get(fCurrentPage);
321 setSelection(selectedElements);
322 fFilteredList.setFocus();
325 private String getPageInfoMessage() {
326 if (fPageInfoMessage == null)
327 return ""; //$NON-NLS-1$
329 String[] args= new String[] { Integer.toString(fCurrentPage + 1), Integer.toString(fNumberOfPages) };
330 return Messages.format(fPageInfoMessage, args);
333 private void initializeResult(int length) {
334 List<Object> result= new ArrayList<Object>(length);
335 for (int i= 0; i != length; i++)
342 * Gets the current Page.
343 * @return Returns a int
345 public int getCurrentPage() {
350 * Set the <code>Comparator</code> used to sort
351 * the elements in the List.
353 * @param comparator the comparator to use, not null.
355 public void setComparator(Comparator<?> comparator) {
356 fComparator= comparator;
357 if (fFilteredList != null)
358 fFilteredList.setComparator(fComparator);
365 protected FilteredList createFilteredList(Composite parent) {
366 FilteredList filteredList= super.createFilteredList(parent);
367 if (fComparator != null) {
368 filteredList.setComparator(fComparator);
373 public TypeNameMatch[] generated_3659039686475181911(TypeNameMatch[][] openChoices, final JavaEditor editor, ISelection sel, TypeNameMatch[] result) {
374 setTitle(ActionMessages.OrganizeImportsAction_selectiondialog_title);
375 setMessage(ActionMessages.OrganizeImportsAction_selectiondialog_message);
376 setElements(openChoices);
377 setComparator(OrganizeImportsAction.ORGANIZE_IMPORT_COMPARATOR);
378 if (open() == Window.OK) {
379 Object[] res= getResult();
380 result= new TypeNameMatch[res.length];
381 for (int i= 0; i < res.length; i++) {
382 Object[] array= (Object[]) res[i];
383 if (array.length > 0) {
384 result[i]= (TypeNameMatch) array[0];
385 QualifiedTypeNameHistory.remember(result[i].getFullyQualifiedName());
390 if (sel instanceof ITextSelection) {
391 ITextSelection textSelection= (ITextSelection) sel;
392 editor.selectAndReveal(textSelection.getOffset(), textSelection.getLength());