KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > internal > junit > buildpath > JUnitContainerWizardPage


1 /*******************************************************************************
2  * Copyright (c) 2006, 2007 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.junit.buildpath;
12
13 import org.eclipse.core.runtime.IPath;
14
15 import org.eclipse.core.resources.IProject;
16 import org.eclipse.core.resources.IWorkspaceRoot;
17 import org.eclipse.core.resources.ResourcesPlugin;
18
19 import org.eclipse.swt.SWT;
20 import org.eclipse.swt.events.ModifyEvent;
21 import org.eclipse.swt.events.ModifyListener;
22 import org.eclipse.swt.layout.GridData;
23 import org.eclipse.swt.layout.GridLayout;
24 import org.eclipse.swt.widgets.Combo;
25 import org.eclipse.swt.widgets.Composite;
26 import org.eclipse.swt.widgets.Label;
27
28 import org.eclipse.jdt.core.IClasspathContainer;
29 import org.eclipse.jdt.core.IClasspathEntry;
30 import org.eclipse.jdt.core.IJavaProject;
31 import org.eclipse.jdt.core.JavaCore;
32 import org.eclipse.jdt.core.JavaModelException;
33
34 import org.eclipse.jdt.ui.wizards.IClasspathContainerPage;
35 import org.eclipse.jdt.ui.wizards.IClasspathContainerPageExtension;
36 import org.eclipse.jdt.ui.wizards.NewElementWizardPage;
37
38 import org.eclipse.jdt.internal.ui.JavaPluginImages;
39
40 import org.eclipse.jdt.internal.junit.ui.JUnitMessages;
41 import org.eclipse.jdt.internal.junit.util.ExceptionHandler;
42 import org.eclipse.jdt.internal.junit.util.JUnitStatus;
43 import org.eclipse.jdt.internal.junit.util.JUnitStubUtility;
44 import org.eclipse.jdt.internal.junit.util.PixelConverter;
45
46 public class JUnitContainerWizardPage extends NewElementWizardPage implements IClasspathContainerPage, IClasspathContainerPageExtension {
47
48     private IJavaProject fProject;
49     private IClasspathEntry fContainerEntryResult;
50     private Combo fVersionCombo;
51     private Label fResolvedPath;
52
53     public JUnitContainerWizardPage() {
54         super("JUnitContainerPage"); //$NON-NLS-1$
55
setTitle(JUnitMessages.JUnitContainerWizardPage_wizard_title);
56         setDescription(JUnitMessages.JUnitContainerWizardPage_wizard_description);
57         setImageDescriptor(JavaPluginImages.DESC_WIZBAN_ADD_LIBRARY);
58         
59         fContainerEntryResult= JavaCore.newContainerEntry(JUnitContainerInitializer.JUNIT3_PATH);
60     }
61
62     public static IJavaProject getPlaceholderProject() {
63         String JavaDoc name= "####internal"; //$NON-NLS-1$
64
IWorkspaceRoot root= ResourcesPlugin.getWorkspace().getRoot();
65         while (true) {
66             IProject project= root.getProject(name);
67             if (!project.exists()) {
68                 return JavaCore.create(project);
69             }
70             name += '1';
71         }
72     }
73     
74     public boolean finish() {
75         try {
76             IJavaProject[] javaProjects= new IJavaProject[] { getPlaceholderProject() };
77             IClasspathContainer[] containers= { null };
78             JavaCore.setClasspathContainer(fContainerEntryResult.getPath(), javaProjects, containers, null);
79         } catch (JavaModelException e) {
80             ExceptionHandler.handle(e, getShell(), JUnitMessages.JUnitContainerWizardPage_error_title, JUnitMessages.JUnitContainerWizardPage_error_problem_configuring_container);
81             return false;
82         }
83         return true;
84     }
85
86     public IClasspathEntry getSelection() {
87         return fContainerEntryResult;
88     }
89
90     public void setSelection(IClasspathEntry containerEntry) {
91         fContainerEntryResult= containerEntry;
92     }
93
94     public void createControl(Composite parent) {
95         PixelConverter converter= new PixelConverter(parent);
96         
97         Composite composite= new Composite(parent, SWT.NONE);
98         composite.setFont(parent.getFont());
99         
100         composite.setLayout(new GridLayout(2, false));
101         
102         Label label= new Label(composite, SWT.NONE);
103         label.setFont(composite.getFont());
104         label.setLayoutData(new GridData(GridData.FILL, GridData.CENTER, false, false, 1, 1));
105         label.setText(JUnitMessages.JUnitContainerWizardPage_combo_label);
106         
107         fVersionCombo= new Combo(composite, SWT.READ_ONLY);
108         fVersionCombo.setItems(new String JavaDoc[] {
109                 JUnitMessages.JUnitContainerWizardPage_option_junit3,
110                 JUnitMessages.JUnitContainerWizardPage_option_junit4
111         });
112         fVersionCombo.setFont(composite.getFont());
113         
114         GridData data= new GridData(GridData.BEGINNING, GridData.CENTER, false, false, 1, 1);
115         data.widthHint= converter.convertWidthInCharsToPixels(15);
116         fVersionCombo.setLayoutData(data);
117         
118         if (fContainerEntryResult != null && JUnitContainerInitializer.JUNIT4_PATH.equals(fContainerEntryResult.getPath())) {
119             fVersionCombo.select(1);
120         } else {
121             fVersionCombo.select(0);
122         }
123         fVersionCombo.addModifyListener(new ModifyListener() {
124             public void modifyText(ModifyEvent e) {
125                 doSelectionChanged();
126             }
127         });
128         
129         label= new Label(composite, SWT.NONE);
130         label.setFont(composite.getFont());
131         label.setText(JUnitMessages.JUnitContainerWizardPage_resolved_label);
132         label.setLayoutData(new GridData(GridData.FILL, GridData.BEGINNING, false, false, 1, 1));
133         
134         fResolvedPath= new Label(composite, SWT.WRAP);
135         data= new GridData(GridData.FILL, GridData.FILL, true, false, 1, 1);
136         data.widthHint= converter.convertWidthInCharsToPixels(60);
137         fResolvedPath.setFont(composite.getFont());
138         fResolvedPath.setLayoutData(data);
139         
140         doSelectionChanged();
141         
142         setControl(composite);
143     }
144
145     protected void doSelectionChanged() {
146         JUnitStatus status= new JUnitStatus();
147         
148         IClasspathEntry libEntry;
149         IPath containerPath;
150         if (fVersionCombo != null && fVersionCombo.getSelectionIndex() == 1) {
151             containerPath= JUnitContainerInitializer.JUNIT4_PATH;
152             libEntry= BuildPathSupport.getJUnit4LibraryEntry();
153         } else {
154             containerPath= JUnitContainerInitializer.JUNIT3_PATH;
155             libEntry= BuildPathSupport.getJUnit3LibraryEntry();
156         }
157         
158         if (libEntry == null) {
159             status.setError(JUnitMessages.JUnitContainerWizardPage_error_version_not_available);
160         } else if (JUnitContainerInitializer.JUNIT4_PATH.equals(containerPath)) {
161             if (fProject != null && !JUnitStubUtility.is50OrHigher(fProject)) {
162                 status.setWarning(JUnitMessages.JUnitContainerWizardPage_warning_java5_required);
163             }
164         }
165         fContainerEntryResult= JavaCore.newContainerEntry(containerPath);
166         
167         if (fResolvedPath != null && !fResolvedPath.isDisposed()) {
168             if (libEntry != null) {
169                 fResolvedPath.setText(libEntry.getPath().toOSString());
170             } else {
171                 fResolvedPath.setText(new String JavaDoc());
172             }
173         }
174         updateStatus(status);
175     }
176
177     /* (non-Javadoc)
178      * @see org.eclipse.jdt.ui.wizards.IClasspathContainerPageExtension#initialize(org.eclipse.jdt.core.IJavaProject, org.eclipse.jdt.core.IClasspathEntry[])
179      */

180     public void initialize(IJavaProject project, IClasspathEntry[] currentEntries) {
181         fProject= project;
182     }
183
184 }
185
Popular Tags