Author: dazarov
Date: 2009-06-04 14:22:36 -0400 (Thu, 04 Jun 2009)
New Revision: 15716
Added:
trunk/seam/plugins/org.jboss.tools.seam.core/src/org/jboss/tools/seam/internal/core/refactoring/RenameSeamContextVariableProcessor.java
trunk/seam/plugins/org.jboss.tools.seam.core/src/org/jboss/tools/seam/internal/core/refactoring/SeamRenameProcessor.java
trunk/seam/plugins/org.jboss.tools.seam.ui/src/org/jboss/tools/seam/ui/wizard/RenameSeamContextVariableWizard.java
Modified:
trunk/seam/plugins/org.jboss.tools.seam.core/src/org/jboss/tools/seam/core/SeamCoreMessages.java
trunk/seam/plugins/org.jboss.tools.seam.core/src/org/jboss/tools/seam/core/messages.properties
trunk/seam/plugins/org.jboss.tools.seam.core/src/org/jboss/tools/seam/internal/core/refactoring/RenameComponentProcessor.java
trunk/seam/plugins/org.jboss.tools.seam.ui/src/org/jboss/tools/seam/ui/refactoring/SeamRefactorContributionFactory.java
trunk/seam/plugins/org.jboss.tools.seam.ui/src/org/jboss/tools/seam/ui/wizard/RenameComponentWizard.java
trunk/seam/tests/org.jboss.tools.seam.core.test/src/org/jboss/tools/seam/core/test/refactoring/SeamComponentRefactoringTest.java
Log:
Rename Seam Context Variable
https://jira.jboss.org/jira/browse/JBIDE-1077
Modified:
trunk/seam/plugins/org.jboss.tools.seam.core/src/org/jboss/tools/seam/core/SeamCoreMessages.java
===================================================================
---
trunk/seam/plugins/org.jboss.tools.seam.core/src/org/jboss/tools/seam/core/SeamCoreMessages.java 2009-06-04
18:19:02 UTC (rev 15715)
+++
trunk/seam/plugins/org.jboss.tools.seam.core/src/org/jboss/tools/seam/core/SeamCoreMessages.java 2009-06-04
18:22:36 UTC (rev 15716)
@@ -41,7 +41,7 @@
public static String
SeamFacetAbstractInstallDelegate_Could_not_save_changes_to_preferences;
public static String SeamFacetAbstractInstallDelegate_Error;
public static String SeamFacetAbstractInstallDelegate_Restrict_raw_XHTML_Documents;
- public static String RENAME_SEAM_COMPONENT_PROCESSOR_RENAME_SEAM_COMPONENT;
+ public static String RENAME_SEAM_COMPONENT_PROCESSOR_TITLE;
public static String RENAME_SEAM_COMPONENT_PROCESSOR_THIS_IS_NOT_A_SEAM_COMPONENT;
-
+ public static String RENAME_SEAM_CONTEXT_VARIABLE_PROCESSOR_TITLE;
}
\ No newline at end of file
Modified:
trunk/seam/plugins/org.jboss.tools.seam.core/src/org/jboss/tools/seam/core/messages.properties
===================================================================
---
trunk/seam/plugins/org.jboss.tools.seam.core/src/org/jboss/tools/seam/core/messages.properties 2009-06-04
18:19:02 UTC (rev 15715)
+++
trunk/seam/plugins/org.jboss.tools.seam.core/src/org/jboss/tools/seam/core/messages.properties 2009-06-04
18:22:36 UTC (rev 15716)
@@ -29,6 +29,7 @@
ERROR_JBOSS_AS_TARGET_SERVER_INCOMPATIBLE=Target Server is incompatible with Target
Runtime selected
ERROR_JBOSS_AS_TARGET_RUNTIME_IS_EMPTY=Target Runtime cannot be empty
ERROR_JBOSS_AS_TARGET_RUNTIME_UNKNOWN=Unknown Target Runtime
-RENAME_SEAM_COMPONENT_PROCESSOR_RENAME_SEAM_COMPONENT=Rename Seam Component
+RENAME_SEAM_COMPONENT_PROCESSOR_TITLE=Rename Seam Component
RENAME_SEAM_COMPONENT_PROCESSOR_THIS_IS_NOT_A_SEAM_COMPONENT=This is not a Seam
Component.
+RENAME_SEAM_CONTEXT_VARIABLE_PROCESSOR_TITLE=Rename Seam Context Variable
SEAM_INSTALL_WIZARD_PROJECT_NAME_WITH_UPPERCASE=Project names which start with a capital
letter may break Seam/JSF applications; use lower case.
\ No newline at end of file
Modified:
trunk/seam/plugins/org.jboss.tools.seam.core/src/org/jboss/tools/seam/internal/core/refactoring/RenameComponentProcessor.java
===================================================================
---
trunk/seam/plugins/org.jboss.tools.seam.core/src/org/jboss/tools/seam/internal/core/refactoring/RenameComponentProcessor.java 2009-06-04
18:19:02 UTC (rev 15715)
+++
trunk/seam/plugins/org.jboss.tools.seam.core/src/org/jboss/tools/seam/internal/core/refactoring/RenameComponentProcessor.java 2009-06-04
18:22:36 UTC (rev 15716)
@@ -10,95 +10,31 @@
******************************************************************************/
package org.jboss.tools.seam.internal.core.refactoring;
-import java.io.IOException;
-import java.util.ArrayList;
-import java.util.Set;
-import java.util.StringTokenizer;
-
-import org.eclipse.core.resources.IContainer;
-import org.eclipse.core.resources.IFile;
-import org.eclipse.core.resources.IFolder;
-import org.eclipse.core.resources.IProject;
-import org.eclipse.core.resources.IResource;
import org.eclipse.core.runtime.CoreException;
-import org.eclipse.core.runtime.IPath;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.OperationCanceledException;
-import org.eclipse.jdt.internal.ui.text.FastJavaPartitionScanner;
-import org.eclipse.jdt.ui.text.IJavaPartitions;
-import org.eclipse.jface.text.BadLocationException;
-import org.eclipse.jface.text.Document;
-import org.eclipse.jface.text.rules.IToken;
-import org.eclipse.jface.text.rules.Token;
import org.eclipse.ltk.core.refactoring.Change;
import org.eclipse.ltk.core.refactoring.CompositeChange;
import org.eclipse.ltk.core.refactoring.RefactoringStatus;
-import org.eclipse.ltk.core.refactoring.TextFileChange;
import org.eclipse.ltk.core.refactoring.participants.CheckConditionsContext;
import org.eclipse.ltk.core.refactoring.participants.RefactoringParticipant;
-import org.eclipse.ltk.core.refactoring.participants.RenameProcessor;
import org.eclipse.ltk.core.refactoring.participants.SharableParticipants;
-import org.eclipse.text.edits.MultiTextEdit;
-import org.eclipse.text.edits.ReplaceEdit;
-import org.eclipse.text.edits.TextEdit;
-import org.eclipse.wst.sse.core.StructuredModelManager;
-import org.eclipse.wst.sse.core.internal.provisional.IModelManager;
-import org.eclipse.wst.sse.core.internal.provisional.IStructuredModel;
-import org.eclipse.wst.sse.core.internal.provisional.text.IStructuredDocumentRegion;
-import org.eclipse.wst.sse.core.internal.provisional.text.ITextRegion;
-import org.eclipse.wst.sse.core.internal.provisional.text.ITextRegionList;
-import org.eclipse.wst.xml.core.internal.provisional.document.IDOMDocument;
-import org.eclipse.wst.xml.core.internal.provisional.document.IDOMModel;
-import org.eclipse.wst.xml.core.internal.provisional.document.IDOMNode;
-import org.eclipse.wst.xml.core.internal.regions.DOMRegionContext;
-import org.jboss.tools.common.el.core.model.ELInstance;
-import org.jboss.tools.common.el.core.model.ELInvocationExpression;
-import org.jboss.tools.common.el.core.model.ELModel;
-import org.jboss.tools.common.el.core.model.ELPropertyInvocation;
-import org.jboss.tools.common.el.core.parser.ELParser;
-import org.jboss.tools.common.el.core.parser.ELParserUtil;
-import org.jboss.tools.common.model.project.ext.ITextSourceReference;
-import org.jboss.tools.common.util.FileUtil;
-import org.jboss.tools.seam.core.BijectedAttributeType;
-import org.jboss.tools.seam.core.IBijectedAttribute;
import org.jboss.tools.seam.core.ISeamComponent;
-import org.jboss.tools.seam.core.ISeamFactory;
-import org.jboss.tools.seam.core.ISeamJavaComponentDeclaration;
-import org.jboss.tools.seam.core.ISeamProject;
-import org.jboss.tools.seam.core.ISeamXmlComponentDeclaration;
import org.jboss.tools.seam.core.SeamCoreMessages;
-import org.jboss.tools.seam.core.SeamCorePlugin;
-import org.jboss.tools.seam.core.SeamProjectsSet;
-import org.jboss.tools.seam.internal.core.SeamComponentDeclaration;
-import org.jboss.tools.seam.internal.core.scanner.java.SeamAnnotations;
-import org.jboss.tools.seam.internal.core.validation.SeamContextValidationHelper;
-import org.w3c.dom.Node;
-import org.w3c.dom.NodeList;
/**
* @author Alexey Kazakov, Daniel Azarov
*/
-public class RenameComponentProcessor extends RenameProcessor {
- private static final String JAVA_EXT = "java"; //$NON-NLS-1$
- private static final String XML_EXT = "xml"; //$NON-NLS-1$
- private static final String XHTML_EXT = "xhtml"; //$NON-NLS-1$
- private static final String JSP_EXT = "jsp"; //$NON-NLS-1$
- private static final String PROPERTIES_EXT = "properties"; //$NON-NLS-1$
+public class RenameComponentProcessor extends SeamRenameProcessor {
+ private ISeamComponent component;
- private static final String SEAM_PROPERTIES_FILE = "seam.properties";
//$NON-NLS-1$
- private IFile declarationFile=null;
- private ISeamComponent component;
- private String newName;
- SeamContextValidationHelper coreHelper = new SeamContextValidationHelper();
- SeamProjectsSet projectsSet;
-
/**
* @param component Renamed component
*/
public RenameComponentProcessor(ISeamComponent component) {
super();
- this.component = component;
+ setComponent(component);
}
public ISeamComponent getComponent() {
@@ -107,292 +43,11 @@
public void setComponent(ISeamComponent component) {
this.component = component;
+ setOldName(component.getName());
}
- public void setNewComponentName(String componentName){
- this.newName = componentName;
- }
- // we need to find references in .java .xml .xhtml .jsp .properties files
- private void findELReferences(){
- if(declarationFile == null)
- return;
-
- IProject[] projects = projectsSet.getAllProjects();
- for (IProject project : projects) {
- scan(project);
- }
- }
- private void scan(IContainer container){
- try{
- for(IResource resource : container.members()){
- if(resource instanceof IFolder)
- scan((IFolder) resource);
- else if(resource instanceof IFile)
- scan((IFile) resource);
- }
- }catch(CoreException ex){
- SeamCorePlugin.getDefault().logError(ex);
- }
- }
-
- private void scan(IFile file){
- String ext = file.getFileExtension();
- String content = null;
- try {
- content = FileUtil.readStream(file.getContents());
- } catch (CoreException e) {
- SeamCorePlugin.getPluginLog().logError(e);
- return;
- }
- if(JAVA_EXT.equalsIgnoreCase(ext)){
- scanJava(file, content);
- } else if(XML_EXT.equalsIgnoreCase(ext) || XHTML_EXT.equalsIgnoreCase(ext) ||
JSP_EXT.equalsIgnoreCase(ext))
- scanDOM(file, content);
- else if(PROPERTIES_EXT.equalsIgnoreCase(ext))
- scanProperties(file, content);
- }
-
- ArrayList<IPath> files = new ArrayList<IPath>();
-
- private void findDeclarations() throws CoreException{
- files.clear();
- findDeclarations(component, true);
-
- if(declarationFile == null)
- return;
-
- projectsSet = new SeamProjectsSet(declarationFile.getProject());
-
- IProject[] projects = projectsSet.getAllProjects();
- for (IProject project : projects) {
- ISeamProject seamProject = SeamCorePlugin.getSeamProject(project, true);
- if(seamProject != null){
- ISeamComponent comp = seamProject.getComponent(component.getName());
- if(comp != null)
- findDeclarations(comp, false);
- }
- }
- }
-
- private void findDeclarations(ISeamComponent component, boolean force) throws
CoreException{
- if(component.getJavaDeclaration() != null){
- if(!files.contains(component.getJavaDeclaration().getResource().getFullPath())){
- files.add(component.getJavaDeclaration().getResource().getFullPath());
- renameJavaDeclaration(component.getJavaDeclaration());
- }else if(force)
- renameJavaDeclaration(component.getJavaDeclaration());
- }
-
- Set<ISeamXmlComponentDeclaration> xmlDecls = component.getXmlDeclarations();
-
- for(ISeamXmlComponentDeclaration xmlDecl : xmlDecls){
- if(!files.contains(xmlDecl.getResource().getFullPath())){
- files.add(xmlDecl.getResource().getFullPath());
- renameXMLDeclaration(xmlDecl);
- }else if(force)
- renameXMLDeclaration(xmlDecl);
- }
- }
-
- private void renameJavaDeclaration(ISeamJavaComponentDeclaration javaDecl) throws
CoreException{
- IFile file = (IFile)javaDecl.getResource();
- if(file != null && !coreHelper.isJar(javaDecl)){
- ITextSourceReference location =
((SeamComponentDeclaration)javaDecl).getLocationFor(ISeamXmlComponentDeclaration.NAME);
- if(location != null && !isBadLocation(location)){
- TextFileChange change = getChange(file);
- TextEdit edit = new ReplaceEdit(location.getStartPosition(), location.getLength(),
"\""+newName+"\""); //$NON-NLS-1$ //$NON-NLS-2$
- change.addEdit(edit);
- }
- }
- declarationFile = file;
- }
-
- private void renameXMLDeclaration(ISeamXmlComponentDeclaration xmlDecl){
- IFile file = (IFile)xmlDecl.getResource();
- if(file != null && !coreHelper.isJar(xmlDecl)){
- ITextSourceReference location =
((SeamComponentDeclaration)xmlDecl).getLocationFor(ISeamXmlComponentDeclaration.NAME);
- if(location != null && !isBadLocation(location))
- changeXMLNode(location, file);
- }
- if(declarationFile == null)
- declarationFile = file;
- }
-
- private void scanJava(IFile file, String content){
- try {
- FastJavaPartitionScanner scaner = new FastJavaPartitionScanner();
- Document document = new Document(content);
- scaner.setRange(document, 0, document.getLength());
- IToken token = scaner.nextToken();
- while(token!=null && token!=Token.EOF) {
- if(IJavaPartitions.JAVA_STRING.equals(token.getData())) {
- int length = scaner.getTokenLength();
- int offset = scaner.getTokenOffset();
- String value = document.get(offset, length);
- if(value.indexOf('{')>-1) {
- scanString(file, value, offset);
- }
- }
- token = scaner.nextToken();
- }
- } catch (BadLocationException e) {
- SeamCorePlugin.getDefault().logError(e);
- }
- }
-
- private void scanDOM(IFile file, String content){
- IModelManager manager = StructuredModelManager.getModelManager();
- if(manager == null) {
- return;
- }
- IStructuredModel model = null;
- try {
- model = manager.getModelForRead(file);
- if (model instanceof IDOMModel) {
- IDOMModel domModel = (IDOMModel) model;
- IDOMDocument document = domModel.getDocument();
- scanChildNodes(file, document);
- }
- } catch (CoreException e) {
- SeamCorePlugin.getDefault().logError(e);
- } catch (IOException e) {
- SeamCorePlugin.getDefault().logError(e);
- } finally {
- if (model != null) {
- model.releaseFromRead();
- }
- }
- }
-
- private void scanChildNodes(IFile file, Node parent) {
- NodeList children = parent.getChildNodes();
- for(int i=0; i<children.getLength(); i++) {
- Node curentValidatedNode = children.item(i);
- if(Node.ELEMENT_NODE == curentValidatedNode.getNodeType()) {
- scanNodeContent(file,
((IDOMNode)curentValidatedNode).getFirstStructuredDocumentRegion(),
DOMRegionContext.XML_TAG_ATTRIBUTE_VALUE);
- } else if(Node.TEXT_NODE == curentValidatedNode.getNodeType()) {
- scanNodeContent(file,
((IDOMNode)curentValidatedNode).getFirstStructuredDocumentRegion(),
DOMRegionContext.XML_CONTENT);
- }
- scanChildNodes(file, curentValidatedNode);
- }
- }
-
- private void scanNodeContent(IFile file, IStructuredDocumentRegion node, String
regionType) {
- ITextRegionList regions = node.getRegions();
- for(int i=0; i<regions.size(); i++) {
- ITextRegion region = regions.get(i);
- if(region.getType() == regionType) {
- String text = node.getFullText(region);
- if(text.indexOf("{")>-1) { //$NON-NLS-1$
- int offset = node.getStartOffset() + region.getStart();
- scanString(file, text, offset);
- }
- }
- }
- }
-
- private CompositeChange rootChange;
- private TextFileChange lastChange;
-
- // lets collect all changes for the same files in one MultiTextEdit
- private TextFileChange getChange(IFile file){
- if(lastChange != null && lastChange.getFile().equals(file))
- return lastChange;
-
- for(int i=0; i < rootChange.getChildren().length; i++){
- TextFileChange change = (TextFileChange)rootChange.getChildren()[i];
- if(change.getFile().equals(file)){
- lastChange = change;
- return lastChange;
- }
- }
- lastChange = new TextFileChange(file.getName(), file);
- MultiTextEdit root = new MultiTextEdit();
- lastChange.setEdit(root);
- rootChange.add(lastChange);
-
- return lastChange;
- }
-
- // looking for component references in EL
- private void scanString(IFile file, String string, int offset) {
- int startEl = string.indexOf("#{"); //$NON-NLS-1$
- if(startEl>-1) {
- ELParser parser = ELParserUtil.getJbossFactory().createParser();
- ELModel model = parser.parse(string);
- for (ELInstance instance : model.getInstances()) {
- for(ELInvocationExpression ie : instance.getExpression().getInvocations()){
- ELPropertyInvocation pi = findComponentReference(ie);
- if(pi != null){
- TextFileChange change = getChange(file);
- TextEdit edit = new ReplaceEdit(offset+pi.getStartPosition(),
pi.getName().getStart()+pi.getName().getLength()-pi.getStartPosition(), newName);
- change.addEdit(edit);
- }
- }
- }
- }
- }
-
- private ELPropertyInvocation findComponentReference(ELInvocationExpression
invocationExpression){
- ELInvocationExpression invExp = invocationExpression;
- while(invExp != null){
- if(invExp instanceof ELPropertyInvocation){
- if(((ELPropertyInvocation)invExp).getQualifiedName() != null &&
((ELPropertyInvocation)invExp).getQualifiedName().equals(component.getName()))
- return (ELPropertyInvocation)invExp;
- else
- invExp = invExp.getLeft();
-
- }else{
- invExp = invExp.getLeft();
- }
- }
- return null;
- }
-
- private void scanProperties(IFile file, String content){
- scanString(file, content, 0);
-
- if(!file.getName().equals(SEAM_PROPERTIES_FILE))
- return;
-
- StringTokenizer tokenizer = new StringTokenizer(content, "#= \t\r\n\f",
true); //$NON-NLS-1$
-
- String lastToken = "\n"; //$NON-NLS-1$
- int offset = 0;
- boolean comment = false;
- boolean key = true;
-
- while(tokenizer.hasMoreTokens()){
- String token = tokenizer.nextToken("#= \t\r\n\f"); //$NON-NLS-1$
- if(token.equals("\r")) //$NON-NLS-1$
- token = "\n"; //$NON-NLS-1$
-
- if(token.equals("#") && lastToken.equals("\n"))
//$NON-NLS-1$ //$NON-NLS-2$
- comment = true;
- else if(token.equals("\n") && comment) //$NON-NLS-1$
- comment = false;
-
- if(!comment){
- if(!token.equals("\n") && lastToken.equals("\n"))
//$NON-NLS-1$ //$NON-NLS-2$
- key = true;
- else if(key && (token.equals("=") || token.equals(" ")))
//$NON-NLS-1$ //$NON-NLS-2$
- key = false;
-
- if(key && token.startsWith(component.getName())){
- String changeText = token.replaceFirst(component.getName(), newName);
- TextFileChange change = getChange(file);
- TextEdit edit = new ReplaceEdit(offset, token.length(), changeText);
- change.addEdit(edit);
- }
- }
-
- lastToken = token;
- offset += token.length();
- }
- }
-
/*
* (non-Javadoc)
* @see
org.eclipse.ltk.core.refactoring.participants.RefactoringProcessor#checkFinalConditions(org.eclipse.core.runtime.IProgressMonitor,
org.eclipse.ltk.core.refactoring.participants.CheckConditionsContext)
@@ -425,156 +80,14 @@
@Override
public Change createChange(IProgressMonitor pm) throws CoreException,
OperationCanceledException {
- rootChange = new
CompositeChange(SeamCoreMessages.RENAME_SEAM_COMPONENT_PROCESSOR_RENAME_SEAM_COMPONENT);
+ rootChange = new
CompositeChange(SeamCoreMessages.RENAME_SEAM_COMPONENT_PROCESSOR_TITLE);
- findDeclarations();
+ renameComponent(component);
- findAnnotations();
-
- findELReferences();
-
return rootChange;
}
- private void findAnnotations(){
- if(declarationFile == null)
- return;
-
- ISeamProject seamProject = SeamCorePlugin.getSeamProject(declarationFile.getProject(),
true);
- files.clear();
- findAnnotations(seamProject, true);
-
- IProject[] projects = projectsSet.getAllProjects();
- for (IProject project : projects) {
- ISeamProject sProject = SeamCorePlugin.getSeamProject(project, true);
- if(sProject != null){
- findAnnotations(sProject, false);
- }
- }
-
- }
-
- private void findAnnotations(ISeamProject seamProject, boolean force){
- // find @In annotations
- Set<IBijectedAttribute> inSet =
seamProject.getBijectedAttributesByName(component.getName(), BijectedAttributeType.IN);
-
- for(IBijectedAttribute inAtt : inSet){
- ITextSourceReference location =
inAtt.getLocationFor(SeamAnnotations.IN_ANNOTATION_TYPE);
- if(location != null){
- if(!files.contains(inAtt.getResource().getFullPath())){
- files.add(inAtt.getResource().getFullPath());
- changeAnnotation(location, (IFile)inAtt.getResource());
- }else if(force)
- changeAnnotation(location, (IFile)inAtt.getResource());
- }
- }
-
- // find @Factory annotations
- Set<ISeamFactory> factorySet =
seamProject.getFactoriesByName(component.getName());
-
- for(ISeamFactory factory : factorySet){
- IFile file = (IFile)factory.getResource();
- if(file.getFileExtension().equalsIgnoreCase(JAVA_EXT)){
- ITextSourceReference location =
factory.getLocationFor(SeamAnnotations.FACTORY_ANNOTATION_TYPE);
- if(location != null){
- if(!files.contains(file.getFullPath())){
- files.add(file.getFullPath());
- changeAnnotation(location, file);
- }else if(force)
- changeAnnotation(location, file);
- }
- }else{
- ITextSourceReference location =
factory.getLocationFor(ISeamXmlComponentDeclaration.NAME);
- if(location != null){
- if(!files.contains(file.getFullPath())){
- files.add(file.getFullPath());
- changeXMLNode(location, file);
- }else if(force)
- changeXMLNode(location, file);
- }
- }
- }
- }
-
- private boolean isBadLocation(ITextSourceReference location){
- return location.getStartPosition() == 0 && location.getLength() == 0;
- }
-
- private void changeXMLNode(ITextSourceReference location, IFile file){
- if(isBadLocation(location))
- return;
-
- String content = null;
- try {
- content = FileUtil.readStream(file.getContents());
- } catch (CoreException e) {
- SeamCorePlugin.getPluginLog().logError(e);
- return;
- }
-
- TextFileChange change = getChange(file);
-
- String text = content.substring(location.getStartPosition(),
location.getStartPosition()+location.getLength());
- if(text.startsWith("<")){ //$NON-NLS-1$
- int position = text.lastIndexOf("/>"); //$NON-NLS-1$
- if(position < 0){
- position = text.lastIndexOf(">"); //$NON-NLS-1$
- }
-
- TextEdit edit = new ReplaceEdit(location.getStartPosition()+position, 0, "
name=\""+newName+"\""); //$NON-NLS-1$ //$NON-NLS-2$
- change.addEdit(edit);
- }else{
- TextEdit edit = new ReplaceEdit(location.getStartPosition(), location.getLength(),
newName);
- change.addEdit(edit);
- }
- }
-
- private void changeAnnotation(ITextSourceReference location, IFile file){
- if(isBadLocation(location))
- return;
- String content = null;
- try {
- content = FileUtil.readStream(file.getContents());
- } catch (CoreException e) {
- SeamCorePlugin.getPluginLog().logError(e);
- return;
- }
-
- TextFileChange change = getChange(file);
-
- String text = content.substring(location.getStartPosition(),
location.getStartPosition()+location.getLength());
- int openBracket = text.indexOf("("); //$NON-NLS-1$
- if(openBracket > 0){
- int closeBracket = text.indexOf(")", openBracket); //$NON-NLS-1$
- int openQuote = text.indexOf("\"", openBracket); //$NON-NLS-1$
- int equals = text.indexOf("=", openBracket); //$NON-NLS-1$
- int value = text.indexOf("value", openBracket); //$NON-NLS-1$
-
- if(closeBracket == openBracket+1){ // empty brackets
- String newText = "\""+newName+"\""; //$NON-NLS-1$
//$NON-NLS-2$
- TextEdit edit = new ReplaceEdit(location.getStartPosition()+openBracket+1, 0,
newText);
- change.addEdit(edit);
- }else if(value > 0){ // construction value="name" found so change name
- String newText = text.replace(component.getName(), newName);
- TextEdit edit = new ReplaceEdit(location.getStartPosition(), location.getLength(),
newText);
- change.addEdit(edit);
- }else if(equals > 0){ // other parameters are found
- String newText = "value=\""+newName+"\",";
//$NON-NLS-1$ //$NON-NLS-2$
- TextEdit edit = new ReplaceEdit(location.getStartPosition()+openBracket+1, 0,
newText);
- change.addEdit(edit);
- }else{ // other cases
- String newText = text.replace(component.getName(), newName);
- TextEdit edit = new ReplaceEdit(location.getStartPosition(), location.getLength(),
newText);
- change.addEdit(edit);
- }
- }else{
- String newText = "(\""+newName+"\")"; //$NON-NLS-1$
//$NON-NLS-2$
- TextEdit edit = new ReplaceEdit(location.getStartPosition()+location.getLength(), 0,
newText);
- change.addEdit(edit);
- }
- }
-
/*
* (non-Javadoc)
* @see
org.eclipse.ltk.core.refactoring.participants.RefactoringProcessor#getElements()
@@ -599,7 +112,7 @@
*/
@Override
public String getProcessorName() {
- return SeamCoreMessages.RENAME_SEAM_COMPONENT_PROCESSOR_RENAME_SEAM_COMPONENT;
+ return SeamCoreMessages.RENAME_SEAM_COMPONENT_PROCESSOR_TITLE;
}
/*
Added:
trunk/seam/plugins/org.jboss.tools.seam.core/src/org/jboss/tools/seam/internal/core/refactoring/RenameSeamContextVariableProcessor.java
===================================================================
---
trunk/seam/plugins/org.jboss.tools.seam.core/src/org/jboss/tools/seam/internal/core/refactoring/RenameSeamContextVariableProcessor.java
(rev 0)
+++
trunk/seam/plugins/org.jboss.tools.seam.core/src/org/jboss/tools/seam/internal/core/refactoring/RenameSeamContextVariableProcessor.java 2009-06-04
18:22:36 UTC (rev 15716)
@@ -0,0 +1,153 @@
+ /*******************************************************************************
+ * Copyright (c) 2008 Red Hat, Inc.
+ * Distributed under license by Red Hat, Inc. All rights reserved.
+ * This program is 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:
+ * Red Hat, Inc. - initial API and implementation
+ ******************************************************************************/
+package org.jboss.tools.seam.internal.core.refactoring;
+
+import java.util.Set;
+
+import org.eclipse.core.resources.IFile;
+import org.eclipse.core.resources.IProject;
+import org.eclipse.core.resources.IResource;
+import org.eclipse.core.runtime.CoreException;
+import org.eclipse.core.runtime.IProgressMonitor;
+import org.eclipse.core.runtime.OperationCanceledException;
+import org.eclipse.ltk.core.refactoring.Change;
+import org.eclipse.ltk.core.refactoring.CompositeChange;
+import org.eclipse.ltk.core.refactoring.RefactoringStatus;
+import org.eclipse.ltk.core.refactoring.participants.CheckConditionsContext;
+import org.eclipse.ltk.core.refactoring.participants.RefactoringParticipant;
+import org.eclipse.ltk.core.refactoring.participants.SharableParticipants;
+import org.jboss.tools.seam.core.ISeamComponent;
+import org.jboss.tools.seam.core.ISeamJavaComponentDeclaration;
+import org.jboss.tools.seam.core.ISeamProject;
+import org.jboss.tools.seam.core.SeamCoreMessages;
+import org.jboss.tools.seam.core.SeamCorePlugin;
+
+/**
+ * @author Daniel Azarov
+ */
+public class RenameSeamContextVariableProcessor extends SeamRenameProcessor {
+
+ /**
+ * @param component Renamed component
+ */
+ public RenameSeamContextVariableProcessor(IFile file, String oldName) {
+ super();
+ declarationFile = file;
+ setOldName(oldName);
+ }
+
+ /*
+ * (non-Javadoc)
+ * @see
org.eclipse.ltk.core.refactoring.participants.RefactoringProcessor#checkFinalConditions(org.eclipse.core.runtime.IProgressMonitor,
org.eclipse.ltk.core.refactoring.participants.CheckConditionsContext)
+ */
+ @Override
+ public RefactoringStatus checkFinalConditions(IProgressMonitor pm,
+ CheckConditionsContext context) throws CoreException,
+ OperationCanceledException {
+ return new RefactoringStatus();
+ }
+
+ /*
+ * (non-Javadoc)
+ * @see
org.eclipse.ltk.core.refactoring.participants.RefactoringProcessor#checkInitialConditions(org.eclipse.core.runtime.IProgressMonitor)
+ */
+ @Override
+ public RefactoringStatus checkInitialConditions(IProgressMonitor pm)
+ throws CoreException, OperationCanceledException {
+ RefactoringStatus result = new RefactoringStatus();
+// if(getNewName()==null) {
+// result.addFatalError(SeamCoreMessages.RENAME_SEAM_COMPONENT_PROCESSOR_THIS_IS_NOT_A_SEAM_COMPONENT);
+// }
+ return result;
+ }
+
+ /*
+ * (non-Javadoc)
+ * @see
org.eclipse.ltk.core.refactoring.participants.RefactoringProcessor#createChange(org.eclipse.core.runtime.IProgressMonitor)
+ */
+ @Override
+ public Change createChange(IProgressMonitor pm) throws CoreException,
+ OperationCanceledException {
+ rootChange = new
CompositeChange(SeamCoreMessages.RENAME_SEAM_CONTEXT_VARIABLE_PROCESSOR_TITLE);
+
+ ISeamComponent component = checkComponent();
+ if(component != null)
+ renameComponent(component);
+
+ return rootChange;
+ }
+
+ private ISeamComponent checkComponent(){
+ IProject project = declarationFile.getProject();
+ ISeamProject seamProject = SeamCorePlugin.getSeamProject(project, true);
+ if (seamProject != null) {
+ Set<ISeamComponent> components =
seamProject.getComponentsByPath(declarationFile.getFullPath());
+ for(ISeamComponent component : components){
+ ISeamJavaComponentDeclaration declaration = component.getJavaDeclaration();
+ if(declaration != null){
+ IResource resource = declaration.getResource();
+ if(resource != null &&
resource.getFullPath().equals(declarationFile.getFullPath())){
+ if(declaration.getName().equals(component.getName())){
+ return component;
+ }
+ }
+ }
+ }
+ }
+ return null;
+ }
+
+ /*
+ * (non-Javadoc)
+ * @see
org.eclipse.ltk.core.refactoring.participants.RefactoringProcessor#getElements()
+ */
+ @Override
+ public Object[] getElements() {
+ return new String[]{getNewName()};
+ }
+
+ /*
+ * (non-Javadoc)
+ * @see
org.eclipse.ltk.core.refactoring.participants.RefactoringProcessor#getIdentifier()
+ */
+ @Override
+ public String getIdentifier() {
+ return getClass().getName();
+ }
+
+ /*
+ * (non-Javadoc)
+ * @see
org.eclipse.ltk.core.refactoring.participants.RefactoringProcessor#getProcessorName()
+ */
+ @Override
+ public String getProcessorName() {
+ return SeamCoreMessages.RENAME_SEAM_CONTEXT_VARIABLE_PROCESSOR_TITLE;
+ }
+
+ /*
+ * (non-Javadoc)
+ * @see
org.eclipse.ltk.core.refactoring.participants.RefactoringProcessor#isApplicable()
+ */
+ @Override
+ public boolean isApplicable() throws CoreException {
+ return getNewName()!=null;
+ }
+
+ /*
+ * (non-Javadoc)
+ * @see
org.eclipse.ltk.core.refactoring.participants.RefactoringProcessor#loadParticipants(org.eclipse.ltk.core.refactoring.RefactoringStatus,
org.eclipse.ltk.core.refactoring.participants.SharableParticipants)
+ */
+ @Override
+ public RefactoringParticipant[] loadParticipants(RefactoringStatus status,
+ SharableParticipants sharedParticipants) throws CoreException {
+ return null;
+ }
+}
\ No newline at end of file
Property changes on:
trunk/seam/plugins/org.jboss.tools.seam.core/src/org/jboss/tools/seam/internal/core/refactoring/RenameSeamContextVariableProcessor.java
___________________________________________________________________
Name: svn:mime-type
+ text/plain
Added:
trunk/seam/plugins/org.jboss.tools.seam.core/src/org/jboss/tools/seam/internal/core/refactoring/SeamRenameProcessor.java
===================================================================
---
trunk/seam/plugins/org.jboss.tools.seam.core/src/org/jboss/tools/seam/internal/core/refactoring/SeamRenameProcessor.java
(rev 0)
+++
trunk/seam/plugins/org.jboss.tools.seam.core/src/org/jboss/tools/seam/internal/core/refactoring/SeamRenameProcessor.java 2009-06-04
18:22:36 UTC (rev 15716)
@@ -0,0 +1,520 @@
+package org.jboss.tools.seam.internal.core.refactoring;
+
+import java.io.IOException;
+import java.util.ArrayList;
+import java.util.Set;
+import java.util.StringTokenizer;
+
+import org.eclipse.core.resources.IContainer;
+import org.eclipse.core.resources.IFile;
+import org.eclipse.core.resources.IFolder;
+import org.eclipse.core.resources.IProject;
+import org.eclipse.core.resources.IResource;
+import org.eclipse.core.runtime.CoreException;
+import org.eclipse.core.runtime.IPath;
+import org.eclipse.jdt.internal.ui.text.FastJavaPartitionScanner;
+import org.eclipse.jdt.ui.text.IJavaPartitions;
+import org.eclipse.jface.text.BadLocationException;
+import org.eclipse.jface.text.Document;
+import org.eclipse.jface.text.rules.IToken;
+import org.eclipse.jface.text.rules.Token;
+import org.eclipse.ltk.core.refactoring.CompositeChange;
+import org.eclipse.ltk.core.refactoring.TextFileChange;
+import org.eclipse.ltk.core.refactoring.participants.RenameProcessor;
+import org.eclipse.text.edits.MultiTextEdit;
+import org.eclipse.text.edits.ReplaceEdit;
+import org.eclipse.text.edits.TextEdit;
+import org.eclipse.wst.sse.core.StructuredModelManager;
+import org.eclipse.wst.sse.core.internal.provisional.IModelManager;
+import org.eclipse.wst.sse.core.internal.provisional.IStructuredModel;
+import org.eclipse.wst.sse.core.internal.provisional.text.IStructuredDocumentRegion;
+import org.eclipse.wst.sse.core.internal.provisional.text.ITextRegion;
+import org.eclipse.wst.sse.core.internal.provisional.text.ITextRegionList;
+import org.eclipse.wst.xml.core.internal.provisional.document.IDOMDocument;
+import org.eclipse.wst.xml.core.internal.provisional.document.IDOMModel;
+import org.eclipse.wst.xml.core.internal.provisional.document.IDOMNode;
+import org.eclipse.wst.xml.core.internal.regions.DOMRegionContext;
+import org.jboss.tools.common.el.core.model.ELInstance;
+import org.jboss.tools.common.el.core.model.ELInvocationExpression;
+import org.jboss.tools.common.el.core.model.ELModel;
+import org.jboss.tools.common.el.core.model.ELPropertyInvocation;
+import org.jboss.tools.common.el.core.parser.ELParser;
+import org.jboss.tools.common.el.core.parser.ELParserUtil;
+import org.jboss.tools.common.model.project.ext.ITextSourceReference;
+import org.jboss.tools.common.util.FileUtil;
+import org.jboss.tools.seam.core.BijectedAttributeType;
+import org.jboss.tools.seam.core.IBijectedAttribute;
+import org.jboss.tools.seam.core.ISeamComponent;
+import org.jboss.tools.seam.core.ISeamFactory;
+import org.jboss.tools.seam.core.ISeamJavaComponentDeclaration;
+import org.jboss.tools.seam.core.ISeamProject;
+import org.jboss.tools.seam.core.ISeamXmlComponentDeclaration;
+import org.jboss.tools.seam.core.SeamCorePlugin;
+import org.jboss.tools.seam.core.SeamProjectsSet;
+import org.jboss.tools.seam.internal.core.SeamComponentDeclaration;
+import org.jboss.tools.seam.internal.core.scanner.java.SeamAnnotations;
+import org.jboss.tools.seam.internal.core.validation.SeamContextValidationHelper;
+import org.w3c.dom.Node;
+import org.w3c.dom.NodeList;
+
+public abstract class SeamRenameProcessor extends RenameProcessor {
+ protected static final String JAVA_EXT = "java"; //$NON-NLS-1$
+ protected static final String XML_EXT = "xml"; //$NON-NLS-1$
+ protected static final String XHTML_EXT = "xhtml"; //$NON-NLS-1$
+ protected static final String JSP_EXT = "jsp"; //$NON-NLS-1$
+ protected static final String PROPERTIES_EXT = "properties"; //$NON-NLS-1$
+
+ protected static final String SEAM_PROPERTIES_FILE = "seam.properties";
//$NON-NLS-1$
+
+ private SeamContextValidationHelper coreHelper = new SeamContextValidationHelper();
+
+ protected CompositeChange rootChange;
+ protected TextFileChange lastChange;
+ protected IFile declarationFile=null;
+ protected SeamProjectsSet projectsSet;
+
+ private String newName;
+ private String oldName;
+
+ public void setNewName(String newName){
+ this.newName = newName;
+ }
+
+ protected String getNewName(){
+ return newName;
+ }
+
+ protected void setOldName(String oldName){
+ this.oldName = oldName;
+ }
+
+ public String getOldName(){
+ return oldName;
+ }
+
+ // lets collect all changes for the same files in one MultiTextEdit
+ protected TextFileChange getChange(IFile file){
+ if(lastChange != null && lastChange.getFile().equals(file))
+ return lastChange;
+
+ for(int i=0; i < rootChange.getChildren().length; i++){
+ TextFileChange change = (TextFileChange)rootChange.getChildren()[i];
+ if(change.getFile().equals(file)){
+ lastChange = change;
+ return lastChange;
+ }
+ }
+ lastChange = new TextFileChange(file.getName(), file);
+ MultiTextEdit root = new MultiTextEdit();
+ lastChange.setEdit(root);
+ rootChange.add(lastChange);
+
+ return lastChange;
+ }
+
+ ArrayList<IPath> files = new ArrayList<IPath>();
+
+ protected void findDeclarations(ISeamComponent component) throws CoreException{
+ files.clear();
+ findDeclarations(component, true);
+
+ if(declarationFile == null)
+ return;
+
+ projectsSet = new SeamProjectsSet(declarationFile.getProject());
+
+ IProject[] projects = projectsSet.getAllProjects();
+ for (IProject project : projects) {
+ ISeamProject seamProject = SeamCorePlugin.getSeamProject(project, true);
+ if(seamProject != null){
+ ISeamComponent comp = seamProject.getComponent(getOldName());
+ if(comp != null)
+ findDeclarations(comp, false);
+ }
+ }
+ }
+
+ protected void findAnnotations(){
+ if(declarationFile == null)
+ return;
+
+ ISeamProject seamProject = SeamCorePlugin.getSeamProject(declarationFile.getProject(),
true);
+ files.clear();
+ findAnnotations(seamProject, true);
+
+ IProject[] projects = projectsSet.getAllProjects();
+ for (IProject project : projects) {
+ ISeamProject sProject = SeamCorePlugin.getSeamProject(project, true);
+ if(sProject != null){
+ findAnnotations(sProject, false);
+ }
+ }
+
+ }
+
+ private void findAnnotations(ISeamProject seamProject, boolean force){
+ // find @In annotations
+ Set<IBijectedAttribute> inSet =
seamProject.getBijectedAttributesByName(getOldName(), BijectedAttributeType.IN);
+
+ for(IBijectedAttribute inAtt : inSet){
+ ITextSourceReference location =
inAtt.getLocationFor(SeamAnnotations.IN_ANNOTATION_TYPE);
+ if(location != null){
+ if(!files.contains(inAtt.getResource().getFullPath())){
+ files.add(inAtt.getResource().getFullPath());
+ changeAnnotation(location, (IFile)inAtt.getResource());
+ }else if(force)
+ changeAnnotation(location, (IFile)inAtt.getResource());
+ }
+ }
+
+ // find @Factory annotations
+ Set<ISeamFactory> factorySet = seamProject.getFactoriesByName(getOldName());
+
+ for(ISeamFactory factory : factorySet){
+ IFile file = (IFile)factory.getResource();
+ if(file.getFileExtension().equalsIgnoreCase(JAVA_EXT)){
+ ITextSourceReference location =
factory.getLocationFor(SeamAnnotations.FACTORY_ANNOTATION_TYPE);
+ if(location != null){
+ if(!files.contains(file.getFullPath())){
+ files.add(file.getFullPath());
+ changeAnnotation(location, file);
+ }else if(force)
+ changeAnnotation(location, file);
+ }
+ }else{
+ ITextSourceReference location =
factory.getLocationFor(ISeamXmlComponentDeclaration.NAME);
+ if(location != null){
+ if(!files.contains(file.getFullPath())){
+ files.add(file.getFullPath());
+ changeXMLNode(location, file);
+ }else if(force)
+ changeXMLNode(location, file);
+ }
+ }
+ }
+ }
+
+ private boolean isBadLocation(ITextSourceReference location){
+ return location.getStartPosition() == 0 && location.getLength() == 0;
+ }
+
+ private void changeXMLNode(ITextSourceReference location, IFile file){
+ if(isBadLocation(location))
+ return;
+
+ String content = null;
+ try {
+ content = FileUtil.readStream(file.getContents());
+ } catch (CoreException e) {
+ SeamCorePlugin.getPluginLog().logError(e);
+ return;
+ }
+
+ TextFileChange change = getChange(file);
+
+ String text = content.substring(location.getStartPosition(),
location.getStartPosition()+location.getLength());
+ if(text.startsWith("<")){ //$NON-NLS-1$
+ int position = text.lastIndexOf("/>"); //$NON-NLS-1$
+ if(position < 0){
+ position = text.lastIndexOf(">"); //$NON-NLS-1$
+ }
+
+ TextEdit edit = new ReplaceEdit(location.getStartPosition()+position, 0, "
name=\""+getNewName()+"\""); //$NON-NLS-1$ //$NON-NLS-2$
+ change.addEdit(edit);
+ }else{
+ TextEdit edit = new ReplaceEdit(location.getStartPosition(), location.getLength(),
getNewName());
+ change.addEdit(edit);
+ }
+ }
+
+ private void changeAnnotation(ITextSourceReference location, IFile file){
+ if(isBadLocation(location))
+ return;
+
+ String content = null;
+ try {
+ content = FileUtil.readStream(file.getContents());
+ } catch (CoreException e) {
+ SeamCorePlugin.getPluginLog().logError(e);
+ return;
+ }
+
+ TextFileChange change = getChange(file);
+
+ String text = content.substring(location.getStartPosition(),
location.getStartPosition()+location.getLength());
+ int openBracket = text.indexOf("("); //$NON-NLS-1$
+ if(openBracket > 0){
+ int closeBracket = text.indexOf(")", openBracket); //$NON-NLS-1$
+ int openQuote = text.indexOf("\"", openBracket); //$NON-NLS-1$
+ int equals = text.indexOf("=", openBracket); //$NON-NLS-1$
+ int value = text.indexOf("value", openBracket); //$NON-NLS-1$
+
+ if(closeBracket == openBracket+1){ // empty brackets
+ String newText = "\""+getNewName()+"\""; //$NON-NLS-1$
//$NON-NLS-2$
+ TextEdit edit = new ReplaceEdit(location.getStartPosition()+openBracket+1, 0,
newText);
+ change.addEdit(edit);
+ }else if(value > 0){ // construction value="name" found so change name
+ String newText = text.replace(getOldName(), getNewName());
+ TextEdit edit = new ReplaceEdit(location.getStartPosition(), location.getLength(),
newText);
+ change.addEdit(edit);
+ }else if(equals > 0){ // other parameters are found
+ String newText = "value=\""+getNewName()+"\",";
//$NON-NLS-1$ //$NON-NLS-2$
+ TextEdit edit = new ReplaceEdit(location.getStartPosition()+openBracket+1, 0,
newText);
+ change.addEdit(edit);
+ }else{ // other cases
+ String newText = text.replace(getOldName(), getNewName());
+ TextEdit edit = new ReplaceEdit(location.getStartPosition(), location.getLength(),
newText);
+ change.addEdit(edit);
+ }
+ }else{
+ String newText = "(\""+getNewName()+"\")"; //$NON-NLS-1$
//$NON-NLS-2$
+ TextEdit edit = new ReplaceEdit(location.getStartPosition()+location.getLength(), 0,
newText);
+ change.addEdit(edit);
+ }
+ }
+
+
+ private void findDeclarations(ISeamComponent component, boolean force) throws
CoreException{
+ if(component.getJavaDeclaration() != null){
+ if(!files.contains(component.getJavaDeclaration().getResource().getFullPath())){
+ files.add(component.getJavaDeclaration().getResource().getFullPath());
+ renameJavaDeclaration(component.getJavaDeclaration());
+ }else if(force)
+ renameJavaDeclaration(component.getJavaDeclaration());
+ }
+
+ Set<ISeamXmlComponentDeclaration> xmlDecls = component.getXmlDeclarations();
+
+ for(ISeamXmlComponentDeclaration xmlDecl : xmlDecls){
+ if(!files.contains(xmlDecl.getResource().getFullPath())){
+ files.add(xmlDecl.getResource().getFullPath());
+ renameXMLDeclaration(xmlDecl);
+ }else if(force)
+ renameXMLDeclaration(xmlDecl);
+ }
+ }
+
+ private void renameJavaDeclaration(ISeamJavaComponentDeclaration javaDecl) throws
CoreException{
+ IFile file = (IFile)javaDecl.getResource();
+ if(file != null && !coreHelper.isJar(javaDecl)){
+ ITextSourceReference location =
((SeamComponentDeclaration)javaDecl).getLocationFor(ISeamXmlComponentDeclaration.NAME);
+ if(location != null && !isBadLocation(location)){
+ TextFileChange change = getChange(file);
+ TextEdit edit = new ReplaceEdit(location.getStartPosition(), location.getLength(),
"\""+getNewName()+"\""); //$NON-NLS-1$ //$NON-NLS-2$
+ change.addEdit(edit);
+ }
+ }
+ declarationFile = file;
+ }
+
+ private void renameXMLDeclaration(ISeamXmlComponentDeclaration xmlDecl){
+ IFile file = (IFile)xmlDecl.getResource();
+ if(file != null && !coreHelper.isJar(xmlDecl)){
+ ITextSourceReference location =
((SeamComponentDeclaration)xmlDecl).getLocationFor(ISeamXmlComponentDeclaration.NAME);
+ if(location != null && !isBadLocation(location))
+ changeXMLNode(location, file);
+ }
+ if(declarationFile == null)
+ declarationFile = file;
+ }
+
+
+ // we need to find references in .java .xml .xhtml .jsp .properties files
+ protected void findELReferences(){
+ if(declarationFile == null)
+ return;
+
+ IProject[] projects = projectsSet.getAllProjects();
+ for (IProject project : projects) {
+ scan(project);
+ }
+ }
+
+ private void scan(IContainer container){
+ try{
+ for(IResource resource : container.members()){
+ if(resource instanceof IFolder)
+ scan((IFolder) resource);
+ else if(resource instanceof IFile)
+ scan((IFile) resource);
+ }
+ }catch(CoreException ex){
+ SeamCorePlugin.getDefault().logError(ex);
+ }
+ }
+
+ private void scan(IFile file){
+ String ext = file.getFileExtension();
+ String content = null;
+ try {
+ content = FileUtil.readStream(file.getContents());
+ } catch (CoreException e) {
+ SeamCorePlugin.getPluginLog().logError(e);
+ return;
+ }
+ if(JAVA_EXT.equalsIgnoreCase(ext)){
+ scanJava(file, content);
+ } else if(XML_EXT.equalsIgnoreCase(ext) || XHTML_EXT.equalsIgnoreCase(ext) ||
JSP_EXT.equalsIgnoreCase(ext))
+ scanDOM(file, content);
+ else if(PROPERTIES_EXT.equalsIgnoreCase(ext))
+ scanProperties(file, content);
+ }
+
+ private void scanJava(IFile file, String content){
+ try {
+ FastJavaPartitionScanner scaner = new FastJavaPartitionScanner();
+ Document document = new Document(content);
+ scaner.setRange(document, 0, document.getLength());
+ IToken token = scaner.nextToken();
+ while(token!=null && token!=Token.EOF) {
+ if(IJavaPartitions.JAVA_STRING.equals(token.getData())) {
+ int length = scaner.getTokenLength();
+ int offset = scaner.getTokenOffset();
+ String value = document.get(offset, length);
+ if(value.indexOf('{')>-1) {
+ scanString(file, value, offset);
+ }
+ }
+ token = scaner.nextToken();
+ }
+ } catch (BadLocationException e) {
+ SeamCorePlugin.getDefault().logError(e);
+ }
+ }
+
+ private void scanDOM(IFile file, String content){
+ IModelManager manager = StructuredModelManager.getModelManager();
+ if(manager == null) {
+ return;
+ }
+ IStructuredModel model = null;
+ try {
+ model = manager.getModelForRead(file);
+ if (model instanceof IDOMModel) {
+ IDOMModel domModel = (IDOMModel) model;
+ IDOMDocument document = domModel.getDocument();
+ scanChildNodes(file, document);
+ }
+ } catch (CoreException e) {
+ SeamCorePlugin.getDefault().logError(e);
+ } catch (IOException e) {
+ SeamCorePlugin.getDefault().logError(e);
+ } finally {
+ if (model != null) {
+ model.releaseFromRead();
+ }
+ }
+ }
+
+ private void scanChildNodes(IFile file, Node parent) {
+ NodeList children = parent.getChildNodes();
+ for(int i=0; i<children.getLength(); i++) {
+ Node curentValidatedNode = children.item(i);
+ if(Node.ELEMENT_NODE == curentValidatedNode.getNodeType()) {
+ scanNodeContent(file,
((IDOMNode)curentValidatedNode).getFirstStructuredDocumentRegion(),
DOMRegionContext.XML_TAG_ATTRIBUTE_VALUE);
+ } else if(Node.TEXT_NODE == curentValidatedNode.getNodeType()) {
+ scanNodeContent(file,
((IDOMNode)curentValidatedNode).getFirstStructuredDocumentRegion(),
DOMRegionContext.XML_CONTENT);
+ }
+ scanChildNodes(file, curentValidatedNode);
+ }
+ }
+
+ private void scanNodeContent(IFile file, IStructuredDocumentRegion node, String
regionType) {
+ ITextRegionList regions = node.getRegions();
+ for(int i=0; i<regions.size(); i++) {
+ ITextRegion region = regions.get(i);
+ if(region.getType() == regionType) {
+ String text = node.getFullText(region);
+ if(text.indexOf("{")>-1) { //$NON-NLS-1$
+ int offset = node.getStartOffset() + region.getStart();
+ scanString(file, text, offset);
+ }
+ }
+ }
+ }
+
+ // looking for component references in EL
+ private void scanString(IFile file, String string, int offset) {
+ int startEl = string.indexOf("#{"); //$NON-NLS-1$
+ if(startEl>-1) {
+ ELParser parser = ELParserUtil.getJbossFactory().createParser();
+ ELModel model = parser.parse(string);
+ for (ELInstance instance : model.getInstances()) {
+ for(ELInvocationExpression ie : instance.getExpression().getInvocations()){
+ ELPropertyInvocation pi = findComponentReference(ie);
+ if(pi != null){
+ TextFileChange change = getChange(file);
+ TextEdit edit = new ReplaceEdit(offset+pi.getStartPosition(),
pi.getName().getStart()+pi.getName().getLength()-pi.getStartPosition(), newName);
+ change.addEdit(edit);
+ }
+ }
+ }
+ }
+ }
+
+ private ELPropertyInvocation findComponentReference(ELInvocationExpression
invocationExpression){
+ ELInvocationExpression invExp = invocationExpression;
+ while(invExp != null){
+ if(invExp instanceof ELPropertyInvocation){
+ if(((ELPropertyInvocation)invExp).getQualifiedName() != null &&
((ELPropertyInvocation)invExp).getQualifiedName().equals(getOldName()))
+ return (ELPropertyInvocation)invExp;
+ else
+ invExp = invExp.getLeft();
+
+ }else{
+ invExp = invExp.getLeft();
+ }
+ }
+ return null;
+ }
+
+ private void scanProperties(IFile file, String content){
+ scanString(file, content, 0);
+
+ if(!file.getName().equals(SEAM_PROPERTIES_FILE))
+ return;
+
+ StringTokenizer tokenizer = new StringTokenizer(content, "#= \t\r\n\f",
true); //$NON-NLS-1$
+
+ String lastToken = "\n"; //$NON-NLS-1$
+ int offset = 0;
+ boolean comment = false;
+ boolean key = true;
+
+ while(tokenizer.hasMoreTokens()){
+ String token = tokenizer.nextToken("#= \t\r\n\f"); //$NON-NLS-1$
+ if(token.equals("\r")) //$NON-NLS-1$
+ token = "\n"; //$NON-NLS-1$
+
+ if(token.equals("#") && lastToken.equals("\n"))
//$NON-NLS-1$ //$NON-NLS-2$
+ comment = true;
+ else if(token.equals("\n") && comment) //$NON-NLS-1$
+ comment = false;
+
+ if(!comment){
+ if(!token.equals("\n") && lastToken.equals("\n"))
//$NON-NLS-1$ //$NON-NLS-2$
+ key = true;
+ else if(key && (token.equals("=") || token.equals(" ")))
//$NON-NLS-1$ //$NON-NLS-2$
+ key = false;
+
+ if(key && token.startsWith(getOldName())){
+ String changeText = token.replaceFirst(getOldName(), getNewName());
+ TextFileChange change = getChange(file);
+ TextEdit edit = new ReplaceEdit(offset, token.length(), changeText);
+ change.addEdit(edit);
+ }
+ }
+
+ lastToken = token;
+ offset += token.length();
+ }
+ }
+
+ protected void renameComponent(ISeamComponent component)throws CoreException{
+ findDeclarations(component);
+
+ findAnnotations();
+
+ findELReferences();
+ }
+}
Property changes on:
trunk/seam/plugins/org.jboss.tools.seam.core/src/org/jboss/tools/seam/internal/core/refactoring/SeamRenameProcessor.java
___________________________________________________________________
Name: svn:mime-type
+ text/plain
Modified:
trunk/seam/plugins/org.jboss.tools.seam.ui/src/org/jboss/tools/seam/ui/refactoring/SeamRefactorContributionFactory.java
===================================================================
---
trunk/seam/plugins/org.jboss.tools.seam.ui/src/org/jboss/tools/seam/ui/refactoring/SeamRefactorContributionFactory.java 2009-06-04
18:19:02 UTC (rev 15715)
+++
trunk/seam/plugins/org.jboss.tools.seam.ui/src/org/jboss/tools/seam/ui/refactoring/SeamRefactorContributionFactory.java 2009-06-04
18:22:36 UTC (rev 15716)
@@ -68,9 +68,11 @@
import org.jboss.tools.seam.core.SeamCorePlugin;
import org.jboss.tools.seam.internal.core.refactoring.RenameComponentProcessor;
import org.jboss.tools.seam.internal.core.refactoring.RenameComponentRefactoring;
+import
org.jboss.tools.seam.internal.core.refactoring.RenameSeamContextVariableProcessor;
import org.jboss.tools.seam.ui.SeamGuiPlugin;
import org.jboss.tools.seam.ui.SeamUIMessages;
import org.jboss.tools.seam.ui.wizard.RenameComponentWizard;
+import org.jboss.tools.seam.ui.wizard.RenameSeamContextVariableWizard;
/**
* @author Daniel Azarov
@@ -83,8 +85,8 @@
private static final String JSP_EXT = "jsp"; //$NON-NLS-1$
private static final String PROPERTIES_EXT = "properties"; //$NON-NLS-1$
- private String selectedText;
- private IFile editorFile;
+ static private String selectedText;
+ static private IFile editorFile;
private String fileContent;
private IEditorPart editor;
private Shell shell;
@@ -101,6 +103,8 @@
public void createContributionItems(IServiceLocator serviceLocator,
IContributionRoot additions) {
+ //System.out.println("createContributionItems");
+
if(serviceLocator.hasService(IWorkbenchLocationService.class)){
IWorkbenchLocationService service =
(IWorkbenchLocationService)serviceLocator.getService(IWorkbenchLocationService.class);
editor = service.getWorkbenchWindow().getActivePage().getActiveEditor();
@@ -120,7 +124,6 @@
editorFile = input.getFile();
-
fileContent = null;
try {
fileContent = FileUtil.readStream(editorFile.getContents());
@@ -314,6 +317,21 @@
}
}
+ public static void invokeRenameSeamContextVariableWizard(String oldName, Shell
activeShell) {
+ saveAndBuild();
+
+ RenameSeamContextVariableProcessor processor = new
RenameSeamContextVariableProcessor(editorFile, selectedText);
+ RenameComponentRefactoring refactoring = new RenameComponentRefactoring(processor);
+ RenameSeamContextVariableWizard wizard = new
RenameSeamContextVariableWizard(refactoring, editorFile);
+ RefactoringWizardOpenOperation op = new RefactoringWizardOpenOperation(wizard);
+ try {
+ String titleForFailedChecks = SeamUIMessages.SEAM_COMPONENT_RENAME_HANDLER_ERROR;
+ op.run(activeShell, titleForFailedChecks);
+ } catch (final InterruptedException irex) {
+ // operation was canceled
+ }
+ }
+
class RenameSeamComponentAction extends Action{
public RenameSeamComponentAction(){
super(SeamUIMessages.RENAME_SEAM_COMPONENT);
@@ -351,7 +369,9 @@
super(SeamUIMessages.RENAME_SEAM_CONTEXT_VARIABLE);
}
public void run(){
+ saveAndBuild();
+ invokeRenameSeamContextVariableWizard(selectedText, shell);
}
}
}
Modified:
trunk/seam/plugins/org.jboss.tools.seam.ui/src/org/jboss/tools/seam/ui/wizard/RenameComponentWizard.java
===================================================================
---
trunk/seam/plugins/org.jboss.tools.seam.ui/src/org/jboss/tools/seam/ui/wizard/RenameComponentWizard.java 2009-06-04
18:19:02 UTC (rev 15715)
+++
trunk/seam/plugins/org.jboss.tools.seam.ui/src/org/jboss/tools/seam/ui/wizard/RenameComponentWizard.java 2009-06-04
18:22:36 UTC (rev 15716)
@@ -122,7 +122,7 @@
}
private void initializeRefactoring() {
- processor.setNewComponentName(editor.getValueAsString());
+ processor.setNewName(editor.getValueAsString());
}
}
Added:
trunk/seam/plugins/org.jboss.tools.seam.ui/src/org/jboss/tools/seam/ui/wizard/RenameSeamContextVariableWizard.java
===================================================================
---
trunk/seam/plugins/org.jboss.tools.seam.ui/src/org/jboss/tools/seam/ui/wizard/RenameSeamContextVariableWizard.java
(rev 0)
+++
trunk/seam/plugins/org.jboss.tools.seam.ui/src/org/jboss/tools/seam/ui/wizard/RenameSeamContextVariableWizard.java 2009-06-04
18:22:36 UTC (rev 15716)
@@ -0,0 +1,130 @@
+ /*******************************************************************************
+ * Copyright (c) 2008 Red Hat, Inc.
+ * Distributed under license by Red Hat, Inc. All rights reserved.
+ * This program is 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:
+ * Red Hat, Inc. - initial API and implementation
+ ******************************************************************************/
+package org.jboss.tools.seam.ui.wizard;
+
+import java.beans.PropertyChangeEvent;
+import java.beans.PropertyChangeListener;
+import java.util.Map;
+
+import org.eclipse.core.resources.IFile;
+import org.eclipse.core.resources.IProject;
+import org.eclipse.core.runtime.IStatus;
+import org.eclipse.core.runtime.Status;
+import org.eclipse.jface.wizard.IWizardPage;
+import org.eclipse.ltk.core.refactoring.Refactoring;
+import org.eclipse.ltk.core.refactoring.RefactoringStatus;
+import org.eclipse.ltk.ui.refactoring.RefactoringWizard;
+import org.eclipse.ltk.ui.refactoring.UserInputWizardPage;
+import org.eclipse.osgi.util.NLS;
+import org.eclipse.swt.SWT;
+import org.eclipse.swt.layout.GridData;
+import org.eclipse.swt.layout.GridLayout;
+import org.eclipse.swt.widgets.Composite;
+import org.jboss.tools.seam.core.ISeamComponent;
+import org.jboss.tools.seam.core.ISeamProject;
+import org.jboss.tools.seam.core.SeamCorePlugin;
+import org.jboss.tools.seam.internal.core.refactoring.RenameComponentProcessor;
+import
org.jboss.tools.seam.internal.core.refactoring.RenameSeamContextVariableProcessor;
+import org.jboss.tools.seam.internal.core.refactoring.SeamRenameProcessor;
+import org.jboss.tools.seam.ui.SeamUIMessages;
+import org.jboss.tools.seam.ui.internal.project.facet.IValidator;
+import org.jboss.tools.seam.ui.internal.project.facet.ValidatorFactory;
+import org.jboss.tools.seam.ui.widget.editor.CompositeEditor;
+import org.jboss.tools.seam.ui.widget.editor.IFieldEditor;
+import org.jboss.tools.seam.ui.widget.editor.IFieldEditorFactory;
+
+/**
+ * @author Alexey Kazakov
+ */
+public class RenameSeamContextVariableWizard extends RefactoringWizard {
+
+ //private ISeamComponent component;
+ private String componentName;
+ private IFieldEditor editor;
+ private ISeamProject seamProject;
+
+ public RenameSeamContextVariableWizard(Refactoring refactoring, IFile file) {
+ super(refactoring, WIZARD_BASED_USER_INTERFACE);
+ seamProject = SeamCorePlugin.getSeamProject(file.getProject(), true);
+ }
+
+ /* (non-Javadoc)
+ * @see org.eclipse.ltk.ui.refactoring.RefactoringWizard#addUserInputPages()
+ */
+ @Override
+ protected void addUserInputPages() {
+ setDefaultPageTitle(getRefactoring().getName());
+ RenameSeamContextVariableProcessor processor= (RenameSeamContextVariableProcessor)
getRefactoring().getAdapter(RenameSeamContextVariableProcessor.class);
+ addPage(new RenameComponentWizardPage(processor));
+ }
+
+ class RenameComponentWizardPage extends UserInputWizardPage{
+ private SeamRenameProcessor processor;
+
+ public RenameComponentWizardPage(SeamRenameProcessor processor){
+ super("");
+ this.processor = processor;
+ }
+
+ public void createControl(Composite parent) {
+ Composite container = new Composite(parent, SWT.NULL);
+ container.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
+ GridLayout layout = new GridLayout();
+ container.setLayout(layout);
+ layout.numColumns = 2;
+
+ String defaultName = processor.getOldName();
+ editor = IFieldEditorFactory.INSTANCE.createTextEditor(componentName,
SeamUIMessages.SEAM_WIZARD_FACTORY_SEAM_COMPONENT_NAME, defaultName);
+ editor.doFillIntoGrid(container);
+
+ ((CompositeEditor)editor).addPropertyChangeListener(new
PropertyChangeListener(){
+ public void propertyChange(PropertyChangeEvent evt){
+ validatePage();
+ }
+ });
+ setControl(container);
+ setPageComplete(false);
+ }
+
+ protected final void validatePage() {
+ Map<String, IStatus> errors =
ValidatorFactory.SEAM_COMPONENT_NAME_VALIDATOR.validate(editor.getValueAsString(),
seamProject);
+ if(!errors.isEmpty()) {
+ setErrorMessage(NLS.bind(errors.get(IValidator.DEFAULT_ERROR).getMessage(),SeamUIMessages.SEAM_BASE_WIZARD_PAGE_SEAM_COMPONENTS));
+ setPageComplete(false);
+ return;
+ }
+ RefactoringStatus status= new RefactoringStatus();
+ setPageComplete(status);
+ }
+
+ /* (non-Javadoc)
+ * @see org.eclipse.ltk.ui.refactoring.UserInputWizardPage#performFinish()
+ */
+ protected boolean performFinish() {
+
+ initializeRefactoring();
+ return super.performFinish();
+ }
+
+ /* (non-Javadoc)
+ * @see org.eclipse.ltk.ui.refactoring.UserInputWizardPage#getNextPage()
+ */
+ public IWizardPage getNextPage() {
+ initializeRefactoring();
+ return super.getNextPage();
+ }
+
+ private void initializeRefactoring() {
+ processor.setNewName(editor.getValueAsString());
+ }
+
+ }
+}
\ No newline at end of file
Property changes on:
trunk/seam/plugins/org.jboss.tools.seam.ui/src/org/jboss/tools/seam/ui/wizard/RenameSeamContextVariableWizard.java
___________________________________________________________________
Name: svn:mime-type
+ text/plain
Modified:
trunk/seam/tests/org.jboss.tools.seam.core.test/src/org/jboss/tools/seam/core/test/refactoring/SeamComponentRefactoringTest.java
===================================================================
---
trunk/seam/tests/org.jboss.tools.seam.core.test/src/org/jboss/tools/seam/core/test/refactoring/SeamComponentRefactoringTest.java 2009-06-04
18:19:02 UTC (rev 15715)
+++
trunk/seam/tests/org.jboss.tools.seam.core.test/src/org/jboss/tools/seam/core/test/refactoring/SeamComponentRefactoringTest.java 2009-06-04
18:22:36 UTC (rev 15716)
@@ -138,7 +138,7 @@
// Rename Seam Component
RenameComponentProcessor processor = new RenameComponentProcessor(component);
- processor.setNewComponentName(newName);
+ processor.setNewName(newName);
CompositeChange rootChange = (CompositeChange)processor.createChange(new
NullProgressMonitor());
for(int i = 0; i < rootChange.getChildren().length;i++){