KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > internal > ui > macbundler > BundleDescription


1 /*******************************************************************************
2  * Copyright (c) 2000, 2004 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.macbundler;
12
13 import java.io.*;
14 import java.io.File JavaDoc;
15 import java.util.*;
16 import java.util.Properties JavaDoc;
17
18 import org.eclipse.core.runtime.CoreException;
19
20 import org.eclipse.jface.util.*;
21 import org.eclipse.jface.util.ListenerList;
22
23 import org.eclipse.debug.core.*;
24 import org.eclipse.jdt.core.IJavaProject;
25 import org.eclipse.jdt.launching.AbstractJavaLaunchConfigurationDelegate;
26
27
28 class BundleDescription implements BundleAttributes {
29     
30     private static final String JavaDoc STUB= "/System/Library/Frameworks/JavaVM.framework/Versions/A/Resources/MacOS/JavaApplicationStub"; //$NON-NLS-1$
31
private static final String JavaDoc ICON= "/System/Library/Frameworks/JavaVM.framework/Versions/CurrentJDK/Resources/GenericApp.icns"; //$NON-NLS-1$
32

33     private ListenerList fListeners= new ListenerList();
34     private Properties JavaDoc fProperties= new Properties JavaDoc();
35     private List fClassPath= new ArrayList();
36     private List fResources= new ArrayList();
37     Properties JavaDoc fProperties2= new Properties JavaDoc();
38     
39     
40     BundleDescription() {
41         clear();
42     }
43     
44     void clear() {
45         fProperties.clear();
46         fClassPath.clear();
47         fResources.clear();
48         fProperties2.clear();
49         fProperties.put(SIGNATURE, "????"); //$NON-NLS-1$
50
fProperties.put(ICONFILE, ICON);
51     }
52     
53     void addResource(ResourceInfo ri, boolean onClasspath) {
54         if (onClasspath)
55             fClassPath.add(ri);
56         else
57             fResources.add(ri);
58     }
59     
60     boolean removeResource(ResourceInfo ri, boolean onClasspath) {
61         if (onClasspath)
62             return fClassPath.remove(ri);
63         return fResources.remove(ri);
64     }
65
66     ResourceInfo[] getResources(boolean onClasspath) {
67         if (onClasspath)
68             return (ResourceInfo[]) fClassPath.toArray(new ResourceInfo[fClassPath.size()]);
69         return (ResourceInfo[]) fResources.toArray(new ResourceInfo[fResources.size()]);
70     }
71     
72     void addListener(IPropertyChangeListener listener) {
73         fListeners.add(listener);
74     }
75     
76     void removeListener(IPropertyChangeListener listener) {
77         fListeners.remove(listener);
78     }
79     
80     String JavaDoc get(String JavaDoc key) {
81         return fProperties.getProperty(key);
82     }
83     
84     public String JavaDoc get(String JavaDoc key, String JavaDoc dflt) {
85         return fProperties.getProperty(key, dflt);
86     }
87     
88     public boolean get(String JavaDoc key, boolean dflt) {
89         Boolean JavaDoc v= (Boolean JavaDoc) fProperties.get(key);
90         if (v == null)
91             return dflt;
92         return v.booleanValue();
93     }
94     
95     void setValue(String JavaDoc key, Object JavaDoc value) {
96         fProperties.put(key, value);
97     }
98     
99     void inititialize(ILaunchConfiguration lc) {
100         AbstractJavaLaunchConfigurationDelegate lcd;
101         try {
102             lcd= (AbstractJavaLaunchConfigurationDelegate) lc.getType().getDelegate(ILaunchManager.RUN_MODE);
103         } catch (CoreException e) {
104             return;
105         }
106         
107         String JavaDoc appName= lc.getName();
108         fProperties.put(APPNAME, appName);
109         fProperties.put(GETINFO, appName + Util.getString("BundleDescription.copyright.format")); //$NON-NLS-1$
110

111         try {
112             fProperties.put(MAINCLASS, lcd.getMainTypeName(lc));
113         } catch (CoreException e) {
114             fProperties.put(MAINCLASS, ""); //$NON-NLS-1$
115
}
116         try {
117             fProperties.put(ARGUMENTS, lcd.getProgramArguments(lc));
118         } catch (CoreException e) {
119             fProperties.put(ARGUMENTS, ""); //$NON-NLS-1$
120
}
121         String JavaDoc wd= null;
122         try {
123             wd= lcd.getWorkingDirectory(lc).getAbsolutePath();
124 // fProperties.put(WORKINGDIR, wd); //$NON-NLS-1$
125
} catch (CoreException e) {
126 // fProperties.put(WORKINGDIR, ""); //$NON-NLS-1$
127
}
128         try {
129             fProperties.put(MAINCLASS, lcd.getMainTypeName(lc));
130         } catch (CoreException e) {
131             fProperties.put(MAINCLASS, ""); //$NON-NLS-1$
132
}
133         
134         try {
135             String JavaDoc[] classpath= lcd.getClasspath(lc);
136             for (int i= 0; i < classpath.length; i++)
137                 addResource(new ResourceInfo(classpath[i]), true); //$NON-NLS-1$
138
} catch (CoreException e) {
139             //
140
}
141         
142         String JavaDoc vmOptions2= ""; //$NON-NLS-1$
143
String JavaDoc vmOptions= null;
144         try {
145             vmOptions= lcd.getVMArguments(lc);
146         } catch (CoreException e) {
147             //
148
}
149         if (vmOptions != null) {
150             StringTokenizer st= new StringTokenizer(vmOptions);
151             while (st.hasMoreTokens()) {
152                 String JavaDoc token= st.nextToken();
153                 int pos= token.indexOf('=');
154                 if (pos > 2 && token.startsWith("-D")) { //$NON-NLS-1$
155
String JavaDoc key= token.substring(2, pos).trim();
156                     String JavaDoc value= token.substring(pos+1).trim();
157                     int l= value.length();
158                     if (l >= 2 && value.charAt(0) == '"' && value.charAt(l-1) == '"')
159                         value= value.substring(1, l-1);
160                     if ("java.library.path".equals(key)) { //$NON-NLS-1$
161
addDllDir(wd, value);
162                     } else {
163                         fProperties2.put(key, value);
164                     }
165                 } else {
166                     vmOptions2= vmOptions2 + token + ' ';
167                 }
168             }
169         }
170
171         fProperties.put(VMOPTIONS, vmOptions2);
172         
173         boolean isSWT= false;
174         Iterator iter= fResources.iterator();
175         while (iter.hasNext()) {
176             ResourceInfo ri= (ResourceInfo) iter.next();
177             if (ri.fPath.indexOf("libswt-carbon") >= 0) { //$NON-NLS-1$
178
isSWT= true;
179                 break;
180             }
181         }
182         fProperties.put(USES_SWT, Boolean.valueOf(isSWT));
183         
184         String JavaDoc launcher= null;
185         if (isSWT)
186             launcher= System.getProperty("org.eclipse.swtlauncher"); //$NON-NLS-1$
187

188         if (launcher == null) {
189             setValue(JVMVERSION, "1.4*"); //$NON-NLS-1$
190
launcher= STUB; //$NON-NLS-1$
191
}
192         setValue(LAUNCHER, launcher);
193
194         
195         IJavaProject p= null;
196         try {
197             p= lcd.getJavaProject(lc);
198         } catch (CoreException e) {
199             // ignore
200
}
201         if (p != null)
202             fProperties.put(IDENTIFIER, p.getElementName());
203         else
204             fProperties.put(IDENTIFIER, ""); //$NON-NLS-1$
205

206         fireChange();
207     }
208     
209     void fireChange() {
210         PropertyChangeEvent e= new PropertyChangeEvent(this, ALL, null, null);
211         Object JavaDoc[] listeners= fListeners.getListeners();
212         for (int i= 0; i < listeners.length; i++)
213             ((IPropertyChangeListener)listeners[i]).propertyChange(e);
214     }
215
216     private void addDllDir(String JavaDoc wd, String JavaDoc path) {
217         File JavaDoc lib_dir;
218         if (path.startsWith("../")) { //$NON-NLS-1$
219
lib_dir= new File JavaDoc(wd, path);
220         } else {
221             lib_dir= new File JavaDoc(path);
222         }
223         if (lib_dir.isDirectory()) {
224             File JavaDoc[] dlls= lib_dir.listFiles();
225             for (int j= 0; j < dlls.length; j++) {
226                 try {
227                     String JavaDoc name= dlls[j].getCanonicalPath();
228                     if (name.endsWith(".jnilib")) //$NON-NLS-1$
229
addResource(new ResourceInfo(name), false); //$NON-NLS-1$
230
} catch (IOException e) {
231                     // NeedWork Auto-generated catch block
232
e.printStackTrace();
233                 }
234             }
235         }
236     }
237     
238     static boolean verify(ILaunchConfiguration lc) {
239         String JavaDoc name= lc.getName();
240         if (name.indexOf("jpage") >= 0) //$NON-NLS-1$
241
return false;
242         AbstractJavaLaunchConfigurationDelegate lcd;
243         try {
244             lcd= (AbstractJavaLaunchConfigurationDelegate) lc.getType().getDelegate(ILaunchManager.RUN_MODE);
245             if (lcd.getMainTypeName(lc) == null)
246                 return false;
247             return true;
248         } catch (CoreException e) {
249             return false;
250         }
251     }
252     
253     static boolean matches(ILaunchConfiguration lc, IJavaProject project) {
254         AbstractJavaLaunchConfigurationDelegate lcd;
255         try {
256             lcd= (AbstractJavaLaunchConfigurationDelegate) lc.getType().getDelegate(ILaunchManager.RUN_MODE);
257         } catch (CoreException e) {
258             return false;
259         }
260         IJavaProject p= null;
261         try {
262             p= lcd.getJavaProject(lc);
263         } catch (CoreException e) {
264             return false;
265         }
266         return project != null && project.equals(p);
267     }
268 }
269
Popular Tags