Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 408c225c996ae0383181c380f14528453e925b28 (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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
/*******************************************************************************
 * Copyright (c) 2004, 2011 Wind River Systems, Inc. and others.
 * 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:
 *     Markus Schorn - initial API and implementation 
 *     Sergey Prigogin (Google)
 *******************************************************************************/
package org.eclipse.cdt.internal.ui.refactoring.rename;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

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.NullProgressMonitor;
import org.eclipse.core.runtime.OperationCanceledException;
import org.eclipse.ltk.core.refactoring.Change;
import org.eclipse.ltk.core.refactoring.RefactoringStatus;
import org.eclipse.ltk.core.refactoring.participants.CheckConditionsContext;
import org.eclipse.ltk.core.refactoring.participants.ParticipantManager;
import org.eclipse.ltk.core.refactoring.participants.RefactoringParticipant;
import org.eclipse.ltk.core.refactoring.participants.RenameArguments;
import org.eclipse.ltk.core.refactoring.participants.RenameParticipant;
import org.eclipse.ltk.core.refactoring.participants.RenameProcessor;
import org.eclipse.ltk.core.refactoring.participants.SharableParticipants;
import org.eclipse.osgi.util.NLS;
import org.eclipse.ui.IWorkingSetManager;
import org.eclipse.ui.PlatformUI;

import org.eclipse.cdt.core.CCProjectNature;
import org.eclipse.cdt.core.CCorePlugin;
import org.eclipse.cdt.core.CProjectNature;
import org.eclipse.cdt.core.dom.ast.IBinding;
import org.eclipse.cdt.core.index.IIndex;
import org.eclipse.cdt.core.model.CoreModel;
import org.eclipse.cdt.core.model.ICProject;

/**
 * This is the processor used for the rename. It decides which of the delegates to
 * use and forwards further calls to the delegate.
 */
public class CRenameProcessor extends RenameProcessor {
    public static final String IDENTIFIER= "org.eclips.cdt.refactoring.RenameProcessor"; //$NON-NLS-1$

    private final CRefactoringArgument fArgument;
    private CRenameProcessorDelegate fDelegate;
    private String fReplacementText;
    private String fWorkingSetName;
    private int fExhaustiveSearchScope;
    private int fSelectedOptions;
    private final CRefactory fManager;
    private final ASTManager fAstManager;
	private IIndex fIndex;
	private int indexLockCount;
	private RefactoringStatus fInitialConditionsStatus;

	private Change fChange;
    
    public CRenameProcessor(CRefactory refactoringManager, CRefactoringArgument arg) {
        fManager= refactoringManager;
        fArgument= arg;
        fAstManager= new ASTManager(arg);
    }
    
    public CRefactoringArgument getArgument() {
        return fArgument;
    }

    @Override
	public Object[] getElements() {
        return new Object[] { fArgument.getBinding() };
    }

    @Override
	public String getProcessorName() {
        String result= null;
        if (fDelegate != null) { 
            result= fDelegate.getProcessorName();
        }
        if (result == null) {
            String identifier= getArgument().getName();
            if (identifier != null && identifier.length() > 0) {
                result= NLS.bind(RenameMessages.CRenameTopProcessor_wizard_title, identifier);
            }
        }
        if (result == null) {
            result= RenameMessages.CRenameTopProcessor_wizard_backup_title;
        }

        return result;
    }

    @Override
	public boolean isApplicable() throws CoreException {
        return true;
    }

    @Override
	public RefactoringStatus checkInitialConditions(IProgressMonitor pm)
            throws CoreException, OperationCanceledException {
    	if (fInitialConditionsStatus != null) {
    		return fInitialConditionsStatus; // Already checked.
    	}
        String identifier= null;
        fInitialConditionsStatus= new RefactoringStatus();
        if (fArgument != null) {
            fAstManager.analyzeArgument(fIndex, pm, fInitialConditionsStatus);
            identifier= fArgument.getName();
        }
        if (identifier == null || identifier.length() == 0) {
        	fInitialConditionsStatus.addFatalError(RenameMessages.CRenameTopProcessor_error_invalidTextSelection);
            return fInitialConditionsStatus;
        }
        IFile file= fArgument.getSourceFile();
        IPath path= null;
        if (file != null) {
            path= file.getLocation();
        }
        if (path == null) {
            return RefactoringStatus.createFatalErrorStatus(RenameMessages.CRenameTopProcessor_error_renameWithoutSourceFile);
        }
        
        fDelegate= createDelegate();
        if (fDelegate == null) {
        	fInitialConditionsStatus.addFatalError(RenameMessages.CRenameTopProcessor_error_invalidName);
            return fInitialConditionsStatus;
        }            
        RefactoringStatus status= fDelegate.checkInitialConditions(new NullProgressMonitor());
        fInitialConditionsStatus.merge(status);
        return fInitialConditionsStatus;
    }

    private CRenameProcessorDelegate createDelegate() {
        switch (fArgument.getArgumentKind()) {
        	case CRefactory.ARGUMENT_LOCAL_VAR: 
                return new CRenameLocalProcessor(this,
                        RenameMessages.CRenameTopProcessor_localVar,
                        fArgument.getScope());
        	case CRefactory.ARGUMENT_PARAMETER:
                return new CRenameLocalProcessor(this,
                        RenameMessages.CRenameTopProcessor_parameter,
                        fArgument.getScope());
        	case CRefactory.ARGUMENT_FILE_LOCAL_VAR:
                return new CRenameLocalProcessor(this,
                        RenameMessages.CRenameTopProcessor_filelocalVar,
                        null);
        	case CRefactory.ARGUMENT_GLOBAL_VAR:
                return new CRenameGlobalProcessor(this, RenameMessages.CRenameTopProcessor_globalVar);
            case CRefactory.ARGUMENT_ENUMERATOR:
                return new CRenameGlobalProcessor(this, RenameMessages.CRenameTopProcessor_enumerator);
        	case CRefactory.ARGUMENT_FIELD:
                return new CRenameGlobalProcessor(this, RenameMessages.CRenameTopProcessor_field);
        	case CRefactory.ARGUMENT_FILE_LOCAL_FUNCTION:
                return new CRenameLocalProcessor(this,
                        RenameMessages.CRenameTopProcessor_filelocalFunction,
                        null);
        	case CRefactory.ARGUMENT_GLOBAL_FUNCTION:
                return new CRenameGlobalProcessor(this, RenameMessages.CRenameTopProcessor_globalFunction);
        	case CRefactory.ARGUMENT_VIRTUAL_METHOD:
                return new CRenameMethodProcessor(this, RenameMessages.CRenameTopProcessor_virtualMethod, true);
        	case CRefactory.ARGUMENT_NON_VIRTUAL_METHOD:
                return new CRenameMethodProcessor(this, RenameMessages.CRenameTopProcessor_method, false);
            case CRefactory.ARGUMENT_CLASS_TYPE:                
                return new CRenameClassProcessor(this, RenameMessages.CRenameTopProcessor_type);
            case CRefactory.ARGUMENT_NAMESPACE:
                return new CRenameTypeProcessor(this, RenameMessages.CRenameTopProcessor_namespace);
        	case CRefactory.ARGUMENT_TYPE:
                return new CRenameTypeProcessor(this, RenameMessages.CRenameTopProcessor_type);
        	case CRefactory.ARGUMENT_MACRO:
                return new CRenameMacroProcessor(this, RenameMessages.CRenameTopProcessor_macro);
        	case CRefactory.ARGUMENT_INCLUDE_DIRECTIVE:
                return new CRenameIncludeProcessor(this, RenameMessages.CRenameIncludeProcessor_includeDirective);
        	default:
                return null;
        }
    }

    @Override
	public RefactoringStatus checkFinalConditions(IProgressMonitor pm, CheckConditionsContext context)
    		throws CoreException, OperationCanceledException {
        return fDelegate.checkFinalConditions(pm, context);
    }

    @Override
	public Change createChange(IProgressMonitor pm) throws CoreException, OperationCanceledException {
        fChange = fDelegate.createChange(pm);
        return fChange;
    }

    /**
     * @return the change if it has been created, or <code>null</code> otherwise.
     */
	Change getChange() {
        return fChange;
    }

	@Override
	public RefactoringParticipant[] loadParticipants(RefactoringStatus status,
            SharableParticipants sharedParticipants) throws CoreException {
        RenameArguments arguments= new RenameArguments(getReplacementText(), true);
        final String[] natures= {CCProjectNature.CC_NATURE_ID, CProjectNature.C_NATURE_ID};
        List<RenameParticipant> result= new ArrayList<RenameParticipant>();
        IBinding binding= getArgument().getBinding();
        if (binding != null) {
            result.addAll(Arrays.asList(ParticipantManager.loadRenameParticipants(status, 
                    this,  binding, arguments, natures, sharedParticipants)));
        }
        return result.toArray(new RefactoringParticipant[result.size()]);
    }

    /**
     * Options for the input page in the refactoring wizard
     */
    public int getAvailableOptions() {
        if (fDelegate == null) {
            return 0;
        }
        return fDelegate.getAvailableOptions();
    }

    /**
     * Options for the input page that trigger the preview
     */
    public int getOptionsForcingPreview() {
        if (fDelegate == null) {
            return 0;
        }
        return fDelegate.getOptionsForcingPreview();
    }

    /**
     * The options that may need exhaustive file search since index lookup is not guaranteed to
     * return all files participating in refactoring. When one of these options is selected,
     * the exhaustive file search is enabled.
     */
    public int getOptionsEnablingExhaustiveSearch() {
        if (fDelegate == null) {
            return 0;
        }
        return fDelegate.getOptionsEnablingExhaustiveSearch();
    }

    @Override
	public String getIdentifier() {
        return IDENTIFIER;
    }

    public int getExhaustiveSearchScope() {
        return (fSelectedOptions & CRefactory.OPTION_EXHAUSTIVE_FILE_SEARCH) != 0 ?
        		fExhaustiveSearchScope : TextSearchWrapper.SCOPE_FILE;
    }

    public void setExhaustiveSearchScope(int scope) {
        fExhaustiveSearchScope = scope;
    }

    public int getSelectedOptions() {
        return fSelectedOptions;
    }

    public void setSelectedOptions(int selectedOptions) {
        fSelectedOptions = selectedOptions;
    }

    public boolean isPreviewRequired() {
    	return (fSelectedOptions & getOptionsForcingPreview()) != 0;
    }

    public String getWorkingSetName() {
        return fWorkingSetName;
    }

    /**
     * Sets the name of the working set. If the name of the working set is invalid,
     * it's set to an empty string. 
     */
    public void setWorkingSetName(String workingSet) {
        fWorkingSetName = checkWorkingSet(workingSet);
    }

    public String getReplacementText() {
        return fReplacementText;
    }

    public void setReplacementText(String replacementText) {
        fReplacementText = replacementText;
    }

    public CRefactory getManager() {
        return fManager;
    }

    public ASTManager getAstManager() {
        return fAstManager;
    }

	public void lockIndex() throws CoreException, InterruptedException {
		if (indexLockCount == 0) {
			if (fIndex == null) {
				ICProject[] projects= CoreModel.getDefault().getCModel().getCProjects();
				fIndex= CCorePlugin.getIndexManager().getIndex(projects);
			}
			fIndex.acquireReadLock();
		}
		indexLockCount++;
	}
	
	public void unlockIndex() {
		if (--indexLockCount <= 0) {
			if (fAstManager != null) {
				fAstManager.dispose();
			}
			if (fIndex != null) {
				fIndex.releaseReadLock();
			}
			fIndex= null;
		}
	}

	public IIndex getIndex() {
		return fIndex;
	}

	/**
	 * @return a save mode from {@link org.eclipse.cdt.internal.ui.refactoring.RefactoringSaveHelper}
	 */
	public int getSaveMode() {
		return fDelegate.getSaveMode();
	}

    private String checkWorkingSet(String workingSet) {
		if (workingSet != null && workingSet.length() > 0) {
		    IWorkingSetManager wsManager= PlatformUI.getWorkbench().getWorkingSetManager();
		    if (wsManager.getWorkingSet(workingSet) != null) {
		        return workingSet;
		    }
		}
	    return ""; //$NON-NLS-1$
    }
}

Back to the top