1 11 package org.eclipse.jdt.internal.ui.wizards.buildpaths.newsourcepage; 12 13 import java.util.ArrayList ; 14 15 import org.eclipse.core.runtime.CoreException; 16 import org.eclipse.core.runtime.IPath; 17 18 import org.eclipse.core.resources.IProject; 19 import org.eclipse.core.resources.IResource; 20 21 import org.eclipse.swt.widgets.Shell; 22 23 import org.eclipse.jface.operation.IRunnableContext; 24 import org.eclipse.jface.resource.JFaceResources; 25 import org.eclipse.jface.viewers.IStructuredSelection; 26 import org.eclipse.jface.viewers.StructuredSelection; 27 import org.eclipse.jface.window.Window; 28 import org.eclipse.jface.wizard.WizardDialog; 29 30 import org.eclipse.ui.IWorkbenchSite; 31 import org.eclipse.ui.PlatformUI; 32 import org.eclipse.ui.part.ISetSelectionTarget; 33 34 import org.eclipse.jdt.core.IClasspathEntry; 35 import org.eclipse.jdt.core.IJavaProject; 36 37 import org.eclipse.jdt.internal.corext.buildpath.BuildpathDelta; 38 39 import org.eclipse.jdt.ui.PreferenceConstants; 40 41 import org.eclipse.jdt.internal.ui.IJavaHelpContextIds; 42 import org.eclipse.jdt.internal.ui.JavaPluginImages; 43 import org.eclipse.jdt.internal.ui.actions.ActionMessages; 44 import org.eclipse.jdt.internal.ui.util.ExceptionHandler; 45 import org.eclipse.jdt.internal.ui.util.PixelConverter; 46 import org.eclipse.jdt.internal.ui.wizards.NewWizardMessages; 47 import org.eclipse.jdt.internal.ui.wizards.buildpaths.AddSourceFolderWizard; 48 import org.eclipse.jdt.internal.ui.wizards.buildpaths.CPListElement; 49 50 public class CreateSourceFolderAction extends BuildpathModifierAction { 52 53 public CreateSourceFolderAction(IWorkbenchSite site) { 54 this(site, null, PlatformUI.getWorkbench().getProgressService()); 55 } 56 57 public CreateSourceFolderAction(IRunnableContext context, ISetSelectionTarget selectionTarget) { 58 this(null, selectionTarget, context); 59 } 60 61 private CreateSourceFolderAction(IWorkbenchSite site, ISetSelectionTarget selectionTarget, IRunnableContext context) { 62 super(site, selectionTarget, BuildpathModifierAction.CREATE_FOLDER); 63 64 setText(ActionMessages.OpenNewSourceFolderWizardAction_text2); 65 setDescription(ActionMessages.OpenNewSourceFolderWizardAction_description); 66 setToolTipText(ActionMessages.OpenNewSourceFolderWizardAction_tooltip); 67 setImageDescriptor(JavaPluginImages.DESC_TOOL_NEWPACKROOT); 68 69 PlatformUI.getWorkbench().getHelpSystem().setHelp(this, IJavaHelpContextIds.OPEN_SOURCEFOLDER_WIZARD_ACTION); 70 } 71 72 75 public String getDetailedDescription() { 76 return NewWizardMessages.PackageExplorerActionGroup_FormText_createNewSourceFolder; 77 } 78 79 82 public void run() { 83 Shell shell= getShell(); 84 85 try { 86 IJavaProject javaProject= (IJavaProject)getSelectedElements().get(0); 87 88 CPListElement newEntrie= new CPListElement(javaProject, IClasspathEntry.CPE_SOURCE); 89 CPListElement[] existing= CPListElement.createFromExisting(javaProject); 90 boolean isProjectSrcFolder= CPListElement.isProjectSourceFolder(existing, javaProject); 91 92 AddSourceFolderWizard wizard= new AddSourceFolderWizard(existing, newEntrie, getOutputLocation(javaProject), false, false, false, isProjectSrcFolder, isProjectSrcFolder); 93 wizard.init(PlatformUI.getWorkbench(), new StructuredSelection(javaProject)); 94 95 WizardDialog dialog= new WizardDialog(shell, wizard); 96 PixelConverter converter= new PixelConverter(JFaceResources.getDialogFont()); 97 dialog.setMinimumPageSize(converter.convertWidthInCharsToPixels(70), converter.convertHeightInCharsToPixels(20)); 98 dialog.create(); 99 int res= dialog.open(); 100 if (res == Window.OK) { 101 BuildpathDelta delta= new BuildpathDelta(getToolTipText()); 102 103 ArrayList newEntries= wizard.getExistingEntries(); 104 delta.setNewEntries((CPListElement[])newEntries.toArray(new CPListElement[newEntries.size()])); 105 106 IResource resource= wizard.getCreatedElement().getCorrespondingResource(); 107 delta.addCreatedResource(resource); 108 109 delta.setDefaultOutputLocation(wizard.getOutputLocation()); 110 111 informListeners(delta); 112 113 selectAndReveal(new StructuredSelection(wizard.getCreatedElement())); 114 } 115 116 notifyResult(res == Window.OK); 117 } catch (CoreException e) { 118 String title= NewWizardMessages.AbstractOpenWizardAction_createerror_title; 119 String message= NewWizardMessages.AbstractOpenWizardAction_createerror_message; 120 ExceptionHandler.handle(e, shell, title, message); 121 } 122 } 123 124 private IPath getOutputLocation(IJavaProject javaProject) { 125 try { 126 return javaProject.getOutputLocation(); 127 } catch (CoreException e) { 128 IProject project= javaProject.getProject(); 129 IPath projPath= project.getFullPath(); 130 return projPath.append(PreferenceConstants.getPreferenceStore().getString(PreferenceConstants.SRCBIN_BINNAME)); 131 } 132 } 133 134 protected boolean canHandle(IStructuredSelection selection) { 135 if (selection.size() != 1) 136 return false; 137 138 if (!(selection.getFirstElement() instanceof IJavaProject)) 139 return false; 140 141 return true; 142 } 143 } 144 | Popular Tags |