KickJava   Java API By Example, From Geeks To Geeks.

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


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.IProgressMonitor;
20 import org.eclipse.core.runtime.IStatus;
21
22 import org.eclipse.core.resources.IFile;
23 import org.eclipse.core.resources.IProject;
24 import org.eclipse.core.resources.IResource;
25
26 import org.eclipse.swt.SWT;
27 import org.eclipse.swt.widgets.Composite;
28 import org.eclipse.swt.widgets.Control;
29 import org.eclipse.swt.widgets.Shell;
30
31 import org.eclipse.jface.dialogs.Dialog;
32 import org.eclipse.jface.operation.IRunnableWithProgress;
33
34 import org.eclipse.ui.PlatformUI;
35 import org.eclipse.ui.dialogs.PropertyPage;
36
37 import org.eclipse.jdt.core.ClasspathContainerInitializer;
38 import org.eclipse.jdt.core.IClasspathAttribute;
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 import org.eclipse.jdt.core.JavaModelException;
46
47 import org.eclipse.jdt.internal.corext.util.JavaModelUtil;
48 import org.eclipse.jdt.internal.corext.util.Messages;
49
50 import org.eclipse.jdt.launching.JavaRuntime;
51
52 import org.eclipse.jdt.internal.ui.JavaPlugin;
53 import org.eclipse.jdt.internal.ui.dialogs.StatusUtil;
54 import org.eclipse.jdt.internal.ui.util.ExceptionHandler;
55 import org.eclipse.jdt.internal.ui.wizards.IStatusChangeListener;
56 import org.eclipse.jdt.internal.ui.wizards.buildpaths.ArchiveFileFilter;
57 import org.eclipse.jdt.internal.ui.wizards.buildpaths.BuildPathSupport;
58 import org.eclipse.jdt.internal.ui.wizards.buildpaths.CPListElement;
59
60 public class NativeLibrariesPropertyPage extends PropertyPage implements IStatusChangeListener {
61
62     private NativeLibrariesConfigurationBlock fConfigurationBlock;
63     private boolean fIsValidElement;
64     private IClasspathEntry fEntry;
65     private IPath fContainerPath;
66     
67     /**
68      * @see org.eclipse.jface.dialogs.IDialogPage#createControl(org.eclipse.swt.widgets.Composite)
69      */

70     public void createControl(Composite parent) {
71         IJavaElement elem= getJavaElement();
72         try {
73             if (elem instanceof IPackageFragmentRoot) {
74                 IPackageFragmentRoot root= (IPackageFragmentRoot) elem;
75                 
76                 IClasspathEntry entry= root.getRawClasspathEntry();
77                 if (entry == null) {
78                     fIsValidElement= false;
79                     setDescription(PreferencesMessages.NativeLibrariesPropertyPage_invalidElementSelection_desription);
80                 } else {
81                     if (entry.getEntryKind() == IClasspathEntry.CPE_CONTAINER) {
82                         fContainerPath= entry.getPath();
83                         fEntry= handleContainerEntry(fContainerPath, elem.getJavaProject(), root.getPath());
84                         fIsValidElement= fEntry != null;
85                     } else {
86                         fContainerPath= null;
87                         fEntry= entry;
88                         fIsValidElement= true;
89                     }
90                 }
91             } else {
92                 fIsValidElement= false;
93                 setDescription(PreferencesMessages.NativeLibrariesPropertyPage_invalidElementSelection_desription);
94             }
95         } catch (JavaModelException e) {
96             fIsValidElement= false;
97             setDescription(PreferencesMessages.NativeLibrariesPropertyPage_invalidElementSelection_desription);
98         }
99         super.createControl(parent);
100     }
101     
102     private IClasspathEntry handleContainerEntry(IPath containerPath, IJavaProject jproject, IPath jarPath) throws JavaModelException {
103         ClasspathContainerInitializer initializer= JavaCore.getClasspathContainerInitializer(containerPath.segment(0));
104         IClasspathContainer container= JavaCore.getClasspathContainer(containerPath, jproject);
105         if (initializer == null || container == null) {
106             setDescription(Messages.format(PreferencesMessages.NativeLibrariesPropertyPage_invalid_container, containerPath.toString()));
107             return null;
108         }
109         String JavaDoc containerName= container.getDescription();
110         IStatus status= initializer.getAttributeStatus(containerPath, jproject, JavaRuntime.CLASSPATH_ATTR_LIBRARY_PATH_ENTRY);
111         if (status.getCode() == ClasspathContainerInitializer.ATTRIBUTE_NOT_SUPPORTED) {
112             setDescription(Messages.format(PreferencesMessages.NativeLibrariesPropertyPage_not_supported, containerName));
113             return null;
114         }
115         if (status.getCode() == ClasspathContainerInitializer.ATTRIBUTE_READ_ONLY) {
116             setDescription(Messages.format(PreferencesMessages.NativeLibrariesPropertyPage_read_only, containerName));
117             return null;
118         }
119         IClasspathEntry entry= JavaModelUtil.findEntryInContainer(container, jarPath);
120         Assert.isNotNull(entry);
121         return entry;
122     }
123
124     /**
125      * {@inheritDoc}
126      */

127     protected Control createContents(Composite parent) {
128         if (!fIsValidElement)
129             return new Composite(parent, SWT.NONE);
130         
131         IJavaElement elem= getJavaElement();
132         if (elem == null)
133             return new Composite(parent, SWT.NONE);
134         
135         String JavaDoc nativeLibPath= null;
136         IClasspathAttribute[] extraAttributes= fEntry.getExtraAttributes();
137         for (int i= 0; i < extraAttributes.length; i++) {
138             if (extraAttributes[i].getName().equals(JavaRuntime.CLASSPATH_ATTR_LIBRARY_PATH_ENTRY)) {
139                 nativeLibPath= extraAttributes[i].getValue();
140                 break;
141             }
142         }
143         fConfigurationBlock= new NativeLibrariesConfigurationBlock(this, getShell(), nativeLibPath, fEntry);
144         Control control= fConfigurationBlock.createContents(parent);
145         control.setVisible(elem != null);
146
147         Dialog.applyDialogFont(control);
148         return control;
149     }
150
151     /**
152      * {@inheritDoc}
153      */

154     public void statusChanged(IStatus status) {
155         setValid(!status.matches(IStatus.ERROR));
156         StatusUtil.applyToStatusLine(this, status);
157     }
158     
159     /*
160      * @see PreferencePage#performDefaults()
161      */

162     protected void performDefaults() {
163         if (fConfigurationBlock != null) {
164             fConfigurationBlock.performDefaults();
165         }
166         super.performDefaults();
167     }
168     
169     /**
170      * @see org.eclipse.jface.preference.IPreferencePage#performOk()
171      */

172     public boolean performOk() {
173         if (fConfigurationBlock != null) {
174             String JavaDoc nativeLibraryPath= fConfigurationBlock.getNativeLibraryPath();
175             if (nativeLibraryPath == null) {
176                 return true;//no change
177
}
178             
179             IJavaElement elem= getJavaElement();
180             try {
181                 IRunnableWithProgress runnable= getRunnable(getShell(), elem, nativeLibraryPath, fEntry, fContainerPath);
182                 PlatformUI.getWorkbench().getProgressService().run(true, true, runnable);
183             } catch (InvocationTargetException JavaDoc e) {
184                 String JavaDoc title= PreferencesMessages.NativeLibrariesPropertyPage_errorAttaching_title;
185                 String JavaDoc message= PreferencesMessages.NativeLibrariesPropertyPage_errorAttaching_message;
186                 ExceptionHandler.handle(e, getShell(), title, message);
187                 return false;
188             } catch (InterruptedException JavaDoc e) {
189                 // Canceled
190
return false;
191             }
192         }
193         return true;
194     }
195     
196     private static IRunnableWithProgress getRunnable(final Shell shell, final IJavaElement elem, final String JavaDoc nativeLibraryPath, final IClasspathEntry entry, final IPath containerPath) {
197         return new IRunnableWithProgress() {
198             public void run(IProgressMonitor monitor) throws InvocationTargetException JavaDoc {
199                 try {
200                     IJavaProject project= elem.getJavaProject();
201                     if (elem instanceof IPackageFragmentRoot) {
202                         CPListElement cpElem= CPListElement.createFromExisting(entry, project);
203                         cpElem.setAttribute(CPListElement.NATIVE_LIB_PATH, nativeLibraryPath);
204                         IClasspathEntry newEntry= cpElem.getClasspathEntry();
205                         String JavaDoc[] changedAttributes= { CPListElement.NATIVE_LIB_PATH };
206                         BuildPathSupport.modifyClasspathEntry(shell, newEntry, changedAttributes, project, containerPath, monitor);
207                     }
208                 } catch (CoreException e) {
209                     throw new InvocationTargetException JavaDoc(e);
210                 }
211             }
212         };
213     }
214     
215     private IJavaElement getJavaElement() {
216         IAdaptable adaptable= getElement();
217         IJavaElement elem= (IJavaElement) adaptable.getAdapter(IJavaElement.class);
218         if (elem == null) {
219
220             IResource resource= (IResource) adaptable.getAdapter(IResource.class);
221             //special case when the .jar is a file
222
try {
223                 if (resource instanceof IFile && ArchiveFileFilter.isArchivePath(resource.getFullPath())) {
224                     IProject proj= resource.getProject();
225                     if (proj.hasNature(JavaCore.NATURE_ID)) {
226                         IJavaProject jproject= JavaCore.create(proj);
227                         elem= jproject.getPackageFragmentRoot(resource); // create a handle
228
}
229                 }
230             } catch (CoreException e) {
231                 JavaPlugin.log(e);
232             }
233         }
234         return elem;
235     }
236
237 }
238
Popular Tags