/*******************************************************************************
* Copyright (c) 2009, 2010 Innovation Gate GmbH.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
* Innovation Gate GmbH - initial API and implementation
******************************************************************************/
package de.innovationgate.eclipse.editors.helpers;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;
import org.eclipse.core.resources.IContainer;
import org.eclipse.core.resources.IFile;
import org.eclipse.core.runtime.IPath;
import org.eclipse.core.runtime.Path;
import org.eclipse.jface.text.IDocument;
import org.eclipse.jface.text.ITextViewer;
import de.innovationgate.eclipse.editors.Plugin;
import de.innovationgate.eclipse.editors.tml.TMLRegion;
import de.innovationgate.eclipse.utils.wga.WGADesignStructureHelper;
import de.innovationgate.wga.model.VersionCompliance;
public class ScriptReferenceLookup implements AttributeValueLookup {
public Set<String> lookupValues(TMLRegion region, IDocument document, ITextViewer viewer, IFile activeFile) {
Set<String> references = new HashSet<String>();
String tagName = region.getTagName();
if (tagName != null && tagName.equals("url") && region.hasAttribute("type") && !region.isDynamicAttributeValue("type")) {
String type = region.getAttributeValue("type");
if (type != null && ( type.equals(WGADesignStructureHelper.SCRIPT_TYPE_CSS) || type.equals(WGADesignStructureHelper.SCRIPT_TYPE_JS)) ) {
WGADesignStructureHelper designHelper = new WGADesignStructureHelper(activeFile);
VersionCompliance versionCompliance = WGADesignStructureHelper.getWGAVersionCompliance(activeFile);
IContainer designRoot = designHelper.getDesignRoot();
String medium = WGADesignStructureHelper.determineMediaKey(activeFile);
ResourceIndexManager rsm = Plugin.getDefault().getResourceIndexManager();
if (rsm != null) {
// the path to the file which we are currently editing
IPath pathOfEditedFile = activeFile.getParent().getProjectRelativePath();
// the path of the medium of the current file
IPath mediumPath = designHelper.getTmlRoot().getFolder(new Path(medium)).getProjectRelativePath();
pathOfEditedFile = pathOfEditedFile.removeFirstSegments(mediumPath.segmentCount());
Iterator<String> scriptPaths = rsm.getScriptPaths(designRoot, type).iterator();
while (scriptPaths.hasNext()) {
String scriptPath = scriptPaths.next();
// scriptfile path
IPath scriptFile = new Path(scriptPath).removeFileExtension();
// path of the current file
IPath currentFilePath = scriptFile.removeLastSegments(1);
// removing the path of the selected medium
currentFilePath = currentFilePath.removeFirstSegments(mediumPath.segmentCount());
// removing the path of the elected medium
scriptFile = scriptFile.removeFirstSegments(mediumPath.segmentCount());
IPath tmp = null;
if (currentFilePath.segmentCount() >= pathOfEditedFile.segmentCount()) {
int a = currentFilePath.segmentCount();
int b = pathOfEditedFile.segmentCount();
tmp = currentFilePath.removeLastSegments(a - b);
if (tmp.equals(pathOfEditedFile)) {
currentFilePath = scriptFile.removeFirstSegments(tmp.segmentCount());
references.add("::" + currentFilePath.toString().replaceAll("\\/", ":"));
} else {
String reference = scriptFile.toString().replaceAll("\\/", ":");
if (versionCompliance != null && versionCompliance.toWGAVersion() != null && versionCompliance.toWGAVersion().isAtLeast(5, 4)) {
if (reference.startsWith("overlay:") && pathOfEditedFile.toString().contains("overlay")) {
reference = reference.substring("overlay:".length());
} else if (pathOfEditedFile.toString().contains("overlay")) {
reference = reference + "@base";
}
}
references.add(reference);
}
} else {
String reference = scriptFile.toString().replaceAll("\\/", ":");
if (versionCompliance != null && versionCompliance.toWGAVersion() != null && versionCompliance.toWGAVersion().isAtLeast(5, 4)) {
if (reference.startsWith("overlay:") && pathOfEditedFile.toString().contains("overlay")) {
reference = reference.substring("overlay:".length());
} else if (pathOfEditedFile.toString().contains("overlay")) {
reference = reference + "@base";
}
}
references.add(reference);
}
}
}
} else if (type.equals("tml")) {
return new TMLReferenceLookup().lookupValues(region, document, viewer, activeFile);
}
}
return references;
}
}