Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: c1eb4ff2ea7975179ff5ec73521fa84bf40acba6 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
/*******************************************************************************
 * Copyright (c) 2005, 2016 IBM Corporation and others.
 *
 * This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License 2.0
 * which accompanies this distribution, and is available at
 * https://www.eclipse.org/legal/epl-2.0/
 *
 * SPDX-License-Identifier: EPL-2.0
 *
 * Contributors:
 *     IBM Corporation - initial API and implementation
 *     Wind River Systems - added saving and restoring properties
 *******************************************************************************/
package org.eclipse.debug.internal.ui.viewers.model.provisional;

import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;

import org.eclipse.core.runtime.ListenerList;
import org.eclipse.core.runtime.SafeRunner;
import org.eclipse.jface.util.IPropertyChangeListener;
import org.eclipse.jface.util.PropertyChangeEvent;
import org.eclipse.jface.util.SafeRunnable;
import org.eclipse.ui.IElementFactory;
import org.eclipse.ui.IMemento;
import org.eclipse.ui.IPersistableElement;
import org.eclipse.ui.IWorkbenchPart;
import org.eclipse.ui.IWorkbenchWindow;
import org.eclipse.ui.PlatformUI;

/**
 * Presentation context.
 * <p>
 * Clients may instantiate and subclass this class.
 * </p>
 * @since 3.2
 */
public class PresentationContext implements IPresentationContext {

    private static final String PRESENTATION_CONTEXT_PROPERTIES = "PRESENTATION_CONTEXT_PROPERTIES";  //$NON-NLS-1$
    private static final String BOOLEAN = "BOOLEAN";  //$NON-NLS-1$
    private static final String STRING = "STRING";  //$NON-NLS-1$
    private static final String INTEGER = "INTEGER";  //$NON-NLS-1$
    private static final String PERSISTABLE = "PERSISTABLE";  //$NON-NLS-1$

    final private String fId;
	final private ListenerList<IPropertyChangeListener> fListeners = new ListenerList<>();
	final private Map<String, Object> fProperties = new HashMap<>();
    private IWorkbenchWindow fWindow;
    private IWorkbenchPart fPart;

    /**
     * Constructs a presentation context for the given id.
     *
     * @param id presentation context id
     */
    public PresentationContext(String id) {
        this (id, null, null);
    }

    /**
     * Constructs a presentation context for the given id and window.
     *
     * @param id presentation context id
     * @param window presentation context window, may be <code>null</code>
     */
    public PresentationContext(String id, IWorkbenchWindow window) {
        this (id, window, null);
    }

    /**
     * Constructs a presentation context for the given id and part.
     * The presentation context window is derived from the part.
     *
     * @param id presentation context id
     * @param part presentation context part, may be <code>null</code>
     */
    public PresentationContext(String id, IWorkbenchPart part) {
        this (id, part == null ? null : part.getSite().getWorkbenchWindow(), part);
    }

    /**
     * Constructs a presentation context for the given id and part.
     * The presentation context id and window are derived from the part.
     *
     * @param part presentation context part, can NOT be <code>null</code>
     */
    public PresentationContext(IWorkbenchPart part) {
        this (part.getSite().getId(), part.getSite().getWorkbenchWindow(), part);
    }

    private PresentationContext(String id, IWorkbenchWindow window, IWorkbenchPart part) {
    	fId = id;
    	fWindow = window;
    	fPart = part;
    }

	@Override
	public String[] getColumns() {
		return (String[]) getProperty(IPresentationContext.PROPERTY_COLUMNS);
	}

	/**
	 * Fires a property change event to all registered listeners
	 *
	 * @param property property name
	 * @param oldValue old value or <code>null</code>
	 * @param newValue new value or <code>null</code>
	 */
	protected void firePropertyChange(String property, Object oldValue, Object newValue) {
		if (!fListeners.isEmpty()) {
			final PropertyChangeEvent event = new PropertyChangeEvent(this, property, oldValue, newValue);
			for (IPropertyChangeListener iPropertyChangeListener : fListeners) {
				final IPropertyChangeListener listener = iPropertyChangeListener;
				SafeRunner.run(new SafeRunnable() {
					@Override
					public void run() throws Exception {
						listener.propertyChange(event);
					}
				});
			}
		}
	}

	/**
	 * Sets the visible column ids.
	 *
	 * @param ids column identifiers
	 */
	public void setColumns(String[] ids) {
		setProperty(IPresentationContext.PROPERTY_COLUMNS, ids);
	}

	@Override
	public void dispose() {
        fProperties.clear();
        setProperty(PROPERTY_DISPOSED, Boolean.TRUE);
		fListeners.clear();
		// Free the reference to fWindow (Bug 321658).
		fWindow = null;
		fPart = null;
	}

	@Override
	public void addPropertyChangeListener(IPropertyChangeListener listener) {
		fListeners.add(listener);
	}

	@Override
	public void removePropertyChangeListener(IPropertyChangeListener listener) {
		fListeners.remove(listener);
	}

	@Override
	public String getId() {
		return fId;
	}

	@Override
	public Object getProperty(String property) {
		synchronized (fProperties) {
			return fProperties.get(property);
		}
	}

	@Override
	public void setProperty(String property, Object value) {
	    Object oldValue = null;
	    boolean propertySet = false;
		synchronized (fProperties) {
			oldValue = fProperties.get(property);
			if (!isEqual(oldValue, value)) {
			    propertySet = true;
				fProperties.put(property, value);
			}
		}

		if (propertySet) {
		    firePropertyChange(property, oldValue, value);
		}
	}

	/**
	 * Restores the presentation context properties from the given memento.
	 * @param memento Memento to restore from.
	 */
	public void initProperties(IMemento memento) {
	    IMemento presentationMemento = null;

        IMemento[] mementos = memento.getChildren(PRESENTATION_CONTEXT_PROPERTIES);
        for (int i = 0; i < mementos.length; i++) {
            if (getId().equals(mementos[i].getID())) {
                presentationMemento = mementos[i];
                break;
            }
        }

        if (presentationMemento != null) {
            IMemento[] stringProperties = presentationMemento.getChildren(STRING);
            for (int i = 0; i < stringProperties.length; i++) {
                fProperties.put(stringProperties[i].getID(), stringProperties[i].getString(STRING));
            }

            IMemento[] integerMementos = presentationMemento.getChildren(INTEGER);
            for (int i = 0; i < integerMementos.length; i++) {
                fProperties.put(integerMementos[i].getID(), integerMementos[i].getInteger(INTEGER));
            }

            IMemento[] booleanMementos = presentationMemento.getChildren(BOOLEAN);
            for (int i = 0; i < booleanMementos.length; i++) {
                fProperties.put(booleanMementos[i].getID(), booleanMementos[i].getBoolean(BOOLEAN));
            }

            IMemento[] persistableMementos = presentationMemento.getChildren(PERSISTABLE);
            for (int i = 0; i < persistableMementos.length; i++) {
                String factoryID = persistableMementos[i].getString(PERSISTABLE);
                if (factoryID != null) {
                    IElementFactory factory = PlatformUI.getWorkbench().getElementFactory(factoryID);
                    if (factory != null) {
                        Object element = factory.createElement(persistableMementos[i]);
                        if (element != null) {
                            fProperties.put(persistableMementos[i].getID(), element);
                        }
                    }
                }
            }
        }
	}

	/**
	 * Saves the current presentation context properties to the given memento.
	 * @param memento Memento to save to.
	 */
	public void saveProperites(IMemento memento) {
	    if (fProperties.size() == 0) {
	        return;
	    }
        IMemento properties = memento.createChild(PRESENTATION_CONTEXT_PROPERTIES, getId());
		for (Entry<String, Object> entry : fProperties.entrySet()) {
            if (entry.getValue() instanceof String) {
                IMemento value = properties.createChild(STRING, entry.getKey());
                value.putString(STRING, (String)entry.getValue());
            } else if (entry.getValue() instanceof Integer) {
                IMemento value = properties.createChild(INTEGER, entry.getKey());
                value.putInteger(INTEGER, ((Integer)entry.getValue()).intValue());
            } else if (entry.getValue() instanceof Boolean) {
                IMemento value = properties.createChild(BOOLEAN, entry.getKey());
                value.putBoolean(BOOLEAN, ((Boolean)entry.getValue()).booleanValue());
            } else if (entry.getValue() instanceof IPersistableElement) {
                IPersistableElement persistable = (IPersistableElement)entry.getValue();
                IMemento value = properties.createChild(PERSISTABLE, entry.getKey());
                value.putString(PERSISTABLE, persistable.getFactoryId());
                persistable.saveState(value);
            }
        }
	}

	private boolean isEqual(Object a, Object b) {
		if (a == null) {
			return b == null;
		}
		return a.equals(b);
	}

	@Override
	public String[] getProperties() {
		synchronized (fProperties) {
			Set<String> keys = fProperties.keySet();
			return keys.toArray(new String[keys.size()]);
		}
	}

    @Override
	public IWorkbenchPart getPart() {
        return fPart;
    }

    @Override
	public IWorkbenchWindow getWindow() {
        return fWindow;
    }


}

Back to the top