Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 5a1cf42cc36e81277970440282de73dec2f05747 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
/*******************************************************************************
 * Copyright (c) 2000, 2008 IBM Corporation and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     IBM Corporation - initial API and implementation
 *******************************************************************************/
package org.eclipse.text.undo;

import java.util.HashMap;
import java.util.Map;

import org.eclipse.core.runtime.Assert;

import org.eclipse.jface.text.IDocument;


/**
 * This document undo manager registry provides access to a document's
 * undo manager. In order to connect a document a document undo manager
 * call <code>connect</code>. After that call has successfully completed
 * undo manager can be obtained via <code>getDocumentUndoManager</code>.
 * The undo manager is created on the first connect and disposed on the last
 * disconnect, i.e. this registry keeps track of how often a undo manager is
 * connected and returns the same undo manager to each client as long as the
 * document is connected.
 * <p>
 * <em>The recoding of changes starts with the first {@link #connect(IDocument)}.</em></p>
 *
 * @since 3.2
 * @noinstantiate This class is not intended to be instantiated by clients.
 */
public final class DocumentUndoManagerRegistry {

	private static final class Record {
		public Record(IDocument document) {
			count= 0;
			undoManager= new DocumentUndoManager(document);
		}
		private int count;
		private IDocumentUndoManager undoManager;
	}

	private static Map fgFactory= new HashMap();

	private DocumentUndoManagerRegistry() {
		// 	Do not instantiate
	}


	/**
	 * Connects the file at the given location to this manager. After that call
	 * successfully completed it is guaranteed that each call to <code>getFileBuffer</code>
	 * returns the same file buffer until <code>disconnect</code> is called.
	 * <p>
	 * <em>The recoding of changes starts with the first {@link #connect(IDocument)}.</em></p>
	 *
	 * @param document the document to be connected
	 */
	public static synchronized void connect(IDocument document) {
		Assert.isNotNull(document);
		Record record= (Record)fgFactory.get(document);
		if (record == null) {
			record= new Record(document);
			fgFactory.put(document, record);
		}
		record.count++;
	}

	/**
	 * Disconnects the given document from this registry.
	 *
	 * @param document the document to be disconnected
	 */
	public static synchronized void disconnect(IDocument document) {
		Assert.isNotNull(document);
		Record record= (Record)fgFactory.get(document);
		record.count--;
		if (record.count == 0)
			fgFactory.remove(document);

	}

	/**
	 * Returns the file buffer managed for the given location or <code>null</code>
	 * if there is no such file buffer.
	 * <p>
	 * The provided location is either a full path of a workspace resource or
	 * an absolute path in the local file system. The file buffer manager does
	 * not resolve the location of workspace resources in the case of linked
	 * resources.
	 * </p>
	 *
	 * @param document the document for which to get its undo manager
	 * @return the document undo manager or <code>null</code>
	 */
	public static synchronized IDocumentUndoManager getDocumentUndoManager(IDocument document) {
		Assert.isNotNull(document);
		Record record= (Record)fgFactory.get(document);
		if (record == null)
			return null;
		return record.undoManager;
	}

}

Back to the top