Mercurial > projects > dwt-addons
comparison dwtx/text/undo/DocumentUndoManagerRegistry.d @ 129:eb30df5ca28b
Added JFace Text sources
author | Frank Benoit <benoit@tionex.de> |
---|---|
date | Sat, 23 Aug 2008 19:10:48 +0200 |
parents | |
children | b56e9be9fe88 |
comparison
equal
deleted
inserted
replaced
128:8df1d4193877 | 129:eb30df5ca28b |
---|---|
1 /******************************************************************************* | |
2 * Copyright (c) 2000, 2008 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 * Port to the D programming language: | |
11 * Frank Benoit <benoit@tionex.de> | |
12 *******************************************************************************/ | |
13 module dwtx.text.undo.DocumentUndoManagerRegistry; | |
14 | |
15 import dwt.dwthelper.utils; | |
16 | |
17 import java.util.HashMap; | |
18 import java.util.Map; | |
19 | |
20 import dwtx.core.runtime.Assert; | |
21 import dwtx.jface.text.IDocument; | |
22 | |
23 | |
24 /** | |
25 * This document undo manager registry provides access to a document's | |
26 * undo manager. In order to connect a document a document undo manager | |
27 * call <code>connect</code>. After that call has successfully completed | |
28 * undo manager can be obtained via <code>getDocumentUndoManager</code>. | |
29 * The undo manager is created on the first connect and disposed on the last | |
30 * disconnect, i.e. this registry keeps track of how often a undo manager is | |
31 * connected and returns the same undo manager to each client as long as the | |
32 * document is connected. | |
33 * <p> | |
34 * <em>The recoding of changes starts with the first {@link #connect(IDocument)}.</em></p> | |
35 * | |
36 * @since 3.2 | |
37 * @noinstantiate This class is not intended to be instantiated by clients. | |
38 */ | |
39 public final class DocumentUndoManagerRegistry { | |
40 | |
41 private static final class Record { | |
42 public Record(IDocument document) { | |
43 count= 0; | |
44 undoManager= new DocumentUndoManager(document); | |
45 } | |
46 private int count; | |
47 private IDocumentUndoManager undoManager; | |
48 } | |
49 | |
50 private static Map fgFactory= new HashMap(); | |
51 | |
52 private DocumentUndoManagerRegistry() { | |
53 // Do not instantiate | |
54 } | |
55 | |
56 | |
57 /** | |
58 * Connects the file at the given location to this manager. After that call | |
59 * successfully completed it is guaranteed that each call to <code>getFileBuffer</code> | |
60 * returns the same file buffer until <code>disconnect</code> is called. | |
61 * <p> | |
62 * <em>The recoding of changes starts with the first {@link #connect(IDocument)}.</em></p> | |
63 * | |
64 * @param document the document to be connected | |
65 */ | |
66 public static synchronized void connect(IDocument document) { | |
67 Assert.isNotNull(document); | |
68 Record record= (Record)fgFactory.get(document); | |
69 if (record is null) { | |
70 record= new Record(document); | |
71 fgFactory.put(document, record); | |
72 } | |
73 record.count++; | |
74 } | |
75 | |
76 /** | |
77 * Disconnects the given document from this registry. | |
78 * | |
79 * @param document the document to be disconnected | |
80 */ | |
81 public static synchronized void disconnect(IDocument document) { | |
82 Assert.isNotNull(document); | |
83 Record record= (Record)fgFactory.get(document); | |
84 record.count--; | |
85 if (record.count is 0) | |
86 fgFactory.remove(document); | |
87 | |
88 } | |
89 | |
90 /** | |
91 * Returns the file buffer managed for the given location or <code>null</code> | |
92 * if there is no such file buffer. | |
93 * <p> | |
94 * The provided location is either a full path of a workspace resource or | |
95 * an absolute path in the local file system. The file buffer manager does | |
96 * not resolve the location of workspace resources in the case of linked | |
97 * resources. | |
98 * </p> | |
99 * | |
100 * @param document the document for which to get its undo manager | |
101 * @return the document undo manager or <code>null</code> | |
102 */ | |
103 public static synchronized IDocumentUndoManager getDocumentUndoManager(IDocument document) { | |
104 Assert.isNotNull(document); | |
105 Record record= (Record)fgFactory.get(document); | |
106 if (record is null) | |
107 return null; | |
108 return record.undoManager; | |
109 } | |
110 | |
111 } |