KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > internal > ui > dialogs > MultiElementListSelectionDialog


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 package org.eclipse.jdt.internal.ui.dialogs;
12
13 import java.util.ArrayList JavaDoc;
14 import java.util.Comparator JavaDoc;
15 import java.util.List JavaDoc;
16
17 import org.eclipse.core.runtime.Assert;
18 import org.eclipse.core.runtime.IStatus;
19
20 import org.eclipse.swt.SWT;
21 import org.eclipse.swt.layout.GridData;
22 import org.eclipse.swt.layout.GridLayout;
23 import org.eclipse.swt.widgets.Button;
24 import org.eclipse.swt.widgets.Composite;
25 import org.eclipse.swt.widgets.Control;
26 import org.eclipse.swt.widgets.Label;
27 import org.eclipse.swt.widgets.Shell;
28
29 import org.eclipse.jface.dialogs.IDialogConstants;
30 import org.eclipse.jface.viewers.ILabelProvider;
31
32 import org.eclipse.ui.dialogs.AbstractElementListSelectionDialog;
33 import org.eclipse.ui.dialogs.FilteredList;
34
35 import org.eclipse.jdt.internal.corext.util.Messages;
36
37 import org.eclipse.jdt.internal.ui.JavaUIMessages;
38
39 /**
40  * A class to select elements out of a list of elements, organized on multiple
41  * pages.
42  */

43 public class MultiElementListSelectionDialog extends AbstractElementListSelectionDialog {
44
45     private static class Page {
46         private Object JavaDoc[] elements;
47         public String JavaDoc filter;
48         public boolean okState= false;
49         
50         public Page(Object JavaDoc[] elements) {
51             this.elements= elements;
52         }
53     }
54     
55     private Page[] fPages;
56     private int fCurrentPage;
57     private int fNumberOfPages;
58     
59     private Button fFinishButton;
60     private Button fBackButton;
61     private Button fNextButton;
62     
63     private Label fPageInfoLabel;
64     private String JavaDoc fPageInfoMessage= JavaUIMessages.MultiElementListSelectionDialog_pageInfoMessage;
65     private Comparator JavaDoc fComparator;
66     
67     /**
68      * Constructs a multi-page list selection dialog.
69      * @param parent The parent shell
70      * @param renderer the label renderer.
71      */

72     public MultiElementListSelectionDialog(Shell parent, ILabelProvider renderer) {
73         super(parent, renderer);
74     }
75     
76     /**
77      * Sets message shown in the right top corner. Use {0} and {1} as placeholders
78      * for the current and the total number of pages.
79      * @param message the message.
80      */

81     public void setPageInfoMessage(String JavaDoc message) {
82         fPageInfoMessage= message;
83     }
84     
85     /**
86      * Sets the elements to be displayed in the dialog.
87      * @param elements an array of pages holding arrays of elements
88      */

89     public void setElements(Object JavaDoc[][] elements) {
90         fNumberOfPages= elements.length;
91         fPages= new Page[fNumberOfPages];
92         for (int i= 0; i != fNumberOfPages; i++)
93             fPages[i]= new Page(elements[i]);
94         
95         initializeResult(fNumberOfPages);
96     }
97
98     /*
99      * @see Window#open()
100      */

101     public int open() {
102         List JavaDoc selection= getInitialElementSelections();
103         if (selection == null || selection.size() != fNumberOfPages) {
104             setInitialSelections(new Object JavaDoc[fNumberOfPages]);
105             selection= getInitialElementSelections();
106         }
107             
108         Assert.isTrue(selection.size() == fNumberOfPages);
109         
110         return super.open();
111     }
112          
113     /*
114      * @see org.eclipse.jface.dialogs.Dialog#createDialogArea(Composite)
115      */

116     protected Control createDialogArea(Composite parent) {
117         Composite contents= (Composite) super.createDialogArea(parent);
118
119         createMessageArea(contents);
120         createFilterText(contents);
121         createFilteredList(contents);
122
123         fCurrentPage= 0;
124         setPageData();
125         
126         applyDialogFont(contents);
127         return contents;
128     }
129
130     /*
131      * @see org.eclipse.jface.dialogs.Dialog#createButtonsForButtonBar(Composite)
132      */

133     protected void createButtonsForButtonBar(Composite parent) {
134         fBackButton= createButton(parent, IDialogConstants.BACK_ID, IDialogConstants.BACK_LABEL, false);
135         fNextButton= createButton(parent, IDialogConstants.NEXT_ID, IDialogConstants.NEXT_LABEL, true);
136         fFinishButton= createButton(parent, IDialogConstants.OK_ID, IDialogConstants.FINISH_LABEL, false);
137         createButton(parent, IDialogConstants.CANCEL_ID, IDialogConstants.CANCEL_LABEL, false);
138     }
139     
140     /*
141      * @see org.eclipse.ui.dialogs.SelectionDialog#createMessageArea(Composite)
142      */

143     protected Label createMessageArea(Composite parent) {
144         Composite composite= new Composite(parent, SWT.NONE);
145
146         GridLayout layout= new GridLayout();
147         layout.marginHeight= 0;
148         layout.marginWidth= 0;
149         layout.horizontalSpacing= 5;
150         layout.numColumns= 2;
151         composite.setLayout(layout);
152         
153         GridData data= new GridData(GridData.HORIZONTAL_ALIGN_FILL);
154         composite.setLayoutData(data);
155         
156         Label messageLabel= super.createMessageArea(composite);
157         
158         fPageInfoLabel= new Label(composite, SWT.NULL);
159         fPageInfoLabel.setText(getPageInfoMessage());
160         
161         data= new GridData(GridData.HORIZONTAL_ALIGN_FILL);
162         data.horizontalAlignment= GridData.END;
163         fPageInfoLabel.setLayoutData(data);
164         applyDialogFont(messageLabel);
165         return messageLabel;
166     }
167     
168     /*
169      * @see org.eclipse.ui.dialogs.SelectionStatusDialog#computeResult()
170      */

171     protected void computeResult() {
172         setResult(fCurrentPage, getSelectedElements());
173     }
174         
175     /*
176      * @see org.eclipse.jface.dialogs.Dialog#buttonPressed(int)
177      */

178     protected void buttonPressed(int buttonId) {
179         if (buttonId == IDialogConstants.BACK_ID) {
180             turnPage(false);
181         } else if (buttonId == IDialogConstants.NEXT_ID) {
182             turnPage(true);
183         } else {
184             super.buttonPressed(buttonId);
185         }
186     }
187     
188     /**
189      * @see AbstractElementListSelectionDialog#handleDefaultSelected()
190      */

191     protected void handleDefaultSelected() {
192         if (validateCurrentSelection()) {
193             if (fCurrentPage == fNumberOfPages - 1) {
194                 buttonPressed(IDialogConstants.OK_ID);
195             } else {
196                 buttonPressed(IDialogConstants.NEXT_ID);
197             }
198         }
199     }
200     
201     /**
202      * @see AbstractElementListSelectionDialog#updateButtonsEnableState(IStatus)
203      */

204     protected void updateButtonsEnableState(IStatus status) {
205         boolean isOK= !status.matches(IStatus.ERROR);
206         fPages[fCurrentPage].okState= isOK;
207         
208         boolean isAllOK= isOK;
209         for (int i= 0; i != fNumberOfPages; i++)
210             isAllOK = isAllOK && fPages[i].okState;
211         
212         fFinishButton.setEnabled(isAllOK);
213         
214         boolean nextButtonEnabled= isOK && (fCurrentPage < fNumberOfPages - 1);
215         
216         fNextButton.setEnabled(nextButtonEnabled);
217         fBackButton.setEnabled(fCurrentPage != 0);
218         
219         if (nextButtonEnabled) {
220             getShell().setDefaultButton(fNextButton);
221         } else if (isAllOK) {
222             getShell().setDefaultButton(fFinishButton);
223         }
224     }
225
226     private void turnPage(boolean toNextPage) {
227         Page page= fPages[fCurrentPage];
228         
229         // store filter
230
String JavaDoc filter= getFilter();
231         if (filter == null)
232             filter= ""; //$NON-NLS-1$
233
page.filter= filter;
234
235         // store selection
236
Object JavaDoc[] selectedElements= getSelectedElements();
237         List JavaDoc list= getInitialElementSelections();
238         list.set(fCurrentPage, selectedElements);
239
240         // store result
241
setResult(fCurrentPage, getSelectedElements());
242
243         if (toNextPage) {
244             if (fCurrentPage + 1 >= fNumberOfPages)
245                 return;
246             
247             fCurrentPage++;
248         } else {
249             if (fCurrentPage - 1 < 0)
250                 return;
251
252             fCurrentPage--;
253         }
254         
255         if (fPageInfoLabel != null && !fPageInfoLabel.isDisposed())
256             fPageInfoLabel.setText(getPageInfoMessage());
257         
258         setPageData();
259         
260         validateCurrentSelection();
261     }
262     
263     private void setPageData() {
264         Page page= fPages[fCurrentPage];
265         
266         // 1. set elements
267
setListElements(page.elements);
268         
269         // 2. apply filter
270
String JavaDoc filter= page.filter;
271         if (filter == null)
272             filter= ""; //$NON-NLS-1$
273
setFilter(filter);
274         
275         // 3. select elements
276
Object JavaDoc[] selectedElements= (Object JavaDoc[]) getInitialElementSelections().get(fCurrentPage);
277         setSelection(selectedElements);
278         fFilteredList.setFocus();
279     }
280     
281     private String JavaDoc getPageInfoMessage() {
282         if (fPageInfoMessage == null)
283             return ""; //$NON-NLS-1$
284

285         String JavaDoc[] args= new String JavaDoc[] { Integer.toString(fCurrentPage + 1), Integer.toString(fNumberOfPages) };
286         return Messages.format(fPageInfoMessage, args);
287     }
288         
289     private void initializeResult(int length) {
290         List JavaDoc result= new ArrayList JavaDoc(length);
291         for (int i= 0; i != length; i++)
292             result.add(null);
293
294         setResult(result);
295     }
296     
297     /**
298      * Gets the current Page.
299      * @return Returns a int
300      */

301     public int getCurrentPage() {
302         return fCurrentPage;
303     }
304
305     /**
306      * Set the <code>Comparator</code> used to sort
307      * the elements in the List.
308      *
309      * @param comparator the comparator to use, not null.
310      */

311     public void setComparator(Comparator JavaDoc comparator) {
312         fComparator= comparator;
313         if (fFilteredList != null)
314             fFilteredList.setComparator(fComparator);
315     }
316     
317     /**
318      * {@inheritDoc}
319      */

320     protected FilteredList createFilteredList(Composite parent) {
321         FilteredList filteredList= super.createFilteredList(parent);
322         if (fComparator != null) {
323             filteredList.setComparator(fComparator);
324         }
325         return filteredList;
326     }
327
328 }
329
Popular Tags