Author: scabanovich
Date: 2011-11-18 20:14:21 -0500 (Fri, 18 Nov 2011)
New Revision: 36466
Modified:
trunk/jsf/plugins/org.jboss.tools.jsf/src/org/jboss/tools/jsf/model/pv/JSFProjectsRoot.java
trunk/jsf/plugins/org.jboss.tools.jsf/src/org/jboss/tools/jsf/project/JSFAutoLoad.java
Log:
JBIDE-10177
https://issues.jboss.org/browse/JBIDE-10177
JBIDE-812
https://issues.jboss.org/browse/JBIDE-812
Multiple web roots loaded into XModel.
Modified:
trunk/jsf/plugins/org.jboss.tools.jsf/src/org/jboss/tools/jsf/model/pv/JSFProjectsRoot.java
===================================================================
---
trunk/jsf/plugins/org.jboss.tools.jsf/src/org/jboss/tools/jsf/model/pv/JSFProjectsRoot.java 2011-11-19
01:12:43 UTC (rev 36465)
+++
trunk/jsf/plugins/org.jboss.tools.jsf/src/org/jboss/tools/jsf/model/pv/JSFProjectsRoot.java 2011-11-19
01:14:21 UTC (rev 36466)
@@ -15,6 +15,7 @@
import org.eclipse.core.runtime.CoreException;
import org.jboss.tools.common.model.XModelObject;
+import org.jboss.tools.common.model.XModelObjectConstants;
import org.jboss.tools.common.model.event.XModelTreeEvent;
import org.jboss.tools.common.model.filesystems.FileSystemsHelper;
import org.jboss.tools.common.model.impl.ExtraRootImpl;
@@ -84,9 +85,15 @@
if(fs == null) return new XModelObject[0];
webroot = fs.getChildByPath("WEB-ROOT");
webxml = WebAppHelper.getWebApp(getModel());
- XModelObject[] cs = getChildren();
List<XModelObject> list = new ArrayList<XModelObject>();
if(webroot != null) list.add(webroot);
+ XModelObject[] ss = fs.getChildren("FileSystemFolder");
+ for (XModelObject s: ss) {
+ if(s.getAttributeValue(XModelObjectConstants.ATTR_NAME).startsWith("WEB-ROOT-"))
{
+ list.add(s);
+ }
+ }
+ XModelObject[] cs = getChildren();
for (int i = 0; i < cs.length; i++) list.add(cs[i]);
if(webxml != null) list.add(webxml);
treeChildren = list.toArray(new XModelObject[0]);
Modified:
trunk/jsf/plugins/org.jboss.tools.jsf/src/org/jboss/tools/jsf/project/JSFAutoLoad.java
===================================================================
---
trunk/jsf/plugins/org.jboss.tools.jsf/src/org/jboss/tools/jsf/project/JSFAutoLoad.java 2011-11-19
01:12:43 UTC (rev 36465)
+++
trunk/jsf/plugins/org.jboss.tools.jsf/src/org/jboss/tools/jsf/project/JSFAutoLoad.java 2011-11-19
01:14:21 UTC (rev 36466)
@@ -1,88 +1,188 @@
package org.jboss.tools.jsf.project;
import java.io.IOException;
-import java.util.Properties;
+import java.util.ArrayList;
+import java.util.List;
+import org.eclipse.core.resources.IContainer;
+import org.eclipse.core.resources.IFolder;
import org.eclipse.core.resources.IProject;
-import org.eclipse.core.resources.IResource;
-import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.core.runtime.IPath;
+import org.eclipse.core.runtime.Path;
+import org.eclipse.wst.common.componentcore.ComponentCore;
+import org.eclipse.wst.common.componentcore.resources.IVirtualComponent;
import org.jboss.tools.common.model.XModel;
import org.jboss.tools.common.model.XModelConstants;
import org.jboss.tools.common.model.XModelObject;
+import org.jboss.tools.common.model.XModelObjectConstants;
import org.jboss.tools.common.model.filesystems.FileSystemsHelper;
-import org.jboss.tools.common.model.filesystems.impl.FileSystemImpl;
+import org.jboss.tools.common.model.impl.XModelImpl;
import org.jboss.tools.common.model.project.IAutoLoad;
-import org.jboss.tools.common.model.project.ProjectHome;
import org.jboss.tools.common.model.util.EclipseResourceUtil;
import org.jboss.tools.common.model.util.XModelObjectUtil;
+import org.jboss.tools.common.util.FileUtil;
+import org.jboss.tools.jsf.model.pv.JSFProjectsTree;
+import org.jboss.tools.jst.web.WebUtils;
+import org.jboss.tools.jst.web.model.pv.WebProjectNode;
-public class JSFAutoLoad implements IAutoLoad {
+public class JSFAutoLoad implements IAutoLoad, XModelObjectConstants {
+ static String WEB_INF = "WEB-INF"; //$NON-NLS-1$
+ static String WEB_ROOT = "WEB-ROOT"; //$NON-NLS-1$
public void load(XModel model) {
- Properties properties = null;
XModelObject fs = FileSystemsHelper.getFileSystems(model);
IProject project = EclipseResourceUtil.getProject(fs);
if(project == null) return;
- fs.setAttributeValue("application name", project.getName());
+ fs.setAttributeValue("application name", project.getName()); //$NON-NLS-1$
- String fsLoc = null;
- FileSystemImpl s = null;
- properties = new Properties();
-
- fsLoc = project.getLocation().toString();
- properties.setProperty("location", fsLoc);
- properties.setProperty("name", project.getName());
- s = (FileSystemImpl)model.createModelObject("FileSystemFolder", properties);
+ XModelObject s = createFileSystemFolder(model, project.getName(),
project.getLocation().toString());
fs.addChild(s);
- XModelObject webinf = model.createModelObject("FileSystemFolder", null);
//$NON-NLS-1$
- webinf.setAttributeValue("name", "WEB-INF"); //$NON-NLS-1$
//$NON-NLS-2$
- webinf.setAttributeValue("location", XModelConstants.WORKSPACE_REF);
//$NON-NLS-1$
+ XModelObject webinf = createFileSystemFolder(model, WEB_INF,
XModelConstants.WORKSPACE_REF);
fs.addChild(webinf);
String webInfLocation = XModelObjectUtil.expand(XModelConstants.WORKSPACE_REF, model,
null);
- String webRootLocation = getWebRootPath(project, webInfLocation);
-
- XModelObject webroot = model.createModelObject("FileSystemFolder", null);
//$NON-NLS-1$
- webroot.setAttributeValue("name", "WEB-ROOT"); //$NON-NLS-1$
//$NON-NLS-2$
- webroot.setAttributeValue("location", webRootLocation); //$NON-NLS-1$
//$NON-NLS-2$
- fs.addChild(webroot);
-
- XModelObject lib = model.createModelObject("FileSystemFolder", null);
//$NON-NLS-1$
- lib.setAttributeValue("name", "lib"); //$NON-NLS-1$ //$NON-NLS-2$
- lib.setAttributeValue("location", XModelConstants.WORKSPACE_REF +
"/lib"); //$NON-NLS-1$ //$NON-NLS-2$
+ List<String> webRootLocations = getWebRootPaths(project, webInfLocation);
+
+ int i = 0;
+ for (String webRootLocation: webRootLocations) {
+ String name = WEB_ROOT;
+ if(i > 0) name += "-" + i;
+ XModelObject webroot = createFileSystemFolder(model, name, webRootLocation);
+ fs.addChild(webroot);
+ i++;
+ }
+
+ XModelObject lib = createFileSystemFolder(model, "lib",
XModelConstants.WORKSPACE_REF + "/lib"); //$NON-NLS-1$ //$NON-NLS-2$
fs.addChild(lib);
}
+ XModelObject createFileSystemFolder(XModel model, String name, String location) {
+ XModelObject f = model.createModelObject(ENT_FILE_SYSTEM_FOLDER, null);
+ f.setAttributeValue(ATTR_NAME, name);
+ f.setAttributeValue(ATTR_NAME_LOCATION, location);
+ return f;
+ }
+
- static String getWebRootPath(IProject project, String webInfLocation) {
- String webRootLocation = XModelConstants.WORKSPACE_REF + "/..";
+ public void update(XModel model) {
+ XModelObject fs = FileSystemsHelper.getFileSystems(model);
+ IProject project = EclipseResourceUtil.getProject(fs);
+ if(project == null) return;
+
+ String webInfLocation = XModelObjectUtil.expand(XModelConstants.WORKSPACE_REF, model,
null);
+ List<String> webRootLocations = getWebRootPaths(project, webInfLocation);
+
+ boolean modified = false;
+ XModelObject webinf = fs.getChildByPath(WEB_INF);
+ String rWebInfLocation = getWebInfPath(project, webInfLocation);
+ if(rWebInfLocation != null) {
+ if(webinf != null &&
!rWebInfLocation.equals(webinf.getAttributeValue(ATTR_NAME_LOCATION))) {
+ webinf.removeFromParent();
+ webinf = null;
+ modified = true;
+ }
+ if(webinf == null) {
+ webinf = createFileSystemFolder(model, WEB_INF, rWebInfLocation);
+ fs.addChild(webinf);
+ modified = true;
+ }
+ } else if(webinf != null) {
+ webinf.removeFromParent();
+ modified = true;
+ }
+
+ List<XModelObject> existingRoots = getExistingWebRoots(fs);
+ boolean rootsChanged = rootsChanged(webRootLocations, existingRoots);
+ if(rootsChanged) {
+ modified = true;
+ for (XModelObject c: existingRoots) {
+ c.removeFromParent();
+ }
+ int i = 0;
+ for (String webRootLocation: webRootLocations) {
+ String name = WEB_ROOT;
+ if(i > 0) name += "-" + i;
+ XModelObject webroot = createFileSystemFolder(model, name, webRootLocation);
+ fs.addChild(webroot);
+ i++;
+ }
+ }
- IPath wrp = ProjectHome.getFirstWebContentPath(project);
- IPath wip = ProjectHome.getWebInfPath(project);
+ if(modified) {
+ ((XModelImpl)fs.getModel()).fireStructureChanged(fs);
+ WebProjectNode n = JSFProjectsTree.getProjectsRoot(model);
+ if(n != null) {
+ n.invalidate();
+ }
+ }
+ }
- if(wrp == null || wip == null) {
- return webRootLocation;
+ boolean rootsChanged(List<String> webRootLocations, List<XModelObject> rs)
{
+ if(webRootLocations.size() != rs.size()) return true;
+ for (int i = 0; i < webRootLocations.size(); i++) {
+ String s = webRootLocations.get(i);
+ XModelObject o = rs.get(i);
+ if(!o.getAttributeValue(ATTR_NAME_LOCATION).equals(s)) {
+ return true;
+ }
}
-
- IResource wrpc = ResourcesPlugin.getWorkspace().getRoot().findMember(wrp);
- IResource wipc = ResourcesPlugin.getWorkspace().getRoot().findMember(wip);
- if(wrpc != null && wipc != null && wipc.isLinked()) {
- IPath p = wrpc.getLocation();
- if(p != null) {
- try {
- webRootLocation = p.toFile().getCanonicalPath().replace('\\', '/');
- } catch (IOException e) {
- }
- String relative =
org.jboss.tools.common.util.FileUtil.getRelativePath(webInfLocation, webRootLocation);
+ return false;
+ }
+
+ List<XModelObject> getExistingWebRoots(XModelObject fs) {
+ List<XModelObject> result = new ArrayList<XModelObject>();
+ XModelObject[] cs = fs.getChildren(ENT_FILE_SYSTEM_FOLDER);
+ for (XModelObject c: cs) {
+ if(c.getAttributeValue(ATTR_NAME).startsWith(WEB_ROOT)) {
+ result.add(c);
+ }
+ }
+ return result;
+ }
+
+ static List<String> getWebRootPaths(IProject project, String webInfLocation) {
+ List<String> result = new ArrayList<String>();
+ IContainer[] cs = WebUtils.getWebRootFolders(project);
+ for (IContainer c: cs) {
+ IPath path = c.getLocation();
+ if(path == null) continue;
+ String webRootLocation = null;
+ try {
+ webRootLocation = path.toFile().getCanonicalPath().replace('\\',
'/');
+ String relative = FileUtil.getRelativePath(webInfLocation, webRootLocation);
if(relative != null) {
webRootLocation = XModelConstants.WORKSPACE_REF + relative;
}
+ result.add(webRootLocation);
+ } catch (IOException e) {
+ continue;
}
}
- return webRootLocation;
+ return result;
}
+ static String getWebInfPath(IProject project, String webInfLocation) {
+ IVirtualComponent component = ComponentCore.createComponent(project);
+ if(component != null && component.getRootFolder() != null) {
+ IContainer[] cs = WebUtils.getWebRootFolders(project, true);
+ for (IContainer c: cs) {
+ if(c.exists()) {
+ IFolder f = c.getFolder(new Path("/WEB-INF")); //$NON-NLS-1$
+ if(f.exists()) {
+ try {
+ String location =
f.getLocation().toFile().getCanonicalPath().replace('\\', '/');
+ String relative = FileUtil.getRelativePath(webInfLocation, location);
+ return (relative != null) ? XModelConstants.WORKSPACE_REF + relative : location;
+ } catch (IOException e) {
+ continue;
+ }
+ }
+ }
+ }
+ }
+ return null;
+ }
+
}