Skip to main content

This CGIT instance is deprecated, and repositories have been moved to Gitlab or Github. See the repository descriptions for specific locations.

summaryrefslogtreecommitdiffstats
blob: cce160c6f9448467f22dc6195f3876a50ce4232e (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
package org.eclipse.wst.xsd.ui.internal.refactor.rename;

import java.util.Iterator;
import java.util.List;

import org.eclipse.core.runtime.Assert;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.OperationCanceledException;
import org.eclipse.core.runtime.SubProgressMonitor;
import org.eclipse.ltk.core.refactoring.Change;
import org.eclipse.ltk.core.refactoring.RefactoringStatus;
import org.eclipse.ltk.core.refactoring.TextChange;
import org.eclipse.ltk.core.refactoring.participants.CheckConditionsContext;
import org.eclipse.ltk.core.refactoring.participants.RenameParticipant;
import org.eclipse.text.edits.ReplaceEdit;
import org.eclipse.wst.common.core.search.SearchMatch;
import org.eclipse.wst.xml.core.internal.provisional.document.IDOMAttr;
import org.eclipse.wst.xml.core.internal.provisional.document.IDOMElement;
import org.eclipse.wst.xsd.ui.internal.refactor.RefactoringMessages;
import org.eclipse.wst.xsd.ui.internal.refactor.TextChangeManager;
import org.eclipse.wst.xsd.ui.internal.refactor.util.TextChangeCompatibility;
import org.eclipse.xsd.util.XSDConstants;
import org.w3c.dom.Node;

public class XMLComponentRenameParticipant extends RenameParticipant {
	
	protected SearchMatch match;

	protected TextChangeManager changeManager;
	protected List matches;

    
    
	protected boolean initialize(Object element) {
		
		if(getArguments() instanceof ComponentRenameArguments){
			// changeManger is passed in from the RenameComponentProcessor to collect all the changes
			changeManager = ((ComponentRenameArguments)getArguments()).getChangeManager();
		}
		
		return false;
	}
	
	public String getName() {
		return "XML Component Rename Participant";
	}

	public RefactoringStatus checkConditions(IProgressMonitor monitor,
			CheckConditionsContext context) throws OperationCanceledException {
		return null;
	}
	
	public TextChangeManager getChangeManager(){
		
		if(changeManager == null){
				changeManager = new TextChangeManager(false);
		}
		return changeManager;
		
	}
	
	private RefactoringStatus createRenameChanges(final IProgressMonitor monitor) throws CoreException {
		Assert.isNotNull(monitor);
		final RefactoringStatus status= new RefactoringStatus();
		try {
			monitor.beginTask("RefactoringMessages.RenameComponentRefactoring_searching", 1); 
			createRenameChanges(new SubProgressMonitor(monitor, 1));
			//updateChangeManager(new SubProgressMonitor(monitor, 1), status);
		} finally {
			monitor.done();
		}
		return status;
	}

	public Change createChange(IProgressMonitor pm) throws CoreException,
			OperationCanceledException {
		//System.out.println("createChange("  + getChangeManager() + ")" + matches.size());
		for (Iterator iter = matches.iterator(); iter.hasNext();) {
			SearchMatch match = (SearchMatch) iter.next();
			TextChange textChange = getChangeManager().get(match.getFile());
			String newName = getArguments().getNewName();
			String qualifier = "";
			if(getArguments() instanceof ComponentRenameArguments){
				qualifier = ((ComponentRenameArguments)getArguments()).getQualifier();
			}
			if(match.getObject() instanceof Node){
				Node node = (Node)match.getObject();
				if(node instanceof IDOMAttr){
					IDOMAttr attr = (IDOMAttr)node;
					IDOMElement element = (IDOMElement)attr.getOwnerElement() ;
					newName = getNewQName(element, qualifier, newName);
				}
				newName = RenameComponentProcessor.quoteString(newName);
			}
			
			ReplaceEdit replaceEdit = new ReplaceEdit(match.getOffset(), match.getLength(), newName );
			String editName = RefactoringMessages.getString("RenameComponentProcessor.Component_Refactoring_update_reference");
          //  System.out.println("textChange " + textChange + "/ replaceEdit " + replaceEdit);
			TextChangeCompatibility.addTextEdit(textChange, editName, replaceEdit);
   		}
		// don't create any change now, all the changes are in changeManger variable and will be combined in RenameComponentProcessor.postCreateChange method
		return null;
	}
	
	private static String getNewQName(Node node, String targetNamespace, String newName) {
		StringBuffer sb = new StringBuffer();
		if (newName != null) {
			String prefix = XSDConstants.lookupQualifier(node, targetNamespace);
			if (prefix != null && prefix.length() > 0) {
				sb.append(prefix);
				sb.append(":");
				sb.append(newName);
			} else {
				sb.append(newName);
			}
		} else {
			sb.append(newName);
		}

		return sb.toString();
	}

  public void setChangeManager(TextChangeManager changeManager)
  {
    this.changeManager = changeManager;
  }

}

Back to the top