KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > internal > ui > wizards > buildpaths > ClasspathContainerSelectionPage


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.wizards.buildpaths;
12
13 import java.util.Arrays JavaDoc;
14
15 import org.eclipse.swt.SWT;
16 import org.eclipse.swt.widgets.Composite;
17
18 import org.eclipse.jface.dialogs.Dialog;
19 import org.eclipse.jface.dialogs.IDialogSettings;
20 import org.eclipse.jface.viewers.ArrayContentProvider;
21 import org.eclipse.jface.viewers.DoubleClickEvent;
22 import org.eclipse.jface.viewers.IDoubleClickListener;
23 import org.eclipse.jface.viewers.ISelection;
24 import org.eclipse.jface.viewers.ISelectionChangedListener;
25 import org.eclipse.jface.viewers.LabelProvider;
26 import org.eclipse.jface.viewers.ListViewer;
27 import org.eclipse.jface.viewers.SelectionChangedEvent;
28 import org.eclipse.jface.viewers.ViewerComparator;
29 import org.eclipse.jface.wizard.WizardPage;
30
31 import org.eclipse.ui.PlatformUI;
32
33 import org.eclipse.jdt.internal.ui.IJavaHelpContextIds;
34 import org.eclipse.jdt.internal.ui.JavaPlugin;
35 import org.eclipse.jdt.internal.ui.JavaPluginImages;
36 import org.eclipse.jdt.internal.ui.util.SelectionUtil;
37 import org.eclipse.jdt.internal.ui.wizards.NewWizardMessages;
38
39 /**
40   */

41 public class ClasspathContainerSelectionPage extends WizardPage {
42
43     private static final String JavaDoc DIALOGSTORE_SECTION= "ClasspathContainerSelectionPage"; //$NON-NLS-1$
44
private static final String JavaDoc DIALOGSTORE_CONTAINER_IDX= "index"; //$NON-NLS-1$
45

46
47     private static class ClasspathContainerLabelProvider extends LabelProvider {
48         public String JavaDoc getText(Object JavaDoc element) {
49             return ((ClasspathContainerDescriptor) element).getName();
50         }
51     }
52
53     private ListViewer fListViewer;
54     private ClasspathContainerDescriptor[] fContainers;
55     private IDialogSettings fDialogSettings;
56
57     /**
58      * Constructor for ClasspathContainerWizardPage.
59      * @param containerPages
60      */

61     protected ClasspathContainerSelectionPage(ClasspathContainerDescriptor[] containerPages) {
62         super("ClasspathContainerWizardPage"); //$NON-NLS-1$
63
setTitle(NewWizardMessages.ClasspathContainerSelectionPage_title);
64         setDescription(NewWizardMessages.ClasspathContainerSelectionPage_description);
65         setImageDescriptor(JavaPluginImages.DESC_WIZBAN_ADD_LIBRARY);
66
67         fContainers= containerPages;
68
69         IDialogSettings settings= JavaPlugin.getDefault().getDialogSettings();
70         fDialogSettings= settings.getSection(DIALOGSTORE_SECTION);
71         if (fDialogSettings == null) {
72             fDialogSettings= settings.addNewSection(DIALOGSTORE_SECTION);
73             fDialogSettings.put(DIALOGSTORE_CONTAINER_IDX, 0);
74         }
75         validatePage();
76     }
77
78     /* (non-Javadoc)
79      * @see IDialogPage#createControl(Composite)
80      */

81     public void createControl(Composite parent) {
82         fListViewer= new ListViewer(parent, SWT.SINGLE | SWT.BORDER);
83         fListViewer.setLabelProvider(new ClasspathContainerLabelProvider());
84         fListViewer.setContentProvider(new ArrayContentProvider());
85         fListViewer.setComparator(new ViewerComparator());
86         fListViewer.setInput(Arrays.asList(fContainers));
87         fListViewer.addSelectionChangedListener(new ISelectionChangedListener() {
88             public void selectionChanged(SelectionChangedEvent event) {
89                 validatePage();
90             }
91         });
92         fListViewer.addDoubleClickListener(new IDoubleClickListener() {
93             public void doubleClick(DoubleClickEvent event) {
94                 doDoubleClick();
95             }
96         });
97         
98         int selectionIndex= fDialogSettings.getInt(DIALOGSTORE_CONTAINER_IDX);
99         if (selectionIndex >= fContainers.length) {
100             selectionIndex= 0;
101         }
102         fListViewer.getList().select(selectionIndex);
103         validatePage();
104         setControl(fListViewer.getList());
105         Dialog.applyDialogFont(fListViewer.getList());
106         
107         PlatformUI.getWorkbench().getHelpSystem().setHelp(parent, IJavaHelpContextIds.BP_SELECT_CLASSPATH_CONTAINER);
108     }
109
110     /**
111      * Method validatePage.
112      */

113     private void validatePage() {
114         setPageComplete(getSelected() != null);
115     }
116
117
118     public ClasspathContainerDescriptor getSelected() {
119         if (fListViewer != null) {
120             ISelection selection= fListViewer.getSelection();
121             return (ClasspathContainerDescriptor) SelectionUtil.getSingleElement(selection);
122         }
123         return null;
124     }
125     
126     public ClasspathContainerDescriptor[] getContainers() {
127         return fContainers;
128     }
129     
130     protected void doDoubleClick() {
131         if (canFlipToNextPage()) {
132             getContainer().showPage(getNextPage());
133         }
134     }
135
136     /* (non-Javadoc)
137      * @see IWizardPage#canFlipToNextPage()
138      */

139     public boolean canFlipToNextPage() {
140         return isPageComplete(); // avoid the getNextPage call to prevent potential plugin load
141
}
142
143     /* (non-Javadoc)
144      * @see org.eclipse.jface.dialogs.IDialogPage#setVisible(boolean)
145      */

146     public void setVisible(boolean visible) {
147         if (!visible && fListViewer != null) {
148             fDialogSettings.put(DIALOGSTORE_CONTAINER_IDX, fListViewer.getList().getSelectionIndex());
149         }
150         super.setVisible(visible);
151     }
152
153 }
154
Popular Tags