Skip to main content
summaryrefslogtreecommitdiffstats
blob: 6d87b3346223e215d016cc68f57918fa774af0f2 (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
/*******************************************************************************
 * Copyright (c) 2008 Oracle. All rights reserved.
 * This program and the accompanying materials are made available under the
 * terms of the Eclipse Public License v1.0, which accompanies this distribution
 * and is available at http://www.eclipse.org/legal/epl-v10.html.
 *
 * Contributors:
 *     Oracle - initial API and implementation
 ******************************************************************************/
package org.eclipse.jpt.ui.internal.jface;

import org.eclipse.jpt.ui.jface.DelegatingContentAndLabelProvider;
import org.eclipse.jpt.ui.jface.ItemLabelProvider;
import org.eclipse.jpt.utility.model.Model;
import org.eclipse.jpt.utility.model.event.PropertyChangeEvent;
import org.eclipse.jpt.utility.model.listener.PropertyChangeListener;
import org.eclipse.jpt.utility.model.value.PropertyValueModel;
import org.eclipse.swt.graphics.Image;

/**
 * Implementation of {@link ItemLabelProvider} that provides updating
 * label information for a Model object.
 * 
 * The typical subclass will override the following methods:
 * #buildImageModel()
 * 	   return a {@link PropertyValueModel} that represents the image for the 
 *     represented model object
 * #buildTextModel()
 *     return a {@link PropertyValueModel} that represents the text for the 
 *     represented model object.
 * #buildDescriptionModel()
 * 	   return a {@link PropertyValueModel} that represents the description for 
 *     the represented model object
 * 
 * Other methods may be overridden, but take care to preserve the logic provided 
 * by this class.
 */
public abstract class AbstractItemLabelProvider implements ItemLabelProvider
{
	private DelegatingContentAndLabelProvider labelProvider;
	
	private Model model;
	
	private PropertyValueModel<Image> imageModel;
	
	private PropertyValueModel<String> textModel;
	
	private PropertyValueModel<String> descriptionModel;
	
	private PropertyChangeListener labelChangeListener;
	
	
	protected AbstractItemLabelProvider(
			Model model, DelegatingContentAndLabelProvider labelProvider) {
		this.model = model;
		this.labelProvider = labelProvider;
		this.labelChangeListener = buildLabelChangeListener();
	}
	
	
	/**
	 * Construct a listener to update the viewer (through the label provider)
	 * if the text or image changes
	 */
	protected PropertyChangeListener buildLabelChangeListener() {
		return new PropertyChangeListener() {
			public void propertyChanged(PropertyChangeEvent event) {
				labelProvider().updateLabel(model());
			}
		};
	}
	
	/**
	 * Return the image value model
	 * (lazy and just-in-time initialized)
	 */
	protected synchronized PropertyValueModel<Image> imageModel() {
		if (imageModel == null) {
			imageModel = buildImageModel();
			engageImageModel();
		}
		return imageModel;
	}
	
	/**
	 * Construct an image model
	 */
	protected abstract PropertyValueModel<Image> buildImageModel();
	
	/** 
	 * Should only be overridden with a call to super.engageImageModel() before 
	 * subclass logic 
	 */
	protected void engageImageModel() {
		imageModel.addPropertyChangeListener(PropertyValueModel.VALUE, labelChangeListener);
	}
	
	/** 
	 * Should only be overridden with a call to super.disengageImageModel() after 
	 * subclass logic 
	 */
	protected void disengageImageModel() {
		imageModel.removePropertyChangeListener(PropertyValueModel.VALUE, labelChangeListener);
	}
	
	/**
	 * Return the text value model
	 * (lazy and just-in-time initialized)
	 */
	protected synchronized PropertyValueModel<String> textModel() {
		if (textModel == null) {
			textModel = buildTextModel();
			engageTextModel();
		}
		return textModel;
	}
	
	/**
	 * Construct a text value model
	 */
	protected abstract PropertyValueModel<String> buildTextModel();
	
	/** 
	 * Should only be overridden with a call to super.engageTextModel() before 
	 * subclass logic 
	 */
	protected void engageTextModel() {
		textModel.addPropertyChangeListener(PropertyValueModel.VALUE, labelChangeListener);
	}
	
	/** 
	 * Should only be overridden with a call to super.disengageTextModel() after 
	 * subclass logic 
	 */
	protected void disengageTextModel() {
		textModel.removePropertyChangeListener(PropertyValueModel.VALUE, labelChangeListener);
	}
	
	/**
	 * Return the description value model
	 * (lazy and just-in-time initialized)
	 */
	protected synchronized PropertyValueModel<String> descriptionModel() {
		if (descriptionModel == null) {
			descriptionModel = buildDescriptionModel();
			engageDescriptionModel();
		}
		return descriptionModel;
	}
	
	/**
	 * Construct a description value model
	 */
	protected abstract PropertyValueModel<String> buildDescriptionModel();
	
	/** 
	 * Should only be overridden with a call to super.engageDescriptionModel() before 
	 * subclass logic 
	 */
	protected void engageDescriptionModel() {
		descriptionModel.addPropertyChangeListener(PropertyValueModel.VALUE, labelChangeListener);
	}
	
	/** 
	 * Should only be overridden with a call to super.disengageDescriptionModel() after 
	 * subclass logic 
	 */
	protected void disengageDescriptionModel() {
		descriptionModel.removePropertyChangeListener(PropertyValueModel.VALUE, labelChangeListener);
	}
	
	/**
	 * Return the model object represented by this item
	 */
	public Model model() {
		return model;
	}
	
	/**
	 * Return the label provider that delegates to this item
	 */
	public DelegatingContentAndLabelProvider labelProvider() {
		return labelProvider;
	}
	
	public Image getImage() {
		return imageModel().getValue();
	}
	
	public String getText() {
		return textModel().getValue();
	}
	
	public String getDescription() {
		return descriptionModel().getValue();
	}
	
	public void dispose() {
		disposeTextModel();
		disposeImageModel();
		disposeDescriptionModel();	
	}
	
	protected synchronized void disposeTextModel() {
		if (this.textModel != null) {
			disengageTextModel();
			this.textModel = null;
		}
	}
	
	protected synchronized void disposeImageModel() {
		if (this.imageModel != null) {
			disengageImageModel();
			this.imageModel = null;
		}
	}
	
	protected synchronized void disposeDescriptionModel() {
		if (this.descriptionModel != null) {
			disengageDescriptionModel();
			this.descriptionModel = null;
		}
	}
}

Back to the top