[jbosstools-commits] JBoss Tools SVN: r23199 - in trunk: jsf/plugins/org.jboss.tools.jsf.ui/src/org/jboss/tools/jsf/ui/el/refactoring and 1 other directories.

jbosstools-commits at lists.jboss.org jbosstools-commits at lists.jboss.org
Fri Jul 2 13:55:44 EDT 2010


Author: dazarov
Date: 2010-07-02 13:55:43 -0400 (Fri, 02 Jul 2010)
New Revision: 23199

Added:
   trunk/jsf/plugins/org.jboss.tools.jsf.ui/src/org/jboss/tools/jsf/ui/el/refactoring/MessagesFileRenameParticipant.java
   trunk/jst/plugins/org.jboss.tools.jst.web.kb/src/org/jboss/tools/jst/web/kb/refactoring/SearchUtil.java
Modified:
   trunk/jsf/plugins/org.jboss.tools.jsf.ui/src/org/jboss/tools/jsf/ui/JsfUIMessages.java
   trunk/jsf/plugins/org.jboss.tools.jsf.ui/src/org/jboss/tools/jsf/ui/messages.properties
Log:
https://jira.jboss.org/browse/JBIDE-4858

Modified: trunk/jsf/plugins/org.jboss.tools.jsf.ui/src/org/jboss/tools/jsf/ui/JsfUIMessages.java
===================================================================
--- trunk/jsf/plugins/org.jboss.tools.jsf.ui/src/org/jboss/tools/jsf/ui/JsfUIMessages.java	2010-07-02 17:38:46 UTC (rev 23198)
+++ trunk/jsf/plugins/org.jboss.tools.jsf.ui/src/org/jboss/tools/jsf/ui/JsfUIMessages.java	2010-07-02 17:55:43 UTC (rev 23199)
@@ -41,6 +41,8 @@
 	public static String MISSING_NATURES_INFO_MESSAGE_TITLE;
 	public static String SKIP_BUTTON_LABEL;
 	
+	public static String MESSAGES_FILE_RENAME_PARTICIPANT_UPDATE_MESSAGE_BUNDLE_REFERENCES;
+	
 	static {
 		// initialize resource bundle
 		NLS.initializeMessages(BUNDLE_NAME, JsfUIMessages.class);

Added: trunk/jsf/plugins/org.jboss.tools.jsf.ui/src/org/jboss/tools/jsf/ui/el/refactoring/MessagesFileRenameParticipant.java
===================================================================
--- trunk/jsf/plugins/org.jboss.tools.jsf.ui/src/org/jboss/tools/jsf/ui/el/refactoring/MessagesFileRenameParticipant.java	                        (rev 0)
+++ trunk/jsf/plugins/org.jboss.tools.jsf.ui/src/org/jboss/tools/jsf/ui/el/refactoring/MessagesFileRenameParticipant.java	2010-07-02 17:55:43 UTC (rev 23199)
@@ -0,0 +1,89 @@
+/*******************************************************************************
+ * Copyright (c) 2010 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.jsf.ui.el.refactoring;
+
+import org.eclipse.core.resources.IFile;
+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.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.RenameParticipant;
+import org.eclipse.text.edits.MultiTextEdit;
+import org.eclipse.text.edits.ReplaceEdit;
+import org.eclipse.text.edits.TextEdit;
+import org.jboss.tools.jsf.ui.JsfUIMessages;
+import org.jboss.tools.jst.web.kb.refactoring.SearchUtil;
+import org.jboss.tools.jst.web.kb.refactoring.SearchUtil.FileResult;
+import org.jboss.tools.jst.web.kb.refactoring.SearchUtil.SearchResult;
+
+public class MessagesFileRenameParticipant extends RenameParticipant {
+	private static final String PROPERTIES_EXT = "properties";
+	private RefactoringStatus status;
+	private CompositeChange rootChange;
+	private IFile file;
+
+	@Override
+	protected boolean initialize(Object element) {
+		System.out.println("MessagesFileRenameParticipant - initialize, element - "+element.getClass());
+		if(element instanceof IFile){
+			rootChange = new CompositeChange(JsfUIMessages.MESSAGES_FILE_RENAME_PARTICIPANT_UPDATE_MESSAGE_BUNDLE_REFERENCES);
+			file = (IFile)element;
+			String ext = file.getFileExtension();
+			if(PROPERTIES_EXT.equals(ext)){
+				
+				IPath path = file.getFullPath();
+				String newName = getArguments().getNewName();
+				String oldName = "\"demo.Messages\"";
+				
+				SearchUtil su = new SearchUtil(SearchUtil.XML_FILES, oldName);
+				SearchResult result = su.searchInNodeAttribute(file.getProject(), ":loadBundle", "basename");
+				for(FileResult fr : result.getEntries()){
+					TextFileChange fileChange = new TextFileChange(fr.getFile().getName(), fr.getFile());
+					MultiTextEdit root = new MultiTextEdit();
+					fileChange.setEdit(root);
+					rootChange.add(fileChange);
+					for(int position : fr.getPositions()){
+						TextEdit edit = new ReplaceEdit(position, oldName.length(), "\""+newName+"\"");
+						fileChange.addEdit(edit);
+					}
+				}
+				return true;
+			}
+		}
+		return false;
+	}
+
+	@Override
+	public RefactoringStatus checkConditions(IProgressMonitor pm,
+			CheckConditionsContext context) throws OperationCanceledException {
+		
+		return status;
+	}
+
+	@Override
+	public Change createChange(IProgressMonitor pm) throws CoreException,
+			OperationCanceledException {
+		return rootChange;
+	}
+
+	@Override
+	public String getName() {
+		if(file != null)
+			return file.getName();
+		return null;
+	}
+
+}


Property changes on: trunk/jsf/plugins/org.jboss.tools.jsf.ui/src/org/jboss/tools/jsf/ui/el/refactoring/MessagesFileRenameParticipant.java
___________________________________________________________________
Name: svn:mime-type
   + text/plain

Modified: trunk/jsf/plugins/org.jboss.tools.jsf.ui/src/org/jboss/tools/jsf/ui/messages.properties
===================================================================
--- trunk/jsf/plugins/org.jboss.tools.jsf.ui/src/org/jboss/tools/jsf/ui/messages.properties	2010-07-02 17:38:46 UTC (rev 23198)
+++ trunk/jsf/plugins/org.jboss.tools.jsf.ui/src/org/jboss/tools/jsf/ui/messages.properties	2010-07-02 17:55:43 UTC (rev 23199)
@@ -30,4 +30,5 @@
 ENABLE_JSF_CODE_COMPLETION_TEXT=The project "{0}" does not have JSF code completion and validation enabled completely.\n\nPlease use "Enabale JSF Code Completion..." fix button if you want these features working.
 DONT_SHOW_CHECKER_DIALOG=Do not show this dialog again!
 MISSING_NATURES_INFO_MESSAGE_TITLE=Missing Natures
-SKIP_BUTTON_LABEL=Skip
\ No newline at end of file
+SKIP_BUTTON_LABEL=Skip
+MESSAGES_FILE_RENAME_PARTICIPANT_UPDATE_MESSAGE_BUNDLE_REFERENCES=Update Message Bundle References
\ No newline at end of file

Added: trunk/jst/plugins/org.jboss.tools.jst.web.kb/src/org/jboss/tools/jst/web/kb/refactoring/SearchUtil.java
===================================================================
--- trunk/jst/plugins/org.jboss.tools.jst.web.kb/src/org/jboss/tools/jst/web/kb/refactoring/SearchUtil.java	                        (rev 0)
+++ trunk/jst/plugins/org.jboss.tools.jst.web.kb/src/org/jboss/tools/jst/web/kb/refactoring/SearchUtil.java	2010-07-02 17:55:43 UTC (rev 23199)
@@ -0,0 +1,292 @@
+/*******************************************************************************
+  * Copyright (c) 2010 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.jst.web.kb.refactoring;
+
+import java.io.IOException;
+import java.util.ArrayList;
+import java.util.List;
+
+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.core.IJavaProject;
+import org.eclipse.jface.text.TextSelection;
+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.ELCorePlugin;
+import org.jboss.tools.common.model.project.ProjectHome;
+import org.jboss.tools.common.model.util.EclipseResourceUtil;
+import org.jboss.tools.common.util.FileUtil;
+import org.jboss.tools.jst.web.kb.WebKbPlugin;
+import org.w3c.dom.Node;
+import org.w3c.dom.NodeList;
+
+public class SearchUtil {
+	public static final int JAVA_FILES = 1 << 1;
+	public static final int XML_FILES = 1 << 2;
+	public static final int PROPERTY_FILES = 1 << 3;
+	public static final int EVERYWHERE = JAVA_FILES|XML_FILES|PROPERTY_FILES;
+	
+	
+	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$
+	
+	private int fileSet;
+	private String nodeName=null;
+	private String attributeName=null;
+	private SearchResult result = null;
+	private FileResult lastResult=null;
+	
+	private String searchString;
+	
+	public SearchUtil(int fileSet, String searchString){
+		this.fileSet = fileSet;
+		this.searchString = searchString;
+		result = new SearchResult();
+	}
+	
+	private boolean isSearchInJavaFiles () {
+		if ((fileSet & JAVA_FILES) != 0)
+			return true;
+		return false;
+	}
+	
+	private boolean isSearchInXMLFiles () {
+		if ((fileSet & XML_FILES) != 0)
+			return true;
+		return false;
+	}
+	
+	private boolean isSearchInPropertyFiles () {
+		if ((fileSet & PROPERTY_FILES) != 0)
+			return true;
+		return false;
+	}
+	
+	public SearchResult searchInNodeAttribute(IProject project, String nodeName, String attributeName){
+		this.nodeName = nodeName;
+		this.attributeName = attributeName;
+		return search(project);
+	}
+	
+	public SearchResult search(IProject project){
+		
+		if(isSearchInJavaFiles()){
+			IJavaProject javaProject = EclipseResourceUtil.getJavaProject(project);
+			
+			// searching java, xml and property files in source folders
+			if(javaProject != null){
+				for(IResource resource : EclipseResourceUtil.getJavaSourceRoots(project)){
+					//if(resource instanceof IFolder)
+						//scanForJava((IFolder) resource);
+					//else if(resource instanceof IFile)
+						//scanForJava((IFile) resource);
+				}
+			}
+		}
+		
+		// searching jsp, xhtml and xml files in WebContent folders
+		if(isSearchInXMLFiles()){
+			if(getViewFolder(project) != null)
+				scan(getViewFolder(project));
+			else
+				scan(project);
+		}
+		
+		return result;
+	}
+	
+	protected IContainer getViewFolder(IProject project){
+		IPath path = ProjectHome.getFirstWebContentPath(project);
+		
+		if(path != null)
+			return project.getFolder(path.removeFirstSegments(1));
+		
+		return null;
+	}
+
+	
+	private boolean isFileCorrect(IFile file){
+		if(!file.isSynchronized(IResource.DEPTH_ZERO)){
+			return false;
+		}else if(file.isPhantom()){
+			return false;
+		}else if(file.isReadOnly()){
+			return false;
+		}
+		return true;
+	}
+
+	
+	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){
+			ELCorePlugin.getDefault().logError(ex);
+		}
+	}
+	
+	private void scan(IFile file){
+		if(isFileCorrect(file)) {
+			String fileContent=null;
+			try {
+				fileContent = FileUtil.readStream(file);
+			} catch (CoreException e) {
+				ELCorePlugin.getDefault().logError(e);
+			}
+			String ext = file.getFileExtension();			
+			if(XHTML_EXT.equalsIgnoreCase(ext) 
+				|| JSP_EXT.equalsIgnoreCase(ext)) {
+				scanInDOM(file, fileContent);
+			}
+		}
+	}
+
+	
+	private boolean scanInDOM(IFile file, String content){
+		IModelManager manager = StructuredModelManager.getModelManager();
+		if(manager == null) {
+			return false;
+		}
+		IStructuredModel model = null;		
+		try {
+			model = manager.getModelForRead(file);
+			if (model instanceof IDOMModel) {
+				IDOMModel domModel = (IDOMModel) model;
+				IDOMDocument document = domModel.getDocument();
+				return scanChildNodes(file, document);
+			}
+		} catch (CoreException e) {
+			WebKbPlugin.getDefault().logError(e);
+        } catch (IOException e) {
+        	WebKbPlugin.getDefault().logError(e);
+		} finally {
+			if (model != null) {
+				model.releaseFromRead();
+			}
+		}
+		return false;
+	}
+	
+	private boolean scanChildNodes(IFile file, Node parent) {
+		boolean status = false;
+		
+		if(parent == null)
+			return false;
+		
+		NodeList children = parent.getChildNodes();
+		for(int i=0; i<children.getLength(); i++) {
+			Node curentValidatedNode = children.item(i);
+			if(Node.ELEMENT_NODE == curentValidatedNode.getNodeType()) {
+				if(nodeName == null || curentValidatedNode.getNodeName().endsWith(nodeName))
+					status = scanNodeContent(file, ((IDOMNode)curentValidatedNode).getFirstStructuredDocumentRegion(), DOMRegionContext.XML_TAG_ATTRIBUTE_VALUE);
+				if(status)
+					return status;
+			}/* else if(Node.TEXT_NODE == curentValidatedNode.getNodeType()) {
+				status = scanNodeContent(file, ((IDOMNode)curentValidatedNode).getFirstStructuredDocumentRegion(), DOMRegionContext.XML_CONTENT);
+				if(status)
+					return status;
+			}*/
+			status = scanChildNodes(file, curentValidatedNode);
+			if(status)
+				return status;
+		}
+		return false;
+	}
+	
+	private boolean scanNodeContent(IFile file, IStructuredDocumentRegion node, String regionType) {
+		boolean status = false;
+		
+		if(node == null)
+			return false;
+		
+		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).trim();
+				if(searchString.equals(text)){
+					if(lastResult == null || !lastResult.getFile().equals(file)){
+						lastResult = new FileResult(file);
+						result.getEntries().add(lastResult);
+					}
+					
+					lastResult.addPosition(node.getStartOffset()+region.getStart());
+				}
+			}
+		}
+		return false;
+	}
+
+
+	
+	public class SearchResult{
+		private List<FileResult> entries;
+		
+		public SearchResult(){
+			entries = new ArrayList<FileResult>();
+		}
+		
+		public List<FileResult> getEntries(){
+			return entries;
+		}
+	}
+	
+	public class FileResult{
+		private IFile file;
+		private List<Integer> positions;
+		
+		public FileResult(IFile file){
+			this.file = file;
+			positions = new ArrayList<Integer>();
+		}
+		
+		public void addPosition(int position){
+			positions.add(new Integer(position));
+		}
+		
+		public IFile getFile(){
+			return file;
+		}
+		
+		public int[] getPositions(){
+			Integer[] integerArray = (Integer[])positions.toArray(new Integer[positions.size()]);
+			
+			int[] intArray = new int[positions.size()];
+			int index = 0;
+			for(Integer position : positions){
+				intArray[index++] = position.intValue(); 
+			}
+			return intArray;
+		}
+	}
+	
+}


Property changes on: trunk/jst/plugins/org.jboss.tools.jst.web.kb/src/org/jboss/tools/jst/web/kb/refactoring/SearchUtil.java
___________________________________________________________________
Name: svn:mime-type
   + text/plain



More information about the jbosstools-commits mailing list