1 11 package org.eclipse.jdt.internal.ui.preferences; 12 13 import java.lang.reflect.InvocationTargetException ; 14 import java.net.URL ; 15 16 import org.eclipse.core.runtime.Assert; 17 import org.eclipse.core.runtime.CoreException; 18 import org.eclipse.core.runtime.IAdaptable; 19 import org.eclipse.core.runtime.IPath; 20 import org.eclipse.core.runtime.IProgressMonitor; 21 import org.eclipse.core.runtime.IStatus; 22 23 import org.eclipse.core.resources.IFile; 24 import org.eclipse.core.resources.IProject; 25 import org.eclipse.core.resources.IResource; 26 27 import org.eclipse.swt.SWT; 28 import org.eclipse.swt.widgets.Composite; 29 import org.eclipse.swt.widgets.Control; 30 import org.eclipse.swt.widgets.Shell; 31 32 import org.eclipse.jface.dialogs.Dialog; 33 import org.eclipse.jface.operation.IRunnableWithProgress; 34 35 import org.eclipse.ui.PlatformUI; 36 import org.eclipse.ui.dialogs.PropertyPage; 37 38 import org.eclipse.jdt.core.ClasspathContainerInitializer; 39 import org.eclipse.jdt.core.IClasspathAttribute; 40 import org.eclipse.jdt.core.IClasspathContainer; 41 import org.eclipse.jdt.core.IClasspathEntry; 42 import org.eclipse.jdt.core.IJavaElement; 43 import org.eclipse.jdt.core.IJavaProject; 44 import org.eclipse.jdt.core.IPackageFragmentRoot; 45 import org.eclipse.jdt.core.JavaCore; 46 import org.eclipse.jdt.core.JavaModelException; 47 48 import org.eclipse.jdt.internal.corext.util.JavaModelUtil; 49 import org.eclipse.jdt.internal.corext.util.Messages; 50 51 import org.eclipse.jdt.ui.JavaUI; 52 53 import org.eclipse.jdt.internal.ui.IJavaHelpContextIds; 54 import org.eclipse.jdt.internal.ui.JavaPlugin; 55 import org.eclipse.jdt.internal.ui.dialogs.StatusUtil; 56 import org.eclipse.jdt.internal.ui.util.ExceptionHandler; 57 import org.eclipse.jdt.internal.ui.wizards.IStatusChangeListener; 58 import org.eclipse.jdt.internal.ui.wizards.buildpaths.ArchiveFileFilter; 59 import org.eclipse.jdt.internal.ui.wizards.buildpaths.BuildPathSupport; 60 import org.eclipse.jdt.internal.ui.wizards.buildpaths.CPListElement; 61 62 65 public class JavadocConfigurationPropertyPage extends PropertyPage implements IStatusChangeListener { 66 67 public static final String PROP_ID= "org.eclipse.jdt.ui.propertyPages.JavadocConfigurationPropertyPage"; 69 private JavadocConfigurationBlock fJavadocConfigurationBlock; 70 private boolean fIsValidElement; 71 72 private IPath fContainerPath; 73 private IClasspathEntry fEntry; 74 private URL fInitalLocation; 75 76 public JavadocConfigurationPropertyPage() { 77 } 78 79 82 public void createControl(Composite parent) { 83 IJavaElement elem= getJavaElement(); 84 try { 85 if (elem instanceof IPackageFragmentRoot && ((IPackageFragmentRoot) elem).getKind() == IPackageFragmentRoot.K_BINARY) { 86 IPackageFragmentRoot root= (IPackageFragmentRoot) elem; 87 88 IClasspathEntry entry= root.getRawClasspathEntry(); 89 if (entry == null) { 90 fIsValidElement= false; 91 setDescription(PreferencesMessages.JavadocConfigurationPropertyPage_IsIncorrectElement_description); 92 } else { 93 if (entry.getEntryKind() == IClasspathEntry.CPE_CONTAINER) { 94 fContainerPath= entry.getPath(); 95 fEntry= handleContainerEntry(fContainerPath, elem.getJavaProject(), root.getPath()); 96 fIsValidElement= fEntry != null; 97 } else { 98 fContainerPath= null; 99 fEntry= entry; 100 fIsValidElement= true; 101 setDescription(PreferencesMessages.JavadocConfigurationPropertyPage_IsPackageFragmentRoot_description); 102 } 103 } 104 105 } else if (elem instanceof IJavaProject) { 106 fIsValidElement= true; 107 setDescription(PreferencesMessages.JavadocConfigurationPropertyPage_IsJavaProject_description); 108 } else { 109 fIsValidElement= false; 110 setDescription(PreferencesMessages.JavadocConfigurationPropertyPage_IsIncorrectElement_description); 111 } 112 } catch (JavaModelException e) { 113 fIsValidElement= false; 114 setDescription(PreferencesMessages.JavadocConfigurationPropertyPage_IsIncorrectElement_description); 115 } 116 super.createControl(parent); 117 PlatformUI.getWorkbench().getHelpSystem().setHelp(getControl(), IJavaHelpContextIds.JAVADOC_CONFIGURATION_PROPERTY_PAGE); 118 } 119 120 private IClasspathEntry handleContainerEntry(IPath containerPath, IJavaProject jproject, IPath jarPath) throws JavaModelException { 121 ClasspathContainerInitializer initializer= JavaCore.getClasspathContainerInitializer(containerPath.segment(0)); 122 IClasspathContainer container= JavaCore.getClasspathContainer(containerPath, jproject); 123 if (initializer == null || container == null) { 124 setDescription(Messages.format(PreferencesMessages.JavadocConfigurationPropertyPage_invalid_container, containerPath.toString())); 125 return null; 126 } 127 String containerName= container.getDescription(); 128 IStatus status= initializer.getAttributeStatus(containerPath, jproject, IClasspathAttribute.JAVADOC_LOCATION_ATTRIBUTE_NAME); 129 if (status.getCode() == ClasspathContainerInitializer.ATTRIBUTE_NOT_SUPPORTED) { 130 setDescription(Messages.format(PreferencesMessages.JavadocConfigurationPropertyPage_not_supported, containerName)); 131 return null; 132 } 133 if (status.getCode() == ClasspathContainerInitializer.ATTRIBUTE_READ_ONLY) { 134 setDescription(Messages.format(PreferencesMessages.JavadocConfigurationPropertyPage_read_only, containerName)); 135 return null; 136 } 137 IClasspathEntry entry= JavaModelUtil.findEntryInContainer(container, jarPath); 138 Assert.isNotNull(entry); 139 setDescription(PreferencesMessages.JavadocConfigurationPropertyPage_IsPackageFragmentRoot_description); 140 return entry; 141 } 142 143 146 protected Control createContents(Composite parent) { 147 if (!fIsValidElement) { 148 return new Composite(parent, SWT.NONE); 149 } 150 151 IJavaElement elem= getJavaElement(); 152 fInitalLocation= null; 153 if (elem != null) { 154 try { 155 fInitalLocation= JavaUI.getJavadocBaseLocation(elem); 156 } catch (JavaModelException e) { 157 JavaPlugin.log(e); 158 } 159 } 160 161 boolean isProject= (elem instanceof IJavaProject); 162 fJavadocConfigurationBlock= new JavadocConfigurationBlock(getShell(), this, fInitalLocation, isProject); 163 Control control= fJavadocConfigurationBlock.createContents(parent); 164 control.setVisible(elem != null); 165 166 Dialog.applyDialogFont(control); 167 return control; 168 } 169 170 private IJavaElement getJavaElement() { 171 IAdaptable adaptable= getElement(); 172 IJavaElement elem= (IJavaElement) adaptable.getAdapter(IJavaElement.class); 173 if (elem == null) { 174 175 IResource resource= (IResource) adaptable.getAdapter(IResource.class); 176 try { 178 if (resource instanceof IFile && ArchiveFileFilter.isArchivePath(resource.getFullPath())) { 179 IProject proj= resource.getProject(); 180 if (proj.hasNature(JavaCore.NATURE_ID)) { 181 IJavaProject jproject= JavaCore.create(proj); 182 elem= jproject.getPackageFragmentRoot(resource); } 184 } 185 } catch (CoreException e) { 186 JavaPlugin.log(e); 187 } 188 } 189 return elem; 190 } 191 192 195 protected void performDefaults() { 196 if (fJavadocConfigurationBlock != null) { 197 fJavadocConfigurationBlock.performDefaults(); 198 } 199 super.performDefaults(); 200 } 201 202 205 public boolean performOk() { 206 if (fJavadocConfigurationBlock != null) { 207 URL javadocLocation= fJavadocConfigurationBlock.getJavadocLocation(); 208 if (javadocLocation == null && fInitalLocation == null || javadocLocation != null && javadocLocation.equals(fInitalLocation)) { 209 return true; } 211 212 213 IJavaElement elem= getJavaElement(); 214 try { 215 IRunnableWithProgress runnable= getRunnable(getShell(), elem, javadocLocation, fEntry, fContainerPath); 216 PlatformUI.getWorkbench().getProgressService().run(true, true, runnable); 217 } catch (InvocationTargetException e) { 218 String title= PreferencesMessages.SourceAttachmentPropertyPage_error_title; 219 String message= PreferencesMessages.SourceAttachmentPropertyPage_error_message; 220 ExceptionHandler.handle(e, getShell(), title, message); 221 return false; 222 } catch (InterruptedException e) { 223 return false; 225 } 226 } 227 return true; 228 } 229 230 231 private static IRunnableWithProgress getRunnable(final Shell shell, final IJavaElement elem, final URL javadocLocation, final IClasspathEntry entry, final IPath containerPath) { 232 return new IRunnableWithProgress() { 233 public void run(IProgressMonitor monitor) throws InvocationTargetException { 234 try { 235 IJavaProject project= elem.getJavaProject(); 236 if (elem instanceof IPackageFragmentRoot) { 237 CPListElement cpElem= CPListElement.createFromExisting(entry, project); 238 String loc= javadocLocation != null ? javadocLocation.toExternalForm() : null; 239 cpElem.setAttribute(CPListElement.JAVADOC, loc); 240 IClasspathEntry newEntry= cpElem.getClasspathEntry(); 241 String [] changedAttributes= { CPListElement.JAVADOC }; 242 BuildPathSupport.modifyClasspathEntry(shell, newEntry, changedAttributes, project, containerPath, monitor); 243 } else { 244 JavaUI.setProjectJavadocLocation(project, javadocLocation); 245 } 246 } catch (CoreException e) { 247 throw new InvocationTargetException (e); 248 } 249 } 250 }; 251 } 252 253 254 257 public void statusChanged(IStatus status) { 258 setValid(!status.matches(IStatus.ERROR)); 259 StatusUtil.applyToStatusLine(this, status); 260 } 261 262 } 263 | Popular Tags |