KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > debug > internal > ui > viewers > provisional > AbstractModelProxy


1 /*******************************************************************************
2  * Copyright (c) 2005, 2007 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  *******************************************************************************/

11 package org.eclipse.debug.internal.ui.viewers.provisional;
12
13 import org.eclipse.core.runtime.ISafeRunnable;
14 import org.eclipse.core.runtime.ListenerList;
15 import org.eclipse.core.runtime.Platform;
16 import org.eclipse.core.runtime.SafeRunner;
17 import org.eclipse.debug.internal.ui.DebugUIPlugin;
18 import org.eclipse.debug.internal.ui.viewers.model.provisional.IModelChangedListener;
19 import org.eclipse.debug.internal.ui.viewers.model.provisional.IModelDelta;
20 import org.eclipse.debug.internal.ui.viewers.model.provisional.IModelProxy;
21 import org.eclipse.debug.internal.ui.viewers.model.provisional.IPresentationContext;
22 import org.eclipse.jface.viewers.Viewer;
23
24 /**
25  * Common function for a model proxy.
26  * <p>
27  * Clients may subclass this class.
28  * </p>
29  * @since 3.2
30  */

31 public abstract class AbstractModelProxy implements IModelProxy {
32     
33     private IPresentationContext fContext;
34     private Viewer fViewer;
35     private boolean fDisposed = false;
36
37     private ListenerList fListeners = new ListenerList();
38     
39     // debug flags
40
public static boolean DEBUG_DELTAS = false;
41     
42     static {
43         DEBUG_DELTAS = DebugUIPlugin.DEBUG && "true".equals( //$NON-NLS-1$
44
Platform.getDebugOption("org.eclipse.debug.ui/debug/viewers/deltas")); //$NON-NLS-1$
45
}
46
47     protected Object JavaDoc[] getListeners() {
48         synchronized (fListeners) {
49             return fListeners.getListeners();
50         }
51     }
52
53     /* (non-Javadoc)
54      * @see org.eclipse.debug.internal.ui.viewers.IModelProxy#addModelChangedListener(org.eclipse.debug.internal.ui.viewers.IModelChangedListener)
55      */

56     public void addModelChangedListener(IModelChangedListener listener) {
57         synchronized (fListeners) {
58             fListeners.add(listener);
59         }
60     }
61
62     /* (non-Javadoc)
63      * @see org.eclipse.debug.internal.ui.viewers.IModelProxy#removeModelChangedListener(org.eclipse.debug.internal.ui.viewers.IModelChangedListener)
64      */

65     public void removeModelChangedListener(IModelChangedListener listener) {
66         synchronized (fListeners) {
67             fListeners.remove(listener);
68         }
69     }
70
71     /**
72      * Notifies registered listeners of the given delta.
73      *
74      * @param delta model delta to broadcast
75      */

76     public void fireModelChanged(IModelDelta delta) {
77         final IModelDelta root = getRootDelta(delta);
78         Object JavaDoc[] listeners = getListeners();
79         if (DEBUG_DELTAS) {
80             DebugUIPlugin.debug("FIRE DELTA: " + delta.toString()); //$NON-NLS-1$
81
}
82         for (int i = 0; i < listeners.length; i++) {
83             final IModelChangedListener listener = (IModelChangedListener) listeners[i];
84             ISafeRunnable safeRunnable = new ISafeRunnable() {
85                 public void handleException(Throwable JavaDoc exception) {
86                     DebugUIPlugin.log(exception);
87                 }
88
89                 public void run() throws Exception JavaDoc {
90                     listener.modelChanged(root, AbstractModelProxy.this);
91                 }
92
93             };
94             SafeRunner.run(safeRunnable);
95         }
96     }
97     
98     /**
99      * Returns the root node of the given delta.
100      *
101      * @param delta delta node
102      * @return returns the root of the given delta
103      */

104     protected IModelDelta getRootDelta(IModelDelta delta) {
105         IModelDelta parent = delta.getParentDelta();
106         while (parent != null) {
107             delta = parent;
108             parent = delta.getParentDelta();
109         }
110         return delta;
111     }
112
113     /* (non-Javadoc)
114      * @see org.eclipse.debug.internal.ui.viewers.IModelProxy#dispose()
115      */

116     public synchronized void dispose() {
117         fDisposed = true;
118         fContext = null;
119         fViewer = null;
120     }
121
122     /* (non-Javadoc)
123      * @see org.eclipse.debug.internal.ui.viewers.IModelProxy#init(org.eclipse.debug.internal.ui.viewers.IPresentationContext)
124      */

125     public void init(IPresentationContext context) {
126         fContext = context;
127     }
128     
129     /**
130      * Returns the context this model proxy is installed in.
131      *
132      * @return presentation context, or <code>null</code> if this
133      * model proxy has been disposed
134      */

135     public IPresentationContext getPresentationContext() {
136         return fContext;
137     }
138
139     /* (non-Javadoc)
140      *
141      * Subclasses should override as required.
142      *
143      * @see org.eclipse.debug.internal.ui.viewers.provisional.IModelProxy#installed(org.eclipse.jface.viewers.Viewer)
144      */

145     public void installed(Viewer viewer) {
146         fViewer = viewer;
147     }
148     
149     /**
150      * Returns the viewer this proxy is installed in.
151      *
152      * @return viewer or <code>null</code> if not installed
153      */

154     protected Viewer getViewer() {
155         return fViewer;
156     }
157     
158     /* (non-Javadoc)
159      * @see org.eclipse.debug.internal.ui.viewers.model.provisional.IModelProxy#isDisposed()
160      */

161     public synchronized boolean isDisposed() {
162         return fDisposed;
163     }
164
165 }
166
Popular Tags