KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > text > undo > DocumentUndoManagerRegistry


1 /*******************************************************************************
2  * Copyright (c) 2000, 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.text.undo;
12
13 import java.util.HashMap JavaDoc;
14 import java.util.Map JavaDoc;
15
16 import org.eclipse.core.runtime.Assert;
17
18 import org.eclipse.jface.text.IDocument;
19
20 /**
21  * This document undo manager registry provides access to a document's
22  * undo manager. In order to connect a document a document undo manager
23  * call <code>connect</code>. After that call has successfully completed
24  * undo manager can be obtained via <code>getDocumentUndoManager</code>.
25  * The undo manager is created on the first connect and disposed on the last
26  * disconnect, i.e. this registry keeps track of how often a undo manager is
27  * connected and returns the same undo manager to each client as long as the
28  * document is connected.
29  * <p>
30  * <em>The recoding of changes starts with the first {@link #connect(IDocument)}.</em></p>
31  * <p>
32  * This class is not intended to be subclassed.
33  * </p>
34  *
35  * @since 3.2
36  */

37 public final class DocumentUndoManagerRegistry {
38     
39     private static final class Record {
40         public Record(IDocument document) {
41             count= 0;
42             undoManager= new DocumentUndoManager(document);
43         }
44         private int count;
45         private IDocumentUndoManager undoManager;
46     }
47     
48     private static Map JavaDoc fgFactory= new HashMap JavaDoc();
49     
50     private DocumentUndoManagerRegistry() {
51         // Do not instantiate
52
}
53
54     
55     /**
56      * Connects the file at the given location to this manager. After that call
57      * successfully completed it is guaranteed that each call to <code>getFileBuffer</code>
58      * returns the same file buffer until <code>disconnect</code> is called.
59      * <p>
60      * <em>The recoding of changes starts with the first {@link #connect(IDocument)}.</em></p>
61      *
62      * @param document the document to be connected
63      */

64     public static synchronized void connect(IDocument document) {
65         Assert.isNotNull(document);
66         Record record= (Record)fgFactory.get(document);
67         if (record == null) {
68             record= new Record(document);
69             fgFactory.put(document, record);
70         }
71         record.count++;
72     }
73
74     /**
75      * Disconnects the given document from this registry.
76      *
77      * @param document the document to be disconnected
78      */

79     public static synchronized void disconnect(IDocument document) {
80         Assert.isNotNull(document);
81         Record record= (Record)fgFactory.get(document);
82         record.count--;
83         if (record.count == 0)
84             fgFactory.remove(document);
85             
86     }
87
88     /**
89      * Returns the file buffer managed for the given location or <code>null</code>
90      * if there is no such file buffer.
91      * <p>
92      * The provided location is either a full path of a workspace resource or
93      * an absolute path in the local file system. The file buffer manager does
94      * not resolve the location of workspace resources in the case of linked
95      * resources.
96      * </p>
97      *
98      * @param document the document for which to get its undo manager
99      * @return the document undo manager or <code>null</code>
100      */

101     public static synchronized IDocumentUndoManager getDocumentUndoManager(IDocument document) {
102         Assert.isNotNull(document);
103         Record record= (Record)fgFactory.get(document);
104         if (record == null)
105             return null;
106         return record.undoManager;
107     }
108
109 }
110
Popular Tags