1 /*******************************************************************************
2 * Copyright (c) 2006, 2009 Wind River Systems and others.
3 * All rights reserved. This program and the accompanying materials
4 * are made available under the terms of the Eclipse Public License v1.0
5 * which accompanies this distribution, and is available at
6 * http://www.eclipse.org/legal/epl-v10.html
9 * Markus Schorn (Wind River Systems) - Initial API and implementation
10 * Anton Leherbauer (Wind River Systems)
11 * Sergey Prigogin (Google)
12 *******************************************************************************/
13 package org.eclipse.cdt.ui.refactoring;
15 import org.eclipse.core.resources.IFile;
16 import org.eclipse.core.runtime.CoreException;
17 import org.eclipse.core.runtime.IProgressMonitor;
18 import org.eclipse.jface.text.IDocument;
19 import org.eclipse.ltk.core.refactoring.Change;
20 import org.eclipse.ltk.core.refactoring.ContentStamp;
21 import org.eclipse.ltk.core.refactoring.TextFileChange;
22 import org.eclipse.text.edits.UndoEdit;
24 import org.eclipse.cdt.core.model.CoreModel;
25 import org.eclipse.cdt.core.model.ICElement;
26 import org.eclipse.cdt.core.model.ITranslationUnit;
27 import org.eclipse.cdt.core.model.IWorkingCopy;
29 import org.eclipse.cdt.internal.core.model.TranslationUnit;
31 import org.eclipse.cdt.internal.ui.refactoring.DocumentAdapter;
32 import org.eclipse.cdt.internal.ui.refactoring.UndoCTextFileChange;
36 * A TextFileChange that uses a working copy in order to generate CModel events.
38 * @noextend This class is not intended to be subclassed by clients.
40 public class CTextFileChange extends TextFileChange {
41 // "c2" is the extension which the CContentViewerCreator is registered
42 // with the extension point "org.eclipse.compare.contentMergeViewers"
43 private static final String TEXT_TYPE = "c2"; //$NON-NLS-1$
44 private ITranslationUnit fTranslationUnit;
45 private IWorkingCopy fWorkingCopy;
46 private int fAquireCount;
48 public CTextFileChange(String name, IFile file) {
50 ICElement element = CoreModel.getDefault().create(file);
51 if (element instanceof ITranslationUnit) {
52 fTranslationUnit = (ITranslationUnit) element;
53 setTextType(TEXT_TYPE);
60 public CTextFileChange(String name, ITranslationUnit tu) {
61 super(name, getFile(tu));
62 fTranslationUnit = tu;
63 setTextType(TEXT_TYPE);
68 * @see org.eclipse.ltk.core.refactoring.TextFileChange#acquireDocument(org.eclipse.core.runtime.IProgressMonitor)
71 protected IDocument acquireDocument(IProgressMonitor pm) throws CoreException {
72 IDocument doc= super.acquireDocument(pm);
73 if (++fAquireCount == 1) {
74 if (fTranslationUnit instanceof TranslationUnit && fWorkingCopy == null) {
75 fWorkingCopy= ((TranslationUnit) fTranslationUnit).getWorkingCopy(null, DocumentAdapter.FACTORY);
76 if (!fTranslationUnit.isOpen()) {
77 fTranslationUnit.open(null);
85 protected void commit(final IDocument document, final IProgressMonitor pm) throws CoreException {
86 if (fWorkingCopy == null) {
87 super.commit(document, pm);
89 else if (needsSaving()) {
90 fWorkingCopy.commit(false, pm);
96 * @see org.eclipse.ltk.core.refactoring.TextFileChange#releaseDocument(org.eclipse.jface.text.IDocument, org.eclipse.core.runtime.IProgressMonitor)
99 protected void releaseDocument(IDocument document, IProgressMonitor pm) throws CoreException {
100 super.releaseDocument(document, pm);
101 if (--fAquireCount == 0) {
102 if (fWorkingCopy != null) {
103 fWorkingCopy.destroy();
111 * @see org.eclipse.ltk.core.refactoring.TextFileChange#createUndoChange(org.eclipse.text.edits.UndoEdit, org.eclipse.ltk.core.refactoring.ContentStamp)
114 protected Change createUndoChange(UndoEdit edit, ContentStamp stampToRestore) {
115 return new UndoCTextFileChange(getName(), getFile(), edit, stampToRestore, getSaveMode());
118 private static IFile getFile(ITranslationUnit tu) {
119 return (IFile) tu.getResource();