KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ant > internal > ui > datatransfer > AntBuildfileExportPage


1 /*******************************************************************************
2  * Copyright (c) 2004, 2007 Richard Hoefter 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  * Richard Hoefter (richard.hoefter@web.de) - initial API and implementation, bug 95296
10  * IBM Corporation - adapted to wizard export page
11  *******************************************************************************/

12
13 package org.eclipse.ant.internal.ui.datatransfer;
14
15 import java.io.IOException JavaDoc;
16 import java.lang.reflect.InvocationTargetException JavaDoc;
17 import com.ibm.icu.text.MessageFormat;
18 import java.util.ArrayList JavaDoc;
19 import java.util.Iterator JavaDoc;
20 import java.util.List JavaDoc;
21 import java.util.Set JavaDoc;
22 import java.util.TreeSet JavaDoc;
23
24 import javax.xml.parsers.ParserConfigurationException JavaDoc;
25 import javax.xml.transform.TransformerConfigurationException JavaDoc;
26 import javax.xml.transform.TransformerException JavaDoc;
27
28 import org.eclipse.ant.internal.ui.AntUIPlugin;
29 import org.eclipse.core.resources.IWorkspaceRoot;
30 import org.eclipse.core.resources.ResourcesPlugin;
31 import org.eclipse.core.runtime.CoreException;
32 import org.eclipse.core.runtime.IProgressMonitor;
33 import org.eclipse.jdt.core.IJavaModel;
34 import org.eclipse.jdt.core.IJavaProject;
35 import org.eclipse.jdt.core.JavaCore;
36 import org.eclipse.jdt.core.JavaModelException;
37 import org.eclipse.jface.dialogs.Dialog;
38 import org.eclipse.jface.dialogs.MessageDialog;
39 import org.eclipse.jface.operation.IRunnableWithProgress;
40 import org.eclipse.jface.viewers.CheckStateChangedEvent;
41 import org.eclipse.jface.viewers.CheckboxTableViewer;
42 import org.eclipse.jface.viewers.ICheckStateListener;
43 import org.eclipse.jface.viewers.TableLayout;
44 import org.eclipse.jface.wizard.WizardPage;
45 import org.eclipse.swt.SWT;
46 import org.eclipse.swt.events.ModifyEvent;
47 import org.eclipse.swt.events.ModifyListener;
48 import org.eclipse.swt.events.SelectionAdapter;
49 import org.eclipse.swt.events.SelectionEvent;
50 import org.eclipse.swt.layout.GridData;
51 import org.eclipse.swt.layout.GridLayout;
52 import org.eclipse.swt.widgets.Button;
53 import org.eclipse.swt.widgets.Composite;
54 import org.eclipse.swt.widgets.Label;
55 import org.eclipse.swt.widgets.Table;
56 import org.eclipse.swt.widgets.Text;
57 import org.eclipse.ui.PlatformUI;
58 import org.eclipse.ui.model.WorkbenchContentProvider;
59 import org.eclipse.ui.model.WorkbenchLabelProvider;
60
61 public class AntBuildfileExportPage extends WizardPage {
62     
63     private CheckboxTableViewer fTableViewer;
64     private List JavaDoc fSelectedJavaProjects = new ArrayList JavaDoc();
65     private Button compatibilityCheckbox;
66     private Button compilerCheckbox;
67     private Text buildfilenameText;
68     private Text junitdirText;
69     
70     public AntBuildfileExportPage()
71     {
72         super("AntBuildfileExportWizardPage"); //$NON-NLS-1$
73
setPageComplete(false);
74         setTitle(DataTransferMessages.AntBuildfileExportPage_0);
75         setDescription(DataTransferMessages.AntBuildfileExportPage_1);
76     }
77     
78     /*
79      * @see IDialogPage#createControl(Composite)
80      */

81     public void createControl(Composite parent) {
82         
83         initializeDialogUnits(parent);
84
85         Composite workArea = new Composite(parent, SWT.NONE);
86         setControl(workArea);
87
88         workArea.setLayout(new GridLayout());
89         workArea.setLayoutData(new GridData(GridData.FILL_BOTH
90                 | GridData.GRAB_HORIZONTAL | GridData.GRAB_VERTICAL));
91         
92         Label titel= new Label(workArea, SWT.NONE);
93         titel.setText(DataTransferMessages.AntBuildfileExportPage_2);
94
95         Composite listComposite = new Composite(workArea, SWT.NONE);
96         GridLayout layout = new GridLayout();
97         layout.numColumns = 2;
98         layout.marginWidth = 0;
99         layout.makeColumnsEqualWidth = false;
100         listComposite.setLayout(layout);
101
102         listComposite.setLayoutData(new GridData(GridData.GRAB_HORIZONTAL
103                 | GridData.GRAB_VERTICAL | GridData.FILL_BOTH));
104
105         //TODO set F1 help
106

107         Table table = new Table(listComposite, SWT.CHECK | SWT.BORDER | SWT.V_SCROLL | SWT.H_SCROLL);
108         fTableViewer = new CheckboxTableViewer(table);
109         table.setLayout(new TableLayout());
110         GridData data = new GridData(SWT.FILL, SWT.FILL, true, true);
111         data.heightHint = 300;
112         table.setLayoutData(data);
113         fTableViewer.setContentProvider(new WorkbenchContentProvider() {
114             public Object JavaDoc[] getElements(Object JavaDoc element) {
115                 if (element instanceof IJavaProject[]) {
116                     return (IJavaProject[]) element;
117                 }
118                 return null;
119             }
120         });
121         fTableViewer.setLabelProvider(new WorkbenchLabelProvider());
122         fTableViewer.addCheckStateListener(new ICheckStateListener() {
123             public void checkStateChanged(CheckStateChangedEvent event) {
124                 if (event.getChecked()) {
125                     fSelectedJavaProjects.add(event.getElement());
126                 } else {
127                     fSelectedJavaProjects.remove(event.getElement());
128                 }
129                 updateEnablement();
130             }
131         });
132        
133         initializeProjects();
134         createSelectionButtons(listComposite);
135         createCheckboxes(workArea);
136         createTextFields(workArea);
137         setControl(workArea);
138         updateEnablement();
139         Dialog.applyDialogFont(parent);
140     }
141
142     private void createSelectionButtons(Composite composite) {
143
144         Composite buttonsComposite = new Composite(composite, SWT.NONE);
145         GridLayout layout = new GridLayout();
146         layout.marginWidth = 0;
147         layout.marginHeight = 0;
148         buttonsComposite.setLayout(layout);
149
150         buttonsComposite.setLayoutData(new GridData(
151                 GridData.VERTICAL_ALIGN_BEGINNING));
152
153         Button selectAll = new Button(buttonsComposite, SWT.PUSH);
154         selectAll.setText(DataTransferMessages.AntBuildfileExportPage_11);
155         selectAll.addSelectionListener(new SelectionAdapter() {
156             public void widgetSelected(SelectionEvent e) {
157                 for (int i = 0; i < fTableViewer.getTable().getItemCount(); i++) {
158                     fSelectedJavaProjects.add(fTableViewer.getElementAt(i));
159                 }
160                 fTableViewer.setAllChecked(true);
161                 updateEnablement();
162             }
163         });
164         setButtonLayoutData(selectAll);
165         
166         Button deselectAll = new Button(buttonsComposite, SWT.PUSH);
167         deselectAll.setText(DataTransferMessages.AntBuildfileExportPage_12);
168         deselectAll.addSelectionListener(new SelectionAdapter() {
169             public void widgetSelected(SelectionEvent e) {
170                 fSelectedJavaProjects.clear();
171                 fTableViewer.setAllChecked(false);
172                 updateEnablement();
173             }
174         });
175         setButtonLayoutData(deselectAll);
176     }
177
178     private void createCheckboxes(Composite composite) {
179
180         compatibilityCheckbox = new Button(composite, SWT.CHECK);
181         compatibilityCheckbox.setSelection(true);
182         compatibilityCheckbox.setText(DataTransferMessages.AntBuildfileExportPage_13);
183         compatibilityCheckbox.setToolTipText(DataTransferMessages.AntBuildfileExportPage_14);
184
185         compilerCheckbox = new Button(composite, SWT.CHECK);
186         compilerCheckbox.setSelection(true);
187         compilerCheckbox.setText(DataTransferMessages.AntBuildfileExportPage_15);
188     }
189
190     private void createTextFields(Composite composite) {
191
192         // buildfilename and junitdir group
193
Composite containerGroup = new Composite(composite, SWT.NONE);
194         GridLayout layout = new GridLayout();
195         layout.numColumns = 2;
196         containerGroup.setLayout(layout);
197         containerGroup.setLayoutData(new GridData(
198                 GridData.HORIZONTAL_ALIGN_FILL | GridData.GRAB_HORIZONTAL));
199
200         // label
201
Label buildfilenameLabel = new Label(containerGroup, SWT.NONE);
202         buildfilenameLabel.setText(DataTransferMessages.AntBuildfileExportPage_16);
203
204         // text field
205
buildfilenameText = new Text(containerGroup, SWT.SINGLE | SWT.BORDER);
206         buildfilenameText.setText("build.xml"); //$NON-NLS-1$
207
GridData data = new GridData(GridData.HORIZONTAL_ALIGN_FILL
208                 | GridData.GRAB_HORIZONTAL);
209         buildfilenameText.setLayoutData(data);
210
211         // label
212
Label junitdirLabel = new Label(containerGroup, SWT.NONE);
213         junitdirLabel.setText(DataTransferMessages.AntBuildfileExportPage_17);
214
215         // text field
216
junitdirText = new Text(containerGroup, SWT.SINGLE | SWT.BORDER);
217         junitdirText.setText("junit"); //$NON-NLS-1$
218
junitdirText.setLayoutData(data);
219         
220         ModifyListener listener = new ModifyListener() {
221             public void modifyText(ModifyEvent e) {
222                 updateEnablement();
223             }
224         };
225         buildfilenameText.addModifyListener(listener);
226         junitdirText.addModifyListener(listener);
227     }
228
229     private void initializeProjects() {
230         IWorkspaceRoot rootWorkspace = ResourcesPlugin.getWorkspace().getRoot();
231         IJavaModel javaModel = JavaCore.create(rootWorkspace);
232         IJavaProject[] javaProjects;
233         try {
234             javaProjects = javaModel.getJavaProjects();
235         }
236         catch (JavaModelException e) {
237             javaProjects= new IJavaProject[0];
238         }
239         fTableViewer.setInput(javaProjects);
240         // Check any necessary projects
241
if (fSelectedJavaProjects != null) {
242             fTableViewer.setCheckedElements(fSelectedJavaProjects.toArray(new IJavaProject[fSelectedJavaProjects.size()]));
243         }
244     }
245     
246     private void updateEnablement() {
247         boolean complete= true;
248         if (fSelectedJavaProjects.size() == 0) {
249             setErrorMessage(DataTransferMessages.AntBuildfileExportPage_18);
250             complete = false;
251         }
252         if (buildfilenameText.getText().length() == 0) {
253             setErrorMessage(DataTransferMessages.AntBuildfileExportPage_19);
254             complete = false;
255         }
256         if (junitdirText.getText().length() == 0) {
257             setErrorMessage(DataTransferMessages.AntBuildfileExportPage_20);
258             complete = false;
259         }
260         if (complete) {
261             setErrorMessage(null);
262         }
263         setPageComplete(complete);
264     }
265     
266     /* (non-Javadoc)
267      * @see org.eclipse.jface.dialogs.IDialogPage#setVisible(boolean)
268      */

269     public void setVisible(boolean visible) {
270         super.setVisible(visible);
271         if (visible) {
272             fTableViewer.getTable().setFocus();
273         }
274     }
275     
276     protected void setSelectedProjects(List JavaDoc selectedJavaProjects) {
277         fSelectedJavaProjects.addAll(selectedJavaProjects);
278     }
279     
280     /**
281      * Convert Eclipse Java projects to Ant build files. Displays error dialogs.
282      */

283     public boolean generateBuildfiles()
284     {
285         setErrorMessage(null);
286         final List JavaDoc projectNames = new ArrayList JavaDoc();
287         final Set JavaDoc projects;
288         try {
289             projects = getProjects();
290         } catch (JavaModelException e) {
291             AntUIPlugin.log(e);
292             setErrorMessage(MessageFormat.format(
293                     DataTransferMessages.AntBuildfileExportPage_10,
294                     new String JavaDoc[] { e.toString() }));
295             return false;
296         }
297         IRunnableWithProgress runnable = new IRunnableWithProgress() {
298             public void run(IProgressMonitor pm) throws InterruptedException JavaDoc {
299                 Exception JavaDoc problem= null;
300                 try {
301                     BuildFileCreator.setOptions(buildfilenameText.getText(),
302                             junitdirText.getText(), compatibilityCheckbox
303                                     .getSelection(), compilerCheckbox
304                                     .getSelection());
305                     projectNames.addAll(BuildFileCreator.createBuildFiles(
306                             projects, getShell(), pm));
307                 } catch (JavaModelException e) {
308                     problem= e;
309                 } catch (TransformerConfigurationException JavaDoc e) {
310                     problem= e;
311                 } catch (ParserConfigurationException JavaDoc e) {
312                     problem= e;
313                 } catch (TransformerException JavaDoc e) {
314                     problem= e;
315                 } catch (IOException JavaDoc e) {
316                     problem= e;
317                 } catch (CoreException e) {
318                     problem= e;
319                 }
320
321                 if (problem != null) {
322                     AntUIPlugin.log(problem);
323                     setErrorMessage(MessageFormat.format(DataTransferMessages.AntBuildfileExportPage_10, new String JavaDoc[] {problem.toString()}));
324                 }
325             }
326         };
327
328         try {
329             PlatformUI.getWorkbench().getProgressService().run(false, false, runnable);
330         } catch (InvocationTargetException JavaDoc e) {
331             AntUIPlugin.log(e);
332             return false;
333         } catch (InterruptedException JavaDoc e) {
334             AntUIPlugin.log(e);
335             return false;
336         }
337         if (getErrorMessage() != null) {
338             return false;
339         }
340         
341         // show success message
342
if (projectNames.size() > 0)
343         {
344             String JavaDoc message = MessageFormat.format(DataTransferMessages.AntBuildfileExportPage_5 + ExportUtil.NEWLINE, new String JavaDoc[] {ExportUtil.NEWLINE + ExportUtil.toString(projectNames, ExportUtil.NEWLINE)});
345             MessageDialog.openInformation(getShell(), DataTransferMessages.AntBuildfileExportPage_0, message);
346             
347             // show warning if project has cycle
348
if (!compatibilityCheckbox.getSelection()) {
349                 return true;
350             }
351             List JavaDoc cyclicProjects;
352             try {
353                 cyclicProjects = getCyclicProjects(projects);
354             } catch (CoreException e) {
355                 AntUIPlugin.log(e);
356                 setErrorMessage(MessageFormat.format(
357                         DataTransferMessages.AntBuildfileExportPage_10,
358                         new String JavaDoc[] { e.toString() }));
359                 return false;
360             }
361             if (cyclicProjects.size() > 0)
362             {
363                 String JavaDoc warningMessage= MessageFormat.format(DataTransferMessages.AntBuildfileExportPage_6 + ExportUtil.NEWLINE + ExportUtil.NEWLINE +
364                         DataTransferMessages.AntBuildfileExportPage_7 + " " + //$NON-NLS-1$
365
DataTransferMessages.AntBuildfileExportPage_8,
366                         new String JavaDoc[] { ExportUtil.NEWLINE + ExportUtil.toString(cyclicProjects, ExportUtil.NEWLINE)});
367                 MessageDialog.openWarning(getShell(), DataTransferMessages.AntBuildfileExportPage_9, warningMessage);
368             }
369         }
370
371         return true;
372     }
373
374     /**
375      * Get projects to write buildfiles for. Opens confirmation dialog.
376      * @return set of project names
377      */

378     private Set JavaDoc getProjects() throws JavaModelException
379     {
380         // collect all projects to create buildfiles for
381
Set JavaDoc projects = new TreeSet JavaDoc(ExportUtil.getJavaProjectComparator());
382         Iterator JavaDoc javaProjects = fSelectedJavaProjects.iterator();
383         while (javaProjects.hasNext()) {
384             IJavaProject javaProject = (IJavaProject) javaProjects.next();
385             projects.addAll(ExportUtil.getClasspathProjectsRecursive(javaProject));
386             projects.add(javaProject);
387         }
388         
389         // confirm overwrite
390
List JavaDoc confirmOverwrite = getConfirmOverwriteSet(projects);
391         if (confirmOverwrite.size() > 0)
392         {
393             String JavaDoc message = DataTransferMessages.AntBuildfileExportPage_3 + ExportUtil.NEWLINE +
394                 ExportUtil.toString(confirmOverwrite, ExportUtil.NEWLINE);
395             if (!MessageDialog.openConfirm(getShell(), DataTransferMessages.AntBuildfileExportPage_4, message))
396             {
397                 return new TreeSet JavaDoc(ExportUtil.getJavaProjectComparator());
398             }
399         }
400         return projects;
401     }
402
403     /**
404      * Returns given projects that have cyclic dependencies.
405      *
406      * @param javaProjects list of IJavaProject objects
407      * @return set of project names
408      */

409     private List JavaDoc getCyclicProjects(Set JavaDoc projects) throws CoreException {
410         
411         List JavaDoc cyclicProjects = new ArrayList JavaDoc();
412         for (Iterator JavaDoc iter = projects.iterator(); iter.hasNext();)
413         {
414             IJavaProject javaProject = (IJavaProject) iter.next();
415             if (ExportUtil.hasCyclicDependency(javaProject))
416             {
417                 cyclicProjects.add(javaProject.getProject().getName());
418             }
419         }
420         return cyclicProjects;
421     }
422
423     /**
424      * Get list of projects which have already a buildfile that was not
425      * created by the buildfile export.
426      *
427      * @param javaProjects list of IJavaProject objects
428      * @return set of project names
429      */

430     private List JavaDoc getConfirmOverwriteSet(Set JavaDoc javaProjects)
431     {
432         List JavaDoc result = new ArrayList JavaDoc(javaProjects.size());
433         for (Iterator JavaDoc iter = javaProjects.iterator(); iter.hasNext();)
434         {
435             IJavaProject project = (IJavaProject) iter.next();
436             String JavaDoc projectRoot = ExportUtil.getProjectRoot(project);
437             if (ExportUtil.existsUserFile(projectRoot + '/'
438                     + buildfilenameText.getText()))
439             {
440                 result.add(project.getProject().getName());
441             }
442         }
443         return result;
444     }
445 }
Popular Tags