1 12 13 package org.eclipse.ant.internal.ui.datatransfer; 14 15 import java.io.IOException ; 16 import java.lang.reflect.InvocationTargetException ; 17 import com.ibm.icu.text.MessageFormat; 18 import java.util.ArrayList ; 19 import java.util.Iterator ; 20 import java.util.List ; 21 import java.util.Set ; 22 import java.util.TreeSet ; 23 24 import javax.xml.parsers.ParserConfigurationException ; 25 import javax.xml.transform.TransformerConfigurationException ; 26 import javax.xml.transform.TransformerException ; 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 fSelectedJavaProjects = new ArrayList (); 65 private Button compatibilityCheckbox; 66 private Button compilerCheckbox; 67 private Text buildfilenameText; 68 private Text junitdirText; 69 70 public AntBuildfileExportPage() 71 { 72 super("AntBuildfileExportWizardPage"); setPageComplete(false); 74 setTitle(DataTransferMessages.AntBuildfileExportPage_0); 75 setDescription(DataTransferMessages.AntBuildfileExportPage_1); 76 } 77 78 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 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 [] getElements(Object 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 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 buildfilenameLabel = new Label(containerGroup, SWT.NONE); 202 buildfilenameLabel.setText(DataTransferMessages.AntBuildfileExportPage_16); 203 204 buildfilenameText = new Text(containerGroup, SWT.SINGLE | SWT.BORDER); 206 buildfilenameText.setText("build.xml"); GridData data = new GridData(GridData.HORIZONTAL_ALIGN_FILL 208 | GridData.GRAB_HORIZONTAL); 209 buildfilenameText.setLayoutData(data); 210 211 Label junitdirLabel = new Label(containerGroup, SWT.NONE); 213 junitdirLabel.setText(DataTransferMessages.AntBuildfileExportPage_17); 214 215 junitdirText = new Text(containerGroup, SWT.SINGLE | SWT.BORDER); 217 junitdirText.setText("junit"); 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 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 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 selectedJavaProjects) { 277 fSelectedJavaProjects.addAll(selectedJavaProjects); 278 } 279 280 283 public boolean generateBuildfiles() 284 { 285 setErrorMessage(null); 286 final List projectNames = new ArrayList (); 287 final Set 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 [] { e.toString() })); 295 return false; 296 } 297 IRunnableWithProgress runnable = new IRunnableWithProgress() { 298 public void run(IProgressMonitor pm) throws InterruptedException { 299 Exception 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 e) { 310 problem= e; 311 } catch (ParserConfigurationException e) { 312 problem= e; 313 } catch (TransformerException e) { 314 problem= e; 315 } catch (IOException 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 [] {problem.toString()})); 324 } 325 } 326 }; 327 328 try { 329 PlatformUI.getWorkbench().getProgressService().run(false, false, runnable); 330 } catch (InvocationTargetException e) { 331 AntUIPlugin.log(e); 332 return false; 333 } catch (InterruptedException e) { 334 AntUIPlugin.log(e); 335 return false; 336 } 337 if (getErrorMessage() != null) { 338 return false; 339 } 340 341 if (projectNames.size() > 0) 343 { 344 String message = MessageFormat.format(DataTransferMessages.AntBuildfileExportPage_5 + ExportUtil.NEWLINE, new String [] {ExportUtil.NEWLINE + ExportUtil.toString(projectNames, ExportUtil.NEWLINE)}); 345 MessageDialog.openInformation(getShell(), DataTransferMessages.AntBuildfileExportPage_0, message); 346 347 if (!compatibilityCheckbox.getSelection()) { 349 return true; 350 } 351 List 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 [] { e.toString() })); 359 return false; 360 } 361 if (cyclicProjects.size() > 0) 362 { 363 String warningMessage= MessageFormat.format(DataTransferMessages.AntBuildfileExportPage_6 + ExportUtil.NEWLINE + ExportUtil.NEWLINE + 364 DataTransferMessages.AntBuildfileExportPage_7 + " " + DataTransferMessages.AntBuildfileExportPage_8, 366 new String [] { ExportUtil.NEWLINE + ExportUtil.toString(cyclicProjects, ExportUtil.NEWLINE)}); 367 MessageDialog.openWarning(getShell(), DataTransferMessages.AntBuildfileExportPage_9, warningMessage); 368 } 369 } 370 371 return true; 372 } 373 374 378 private Set getProjects() throws JavaModelException 379 { 380 Set projects = new TreeSet (ExportUtil.getJavaProjectComparator()); 382 Iterator 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 List confirmOverwrite = getConfirmOverwriteSet(projects); 391 if (confirmOverwrite.size() > 0) 392 { 393 String 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 (ExportUtil.getJavaProjectComparator()); 398 } 399 } 400 return projects; 401 } 402 403 409 private List getCyclicProjects(Set projects) throws CoreException { 410 411 List cyclicProjects = new ArrayList (); 412 for (Iterator 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 430 private List getConfirmOverwriteSet(Set javaProjects) 431 { 432 List result = new ArrayList (javaProjects.size()); 433 for (Iterator iter = javaProjects.iterator(); iter.hasNext();) 434 { 435 IJavaProject project = (IJavaProject) iter.next(); 436 String 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 |