001: /*******************************************************************************
002: * Copyright (c) 2000, 2007 IBM Corporation and others.
003: * All rights reserved. This program and the accompanying materials
004: * are made available under the terms of the Eclipse Public License v1.0
005: * which accompanies this distribution, and is available at
006: * http://www.eclipse.org/legal/epl-v10.html
007: *
008: * Contributors:
009: * IBM Corporation - initial API and implementation
010: *******************************************************************************/package org.eclipse.jdt.internal.ui.preferences;
011:
012: import java.lang.reflect.InvocationTargetException;
013: import java.net.URL;
014:
015: import org.eclipse.core.runtime.Assert;
016: import org.eclipse.core.runtime.CoreException;
017: import org.eclipse.core.runtime.IAdaptable;
018: import org.eclipse.core.runtime.IPath;
019: import org.eclipse.core.runtime.IProgressMonitor;
020: import org.eclipse.core.runtime.IStatus;
021:
022: import org.eclipse.core.resources.IFile;
023: import org.eclipse.core.resources.IProject;
024: import org.eclipse.core.resources.IResource;
025:
026: import org.eclipse.swt.SWT;
027: import org.eclipse.swt.widgets.Composite;
028: import org.eclipse.swt.widgets.Control;
029: import org.eclipse.swt.widgets.Shell;
030:
031: import org.eclipse.jface.dialogs.Dialog;
032: import org.eclipse.jface.operation.IRunnableWithProgress;
033:
034: import org.eclipse.ui.PlatformUI;
035: import org.eclipse.ui.dialogs.PropertyPage;
036:
037: import org.eclipse.jdt.core.ClasspathContainerInitializer;
038: import org.eclipse.jdt.core.IClasspathAttribute;
039: import org.eclipse.jdt.core.IClasspathContainer;
040: import org.eclipse.jdt.core.IClasspathEntry;
041: import org.eclipse.jdt.core.IJavaElement;
042: import org.eclipse.jdt.core.IJavaProject;
043: import org.eclipse.jdt.core.IPackageFragmentRoot;
044: import org.eclipse.jdt.core.JavaCore;
045: import org.eclipse.jdt.core.JavaModelException;
046:
047: import org.eclipse.jdt.internal.corext.util.JavaModelUtil;
048: import org.eclipse.jdt.internal.corext.util.Messages;
049:
050: import org.eclipse.jdt.ui.JavaUI;
051:
052: import org.eclipse.jdt.internal.ui.IJavaHelpContextIds;
053: import org.eclipse.jdt.internal.ui.JavaPlugin;
054: import org.eclipse.jdt.internal.ui.dialogs.StatusUtil;
055: import org.eclipse.jdt.internal.ui.util.ExceptionHandler;
056: import org.eclipse.jdt.internal.ui.wizards.IStatusChangeListener;
057: import org.eclipse.jdt.internal.ui.wizards.buildpaths.ArchiveFileFilter;
058: import org.eclipse.jdt.internal.ui.wizards.buildpaths.BuildPathSupport;
059: import org.eclipse.jdt.internal.ui.wizards.buildpaths.CPListElement;
060:
061: /**
062: * Property page used to set the project's Javadoc location for sources
063: */
064: public class JavadocConfigurationPropertyPage extends PropertyPage
065: implements IStatusChangeListener {
066:
067: public static final String PROP_ID = "org.eclipse.jdt.ui.propertyPages.JavadocConfigurationPropertyPage"; //$NON-NLS-1$
068:
069: private JavadocConfigurationBlock fJavadocConfigurationBlock;
070: private boolean fIsValidElement;
071:
072: private IPath fContainerPath;
073: private IClasspathEntry fEntry;
074: private URL fInitalLocation;
075:
076: public JavadocConfigurationPropertyPage() {
077: }
078:
079: /**
080: * @see org.eclipse.jface.dialogs.IDialogPage#createControl(org.eclipse.swt.widgets.Composite)
081: */
082: public void createControl(Composite parent) {
083: IJavaElement elem = getJavaElement();
084: try {
085: if (elem instanceof IPackageFragmentRoot
086: && ((IPackageFragmentRoot) elem).getKind() == IPackageFragmentRoot.K_BINARY) {
087: IPackageFragmentRoot root = (IPackageFragmentRoot) elem;
088:
089: IClasspathEntry entry = root.getRawClasspathEntry();
090: if (entry == null) {
091: fIsValidElement = false;
092: setDescription(PreferencesMessages.JavadocConfigurationPropertyPage_IsIncorrectElement_description);
093: } else {
094: if (entry.getEntryKind() == IClasspathEntry.CPE_CONTAINER) {
095: fContainerPath = entry.getPath();
096: fEntry = handleContainerEntry(fContainerPath,
097: elem.getJavaProject(), root.getPath());
098: fIsValidElement = fEntry != null;
099: } else {
100: fContainerPath = null;
101: fEntry = entry;
102: fIsValidElement = true;
103: setDescription(PreferencesMessages.JavadocConfigurationPropertyPage_IsPackageFragmentRoot_description);
104: }
105: }
106:
107: } else if (elem instanceof IJavaProject) {
108: fIsValidElement = true;
109: setDescription(PreferencesMessages.JavadocConfigurationPropertyPage_IsJavaProject_description);
110: } else {
111: fIsValidElement = false;
112: setDescription(PreferencesMessages.JavadocConfigurationPropertyPage_IsIncorrectElement_description);
113: }
114: } catch (JavaModelException e) {
115: fIsValidElement = false;
116: setDescription(PreferencesMessages.JavadocConfigurationPropertyPage_IsIncorrectElement_description);
117: }
118: super .createControl(parent);
119: PlatformUI
120: .getWorkbench()
121: .getHelpSystem()
122: .setHelp(
123: getControl(),
124: IJavaHelpContextIds.JAVADOC_CONFIGURATION_PROPERTY_PAGE);
125: }
126:
127: private IClasspathEntry handleContainerEntry(IPath containerPath,
128: IJavaProject jproject, IPath jarPath)
129: throws JavaModelException {
130: ClasspathContainerInitializer initializer = JavaCore
131: .getClasspathContainerInitializer(containerPath
132: .segment(0));
133: IClasspathContainer container = JavaCore.getClasspathContainer(
134: containerPath, jproject);
135: if (initializer == null || container == null) {
136: setDescription(Messages
137: .format(
138: PreferencesMessages.JavadocConfigurationPropertyPage_invalid_container,
139: containerPath.toString()));
140: return null;
141: }
142: String containerName = container.getDescription();
143: IStatus status = initializer.getAttributeStatus(containerPath,
144: jproject,
145: IClasspathAttribute.JAVADOC_LOCATION_ATTRIBUTE_NAME);
146: if (status.getCode() == ClasspathContainerInitializer.ATTRIBUTE_NOT_SUPPORTED) {
147: setDescription(Messages
148: .format(
149: PreferencesMessages.JavadocConfigurationPropertyPage_not_supported,
150: containerName));
151: return null;
152: }
153: if (status.getCode() == ClasspathContainerInitializer.ATTRIBUTE_READ_ONLY) {
154: setDescription(Messages
155: .format(
156: PreferencesMessages.JavadocConfigurationPropertyPage_read_only,
157: containerName));
158: return null;
159: }
160: IClasspathEntry entry = JavaModelUtil.findEntryInContainer(
161: container, jarPath);
162: Assert.isNotNull(entry);
163: setDescription(PreferencesMessages.JavadocConfigurationPropertyPage_IsPackageFragmentRoot_description);
164: return entry;
165: }
166:
167: /*
168: * @see PreferencePage#createContents(Composite)
169: */
170: protected Control createContents(Composite parent) {
171: if (!fIsValidElement) {
172: return new Composite(parent, SWT.NONE);
173: }
174:
175: IJavaElement elem = getJavaElement();
176: fInitalLocation = null;
177: if (elem != null) {
178: try {
179: fInitalLocation = JavaUI.getJavadocBaseLocation(elem);
180: } catch (JavaModelException e) {
181: JavaPlugin.log(e);
182: }
183: }
184:
185: boolean isProject = (elem instanceof IJavaProject);
186: fJavadocConfigurationBlock = new JavadocConfigurationBlock(
187: getShell(), this , fInitalLocation, isProject);
188: Control control = fJavadocConfigurationBlock
189: .createContents(parent);
190: control.setVisible(elem != null);
191:
192: Dialog.applyDialogFont(control);
193: return control;
194: }
195:
196: private IJavaElement getJavaElement() {
197: IAdaptable adaptable = getElement();
198: IJavaElement elem = (IJavaElement) adaptable
199: .getAdapter(IJavaElement.class);
200: if (elem == null) {
201:
202: IResource resource = (IResource) adaptable
203: .getAdapter(IResource.class);
204: //special case when the .jar is a file
205: try {
206: if (resource instanceof IFile
207: && ArchiveFileFilter.isArchivePath(resource
208: .getFullPath())) {
209: IProject proj = resource.getProject();
210: if (proj.hasNature(JavaCore.NATURE_ID)) {
211: IJavaProject jproject = JavaCore.create(proj);
212: elem = jproject
213: .getPackageFragmentRoot(resource); // create a handle
214: }
215: }
216: } catch (CoreException e) {
217: JavaPlugin.log(e);
218: }
219: }
220: return elem;
221: }
222:
223: /*
224: * @see PreferencePage#performDefaults()
225: */
226: protected void performDefaults() {
227: if (fJavadocConfigurationBlock != null) {
228: fJavadocConfigurationBlock.performDefaults();
229: }
230: super .performDefaults();
231: }
232:
233: /**
234: * @see org.eclipse.jface.preference.IPreferencePage#performOk()
235: */
236: public boolean performOk() {
237: if (fJavadocConfigurationBlock != null) {
238: URL javadocLocation = fJavadocConfigurationBlock
239: .getJavadocLocation();
240: if (javadocLocation == null && fInitalLocation == null
241: || javadocLocation != null
242: && javadocLocation.equals(fInitalLocation)) {
243: return true; // no change
244: }
245:
246: IJavaElement elem = getJavaElement();
247: try {
248: IRunnableWithProgress runnable = getRunnable(
249: getShell(), elem, javadocLocation, fEntry,
250: fContainerPath);
251: PlatformUI.getWorkbench().getProgressService().run(
252: true, true, runnable);
253: } catch (InvocationTargetException e) {
254: String title = PreferencesMessages.SourceAttachmentPropertyPage_error_title;
255: String message = PreferencesMessages.SourceAttachmentPropertyPage_error_message;
256: ExceptionHandler.handle(e, getShell(), title, message);
257: return false;
258: } catch (InterruptedException e) {
259: // cancelled
260: return false;
261: }
262: }
263: return true;
264: }
265:
266: private static IRunnableWithProgress getRunnable(final Shell shell,
267: final IJavaElement elem, final URL javadocLocation,
268: final IClasspathEntry entry, final IPath containerPath) {
269: return new IRunnableWithProgress() {
270: public void run(IProgressMonitor monitor)
271: throws InvocationTargetException {
272: try {
273: IJavaProject project = elem.getJavaProject();
274: if (elem instanceof IPackageFragmentRoot) {
275: CPListElement cpElem = CPListElement
276: .createFromExisting(entry, project);
277: String loc = javadocLocation != null ? javadocLocation
278: .toExternalForm()
279: : null;
280: cpElem.setAttribute(CPListElement.JAVADOC, loc);
281: IClasspathEntry newEntry = cpElem
282: .getClasspathEntry();
283: String[] changedAttributes = { CPListElement.JAVADOC };
284: BuildPathSupport.modifyClasspathEntry(shell,
285: newEntry, changedAttributes, project,
286: containerPath, monitor);
287: } else {
288: JavaUI.setProjectJavadocLocation(project,
289: javadocLocation);
290: }
291: } catch (CoreException e) {
292: throw new InvocationTargetException(e);
293: }
294: }
295: };
296: }
297:
298: /**
299: * @see IStatusChangeListener#statusChanged(IStatus)
300: */
301: public void statusChanged(IStatus status) {
302: setValid(!status.matches(IStatus.ERROR));
303: StatusUtil.applyToStatusLine(this, status);
304: }
305:
306: }
|