Bug 2835 - Add JavaScript library is grayed out even if not already included
authorEugene Ostroukhov <eugeneo@symbian.org>
Thu, 01 Jul 2010 17:05:34 -0700
changeset 415 8198c73b9e1f
parent 414 dbe3521f62de
child 416 461da1f79f43
Bug 2835 - Add JavaScript library is grayed out even if not already included
org.symbian.tools.wrttools/src/org/symbian/tools/wrttools/core/libraries/PhoneGapInstaller.java
org.symbian.tools.wrttools/src/org/symbian/tools/wrttools/core/libraries/WRTKitInstaller.java
org.symbian.tools.wrttools/src/org/symbian/tools/wrttools/util/CoreUtil.java
--- a/org.symbian.tools.wrttools/src/org/symbian/tools/wrttools/core/libraries/PhoneGapInstaller.java	Thu Jul 01 16:35:38 2010 -0700
+++ b/org.symbian.tools.wrttools/src/org/symbian/tools/wrttools/core/libraries/PhoneGapInstaller.java	Thu Jul 01 17:05:34 2010 -0700
@@ -32,10 +32,9 @@
 import org.eclipse.core.runtime.Path;
 import org.eclipse.core.runtime.SubProgressMonitor;
 import org.eclipse.wst.jsdt.core.IJavaScriptProject;
-import org.eclipse.wst.jsdt.core.IType;
 import org.eclipse.wst.jsdt.core.JavaScriptCore;
-import org.eclipse.wst.jsdt.core.JavaScriptModelException;
 import org.symbian.tools.wrttools.Activator;
+import org.symbian.tools.wrttools.util.CoreUtil;
 
 public class PhoneGapInstaller implements IJSLibraryInstaller {
     private static final String PHONEGAP_JS = "phonegap.js";
@@ -50,8 +49,8 @@
         }
         IFile file = folder.getFile(PHONEGAP_JS);
         if (!file.isAccessible()) {
-            InputStream stream = FileLocator.openStream(Activator.getDefault().getBundle(), new Path("libraries")
-                    .append(PHONEGAP_JS), true);
+            InputStream stream = FileLocator.openStream(Activator.getDefault().getBundle(),
+                    new Path("libraries").append(PHONEGAP_JS), true);
             file.create(stream, false, new SubProgressMonitor(monitor, 3));
         }
         IPath path = new Path(folderName).append(PHONEGAP_JS);
@@ -60,16 +59,9 @@
     }
 
     public boolean isInstalled(IProject project) {
-        IJavaScriptProject jsProject = JavaScriptCore.create(project);
-        try {
-            final IType accel = jsProject.findType("Accelerometer");
-            final IType camera = jsProject.findType("Camera");
-            final IType geo = jsProject.findType("Geolocation");
-            return accel != null && camera != null && geo != null;
-        } catch (JavaScriptModelException e) {
-            Activator.log(e);
-        }
-        return false;
+        final IJavaScriptProject jsProject = JavaScriptCore.create(project);
+        return CoreUtil.hasType(jsProject, "Accelerometer") && CoreUtil.hasType(jsProject, "Camera")
+                && CoreUtil.hasType(jsProject, "Geolocation");
     }
 
 }
--- a/org.symbian.tools.wrttools/src/org/symbian/tools/wrttools/core/libraries/WRTKitInstaller.java	Thu Jul 01 16:35:38 2010 -0700
+++ b/org.symbian.tools.wrttools/src/org/symbian/tools/wrttools/core/libraries/WRTKitInstaller.java	Thu Jul 01 17:05:34 2010 -0700
@@ -30,10 +30,9 @@
 import org.eclipse.core.runtime.Path;
 import org.eclipse.core.runtime.SubProgressMonitor;
 import org.eclipse.wst.jsdt.core.IJavaScriptProject;
-import org.eclipse.wst.jsdt.core.IType;
 import org.eclipse.wst.jsdt.core.JavaScriptCore;
-import org.eclipse.wst.jsdt.core.JavaScriptModelException;
 import org.symbian.tools.wrttools.Activator;
+import org.symbian.tools.wrttools.util.CoreUtil;
 import org.symbian.tools.wrttools.util.ProjectUtils;
 
 public class WRTKitInstaller implements IJSLibraryInstaller {
@@ -44,27 +43,20 @@
         monitor.beginTask("Installing WRTKit library", 15);
 
         IFolder folder = project.getFolder("WRTKit");
-        
+
         if (folder != null && !folder.exists()) {
             folder.create(false, true, new SubProgressMonitor(monitor, 1));
         }
         InputStream zip = FileLocator.openStream(Activator.getDefault().getBundle(), new Path("/libraries/wrtkit.zip"),
                 true);
         ProjectUtils.unzip(zip, folder, 0, "WRTKit", new SubProgressMonitor(monitor, 10));
-        
+
         LibrariesUtils.addJSToHtml(project, "Adding WRTKit Library", new String[] { JS_PATH }, null);
         monitor.done();
     }
 
     public boolean isInstalled(IProject project) {
-        IJavaScriptProject jsProject = JavaScriptCore.create(project);
-        try {
-            IType npopup = jsProject.findType("NotificationPopup");
-            IType uimanager = jsProject.findType("UIManager");
-            return npopup != null && uimanager != null;
-        } catch (JavaScriptModelException e) {
-            Activator.log(e);
-            return false;
-        }
+        final IJavaScriptProject jsProject = JavaScriptCore.create(project);
+        return CoreUtil.hasType(jsProject, "NotificationPopup") && CoreUtil.hasType(jsProject, "UIManager");
     }
 }
--- a/org.symbian.tools.wrttools/src/org/symbian/tools/wrttools/util/CoreUtil.java	Thu Jul 01 16:35:38 2010 -0700
+++ b/org.symbian.tools.wrttools/src/org/symbian/tools/wrttools/util/CoreUtil.java	Thu Jul 01 17:05:34 2010 -0700
@@ -19,33 +19,36 @@
 import org.eclipse.core.runtime.Status;
 import org.eclipse.jface.text.IRegion;
 import org.eclipse.jface.text.Region;
+import org.eclipse.wst.jsdt.core.IJavaScriptProject;
+import org.eclipse.wst.jsdt.core.IType;
+import org.eclipse.wst.jsdt.core.JavaScriptModelException;
 import org.symbian.tools.wrttools.Activator;
 
 public class CoreUtil {
-	public static final String METADATA_FILE = "Info.plist";
-	public static final String PROPERTY_PATTERN = "<key>\\s*{0}\\s*</key>\\s*<string>\\s*(.*)\\s*</string>";
+    public static final String METADATA_FILE = "Info.plist";
+    public static final String PROPERTY_PATTERN = "<key>\\s*{0}\\s*</key>\\s*<string>\\s*(.*)\\s*</string>";
 
-	public static boolean isWindows() {
-		return "windows".equals(Platform.getOS());
-	}
+    public static boolean isWindows() {
+        return "windows".equals(Platform.getOS());
+    }
 
-	public static boolean isMac() {
-		return "macosx".equals(Platform.getOS());
-	}
+    public static boolean isMac() {
+        return "macosx".equals(Platform.getOS());
+    }
 
-	public static boolean isLinux() {
-		return "linux".equals(Platform.getOS());
-	}
+    public static boolean isLinux() {
+        return "linux".equals(Platform.getOS());
+    }
 
-	public static String getIndexFileName(String buffer) {
-		if (buffer != null) {
-			Matcher matcher = getPropertyLookupPattern("MainHTML").matcher(buffer);
-			if (matcher.find()) {
-				return matcher.group(1);
-			}
-		}
-		return null;
-	}
+    public static String getIndexFileName(String buffer) {
+        if (buffer != null) {
+            Matcher matcher = getPropertyLookupPattern("MainHTML").matcher(buffer);
+            if (matcher.find()) {
+                return matcher.group(1);
+            }
+        }
+        return null;
+    }
 
     public static String getApplicationName(String buffer) {
         if (buffer != null) {
@@ -57,89 +60,105 @@
         return null;
     }
 
-	public static Pattern getPropertyLookupPattern(String propertyName) {
-		return Pattern.compile(MessageFormat.format(PROPERTY_PATTERN, propertyName), Pattern.CASE_INSENSITIVE);
-	}
+    public static Pattern getPropertyLookupPattern(String propertyName) {
+        return Pattern.compile(MessageFormat.format(PROPERTY_PATTERN, propertyName), Pattern.CASE_INSENSITIVE);
+    }
 
-	public static IRegion getIndexFileNameRegion(String string) {
-		Matcher matcher = getPropertyLookupPattern("MainHTML").matcher(string);
-		if (matcher.find()) {
-			int start = matcher.start(1);
-			return new Region(start, matcher.end(1) - start);
-		}
-		return null;
-	}
+    public static IRegion getIndexFileNameRegion(String string) {
+        Matcher matcher = getPropertyLookupPattern("MainHTML").matcher(string);
+        if (matcher.find()) {
+            int start = matcher.start(1);
+            return new Region(start, matcher.end(1) - start);
+        }
+        return null;
+    }
 
-	public static IFile getFile(IProject project, String fileName) throws CoreException {
-		String n = fileName.toLowerCase();
-		IResource[] members = project.members();
-		for (IResource iResource : members) {
-			if (iResource.getType() == IResource.FILE
-					&& n.equals(iResource.getName().toLowerCase())
-					&& iResource.isAccessible()) {
-				return (IFile) iResource;
-			}
-		}
-		return null;
-	}
+    public static IFile getFile(IProject project, String fileName) throws CoreException {
+        String n = fileName.toLowerCase();
+        IResource[] members = project.members();
+        for (IResource iResource : members) {
+            if (iResource.getType() == IResource.FILE && n.equals(iResource.getName().toLowerCase())
+                    && iResource.isAccessible()) {
+                return (IFile) iResource;
+            }
+        }
+        return null;
+    }
 
-	public static String readFile(IProject project, IFile file)
-			throws CoreException {
-		try {
-			if (file != null && file.isAccessible()) {
-				final BufferedReader reader = new BufferedReader(
-						new InputStreamReader(file.getContents(), file.getCharset()));
-				return read(reader);
-			}
-			return null;
-		} catch (IOException e) {
-			throw new CoreException(new Status(IStatus.ERROR, Activator.PLUGIN_ID, MessageFormat.format("Failed to read file {0} in project {1}", file.getName(), project.getName())));
-		}
-	}
+    public static String readFile(IProject project, IFile file) throws CoreException {
+        try {
+            if (file != null && file.isAccessible()) {
+                final BufferedReader reader = new BufferedReader(new InputStreamReader(file.getContents(),
+                        file.getCharset()));
+                return read(reader);
+            }
+            return null;
+        } catch (IOException e) {
+            throw new CoreException(new Status(IStatus.ERROR, Activator.PLUGIN_ID, MessageFormat.format(
+                    "Failed to read file {0} in project {1}", file.getName(), project.getName())));
+        }
+    }
 
     public static String read(final Reader reader) throws IOException {
         StringBuffer buffer = new StringBuffer();
         try {
-        	int c = 0;
-        	char[] buf = new char[4096];
-        	while ((c = reader.read(buf)) > 0) {
-        		buffer.append(buf, 0, c);
-        	}
-        	return buffer.toString();
+            int c = 0;
+            char[] buf = new char[4096];
+            while ((c = reader.read(buf)) > 0) {
+                buffer.append(buf, 0, c);
+            }
+            return buffer.toString();
         } finally {
-        	reader.close();
+            reader.close();
         }
     }
 
-	private static final Map<IProject, IndexFileRecord> INDEX_FILES = new HashMap<IProject, IndexFileRecord>();
-	
-	public static synchronized String getIndexFile(IProject project) throws CoreException {
-		// There will really be a lot of calls to this method. We need to cache values.
-		IFile file = getFile(project, METADATA_FILE);
-		if (file == null) {
-			return null;
-		}
-		if (INDEX_FILES.containsKey(project)) {
-			IndexFileRecord record = INDEX_FILES.get(project);
-			if (file == null || !file.isAccessible()) {
-				throw new CoreException(new Status(IStatus.ERROR, Activator.PLUGIN_ID, MessageFormat.format("No Info.plist for project {0}", project.getName())));
-			}
-			if (record.timeStamp == file.getModificationStamp()) {
-				return record.fileName;
-			}
-		}
-		String fileName = getIndexFileName(readFile(project, file));
-		INDEX_FILES.put(project, new IndexFileRecord(fileName, file.getModificationStamp()));
-		return fileName;
-	}
-	
-	private static class IndexFileRecord {
-		public final String fileName;
-		public final long timeStamp;
-		
-		public IndexFileRecord(String fileName, long timeStamp) {
-			this.fileName = fileName;
-			this.timeStamp = timeStamp;
-		}
-	}
+    private static final Map<IProject, IndexFileRecord> INDEX_FILES = new HashMap<IProject, IndexFileRecord>();
+
+    public static synchronized String getIndexFile(IProject project) throws CoreException {
+        // There will really be a lot of calls to this method. We need to cache values.
+        IFile file = getFile(project, METADATA_FILE);
+        if (file == null) {
+            return null;
+        }
+        if (INDEX_FILES.containsKey(project)) {
+            IndexFileRecord record = INDEX_FILES.get(project);
+            if (file == null || !file.isAccessible()) {
+                throw new CoreException(new Status(IStatus.ERROR, Activator.PLUGIN_ID, MessageFormat.format(
+                        "No Info.plist for project {0}", project.getName())));
+            }
+            if (record.timeStamp == file.getModificationStamp()) {
+                return record.fileName;
+            }
+        }
+        String fileName = getIndexFileName(readFile(project, file));
+        INDEX_FILES.put(project, new IndexFileRecord(fileName, file.getModificationStamp()));
+        return fileName;
+    }
+
+    private static class IndexFileRecord {
+        public final String fileName;
+        public final long timeStamp;
+
+        public IndexFileRecord(String fileName, long timeStamp) {
+            this.fileName = fileName;
+            this.timeStamp = timeStamp;
+        }
+    }
+
+    public static boolean hasType(IJavaScriptProject project, String name) {
+        try {
+            final IType[] types = project.findTypes(name);
+            if (types != null) {
+                for (IType type : types) {
+                    if (type.getJavaScriptProject().equals(project)) {
+                        return true;
+                    }
+                }
+            }
+        } catch (JavaScriptModelException e) {
+            Activator.log(e);
+        }
+        return false;
+    }
 }