Skip to main content
summaryrefslogtreecommitdiffstats
blob: 74891f3a08a86e455474beadee72d725d53aae5d (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
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
/*******************************************************************************
 * Copyright (c) 2008, 2012 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.common.ui.tests.internal.jface;

import org.eclipse.jface.action.Action;
import org.eclipse.jface.action.ActionContributionItem;
import org.eclipse.jface.action.IAction;
import org.eclipse.jface.viewers.ArrayContentProvider;
import org.eclipse.jface.viewers.BaseLabelProvider;
import org.eclipse.jface.viewers.ComboViewer;
import org.eclipse.jface.viewers.ILabelProvider;
import org.eclipse.jface.viewers.ISelectionChangedListener;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.jface.viewers.SelectionChangedEvent;
import org.eclipse.jface.viewers.StructuredSelection;
import org.eclipse.jface.viewers.TreeViewer;
import org.eclipse.jface.window.ApplicationWindow;
import org.eclipse.jface.window.Window;
import org.eclipse.jpt.common.ui.internal.jface.AbstractItemExtendedLabelProvider;
import org.eclipse.jpt.common.ui.internal.jface.ItemTreeStateProviderManager;
import org.eclipse.jpt.common.ui.internal.jface.StaticItemTreeContentProvider;
import org.eclipse.jpt.common.ui.jface.ItemExtendedLabelProvider;
import org.eclipse.jpt.common.ui.jface.ItemExtendedLabelProviderFactory;
import org.eclipse.jpt.common.ui.jface.ItemLabelProvider;
import org.eclipse.jpt.common.ui.jface.ItemTreeContentProvider;
import org.eclipse.jpt.common.ui.jface.ItemTreeContentProviderFactory;
import org.eclipse.jpt.common.ui.jface.TreeStateProvider;
import org.eclipse.jpt.common.utility.internal.model.AbstractModel;
import org.eclipse.jpt.common.utility.internal.model.value.PropertyAspectAdapter;
import org.eclipse.jpt.common.utility.internal.model.value.SimplePropertyValueModel;
import org.eclipse.jpt.common.utility.model.event.PropertyChangeEvent;
import org.eclipse.jpt.common.utility.model.listener.PropertyChangeListener;
import org.eclipse.jpt.common.utility.model.value.PropertyValueModel;
import org.eclipse.jpt.common.utility.model.value.ModifiablePropertyValueModel;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.graphics.ImageData;
import org.eclipse.swt.graphics.PaletteData;
import org.eclipse.swt.graphics.RGB;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;

public class DelegatingLabelProviderUiTest extends ApplicationWindow
{
	private TreeViewer tree;
	
	private ModifiablePropertyValueModel<Vehicle> selectedVehicle;
	
	
	public static void main(String[] args) {
		Window window = new DelegatingLabelProviderUiTest(args);
		window.setBlockOnOpen(true);
		window.open();
		Display.getCurrent().dispose();
		System.exit(0);
	}
	
	
	private DelegatingLabelProviderUiTest(String[] args) {
		super(null);
		this.selectedVehicle = new SimplePropertyValueModel<Vehicle>();
	}
	
	
	@Override
	protected Control createContents(Composite parent) {
		((Shell) parent).setText(this.getClass().getSimpleName());
		parent.setSize(400, 400);
		parent.setLayout(new GridLayout());
		Composite mainPanel = new Composite(parent, SWT.NONE);
		mainPanel.setLayoutData(new GridData(GridData.FILL_BOTH));
		mainPanel.setLayout(new GridLayout());
		buildTreePanel(mainPanel);
		buildControlPanel(mainPanel);
		return mainPanel;
	}
	
	private void buildTreePanel(Composite parent) {
		Composite panel = new Composite(parent, SWT.NONE);
		panel.setLayoutData(new GridData(GridData.FILL_BOTH));
		panel.setLayout(new GridLayout());
		
		Label label = new Label(panel, SWT.NONE);
		label.setLayoutData(new GridData(GridData.BEGINNING, GridData.CENTER, false, false));
		label.setText("My Vehicles");
		
		tree = new TreeViewer(panel, SWT.SINGLE | SWT.H_SCROLL | SWT.V_SCROLL | SWT.BORDER);
		tree.getTree().setLayoutData(new GridData(GridData.FILL, GridData.FILL, true, true));
		TreeStateProvider contentAndLabelProvider = 
			new ItemTreeStateProviderManager(
				new VehicleContentProviderFactory(),
				new VehicleLabelProviderFactory());
		tree.setContentProvider(contentAndLabelProvider);
		tree.setLabelProvider(contentAndLabelProvider);
		tree.setInput(new Root());
		tree.addSelectionChangedListener(buildTreeSelectionChangedListener());
	}
	
	private ISelectionChangedListener buildTreeSelectionChangedListener() {
		return new ISelectionChangedListener() {
			public void selectionChanged(SelectionChangedEvent event) {
				selectedVehicle.setValue((Vehicle) ((IStructuredSelection) event.getSelection()).getFirstElement());
			}
		};
	}
	
	private void buildControlPanel(Composite parent) {
		Composite panel = new Composite(parent, SWT.NONE);
		panel.setLayoutData(new GridData(GridData.FILL, GridData.CENTER, true, false));
		panel.setLayout(new GridLayout());
		buildUpperControlPanel(panel);
		buildLowerControlPanel(panel);
	}
	
	private void buildUpperControlPanel(Composite parent) {
		Composite panel = new Composite(parent, SWT.NONE);
		panel.setLayoutData(new GridData(GridData.FILL, GridData.CENTER, true, false));
		panel.setLayout(new GridLayout(2, true));
		buildVehicleCombo(panel);
		buildColorCombo(panel);
	}
	
	private void buildVehicleCombo(Composite parent) {
		final ComboViewer combo = new ComboViewer(parent, SWT.READ_ONLY);
		combo.getCombo().setLayoutData(new GridData(GridData.FILL, GridData.CENTER, true, false));
		combo.setContentProvider(new ArrayContentProvider());
		combo.setLabelProvider(new VehicleTypeLabelProvider());
		combo.setInput(
			new VehicleType[] {
				VehicleType.BICYCLE, VehicleType.CAR, 
				VehicleType.TRUCK, VehicleType.BOAT
			});
		combo.getCombo().setEnabled(false);
		combo.addSelectionChangedListener(
			new ISelectionChangedListener() {
				public void selectionChanged(SelectionChangedEvent event) {
					selectedVehicle().setVehicleType((VehicleType) ((StructuredSelection) event.getSelection()).getFirstElement()); 
				}
			});
		selectedVehicle.addPropertyChangeListener(
			PropertyValueModel.VALUE, 
			new PropertyChangeListener() {
				public void propertyChanged(PropertyChangeEvent event) {
					Vehicle vehicle = selectedVehicle();
					combo.getCombo().setEnabled(vehicle != null);
					combo.setSelection(new StructuredSelection((vehicle == null) ? null : vehicle.vehicleType()));
				}
			});
	}
	
	private void buildColorCombo(Composite parent) {
		final ComboViewer combo = new ComboViewer(parent, SWT.READ_ONLY);
		combo.getCombo().setLayoutData(new GridData(GridData.FILL, GridData.CENTER, true, false));
		combo.setContentProvider(new ArrayContentProvider());
		combo.setLabelProvider(new ColorLabelProvider());
		combo.setInput(new Color[] {Color.RED, Color.BLUE, Color.YELLOW, Color.GREEN});
		combo.addSelectionChangedListener(
			new ISelectionChangedListener() {
				public void selectionChanged(SelectionChangedEvent event) {
					selectedVehicle().setColor((Color) ((StructuredSelection) event.getSelection()).getFirstElement()); 
				}
			});
		selectedVehicle.addPropertyChangeListener(
			PropertyValueModel.VALUE, 
			new PropertyChangeListener() {
				public void propertyChanged(PropertyChangeEvent event) {
					Vehicle vehicle = selectedVehicle();
					combo.getCombo().setEnabled(vehicle != null);
					combo.setSelection(new StructuredSelection((vehicle == null) ? null : vehicle.color()));
				}
			});
	}
	
	private void buildLowerControlPanel(Composite parent) {
		Composite panel = new Composite(parent, SWT.NONE);
		panel.setLayoutData(new GridData(GridData.FILL, GridData.CENTER, true, false));
		panel.setLayout(new GridLayout(3, false));
		buildEffectsLabel(panel);
		buildGreyedCheckBox(panel);
		buildTranslucentCheckBox(panel);
		buildActionPanel(panel);
	}
	
	private void buildEffectsLabel(Composite parent) {
		Label label = new Label(parent, SWT.LEFT);
		label.setText("Color effects: ");
		label.setLayoutData(new GridData(GridData.BEGINNING, GridData.CENTER, false, false, 3, 1));
	}
	
	private void buildGreyedCheckBox(Composite parent) {
		final Button button = new Button(parent, SWT.CHECK);
		button.setLayoutData(new GridData(GridData.BEGINNING, GridData.CENTER, false, false));
		button.setText("greyed");
		button.setEnabled(false);
		button.addSelectionListener(
			new SelectionAdapter() {
				@Override
				public void widgetSelected(SelectionEvent e) {
					selectedVehicle().setGreyed(button.getSelection());
				}
			});
		selectedVehicle.addPropertyChangeListener(
			PropertyValueModel.VALUE, 
			new PropertyChangeListener() {
				public void propertyChanged(PropertyChangeEvent event) {
					Vehicle vehicle = selectedVehicle();
					button.setEnabled(vehicle != null);
					button.setSelection(vehicle != null && vehicle.isGreyed());
				}
			});
	}
	
	private void buildTranslucentCheckBox(Composite parent) {
		final Button button = new Button(parent, SWT.CHECK);
		button.setLayoutData(new GridData(GridData.BEGINNING, GridData.CENTER, true, false));
		button.setText("translucent");
		button.setEnabled(false);
		button.addSelectionListener(
			new SelectionAdapter() {
				@Override
				public void widgetSelected(SelectionEvent e) {
					selectedVehicle().setTranslucent(button.getSelection());
				}
			});
		selectedVehicle.addPropertyChangeListener(
			PropertyValueModel.VALUE, 
			new PropertyChangeListener() {
				public void propertyChanged(PropertyChangeEvent event) {
					Vehicle vehicle = selectedVehicle();
					button.setEnabled(vehicle != null);
					button.setSelection(vehicle != null && vehicle.isTranslucent());
				}
			});
	}
	
	private void buildActionPanel(Composite parent) {
		Composite panel = new Composite(parent, SWT.NONE);
		panel.setLayoutData(new GridData(GridData.END, GridData.FILL, false, false));
		panel.setLayout(new GridLayout());
		buildRefreshTreeACI().fill(panel);
	}
	
	private ActionContributionItem buildRefreshTreeACI() {
		Action action = new Action("Refresh tree", IAction.AS_PUSH_BUTTON) {
			@Override
			public void run() {
				refreshTree();
			}
		};
		action.setToolTipText("Refresh the tree's labels");
		return new ActionContributionItem(action);
	}
	
	void refreshTree() {
		tree.refresh();
	}
	
	private Vehicle selectedVehicle() {
		return selectedVehicle.getValue();
	}
	
	
	private static class VehicleTypeLabelProvider extends BaseLabelProvider
		implements ILabelProvider
	{
		public Image getImage(Object element) {
			return null;
		}
		
		public String getText(Object element) {
			return ((VehicleType) element).description();
		}
	}
	
	
	private static class ColorLabelProvider extends BaseLabelProvider
		implements ILabelProvider
	{
		public Image getImage(Object element) {
			return null;
		}
		
		public String getText(Object element) {
			return ((Color) element).description();
		}
	}
	
	
	private static class VehicleContentProviderFactory
		implements ItemTreeContentProviderFactory
	{
		public ItemTreeContentProvider buildProvider(Object item, ItemTreeContentProvider.Manager manager) {
			if (item instanceof Root) {
				return this.buildRootProvider((Root) item);
			}
			return this.buildVehicleProvider((Vehicle) item);
		}
		protected ItemTreeContentProvider buildRootProvider(Root item) {
			return new StaticItemTreeContentProvider(null, item.vehicles());
		}
		protected ItemTreeContentProvider buildVehicleProvider(Vehicle item) {
			return new StaticItemTreeContentProvider(item.parent());
		}
	}
	
	
	private static class VehicleLabelProviderFactory
		implements ItemExtendedLabelProviderFactory
	{
		public ItemExtendedLabelProvider buildProvider(Object item, ItemExtendedLabelProvider.Manager manager) {
			return new VehicleLabelProvider((Vehicle) item, manager);
		}
	}
	
	
	private static class VehicleLabelProvider
		extends AbstractItemExtendedLabelProvider<Vehicle>
	{
		public VehicleLabelProvider(Vehicle vehicle, ItemLabelProvider.Manager manager) {
			super(vehicle, manager);
		}
		
		@Override
		protected PropertyValueModel<Image> buildImageModel() {
			return new PropertyAspectAdapter<Vehicle, Image>(IMAGE_ASPECT_NAMES, this.item) {
				@Override
				protected Image buildValue_() {
					return subject.image();
				}
			};
		}
		private static final String[] IMAGE_ASPECT_NAMES =
				new String[] {
					Vehicle.COLOR_PROPERTY,
					Vehicle.GREYED_PROPERTY,
					Vehicle.TRANSLUCENT_PROPERTY
				};
		
		@Override
		protected PropertyValueModel<String> buildTextModel() {
			return new PropertyAspectAdapter<Vehicle, String>(TEXT_ASPECT_NAMES, this.item) {
				@Override
				protected String buildValue_() {
					return subject.color().description() + ' ' + subject.vehicleType().description();
				}
			};
		}
		private static final String[] TEXT_ASPECT_NAMES =
				new String[] {
					Vehicle.VEHICLE_TYPE_PROPERTY,
					Vehicle.COLOR_PROPERTY
				};
		
		@Override
		protected PropertyValueModel<String> buildDescriptionModel() {
			return buildTextModel();
		}
	}
	
	
	private static abstract class TreeNode extends AbstractModel
	{
		private TreeNode parent;
		
		
		public TreeNode(TreeNode parent) {
			this.parent = parent;
		}
		
		
		public TreeNode parent() {
			return parent;
		}
	}
	
	
	private static class Root extends TreeNode
	{
		protected final Vehicle[] vehicles;
		
		
		public Root() {
			super(null);
			vehicles = new Vehicle[] {
				new Vehicle(this, VehicleType.BICYCLE, Color.BLUE),
				new Vehicle(this, VehicleType.CAR, Color.YELLOW),
				new Vehicle(this, VehicleType.TRUCK, Color.RED),
				new Vehicle(this, VehicleType.BOAT, Color.GREEN)};
		}
		
		public Vehicle[] vehicles() {
			return vehicles;
		}
	}
	
	
	private static class Vehicle extends TreeNode
	{
		private VehicleType vehicleType;
		public final static String VEHICLE_TYPE_PROPERTY = "vehicleType";
		
		private Color color;
		public final static String COLOR_PROPERTY = "color";
		
		private boolean greyed = false;
		public final static String GREYED_PROPERTY = "greyed";
		
		private boolean translucent = false;
		public final static String TRANSLUCENT_PROPERTY = "translucent";
		
		private Image image;
		
			
		public Vehicle(TreeNode parent, VehicleType vehicleType, Color color) {
			super(parent);
			this.vehicleType = vehicleType;
			this.color = color;
		}
		
		public VehicleType vehicleType() {
			return vehicleType;
		}
		
		public void setVehicleType(VehicleType newVehicleType) {
			VehicleType oldVehicleType = vehicleType;
			vehicleType = newVehicleType;
			firePropertyChanged(VEHICLE_TYPE_PROPERTY, oldVehicleType, newVehicleType);
		}
		
		public Color color() {
			return color;
		}
		
		public void setColor(Color newColor) {
			Color oldColor = color;
			color = newColor;
			firePropertyChanged(COLOR_PROPERTY, oldColor, newColor);
		}
		
		public boolean isGreyed() {
			return greyed;
		}
		
		public void setGreyed(boolean newGreyed) {
			boolean oldGreyed = greyed;
			greyed = newGreyed;
			firePropertyChanged(GREYED_PROPERTY, oldGreyed, newGreyed);
		}
		
		public boolean isTranslucent() {
			return translucent;
		}
		
		public void setTranslucent(boolean newTranslucent) {
			boolean oldTranslucent = translucent;
			translucent = newTranslucent;
			firePropertyChanged(TRANSLUCENT_PROPERTY, oldTranslucent, newTranslucent);
		}
		
		public Image image() {
			if (image != null) {
				image.dispose();
			}
			
			return ImageFactory.image(color(), greyed, translucent);
		}
	}
	
	
	private static enum VehicleType
	{
		BICYCLE("bicycle"),
		CAR("car"),
		TRUCK("truck"),
		BOAT("boat");
		
		private final String description;
		
		private VehicleType(String description) {
			this.description = description;
		}
		
		public String description() {
			return description;
		}
		
		@Override
		public String toString() {
			return description();
		}
	}
	
	
	private static enum Color
	{
		RED("red", new RGB(255, 0, 0)),
		BLUE("blue", new RGB(0, 0, 255)),
		YELLOW("yellow", new RGB(255, 255, 0)),
		GREEN("green", new RGB(0, 255, 0));
		
		private final String description;
		
		private final RGB rgb;
		
		private Color(String description, RGB rgb) {
			this.description = description;
			this.rgb = rgb;
		}
		
		public String description() {
			return description;
		}
		
		public RGB rgb() {
			return rgb;
		}
		
		@Override
		public String toString() {
			return description();
		}
	}
	
	
	private static class ImageFactory
	{
		private static RGB rgb(Color color, boolean greyed, boolean translucent) {
			RGB rgb = (greyed) ? new RGB(127, 127, 127) : color.rgb();
			if (translucent) {
				rgb = new RGB(translucify(rgb.red), translucify(rgb.green), translucify(rgb.blue));
			}
			return rgb;
		}
		
		private static int translucify(int color) {
			return 255 - (int) ((255 - color) * 0.3);
		}
		
		public static Image image(Color color, boolean greyed, boolean translucent) {
			PaletteData pd = new PaletteData(new RGB[] {rgb(color, greyed, translucent)});
			ImageData id = new ImageData(20, 20, 1, pd);
			for (int x = 0; x < 20; x ++) {
				for (int y = 0; y < 20; y ++) {
					id.setPixel(x, y, 0);
				}
			}
			return new Image(Display.getCurrent(), id);
		}
	}
}

Back to the top