511d0412cac79728bd93e3961741d164507fd3d6
[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.IJavaElement;
18 import net.sourceforge.phpdt.core.IOpenable;
19
20 import org.eclipse.core.resources.IFile;
21 import org.eclipse.core.resources.IResource;
22
23
24 /**
25  * The buffer manager manages the set of open buffers.
26  * It implements an LRU cache of buffers.
27  */
28 public class BufferManager implements IBufferFactory {
29
30         protected static BufferManager DEFAULT_BUFFER_MANAGER;
31
32         /**
33          * LRU cache of buffers. The key and value for an entry
34          * in the table is the identical buffer.
35          */
36         protected OverflowingLRUCache openBuffers = new BufferCache(60);
37
38 /**
39  * Creates a new buffer manager.
40  */
41 public BufferManager() {
42 }
43 /**
44  * Adds a buffer to the table of open buffers.
45  */
46 protected void addBuffer(IBuffer buffer) {
47         openBuffers.put(buffer.getOwner(), buffer);
48 }
49 /**
50  * @see IBufferFactory#createBuffer(IOpenable)
51  */
52 public IBuffer createBuffer(IOpenable owner) {
53         IJavaElement element = (IJavaElement)owner;
54         IResource resource = element.getResource();
55         return 
56                 new Buffer(
57                         resource instanceof IFile ? (IFile)resource : null, 
58                         owner, 
59                         element.isReadOnly());
60 }
61
62 /**
63  * Returns the open buffer associated with the given owner,
64  * or <code>null</code> if the owner does not have an open
65  * buffer associated with it.
66  */
67 public IBuffer getBuffer(IOpenable owner) {
68         return (IBuffer)openBuffers.get(owner);
69 }
70 /**
71  * Returns the default buffer manager.
72  */
73 public synchronized static BufferManager getDefaultBufferManager() {
74         if (DEFAULT_BUFFER_MANAGER == null) {
75                 DEFAULT_BUFFER_MANAGER = new BufferManager();
76         }
77         return DEFAULT_BUFFER_MANAGER;
78 }
79 /**
80  * Returns the default buffer factory.
81  */
82 public IBufferFactory getDefaultBufferFactory() {
83         return this;
84 }
85 /**
86  * Returns an enumeration of all open buffers.
87  * <p> 
88  * The <code>Enumeration</code> answered is thread safe.
89  *
90  * @see OverflowingLRUCache
91  * @return Enumeration of IBuffer
92  */
93 public Enumeration getOpenBuffers() {
94         synchronized (openBuffers) {
95                 openBuffers.shrink();
96                 return openBuffers.elements();
97         }
98 }
99
100
101 /**
102  * Removes a buffer from the table of open buffers.
103  */
104 protected void removeBuffer(IBuffer buffer) {
105         openBuffers.remove(buffer.getOwner());
106 }
107 }