KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jface > text > RewriteSessionEditProcessor


1 /*******************************************************************************
2  * Copyright (c) 2006 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.jface.text;
12
13 import org.eclipse.text.edits.CopyTargetEdit;
14 import org.eclipse.text.edits.DeleteEdit;
15 import org.eclipse.text.edits.InsertEdit;
16 import org.eclipse.text.edits.MalformedTreeException;
17 import org.eclipse.text.edits.MoveTargetEdit;
18 import org.eclipse.text.edits.ReplaceEdit;
19 import org.eclipse.text.edits.TextEdit;
20 import org.eclipse.text.edits.TextEditProcessor;
21 import org.eclipse.text.edits.TextEditVisitor;
22 import org.eclipse.text.edits.UndoEdit;
23
24 /**
25  * A text edit processor that brackets the application of edits into a document rewrite session.
26  *
27  * @since 3.3
28  */

29 public final class RewriteSessionEditProcessor extends TextEditProcessor {
30     /** The threshold for <em>large</em> text edits. */
31     private static final int THRESHOLD= 1000;
32
33     /**
34      * Text edit visitor that estimates the compound size of an edit tree in characters.
35      */

36     private static final class SizeVisitor extends TextEditVisitor {
37         int fSize= 0;
38
39         public boolean visit(CopyTargetEdit edit) {
40             fSize += edit.getLength();
41             return super.visit(edit);
42         }
43
44         public boolean visit(DeleteEdit edit) {
45             fSize += edit.getLength();
46             return super.visit(edit);
47         }
48
49         public boolean visit(InsertEdit edit) {
50             fSize += edit.getText().length();
51             return super.visit(edit);
52         }
53
54         public boolean visit(MoveTargetEdit edit) {
55             fSize += edit.getLength();
56             return super.visit(edit);
57         }
58
59         public boolean visit(ReplaceEdit edit) {
60             fSize += Math.max(edit.getLength(), edit.getText().length());
61             return super.visit(edit);
62         }
63     }
64
65     /**
66      * Constructs a new edit processor for the given document.
67      *
68      * @param document the document to manipulate
69      * @param root the root of the text edit tree describing the modifications. By passing a text
70      * edit a a text edit processor the ownership of the edit is transfered to the text edit
71      * processors. Clients must not modify the edit (e.g adding new children) any longer.
72      * @param style {@link TextEdit#NONE}, {@link TextEdit#CREATE_UNDO} or
73      * {@link TextEdit#UPDATE_REGIONS})
74      */

75     public RewriteSessionEditProcessor(IDocument document, TextEdit root, int style) {
76         super(document, root, style);
77     }
78     
79     /*
80      * @see org.eclipse.text.edits.TextEditProcessor#performEdits()
81      */

82     public UndoEdit performEdits() throws MalformedTreeException, BadLocationException {
83         IDocument document= getDocument();
84         if (!(document instanceof IDocumentExtension4))
85             return super.performEdits();
86
87         IDocumentExtension4 extension= (IDocumentExtension4) document;
88         boolean isLargeEdit= isLargeEdit(getRoot());
89         DocumentRewriteSessionType type= isLargeEdit ? DocumentRewriteSessionType.UNRESTRICTED : DocumentRewriteSessionType.UNRESTRICTED_SMALL;
90
91         DocumentRewriteSession session= extension.startRewriteSession(type);
92         try {
93             return super.performEdits();
94         } finally {
95             extension.stopRewriteSession(session);
96         }
97     }
98
99     /**
100      * Returns <code>true</code> if the passed edit is considered <em>large</em>,
101      * <code>false</code> otherwise.
102      *
103      * @param edit the edit to check
104      * @return <code>true</code> if <code>edit</code> is considered <em>large</em>,
105      * <code>false</code> otherwise
106      * @since 3.3
107      */

108     public static boolean isLargeEdit(TextEdit edit) {
109         SizeVisitor sizeVisitor= new SizeVisitor();
110         edit.accept(sizeVisitor);
111         return sizeVisitor.fSize > THRESHOLD;
112     }
113
114 }
115
Popular Tags