1 11 package org.eclipse.jdt.ui.wizards; 12 13 import java.lang.reflect.InvocationTargetException ; 14 import java.net.URI ; 15 16 import org.eclipse.core.filesystem.EFS; 17 import org.eclipse.core.filesystem.IFileStore; 18 19 import org.eclipse.core.runtime.CoreException; 20 import org.eclipse.core.runtime.IPath; 21 import org.eclipse.core.runtime.IProgressMonitor; 22 import org.eclipse.core.runtime.IStatus; 23 import org.eclipse.core.runtime.NullProgressMonitor; 24 25 import org.eclipse.core.resources.IResource; 26 27 import org.eclipse.swt.SWT; 28 import org.eclipse.swt.layout.GridData; 29 import org.eclipse.swt.layout.GridLayout; 30 import org.eclipse.swt.widgets.Composite; 31 import org.eclipse.swt.widgets.Label; 32 import org.eclipse.swt.widgets.Text; 33 34 import org.eclipse.jface.dialogs.Dialog; 35 import org.eclipse.jface.operation.IRunnableWithProgress; 36 import org.eclipse.jface.viewers.IStructuredSelection; 37 38 import org.eclipse.ui.PlatformUI; 39 40 import org.eclipse.jdt.core.IJavaElement; 41 import org.eclipse.jdt.core.IJavaProject; 42 import org.eclipse.jdt.core.IPackageFragment; 43 import org.eclipse.jdt.core.IPackageFragmentRoot; 44 import org.eclipse.jdt.core.JavaConventions; 45 import org.eclipse.jdt.core.JavaCore; 46 47 import org.eclipse.jdt.internal.corext.util.Messages; 48 49 import org.eclipse.jdt.internal.ui.IJavaHelpContextIds; 50 import org.eclipse.jdt.internal.ui.JavaPlugin; 51 import org.eclipse.jdt.internal.ui.dialogs.StatusInfo; 52 import org.eclipse.jdt.internal.ui.dialogs.TextFieldNavigationHandler; 53 import org.eclipse.jdt.internal.ui.wizards.NewWizardMessages; 54 import org.eclipse.jdt.internal.ui.wizards.dialogfields.DialogField; 55 import org.eclipse.jdt.internal.ui.wizards.dialogfields.IDialogFieldListener; 56 import org.eclipse.jdt.internal.ui.wizards.dialogfields.LayoutUtil; 57 import org.eclipse.jdt.internal.ui.wizards.dialogfields.StringDialogField; 58 59 69 public class NewPackageWizardPage extends NewContainerWizardPage { 70 71 private static final String PAGE_NAME= "NewPackageWizardPage"; 73 private static final String PACKAGE= "NewPackageWizardPage.package"; 75 private StringDialogField fPackageDialogField; 76 77 80 private IStatus fPackageStatus; 81 82 private IPackageFragment fCreatedPackageFragment; 83 84 87 public NewPackageWizardPage() { 88 super(PAGE_NAME); 89 90 setTitle(NewWizardMessages.NewPackageWizardPage_title); 91 setDescription(NewWizardMessages.NewPackageWizardPage_description); 92 93 fCreatedPackageFragment= null; 94 95 PackageFieldAdapter adapter= new PackageFieldAdapter(); 96 97 fPackageDialogField= new StringDialogField(); 98 fPackageDialogField.setDialogFieldListener(adapter); 99 fPackageDialogField.setLabelText(NewWizardMessages.NewPackageWizardPage_package_label); 100 101 fPackageStatus= new StatusInfo(); 102 } 103 104 106 113 public void init(IStructuredSelection selection) { 114 IJavaElement jelem= getInitialJavaElement(selection); 115 116 initContainerPage(jelem); 117 String pName= ""; if (jelem != null) { 119 IPackageFragment pf= (IPackageFragment) jelem.getAncestor(IJavaElement.PACKAGE_FRAGMENT); 120 if (pf != null && !pf.isDefaultPackage()) 121 pName= pf.getElementName(); 122 } 123 setPackageText(pName, true); 124 updateStatus(new IStatus[] { fContainerStatus, fPackageStatus }); 125 } 126 127 129 132 public void createControl(Composite parent) { 133 initializeDialogUnits(parent); 134 135 Composite composite= new Composite(parent, SWT.NONE); 136 composite.setFont(parent.getFont()); 137 int nColumns= 3; 138 139 GridLayout layout= new GridLayout(); 140 layout.numColumns= 3; 141 composite.setLayout(layout); 142 143 Label label= new Label(composite, SWT.WRAP); 144 label.setText(NewWizardMessages.NewPackageWizardPage_info); 145 GridData gd= new GridData(); 146 gd.widthHint= convertWidthInCharsToPixels(60); 147 gd.horizontalSpan= 3; 148 label.setLayoutData(gd); 149 150 createContainerControls(composite, nColumns); 151 createPackageControls(composite, nColumns); 152 153 setControl(composite); 154 Dialog.applyDialogFont(composite); 155 PlatformUI.getWorkbench().getHelpSystem().setHelp(composite, IJavaHelpContextIds.NEW_PACKAGE_WIZARD_PAGE); 156 } 157 158 161 public void setVisible(boolean visible) { 162 super.setVisible(visible); 163 if (visible) { 164 setFocus(); 165 } 166 } 167 168 171 protected void setFocus() { 172 fPackageDialogField.setFocus(); 173 } 174 175 176 private void createPackageControls(Composite composite, int nColumns) { 177 fPackageDialogField.doFillIntoGrid(composite, nColumns - 1); 178 Text text= fPackageDialogField.getTextControl(null); 179 LayoutUtil.setWidthHint(text, getMaxFieldWidth()); 180 LayoutUtil.setHorizontalGrabbing(text); 181 DialogField.createEmptySpace(composite); 182 183 TextFieldNavigationHandler.install(text); 184 } 185 186 188 private class PackageFieldAdapter implements IDialogFieldListener { 189 190 192 public void dialogFieldChanged(DialogField field) { 193 fPackageStatus= packageChanged(); 194 handleFieldChanged(PACKAGE); 196 } 197 } 198 199 201 204 protected void handleFieldChanged(String fieldName) { 205 super.handleFieldChanged(fieldName); 206 if (fieldName == CONTAINER) { 207 fPackageStatus= packageChanged(); 208 } 209 updateStatus(new IStatus[] { fContainerStatus, fPackageStatus }); 211 } 212 213 215 private IStatus validatePackageName(String text) { 216 IJavaProject project= getJavaProject(); 217 if (project == null || !project.exists()) { 218 return JavaConventions.validatePackageName(text, JavaCore.VERSION_1_3, JavaCore.VERSION_1_3); 219 } 220 String sourceLevel= project.getOption(JavaCore.COMPILER_SOURCE, true); 221 String compliance= project.getOption(JavaCore.COMPILER_COMPLIANCE, true); 222 return JavaConventions.validatePackageName(text, sourceLevel, compliance); 223 } 224 225 228 private IStatus packageChanged() { 229 StatusInfo status= new StatusInfo(); 230 String packName= getPackageText(); 231 if (packName.length() > 0) { 232 IStatus val= validatePackageName(packName); 233 if (val.getSeverity() == IStatus.ERROR) { 234 status.setError(Messages.format(NewWizardMessages.NewPackageWizardPage_error_InvalidPackageName, val.getMessage())); 235 return status; 236 } else if (val.getSeverity() == IStatus.WARNING) { 237 status.setWarning(Messages.format(NewWizardMessages.NewPackageWizardPage_warning_DiscouragedPackageName, val.getMessage())); 238 } 239 } else { 240 status.setError(NewWizardMessages.NewPackageWizardPage_error_EnterName); 241 return status; 242 } 243 244 IPackageFragmentRoot root= getPackageFragmentRoot(); 245 if (root != null && root.getJavaProject().exists()) { 246 IPackageFragment pack= root.getPackageFragment(packName); 247 try { 248 IPath rootPath= root.getPath(); 249 IPath outputPath= root.getJavaProject().getOutputLocation(); 250 if (rootPath.isPrefixOf(outputPath) && !rootPath.equals(outputPath)) { 251 IPath packagePath= pack.getPath(); 254 if (outputPath.isPrefixOf(packagePath)) { 255 status.setError(NewWizardMessages.NewPackageWizardPage_error_IsOutputFolder); 256 return status; 257 } 258 } 259 if (pack.exists()) { 260 if (pack.containsJavaResources() || !pack.hasSubpackages()) { 261 status.setError(NewWizardMessages.NewPackageWizardPage_error_PackageExists); 262 } else { 263 status.setError(NewWizardMessages.NewPackageWizardPage_error_PackageNotShown); 264 } 265 } else { 266 URI location= pack.getResource().getLocationURI(); 267 if (location != null) { 268 IFileStore store= EFS.getStore(location); 269 if (store.fetchInfo().exists()) { 270 status.setError(NewWizardMessages.NewPackageWizardPage_error_PackageExistsDifferentCase); 271 } 272 } 273 } 274 } catch (CoreException e) { 275 JavaPlugin.log(e); 276 } 277 } 278 return status; 279 } 280 281 286 public String getPackageText() { 287 return fPackageDialogField.getText(); 288 } 289 290 297 public void setPackageText(String str, boolean canBeModified) { 298 fPackageDialogField.setText(str); 299 300 fPackageDialogField.setEnabled(canBeModified); 301 } 302 303 309 public IResource getModifiedResource() { 310 IPackageFragmentRoot root= getPackageFragmentRoot(); 311 if (root != null) { 312 return root.getPackageFragment(getPackageText()).getResource(); 313 } 314 return null; 315 } 316 317 318 320 325 public IRunnableWithProgress getRunnable() { 326 return new IRunnableWithProgress() { 327 public void run(IProgressMonitor monitor) throws InvocationTargetException , InterruptedException { 328 try { 329 createPackage(monitor); 330 } catch (CoreException e) { 331 throw new InvocationTargetException (e); 332 } 333 } 334 }; 335 } 336 337 344 public IPackageFragment getNewPackageFragment() { 345 return fCreatedPackageFragment; 346 } 347 348 357 public void createPackage(IProgressMonitor monitor) throws CoreException, InterruptedException { 358 if (monitor == null) { 359 monitor= new NullProgressMonitor(); 360 } 361 362 IPackageFragmentRoot root= getPackageFragmentRoot(); 363 String packName= getPackageText(); 364 fCreatedPackageFragment= root.createPackageFragment(packName, true, monitor); 365 366 if (monitor.isCanceled()) { 367 throw new InterruptedException (); 368 } 369 } 370 } 371 | Popular Tags |