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:
014: import org.eclipse.core.runtime.Assert;
015: import org.eclipse.core.runtime.CoreException;
016: import org.eclipse.core.runtime.IAdaptable;
017: import org.eclipse.core.runtime.IPath;
018: import org.eclipse.core.runtime.IStatus;
019:
020: import org.eclipse.core.resources.IFile;
021: import org.eclipse.core.resources.IProject;
022: import org.eclipse.core.resources.IResource;
023:
024: import org.eclipse.swt.SWT;
025: import org.eclipse.swt.layout.GridData;
026: import org.eclipse.swt.layout.GridLayout;
027: import org.eclipse.swt.widgets.Composite;
028: import org.eclipse.swt.widgets.Control;
029: import org.eclipse.swt.widgets.Label;
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.IClasspathContainer;
039: import org.eclipse.jdt.core.IClasspathEntry;
040: import org.eclipse.jdt.core.IJavaElement;
041: import org.eclipse.jdt.core.IJavaProject;
042: import org.eclipse.jdt.core.IPackageFragmentRoot;
043: import org.eclipse.jdt.core.JavaCore;
044:
045: import org.eclipse.jdt.internal.corext.util.JavaModelUtil;
046: import org.eclipse.jdt.internal.corext.util.Messages;
047:
048: import org.eclipse.jdt.internal.ui.IJavaHelpContextIds;
049: import org.eclipse.jdt.internal.ui.JavaPlugin;
050: import org.eclipse.jdt.internal.ui.dialogs.StatusUtil;
051: import org.eclipse.jdt.internal.ui.util.ExceptionHandler;
052: import org.eclipse.jdt.internal.ui.wizards.IStatusChangeListener;
053: import org.eclipse.jdt.internal.ui.wizards.buildpaths.SourceAttachmentBlock;
054:
055: /**
056: * Property page to configure a archive's JARs source attachment
057: */
058: public class SourceAttachmentPropertyPage extends PropertyPage
059: implements IStatusChangeListener {
060:
061: private SourceAttachmentBlock fSourceAttachmentBlock;
062: private IPackageFragmentRoot fRoot;
063: private IPath fContainerPath;
064: private IClasspathEntry fEntry;
065:
066: public SourceAttachmentPropertyPage() {
067: }
068:
069: /*
070: * @see org.eclipse.jface.dialogs.IDialogPage#createControl(org.eclipse.swt.widgets.Composite)
071: */
072: public void createControl(Composite parent) {
073: super .createControl(parent);
074: PlatformUI.getWorkbench().getHelpSystem().setHelp(getControl(),
075: IJavaHelpContextIds.SOURCE_ATTACHMENT_PROPERTY_PAGE);
076: }
077:
078: /*
079: * @see PreferencePage#createContents
080: */
081: protected Control createContents(Composite composite) {
082: initializeDialogUnits(composite);
083: Control result = createPageContent(composite);
084: Dialog.applyDialogFont(result);
085: return result;
086: }
087:
088: private Control createPageContent(Composite composite) {
089: try {
090: fContainerPath = null;
091: fEntry = null;
092: fRoot = getJARPackageFragmentRoot();
093: if (fRoot == null
094: || fRoot.getKind() != IPackageFragmentRoot.K_BINARY) {
095: return createMessageContent(
096: composite,
097: PreferencesMessages.SourceAttachmentPropertyPage_noarchive_message);
098: }
099:
100: IPath containerPath = null;
101: IJavaProject jproject = fRoot.getJavaProject();
102: IClasspathEntry entry = fRoot.getRawClasspathEntry();
103: if (entry == null) {
104: // use a dummy entry to use for initialization
105: entry = JavaCore.newLibraryEntry(fRoot.getPath(), null,
106: null);
107: } else {
108: if (entry.getEntryKind() == IClasspathEntry.CPE_CONTAINER) {
109: containerPath = entry.getPath();
110: ClasspathContainerInitializer initializer = JavaCore
111: .getClasspathContainerInitializer(containerPath
112: .segment(0));
113: IClasspathContainer container = JavaCore
114: .getClasspathContainer(containerPath,
115: jproject);
116: if (initializer == null || container == null) {
117: return createMessageContent(
118: composite,
119: Messages
120: .format(
121: PreferencesMessages.SourceAttachmentPropertyPage_invalid_container,
122: containerPath
123: .toString()));
124: }
125: String containerName = container.getDescription();
126:
127: IStatus status = initializer
128: .getSourceAttachmentStatus(containerPath,
129: jproject);
130: if (status.getCode() == ClasspathContainerInitializer.ATTRIBUTE_NOT_SUPPORTED) {
131: return createMessageContent(
132: composite,
133: Messages
134: .format(
135: PreferencesMessages.SourceAttachmentPropertyPage_not_supported,
136: containerName));
137: }
138: if (status.getCode() == ClasspathContainerInitializer.ATTRIBUTE_READ_ONLY) {
139: return createMessageContent(
140: composite,
141: Messages
142: .format(
143: PreferencesMessages.SourceAttachmentPropertyPage_read_only,
144: containerName));
145: }
146: entry = JavaModelUtil.findEntryInContainer(
147: container, fRoot.getPath());
148: Assert.isNotNull(entry);
149: }
150: }
151: fContainerPath = containerPath;
152: fEntry = entry;
153:
154: fSourceAttachmentBlock = new SourceAttachmentBlock(this ,
155: entry);
156: return fSourceAttachmentBlock.createControl(composite);
157: } catch (CoreException e) {
158: JavaPlugin.log(e);
159: return createMessageContent(
160: composite,
161: PreferencesMessages.SourceAttachmentPropertyPage_noarchive_message);
162: }
163: }
164:
165: private Control createMessageContent(Composite composite,
166: String message) {
167: Composite inner = new Composite(composite, SWT.NONE);
168: GridLayout layout = new GridLayout();
169: layout.marginHeight = 0;
170: layout.marginWidth = 0;
171: inner.setLayout(layout);
172:
173: GridData gd = new GridData(GridData.HORIZONTAL_ALIGN_FILL);
174: gd.widthHint = convertWidthInCharsToPixels(80);
175:
176: Label label = new Label(inner, SWT.LEFT + SWT.WRAP);
177: label.setText(message);
178: label.setLayoutData(gd);
179: return inner;
180: }
181:
182: /*
183: * @see IPreferencePage#performOk
184: */
185: public boolean performOk() {
186: if (fSourceAttachmentBlock != null) {
187: try {
188: IClasspathEntry entry = fSourceAttachmentBlock
189: .getNewEntry();
190: if (entry.equals(fEntry)) {
191: return true; // no change
192: }
193:
194: IRunnableWithProgress runnable = SourceAttachmentBlock
195: .getRunnable(getShell(), entry, fRoot
196: .getJavaProject(), fContainerPath);
197: PlatformUI.getWorkbench().getProgressService().run(
198: true, true, runnable);
199: } catch (InvocationTargetException e) {
200: String title = PreferencesMessages.SourceAttachmentPropertyPage_error_title;
201: String message = PreferencesMessages.SourceAttachmentPropertyPage_error_message;
202: ExceptionHandler.handle(e, getShell(), title, message);
203: return false;
204: } catch (InterruptedException e) {
205: // cancelled
206: return false;
207: }
208: }
209: return true;
210: }
211:
212: /*
213: * @see PreferencePage#performDefaults()
214: */
215: protected void performDefaults() {
216: if (fSourceAttachmentBlock != null) {
217: fSourceAttachmentBlock.setDefaults();
218: }
219: super .performDefaults();
220: }
221:
222: private IPackageFragmentRoot getJARPackageFragmentRoot()
223: throws CoreException {
224: // try to find it as Java element (needed for external jars)
225: IAdaptable adaptable = getElement();
226: IJavaElement elem = (IJavaElement) adaptable
227: .getAdapter(IJavaElement.class);
228: if (elem instanceof IPackageFragmentRoot) {
229: return (IPackageFragmentRoot) elem;
230: }
231: // not on classpath or not in a java project
232: IResource resource = (IResource) adaptable
233: .getAdapter(IResource.class);
234: if (resource instanceof IFile) {
235: IProject proj = resource.getProject();
236: if (proj.hasNature(JavaCore.NATURE_ID)) {
237: IJavaProject jproject = JavaCore.create(proj);
238: return jproject.getPackageFragmentRoot(resource);
239: }
240: }
241: return null;
242: }
243:
244: /*
245: * @see IStatusChangeListener#statusChanged
246: */
247: public void statusChanged(IStatus status) {
248: setValid(!status.matches(IStatus.ERROR));
249: StatusUtil.applyToStatusLine(this, status);
250: }
251:
252: }
|