1 11 12 package org.eclipse.jdt.internal.ui.wizards.buildpaths.newsourcepage; 13 14 import java.util.ArrayList ; 15 import java.util.Arrays ; 16 import java.util.List ; 17 18 import org.eclipse.core.runtime.CoreException; 19 import org.eclipse.core.runtime.IPath; 20 import org.eclipse.core.runtime.Path; 21 22 import org.eclipse.core.resources.IFolder; 23 24 import org.eclipse.swt.SWT; 25 import org.eclipse.swt.custom.SashForm; 26 import org.eclipse.swt.layout.GridData; 27 import org.eclipse.swt.layout.GridLayout; 28 import org.eclipse.swt.widgets.Composite; 29 import org.eclipse.swt.widgets.Control; 30 import org.eclipse.swt.widgets.Display; 31 32 import org.eclipse.jface.action.ToolBarManager; 33 import org.eclipse.jface.operation.IRunnableContext; 34 import org.eclipse.jface.preference.IPreferenceStore; 35 import org.eclipse.jface.viewers.StructuredSelection; 36 37 import org.eclipse.ui.forms.events.ExpansionAdapter; 38 import org.eclipse.ui.forms.events.ExpansionEvent; 39 import org.eclipse.ui.forms.widgets.ExpandableComposite; 40 41 import org.eclipse.jdt.core.IClasspathEntry; 42 import org.eclipse.jdt.core.IJavaProject; 43 import org.eclipse.jdt.core.IPackageFragmentRoot; 44 import org.eclipse.jdt.core.JavaCore; 45 import org.eclipse.jdt.core.JavaModelException; 46 47 import org.eclipse.jdt.internal.corext.buildpath.BuildpathDelta; 48 import org.eclipse.jdt.internal.corext.buildpath.ClasspathModifier; 49 import org.eclipse.jdt.internal.corext.buildpath.IBuildpathModifierListener; 50 51 import org.eclipse.jdt.internal.ui.JavaPlugin; 52 import org.eclipse.jdt.internal.ui.preferences.ScrolledPageContent; 53 import org.eclipse.jdt.internal.ui.util.PixelConverter; 54 import org.eclipse.jdt.internal.ui.util.ViewerPane; 55 import org.eclipse.jdt.internal.ui.wizards.NewWizardMessages; 56 import org.eclipse.jdt.internal.ui.wizards.buildpaths.BuildPathBasePage; 57 import org.eclipse.jdt.internal.ui.wizards.buildpaths.BuildPathsBlock; 58 import org.eclipse.jdt.internal.ui.wizards.buildpaths.CPListElement; 59 import org.eclipse.jdt.internal.ui.wizards.buildpaths.CPListElementAttribute; 60 import org.eclipse.jdt.internal.ui.wizards.dialogfields.DialogField; 61 import org.eclipse.jdt.internal.ui.wizards.dialogfields.IDialogFieldListener; 62 import org.eclipse.jdt.internal.ui.wizards.dialogfields.LayoutUtil; 63 import org.eclipse.jdt.internal.ui.wizards.dialogfields.ListDialogField; 64 import org.eclipse.jdt.internal.ui.wizards.dialogfields.SelectionButtonDialogField; 65 import org.eclipse.jdt.internal.ui.wizards.dialogfields.StringDialogField; 66 67 public class NewSourceContainerWorkbookPage extends BuildPathBasePage implements IBuildpathModifierListener { 68 69 public static final String OPEN_SETTING= "org.eclipse.jdt.internal.ui.wizards.buildpaths.NewSourceContainerPage.openSetting"; 71 private ListDialogField fClassPathList; 72 private HintTextGroup fHintTextGroup; 73 private DialogPackageExplorer fPackageExplorer; 74 private SelectionButtonDialogField fUseFolderOutputs; 75 private final StringDialogField fOutputLocationField; 76 private DialogPackageExplorerActionGroup fActionGroup; 77 78 private IJavaProject fJavaProject; 79 80 private final IRunnableContext fContext; 81 82 private final BuildPathsBlock fBuildPathsBlock; 83 84 85 96 public NewSourceContainerWorkbookPage(ListDialogField classPathList, StringDialogField outputLocationField, IRunnableContext context, BuildPathsBlock buildPathsBlock) { 97 fClassPathList= classPathList; 98 fOutputLocationField= outputLocationField; 99 fContext= context; 100 fBuildPathsBlock= buildPathsBlock; 101 102 fUseFolderOutputs= new SelectionButtonDialogField(SWT.CHECK); 103 fUseFolderOutputs.setSelection(false); 104 fUseFolderOutputs.setLabelText(NewWizardMessages.SourceContainerWorkbookPage_folders_check); 105 106 fPackageExplorer= new DialogPackageExplorer(); 107 fHintTextGroup= new HintTextGroup(); 108 } 109 110 120 public void init(IJavaProject javaProject) { 121 fJavaProject= javaProject; 122 fPackageExplorer.addPostSelectionChangedListener(fHintTextGroup); 123 fActionGroup.getResetAllAction().setBreakPoint(javaProject); 124 125 if (Display.getCurrent() != null) { 126 doUpdateUI(); 127 } else { 128 Display.getDefault().asyncExec(new Runnable () { 129 public void run() { 130 doUpdateUI(); 131 } 132 }); 133 } 134 } 135 136 private void doUpdateUI() { 137 fPackageExplorer.setInput(fJavaProject); 138 139 boolean useFolderOutputs= false; 140 List cpelements= fClassPathList.getElements(); 141 for (int i= 0; i < cpelements.size() && !useFolderOutputs; i++) { 142 CPListElement cpe= (CPListElement) cpelements.get(i); 143 if (cpe.getEntryKind() == IClasspathEntry.CPE_SOURCE) { 144 if (cpe.getAttribute(CPListElement.OUTPUT) != null) { 145 useFolderOutputs= true; 146 } 147 } 148 } 149 fUseFolderOutputs.setSelection(useFolderOutputs); 150 } 151 152 public void dispose() { 153 if (fActionGroup != null) { 154 fActionGroup.removeBuildpathModifierListener(this); 155 fActionGroup= null; 156 } 157 fPackageExplorer.removePostSelectionChangedListener(fHintTextGroup); 158 fPackageExplorer.dispose(); 159 } 160 161 174 public Control getControl(Composite parent) { 175 final int[] sashWeight= {60}; 176 final IPreferenceStore preferenceStore= JavaPlugin.getDefault().getPreferenceStore(); 177 preferenceStore.setDefault(OPEN_SETTING, true); 178 179 ScrolledPageContent scrolledContent = new ScrolledPageContent(parent); 181 Composite body= scrolledContent.getBody(); 182 body.setLayout(new GridLayout()); 183 184 final SashForm sashForm= new SashForm(body, SWT.VERTICAL | SWT.NONE); 185 sashForm.setFont(sashForm.getFont()); 186 187 ViewerPane pane= new ViewerPane(sashForm, SWT.BORDER | SWT.FLAT); 188 pane.setContent(fPackageExplorer.createControl(pane)); 189 fPackageExplorer.setContentProvider(); 190 191 final ExpandableComposite excomposite= new ExpandableComposite(sashForm, SWT.NONE, ExpandableComposite.TWISTIE | ExpandableComposite.CLIENT_INDENT); 192 excomposite.setFont(sashForm.getFont()); 193 excomposite.setText(NewWizardMessages.NewSourceContainerWorkbookPage_HintTextGroup_title); 194 final boolean isExpanded= preferenceStore.getBoolean(OPEN_SETTING); 195 excomposite.setExpanded(isExpanded); 196 excomposite.addExpansionListener(new ExpansionAdapter() { 197 public void expansionStateChanged(ExpansionEvent e) { 198 ScrolledPageContent parentScrolledComposite= getParentScrolledComposite(excomposite); 199 if (parentScrolledComposite != null) { 200 boolean expanded= excomposite.isExpanded(); 201 parentScrolledComposite.reflow(true); 202 adjustSashForm(sashWeight, sashForm, expanded); 203 preferenceStore.setValue(OPEN_SETTING, expanded); 204 } 205 } 206 }); 207 208 excomposite.setClient(fHintTextGroup.createControl(excomposite)); 209 fUseFolderOutputs.doFillIntoGrid(body, 1); 210 211 fActionGroup= new DialogPackageExplorerActionGroup(fHintTextGroup, fContext, fPackageExplorer, this); 212 fActionGroup.addBuildpathModifierListener(this); 213 214 215 fUseFolderOutputs.setDialogFieldListener(new IDialogFieldListener() { 216 public void dialogFieldChanged(DialogField field) { 217 boolean isUseFolders= fUseFolderOutputs.isSelected(); 218 if (!isUseFolders) { 219 ResetAllOutputFoldersAction action= new ResetAllOutputFoldersAction(fContext, fJavaProject, fPackageExplorer) { 220 public void run() { 221 commitDefaultOutputFolder(); 222 super.run(); 223 } 224 }; 225 action.addBuildpathModifierListener(NewSourceContainerWorkbookPage.this); 226 action.run(); 227 } 228 fPackageExplorer.showOutputFolders(isUseFolders); 229 } 230 }); 231 232 Composite outputLocation= new Composite(body, SWT.NONE); 233 outputLocation.setLayout(new GridLayout(2, false)); 234 outputLocation.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false)); 235 236 LayoutUtil.doDefaultLayout(outputLocation, new DialogField[] {fOutputLocationField }, true, SWT.DEFAULT, SWT.DEFAULT); 237 LayoutUtil.setHorizontalGrabbing(fOutputLocationField.getTextControl(null)); 238 239 ToolBarManager tbm= fActionGroup.createLeftToolBarManager(pane); 241 pane.setTopCenter(null); 242 pane.setTopLeft(tbm.getControl()); 243 244 tbm= fActionGroup.createLeftToolBar(pane); 246 pane.setTopRight(tbm.getControl()); 247 248 fHintTextGroup.setActionGroup(fActionGroup); 249 fPackageExplorer.setActionGroup(fActionGroup); 250 251 sashForm.setWeights(new int[] {60, 40}); 252 adjustSashForm(sashWeight, sashForm, excomposite.isExpanded()); 253 GridData gd= new GridData(GridData.FILL_BOTH); 254 PixelConverter converter= new PixelConverter(parent); 255 gd.heightHint= converter.convertHeightInCharsToPixels(20); 256 sashForm.setLayoutData(gd); 257 258 parent.layout(true); 259 260 return scrolledContent; 261 } 262 263 271 private void adjustSashForm(int[] sashWeight, SashForm sashForm, boolean isExpanded) { 272 if (isExpanded) { 273 int upperWeight= sashWeight[0]; 274 sashForm.setWeights(new int[]{upperWeight, 100 - upperWeight}); 275 } 276 else { 277 sashWeight[0]= sashForm.getWeights()[0] / 10; 279 sashForm.setWeights(new int[]{95, 5}); 280 } 281 sashForm.layout(true); 282 } 283 284 291 private ScrolledPageContent getParentScrolledComposite(Control control) { 292 Control parent= control.getParent(); 293 while (!(parent instanceof ScrolledPageContent)) { 294 parent= parent.getParent(); 295 } 296 if (parent instanceof ScrolledPageContent) { 297 return (ScrolledPageContent) parent; 298 } 299 return null; 300 } 301 302 305 public List getSelection() { 306 List selectedList= new ArrayList (); 307 308 IJavaProject project= fJavaProject; 309 try { 310 List list= ((StructuredSelection)fPackageExplorer.getSelection()).toList(); 311 List existingEntries= ClasspathModifier.getExistingEntries(project); 312 313 for(int i= 0; i < list.size(); i++) { 314 Object obj= list.get(i); 315 if (obj instanceof IPackageFragmentRoot) { 316 IPackageFragmentRoot element= (IPackageFragmentRoot)obj; 317 CPListElement cpElement= ClasspathModifier.getClasspathEntry(existingEntries, element); 318 selectedList.add(cpElement); 319 } 320 else if (obj instanceof IJavaProject) { 321 IClasspathEntry entry= ClasspathModifier.getClasspathEntryFor(project.getPath(), project, IClasspathEntry.CPE_SOURCE); 322 if (entry == null) 323 continue; 324 CPListElement cpElement= CPListElement.createFromExisting(entry, project); 325 selectedList.add(cpElement); 326 } 327 } 328 } catch (JavaModelException e) { 329 return new ArrayList (); 330 } 331 return selectedList; 332 } 333 334 337 public void setSelection(List selection, boolean expand) { 338 340 if (selection.size() == 0) 341 return; 342 343 List cpEntries= new ArrayList (); 344 345 for (int i= 0; i < selection.size(); i++) { 346 Object obj= selection.get(i); 347 if (obj instanceof CPListElement) { 348 CPListElement element= (CPListElement) obj; 349 if (element.getEntryKind() == IClasspathEntry.CPE_SOURCE) { 350 cpEntries.add(element); 351 } 352 } else if (obj instanceof CPListElementAttribute) { 353 CPListElementAttribute attribute= (CPListElementAttribute)obj; 354 CPListElement element= attribute.getParent(); 355 if (element.getEntryKind() == IClasspathEntry.CPE_SOURCE) { 356 cpEntries.add(element); 357 } 358 } 359 } 360 361 List list= fClassPathList.getElements(); 363 IClasspathEntry[] entries= new IClasspathEntry[list.size()]; 364 for(int i= 0; i < list.size(); i++) { 365 CPListElement entry= (CPListElement) list.get(i); 366 entries[i]= entry.getClasspathEntry(); 367 } 368 try { 369 fJavaProject.setRawClasspath(entries, null); 370 } catch (JavaModelException e) { 371 JavaPlugin.log(e); 372 } 373 374 fPackageExplorer.setSelection(cpEntries); 375 } 376 377 380 public boolean isEntryKind(int kind) { 381 return kind == IClasspathEntry.CPE_SOURCE; 382 } 383 384 388 public void buildpathChanged(BuildpathDelta delta) { 389 fClassPathList.setElements(Arrays.asList(delta.getNewEntries())); 390 391 try { 392 fOutputLocationField.setText(fJavaProject.getOutputLocation().makeRelative().toString()); 393 } catch (JavaModelException e) { 394 JavaPlugin.log(e); 395 } 396 } 397 398 public void commitDefaultOutputFolder() { 399 if (!fBuildPathsBlock.isOKStatus()) 400 return; 401 try { 402 IPath path= new Path(fOutputLocationField.getText()).makeAbsolute(); 403 IPath outputLocation= fJavaProject.getOutputLocation(); 404 if (path.equals(outputLocation)) 405 return; 406 407 if (!outputLocation.equals(fJavaProject.getPath())) { 408 IFolder folder= fJavaProject.getProject().getWorkspace().getRoot().getFolder(outputLocation); 409 if (folder.exists() && JavaCore.create(folder) == null) { 410 folder.delete(true, null); 411 } 412 } 413 fJavaProject.setOutputLocation(path, null); 414 } catch (JavaModelException e) { 415 JavaPlugin.log(e); 416 } catch (CoreException e) { 417 JavaPlugin.log(e); 418 } 419 } 420 421 424 public void setFocus() { 425 fPackageExplorer.getViewerControl().setFocus(); 426 } 427 428 public IJavaProject getJavaProject() { 429 return fJavaProject; 430 } 431 } 432 | Popular Tags |