KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*******************************************************************************
2  * Copyright (c) 2005, 2006 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.model.provisional;
12
13 import java.util.HashMap JavaDoc;
14 import java.util.Map JavaDoc;
15
16 import org.eclipse.core.runtime.ListenerList;
17 import org.eclipse.core.runtime.SafeRunner;
18 import org.eclipse.jface.util.IPropertyChangeListener;
19 import org.eclipse.jface.util.PropertyChangeEvent;
20 import org.eclipse.jface.util.SafeRunnable;
21
22 /**
23  * Presentation context.
24  * <p>
25  * Clients may instantiate and subclass this class.
26  * </p>
27  * @since 3.2
28  */

29 public class PresentationContext implements IPresentationContext {
30     
31     private String JavaDoc fId;
32     private ListenerList fListeners = new ListenerList();
33     private Map JavaDoc fProperties = new HashMap JavaDoc();
34     
35     /**
36      * Constructs a presentation context for the given id.
37      *
38      * @param id presentation context id
39      */

40     public PresentationContext(String JavaDoc id) {
41         fId = id;
42     }
43
44     /* (non-Javadoc)
45      * @see org.eclipse.debug.internal.ui.viewers.provisional.IPresentationContext#getColumns()
46      */

47     public String JavaDoc[] getColumns() {
48         return (String JavaDoc[]) getProperty(IPresentationContext.PROPERTY_COLUMNS);
49     }
50     
51     /**
52      * Fires a property change event to all registered listeners
53      *
54      * @param property property name
55      * @param oldValue old value or <code>null</code>
56      * @param newValue new value or <code>null</code>
57      */

58     protected void firePropertyChange(String JavaDoc property, Object JavaDoc oldValue, Object JavaDoc newValue) {
59         if (!fListeners.isEmpty()) {
60             final PropertyChangeEvent event = new PropertyChangeEvent(this, property, oldValue, newValue);
61             Object JavaDoc[] listeners = fListeners.getListeners();
62             for (int i = 0; i < listeners.length; i++) {
63                 final IPropertyChangeListener listener = (IPropertyChangeListener) listeners[i];
64                 SafeRunner.run(new SafeRunnable() {
65                     public void run() throws Exception JavaDoc {
66                         listener.propertyChange(event);
67                     }
68                 });
69             }
70         }
71     }
72     
73     /**
74      * Sets the visible column ids.
75      *
76      * @param ids column identifiers
77      */

78     public void setColumns(String JavaDoc[] ids) {
79         setProperty(IPresentationContext.PROPERTY_COLUMNS, ids);
80     }
81     
82
83     /* (non-Javadoc)
84      * @see org.eclipse.debug.internal.ui.viewers.model.provisional.IPresentationContext#dispose()
85      */

86     public void dispose() {
87         fListeners.clear();
88         fProperties.clear();
89     }
90
91     /* (non-Javadoc)
92      * @see org.eclipse.debug.internal.ui.viewers.provisional.IPresentationContext#addPropertyChangeListener(org.eclipse.jface.util.IPropertyChangeListener)
93      */

94     public void addPropertyChangeListener(IPropertyChangeListener listener) {
95         fListeners.add(listener);
96     }
97
98     /* (non-Javadoc)
99      * @see org.eclipse.debug.internal.ui.viewers.provisional.IPresentationContext#removePropertyChangeListener(org.eclipse.jface.util.IPropertyChangeListener)
100      */

101     public void removePropertyChangeListener(IPropertyChangeListener listener) {
102         fListeners.remove(listener);
103     }
104
105     /* (non-Javadoc)
106      * @see org.eclipse.debug.internal.ui.viewers.provisional.IPresentationContext#getId()
107      */

108     public String JavaDoc getId() {
109         return fId;
110     }
111
112     /* (non-Javadoc)
113      * @see org.eclipse.debug.internal.ui.viewers.provisional.IPresentationContext#getProperty(java.lang.String)
114      */

115     public Object JavaDoc getProperty(String JavaDoc property) {
116         synchronized (fProperties) {
117             return fProperties.get(property);
118         }
119     }
120
121     /* (non-Javadoc)
122      * @see org.eclipse.debug.internal.ui.viewers.provisional.IPresentationContext#setProperty(java.lang.String, java.lang.Object)
123      */

124     public void setProperty(String JavaDoc property, Object JavaDoc value) {
125         synchronized (fProperties) {
126             Object JavaDoc oldValue = fProperties.get(property);
127             if (!isEqual(oldValue, value)) {
128                 fProperties.put(property, value);
129                 firePropertyChange(property, oldValue, value);
130             }
131         }
132     }
133     
134     private boolean isEqual(Object JavaDoc a, Object JavaDoc b) {
135         if (a == null) {
136             return b == null;
137         }
138         return a.equals(b);
139     }
140     
141
142 }
143
Popular Tags