KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > de > loskutov > bco > editors > BytecodeBufferManager


1 package de.loskutov.bco.editors;
2
3 import java.lang.reflect.Method JavaDoc;
4
5 import org.eclipse.jdt.core.IBuffer;
6 import org.eclipse.jdt.core.IOpenable;
7 import org.eclipse.jdt.internal.core.BufferManager;
8
9 /**
10  * This class is a hack that mades JDT <code>BufferManager</code> methods
11  * <code>addBuffer()</code> and <code>removeBuffer()</code> accessible.
12  *
13  * It does NOT modify or replace the instance of default buffer manager, because if they
14  * are more then one plugin which performs such hack, then it will end up in a big bang.
15  *
16  * @author Andrei
17  */

18 public final class BytecodeBufferManager {
19
20     private BytecodeBufferManager() {
21         super();
22     }
23
24     public static IBuffer getBuffer(IOpenable owner) {
25         return BufferManager.getDefaultBufferManager().getBuffer(owner);
26     }
27
28     public static IBuffer createBuffer(IOpenable owner) {
29         return BufferManager.getDefaultBufferManager().createBuffer(owner);
30     }
31
32     /**
33      * @see BufferManager#addBuffer(IBuffer)
34      */

35     public static void addBuffer(IBuffer buffer) {
36         BufferManager manager = BufferManager.getDefaultBufferManager();
37         try {
38             Method JavaDoc addMethod = BufferManager.class.getDeclaredMethod(
39                 "addBuffer", new Class JavaDoc[]{IBuffer.class});
40             addMethod.setAccessible(true);
41             addMethod.invoke(manager, new Object JavaDoc[]{buffer});
42         } catch (Exception JavaDoc e) {
43             // TODO Auto-generated catch block
44
e.printStackTrace();
45         }
46     }
47
48     /**
49      * @see BufferManager#removeBuffer(IBuffer)
50      */

51     public static void removeBuffer(IBuffer buffer) {
52         if (buffer != null) {
53             BufferManager manager = BufferManager.getDefaultBufferManager();
54             try {
55                 Method JavaDoc removeMethod = BufferManager.class.getDeclaredMethod(
56                     "removeBuffer", new Class JavaDoc[]{IBuffer.class});
57                 removeMethod.setAccessible(true);
58                 removeMethod.invoke(manager, new Object JavaDoc[]{buffer});
59             } catch (Exception JavaDoc e) {
60                 // TODO Auto-generated catch block
61
e.printStackTrace();
62             }
63         }
64     }
65 }
66
Popular Tags