KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > internal > ui > preferences > SourceAttachmentPropertyPage


1 /*******************************************************************************
2  * Copyright (c) 2000, 2007 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.jdt.internal.ui.preferences;
12
13 import java.lang.reflect.InvocationTargetException JavaDoc;
14
15 import org.eclipse.core.runtime.Assert;
16 import org.eclipse.core.runtime.CoreException;
17 import org.eclipse.core.runtime.IAdaptable;
18 import org.eclipse.core.runtime.IPath;
19 import org.eclipse.core.runtime.IStatus;
20
21 import org.eclipse.core.resources.IFile;
22 import org.eclipse.core.resources.IProject;
23 import org.eclipse.core.resources.IResource;
24
25 import org.eclipse.swt.SWT;
26 import org.eclipse.swt.layout.GridData;
27 import org.eclipse.swt.layout.GridLayout;
28 import org.eclipse.swt.widgets.Composite;
29 import org.eclipse.swt.widgets.Control;
30 import org.eclipse.swt.widgets.Label;
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.IClasspathContainer;
40 import org.eclipse.jdt.core.IClasspathEntry;
41 import org.eclipse.jdt.core.IJavaElement;
42 import org.eclipse.jdt.core.IJavaProject;
43 import org.eclipse.jdt.core.IPackageFragmentRoot;
44 import org.eclipse.jdt.core.JavaCore;
45
46 import org.eclipse.jdt.internal.corext.util.JavaModelUtil;
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.StatusUtil;
52 import org.eclipse.jdt.internal.ui.util.ExceptionHandler;
53 import org.eclipse.jdt.internal.ui.wizards.IStatusChangeListener;
54 import org.eclipse.jdt.internal.ui.wizards.buildpaths.SourceAttachmentBlock;
55
56 /**
57  * Property page to configure a archive's JARs source attachment
58  */

59 public class SourceAttachmentPropertyPage extends PropertyPage implements IStatusChangeListener {
60
61     private SourceAttachmentBlock fSourceAttachmentBlock;
62     private IPackageFragmentRoot fRoot;
63     private IPath fContainerPath;
64     private IClasspathEntry fEntry;
65
66     public SourceAttachmentPropertyPage() {
67     }
68
69     /*
70      * @see org.eclipse.jface.dialogs.IDialogPage#createControl(org.eclipse.swt.widgets.Composite)
71      */

72     public void createControl(Composite parent) {
73         super.createControl(parent);
74         PlatformUI.getWorkbench().getHelpSystem().setHelp(getControl(), IJavaHelpContextIds.SOURCE_ATTACHMENT_PROPERTY_PAGE);
75     }
76     
77     /*
78      * @see PreferencePage#createContents
79      */

80     protected Control createContents(Composite composite) {
81         initializeDialogUnits(composite);
82         Control result= createPageContent(composite);
83         Dialog.applyDialogFont(result);
84         return result;
85     }
86     
87     private Control createPageContent(Composite composite) {
88         try {
89             fContainerPath= null;
90             fEntry= null;
91             fRoot= getJARPackageFragmentRoot();
92             if (fRoot == null || fRoot.getKind() != IPackageFragmentRoot.K_BINARY) {
93                 return createMessageContent(composite, PreferencesMessages.SourceAttachmentPropertyPage_noarchive_message);
94             }
95     
96             IPath containerPath= null;
97             IJavaProject jproject= fRoot.getJavaProject();
98             IClasspathEntry entry= fRoot.getRawClasspathEntry();
99             if (entry == null) {
100                 // use a dummy entry to use for initialization
101
entry= JavaCore.newLibraryEntry(fRoot.getPath(), null, null);
102             } else {
103                 if (entry.getEntryKind() == IClasspathEntry.CPE_CONTAINER) {
104                     containerPath= entry.getPath();
105                     ClasspathContainerInitializer initializer= JavaCore.getClasspathContainerInitializer(containerPath.segment(0));
106                     IClasspathContainer container= JavaCore.getClasspathContainer(containerPath, jproject);
107                     if (initializer == null || container == null) {
108                         return createMessageContent(composite, Messages.format(PreferencesMessages.SourceAttachmentPropertyPage_invalid_container, containerPath.toString()));
109                     }
110                     String JavaDoc containerName= container.getDescription();
111
112                     IStatus status= initializer.getSourceAttachmentStatus(containerPath, jproject);
113                     if (status.getCode() == ClasspathContainerInitializer.ATTRIBUTE_NOT_SUPPORTED) {
114                         return createMessageContent(composite, Messages.format(PreferencesMessages.SourceAttachmentPropertyPage_not_supported, containerName));
115                     }
116                     if (status.getCode() == ClasspathContainerInitializer.ATTRIBUTE_READ_ONLY) {
117                         return createMessageContent(composite, Messages.format(PreferencesMessages.SourceAttachmentPropertyPage_read_only, containerName));
118                     }
119                     entry= JavaModelUtil.findEntryInContainer(container, fRoot.getPath());
120                     Assert.isNotNull(entry);
121                 }
122             }
123             fContainerPath= containerPath;
124             fEntry= entry;
125             
126             fSourceAttachmentBlock= new SourceAttachmentBlock(this, entry);
127             return fSourceAttachmentBlock.createControl(composite);
128         } catch (CoreException e) {
129             JavaPlugin.log(e);
130             return createMessageContent(composite, PreferencesMessages.SourceAttachmentPropertyPage_noarchive_message);
131         }
132     }
133     
134     
135     private Control createMessageContent(Composite composite, String JavaDoc message) {
136         Composite inner= new Composite(composite, SWT.NONE);
137         GridLayout layout= new GridLayout();
138         layout.marginHeight= 0;
139         layout.marginWidth= 0;
140         inner.setLayout(layout);
141         
142         GridData gd= new GridData(GridData.HORIZONTAL_ALIGN_FILL);
143         gd.widthHint= convertWidthInCharsToPixels(80);
144         
145         Label label= new Label(inner, SWT.LEFT + SWT.WRAP);
146         label.setText(message);
147         label.setLayoutData(gd);
148         return inner;
149     }
150     
151
152     /*
153      * @see IPreferencePage#performOk
154      */

155     public boolean performOk() {
156         if (fSourceAttachmentBlock != null) {
157             try {
158                 IClasspathEntry entry= fSourceAttachmentBlock.getNewEntry();
159                 if (entry.equals(fEntry)) {
160                     return true; // no change
161
}
162                 
163                 IRunnableWithProgress runnable= SourceAttachmentBlock.getRunnable(getShell(), entry, fRoot.getJavaProject(), fContainerPath);
164                 PlatformUI.getWorkbench().getProgressService().run(true, true, runnable);
165             } catch (InvocationTargetException JavaDoc e) {
166                 String JavaDoc title= PreferencesMessages.SourceAttachmentPropertyPage_error_title;
167                 String JavaDoc message= PreferencesMessages.SourceAttachmentPropertyPage_error_message;
168                 ExceptionHandler.handle(e, getShell(), title, message);
169                 return false;
170             } catch (InterruptedException JavaDoc e) {
171                 // cancelled
172
return false;
173             }
174         }
175         return true;
176     }
177     
178     /*
179      * @see PreferencePage#performDefaults()
180      */

181     protected void performDefaults() {
182         if (fSourceAttachmentBlock != null) {
183             fSourceAttachmentBlock.setDefaults();
184         }
185         super.performDefaults();
186     }
187                 
188     private IPackageFragmentRoot getJARPackageFragmentRoot() throws CoreException {
189         // try to find it as Java element (needed for external jars)
190
IAdaptable adaptable= getElement();
191         IJavaElement elem= (IJavaElement) adaptable.getAdapter(IJavaElement.class);
192         if (elem instanceof IPackageFragmentRoot) {
193             return (IPackageFragmentRoot) elem;
194         }
195         // not on classpath or not in a java project
196
IResource resource= (IResource) adaptable.getAdapter(IResource.class);
197         if (resource instanceof IFile) {
198             IProject proj= resource.getProject();
199             if (proj.hasNature(JavaCore.NATURE_ID)) {
200                 IJavaProject jproject= JavaCore.create(proj);
201                 return jproject.getPackageFragmentRoot(resource);
202             }
203         }
204         return null;
205     }
206         
207
208     /*
209      * @see IStatusChangeListener#statusChanged
210      */

211     public void statusChanged(IStatus status) {
212         setValid(!status.matches(IStatus.ERROR));
213         StatusUtil.applyToStatusLine(this, status);
214     }
215
216
217
218 }
219
Popular Tags