KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > core > filebuffers > manipulation > MultiTextEditWithProgress


1 /*******************************************************************************
2  * Copyright (c) 2000, 2005 IBM Corporation 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  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.core.filebuffers.manipulation;
12
13 import org.eclipse.core.runtime.IProgressMonitor;
14 import org.eclipse.core.runtime.OperationCanceledException;
15
16 import org.eclipse.text.edits.MalformedTreeException;
17 import org.eclipse.text.edits.MultiTextEdit;
18 import org.eclipse.text.edits.TextEdit;
19 import org.eclipse.text.edits.UndoEdit;
20
21 import org.eclipse.jface.text.BadLocationException;
22 import org.eclipse.jface.text.IDocument;
23
24 /**
25  * Multi-text edit with progress reporting.
26  *
27  * @since 3.1
28  */

29 public class MultiTextEditWithProgress extends MultiTextEdit {
30
31     IProgressMonitor fProgressMonitor;
32     private final String JavaDoc fTaskName;
33
34     public MultiTextEditWithProgress(String JavaDoc taskName) {
35         fTaskName= taskName;
36     }
37
38     /*
39      * @see TextEdit#apply(IDocument)
40      */

41     public final UndoEdit apply(IDocument document, IProgressMonitor progressMonitor) throws MalformedTreeException, BadLocationException {
42         return apply(document, CREATE_UNDO | UPDATE_REGIONS, progressMonitor);
43     }
44
45     /*
46      * @see TextEdit#apply(IDocument, int)
47      */

48     public final UndoEdit apply(IDocument document, int style, IProgressMonitor progressMonitor) throws MalformedTreeException, BadLocationException {
49         fProgressMonitor= progressMonitor;
50         try {
51
52             int count= getChildrenSize();
53             if ((style & TextEdit.UPDATE_REGIONS) != 0)
54                 count= 2*count;
55
56             fProgressMonitor.beginTask(fTaskName, count);
57             try {
58                 return super.apply(document, style);
59             } finally {
60                 fProgressMonitor.done();
61             }
62
63         } finally {
64             fProgressMonitor= null;
65         }
66     }
67
68     /*
69      * @see org.eclipse.text.edits.TextEdit#childDocumentUpdated()
70      */

71     protected void childDocumentUpdated() {
72         if (fProgressMonitor.isCanceled())
73             throw new OperationCanceledException();
74         fProgressMonitor.worked(1);
75     }
76
77     /*
78      * @see org.eclipse.text.edits.TextEdit#childRegionUpdated()
79      */

80     protected void childRegionUpdated() {
81         if (fProgressMonitor.isCanceled())
82             throw new OperationCanceledException();
83         fProgressMonitor.worked(1);
84     }
85 }
86
Popular Tags