upload tizen1.0 source
[sdk/ide/product.git] / org.eclipse.cdt.ui / src / org / eclipse / cdt / ui / refactoring / CTextFileChange.java
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
7  *
8  * Contributors:
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;
14
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;
23
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;
28
29 import org.eclipse.cdt.internal.core.model.TranslationUnit;
30
31 import org.eclipse.cdt.internal.ui.refactoring.DocumentAdapter;
32 import org.eclipse.cdt.internal.ui.refactoring.UndoCTextFileChange;
33
34
35 /**
36  * A TextFileChange that uses a working copy in order to generate CModel events.
37  * 
38  * @noextend This class is not intended to be subclassed by clients.
39  */
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;
47     
48     public CTextFileChange(String name, IFile file) {
49         super(name, file);
50         ICElement element = CoreModel.getDefault().create(file);
51         if (element instanceof ITranslationUnit) {
52             fTranslationUnit = (ITranslationUnit) element;
53             setTextType(TEXT_TYPE);
54         }
55     }
56     
57     /**
58          * @since 5.1
59          */
60     public CTextFileChange(String name, ITranslationUnit tu) {
61         super(name, getFile(tu));
62         fTranslationUnit = tu;
63         setTextType(TEXT_TYPE);
64     }
65
66     /*
67      * (non-Javadoc)
68      * @see org.eclipse.ltk.core.refactoring.TextFileChange#acquireDocument(org.eclipse.core.runtime.IProgressMonitor)
69      */
70     @Override
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);
78                 }
79             }
80         }
81         return doc;
82     }
83        
84     @Override
85         protected void commit(final IDocument document, final IProgressMonitor pm) throws CoreException {
86         if (fWorkingCopy == null) {
87                 super.commit(document, pm);
88         }
89         else if (needsSaving()) {
90                 fWorkingCopy.commit(false, pm);
91         }
92     }
93     
94     /*
95      * (non-Javadoc)
96      * @see org.eclipse.ltk.core.refactoring.TextFileChange#releaseDocument(org.eclipse.jface.text.IDocument, org.eclipse.core.runtime.IProgressMonitor)
97      */
98     @Override
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();
104                 fWorkingCopy= null;
105             }
106         }
107     }
108     
109     /*
110      * (non-Javadoc)
111      * @see org.eclipse.ltk.core.refactoring.TextFileChange#createUndoChange(org.eclipse.text.edits.UndoEdit, org.eclipse.ltk.core.refactoring.ContentStamp)
112      */
113     @Override
114         protected Change createUndoChange(UndoEdit edit, ContentStamp stampToRestore) {
115         return new UndoCTextFileChange(getName(), getFile(), edit, stampToRestore, getSaveMode());
116     }
117
118         private static IFile getFile(ITranslationUnit tu) {
119                 return (IFile) tu.getResource();
120         }
121 }