Changes:
[phpeclipse.git] / net.sourceforge.phpeclipse / src / net / sourceforge / phpdt / internal / core / BufferManager.java
1 /*******************************************************************************
2  * Copyright (c) 2000, 2003 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials 
4  * are made available under the terms of the Common Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/cpl-v10.html
7  * 
8  * Contributors:
9  *     IBM Corporation - initial API and implementation
10  *******************************************************************************/
11 package net.sourceforge.phpdt.internal.core;
12
13 import java.util.Enumeration;
14
15 import net.sourceforge.phpdt.core.IBuffer;
16 import net.sourceforge.phpdt.core.IBufferFactory;
17 import net.sourceforge.phpdt.core.IOpenable;
18
19
20 /**
21  * The buffer manager manages the set of open buffers.
22  * It implements an LRU cache of buffers.
23  */
24 public class BufferManager implements IBufferFactory {
25
26         protected static BufferManager DEFAULT_BUFFER_MANAGER;
27
28         /**
29          * LRU cache of buffers. The key and value for an entry
30          * in the table is the identical buffer.
31          */
32         protected OverflowingLRUCache openBuffers = new BufferCache(60);
33
34 /**
35  * Creates a new buffer manager.
36  */
37 public BufferManager() {
38 }
39 /**
40  * Adds a buffer to the table of open buffers.
41  */
42 protected void addBuffer(IBuffer buffer) {
43         openBuffers.put(buffer.getOwner(), buffer);
44 }
45 /**
46  * @see IBufferFactory#createBuffer(IOpenable)
47  */
48 //public IBuffer createBuffer(IOpenable owner) {
49 //      IJavaElement element = (IJavaElement)owner;
50 //      IResource resource = element.getResource();
51 //      return 
52 //              new Buffer(
53 //                      resource instanceof IFile ? (IFile)resource : null, 
54 //                      owner, 
55 //                      element.isReadOnly());
56 //}
57
58 /**
59  * Returns the open buffer associated with the given owner,
60  * or <code>null</code> if the owner does not have an open
61  * buffer associated with it.
62  */
63 public IBuffer getBuffer(IOpenable owner) {
64         return (IBuffer)openBuffers.get(owner);
65 }
66 /**
67  * Returns the default buffer manager.
68  */
69 public synchronized static BufferManager getDefaultBufferManager() {
70         if (DEFAULT_BUFFER_MANAGER == null) {
71                 DEFAULT_BUFFER_MANAGER = new BufferManager();
72         }
73         return DEFAULT_BUFFER_MANAGER;
74 }
75 /**
76  * Returns the default buffer factory.
77  */
78 public IBufferFactory getDefaultBufferFactory() {
79         return this;
80 }
81 /**
82  * Returns an enumeration of all open buffers.
83  * <p> 
84  * The <code>Enumeration</code> answered is thread safe.
85  *
86  * @see OverflowingLRUCache
87  * @return Enumeration of IBuffer
88  */
89 public Enumeration getOpenBuffers() {
90         synchronized (openBuffers) {
91                 openBuffers.shrink();
92                 return openBuffers.elements();
93         }
94 }
95
96
97 /**
98  * Removes a buffer from the table of open buffers.
99  */
100 protected void removeBuffer(IBuffer buffer) {
101         openBuffers.remove(buffer.getOwner());
102 }
103 }