Skip to main content
summaryrefslogtreecommitdiffstats
blob: 6459fccb14cb90ceb19f07bdcd339e69a2d7758f (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
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
/*******************************************************************************
 * Copyright (c) 2007, 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.swt;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.EventObject;
import java.util.Iterator;
import java.util.ListIterator;
import org.eclipse.jpt.ui.internal.listeners.SWTCollectionChangeListenerWrapper;
import org.eclipse.jpt.ui.internal.listeners.SWTListChangeListenerWrapper;
import org.eclipse.jpt.utility.internal.CollectionTools;
import org.eclipse.jpt.utility.internal.StringConverter;
import org.eclipse.jpt.utility.internal.StringTools;
import org.eclipse.jpt.utility.internal.model.value.PropertyCollectionValueModelAdapter;
import org.eclipse.jpt.utility.model.event.CollectionChangeEvent;
import org.eclipse.jpt.utility.model.event.ListChangeEvent;
import org.eclipse.jpt.utility.model.listener.CollectionChangeListener;
import org.eclipse.jpt.utility.model.listener.ListChangeListener;
import org.eclipse.jpt.utility.model.value.CollectionValueModel;
import org.eclipse.jpt.utility.model.value.ListValueModel;
import org.eclipse.jpt.utility.model.value.PropertyValueModel;
import org.eclipse.swt.events.DisposeEvent;
import org.eclipse.swt.events.DisposeListener;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.SelectionListener;
import org.eclipse.swt.widgets.List;

/**
 * This adapter provides a more object-oriented interface to the items and
 * selected items in a list box.
 * 'listHolder' contains the items in the list box.
 * 'selectedItemsHolder' contains the items in 'listHolder' that are selected
 * in the list box.
 */
@SuppressWarnings("nls")
public class ListBoxModelAdapter<E> {

	// ********** model **********
	/**
	 * A value model on the underlying model list.
	 */
	protected final ListValueModel<E> listHolder;

	/**
	 * A listener that allows us to synchronize the list box's contents with
	 * the model list.
	 */
	protected final ListChangeListener listChangeListener;

	/**
	 * A value model on the underlying model selections.
	 */
	protected final CollectionValueModel<E> selectedItemsHolder;

	/**
	 * A listener that allows us to synchronize the list box's selection with
	 * the model selections.
	 */
	protected final CollectionChangeListener selectedItemsChangeListener;

	/**
	 * A converter that converts items in the model list
	 * to strings that can be put in the list box.
	 */
	protected StringConverter<E> stringConverter;

	// ********** UI **********
	/**
	 * The list box we keep synchronized with the model list.
	 */
	protected final List listBox;

	/**
	 * A listener that allows us to synchronize our selection list holder
	 * with the list box's selection.
	 */
	protected final SelectionListener listBoxSelectionListener;

	/**
	 * Clients that are interested in selection change events.
	 */
	protected SelectionChangeListener<E>[] selectionChangeListeners;

	/**
	 * Clients that are interested in double click events.
	 */
	protected DoubleClickListener<E>[] doubleClickListeners;

	/**
	 * A listener that allows us to stop listening to stuff when the list box
	 * is disposed.
	 */
	protected final DisposeListener listBoxDisposeListener;


	// ********** static methods **********

	/**
	 * Adapt the specified model list and selections to the specified list box.
	 * Use the default string converter to convert the model items to strings
	 * to be displayed in the list box, which calls #toString() on the
	 * items in the model list.
	 */
	public static <T> ListBoxModelAdapter<T> adapt(
			ListValueModel<T> listHolder,
			CollectionValueModel<T> selectedItemsHolder,
			List listBox)
	{
		return adapt(listHolder, selectedItemsHolder, listBox, StringConverter.Default.<T>instance());
	}

	/**
	 * Adapt the specified model list and selections to the specified list box.
	 * Use the specified string converter to convert the model items to strings
	 * to be displayed in the list box.
	 */
	public static <T> ListBoxModelAdapter<T> adapt(
			ListValueModel<T> listHolder,
			CollectionValueModel<T> selectedItemsHolder,
			List listBox,
			StringConverter<T> stringConverter)
	{
		return new ListBoxModelAdapter<T>(listHolder, selectedItemsHolder, listBox, stringConverter);
	}

	/**
	 * Adapt the specified model list and selection to the specified list box.
	 * Use the default string converter to convert the model items to strings
	 * to be displayed in the list box, which calls #toString() on the
	 * items in the model list.
	 */
	public static <T> ListBoxModelAdapter<T> adapt(
			ListValueModel<T> listHolder,
			PropertyValueModel<T> selectedItemHolder,
			List listBox)
	{
		return adapt(listHolder, selectedItemHolder, listBox, StringConverter.Default.<T>instance());
	}

	/**
	 * Adapt the specified model list and selection to the specified list box.
	 * Use the specified string converter to convert the model items to strings
	 * to be displayed in the list box.
	 */
	public static <T> ListBoxModelAdapter<T> adapt(
			ListValueModel<T> listHolder,
			PropertyValueModel<T> selectedItemHolder,
			List listBox,
			StringConverter<T> stringConverter)
	{
		return new ListBoxModelAdapter<T>(listHolder, new PropertyCollectionValueModelAdapter<T>(selectedItemHolder), listBox, stringConverter);
	}


	// ********** constructors **********

	/**
	 * Constructor - the list holder, selections holder, list box, and
	 * string converter are required.
	 */
	protected ListBoxModelAdapter(
			ListValueModel<E> listHolder,
			CollectionValueModel<E> selectedItemsHolder,
			List listBox,
			StringConverter<E> stringConverter)
	{
		super();
		if ((listHolder == null) || (selectedItemsHolder == null) || (listBox == null) || (stringConverter == null)) {
			throw new NullPointerException();
		}
		this.listHolder = listHolder;
		this.selectedItemsHolder = selectedItemsHolder;
		this.listBox = listBox;
		this.stringConverter = stringConverter;

		this.listChangeListener = this.buildListChangeListener();
		this.listHolder.addListChangeListener(ListValueModel.LIST_VALUES, this.listChangeListener);

		this.selectedItemsChangeListener = this.buildSelectedItemsChangeListener();
		this.selectedItemsHolder.addCollectionChangeListener(CollectionValueModel.VALUES, this.selectedItemsChangeListener);

		this.listBoxSelectionListener = this.buildListBoxSelectionListener();
		this.listBox.addSelectionListener(this.listBoxSelectionListener);

		this.selectionChangeListeners = this.buildSelectionChangeListeners();
		this.doubleClickListeners = this.buildDoubleClickListeners();

		this.listBoxDisposeListener = this.buildListBoxDisposeListener();
		this.listBox.addDisposeListener(this.listBoxDisposeListener);

		this.synchronizeListBox();
	}


	// ********** initialization **********

	protected ListChangeListener buildListChangeListener() {
		return new SWTListChangeListenerWrapper(this.buildListChangeListener_());
	}

	protected ListChangeListener buildListChangeListener_() {
		return new ListChangeListener() {
			public void itemsAdded(ListChangeEvent event) {
				ListBoxModelAdapter.this.listItemsAdded(event);
			}
			public void itemsRemoved(ListChangeEvent event) {
				ListBoxModelAdapter.this.listItemsRemoved(event);
			}
			public void itemsMoved(ListChangeEvent event) {
				ListBoxModelAdapter.this.listItemsMoved(event);
			}
			public void itemsReplaced(ListChangeEvent event) {
				ListBoxModelAdapter.this.listItemsReplaced(event);
			}
			public void listCleared(ListChangeEvent event) {
				ListBoxModelAdapter.this.listCleared(event);
			}
			public void listChanged(ListChangeEvent event) {
				ListBoxModelAdapter.this.listChanged(event);
			}
			@Override
			public String toString() {
				return "list listener";
			}
		};
	}

	protected CollectionChangeListener buildSelectedItemsChangeListener() {
		return new SWTCollectionChangeListenerWrapper(this.buildSelectedItemsChangeListener_());
	}

	protected CollectionChangeListener buildSelectedItemsChangeListener_() {
		return new CollectionChangeListener() {
			public void itemsAdded(CollectionChangeEvent event) {
				ListBoxModelAdapter.this.selectedItemsAdded(event);
			}
			public void itemsRemoved(CollectionChangeEvent event) {
				ListBoxModelAdapter.this.selectedItemsRemoved(event);
			}
			public void collectionCleared(CollectionChangeEvent event) {
				ListBoxModelAdapter.this.selectedItemsCleared(event);
			}
			public void collectionChanged(CollectionChangeEvent event) {
				ListBoxModelAdapter.this.selectedItemsChanged(event);
			}
			@Override
			public String toString() {
				return "selected items listener";
			}
		};
	}

	protected SelectionListener buildListBoxSelectionListener() {
		return new SelectionListener() {
			public void widgetSelected(SelectionEvent event) {
				ListBoxModelAdapter.this.listBoxSelectionChanged(event);
			}
			public void widgetDefaultSelected(SelectionEvent event) {
				ListBoxModelAdapter.this.listBoxDoubleClicked(event);
			}
			@Override
			public String toString() {
				return "list box selection listener";
			}
		};
	}

	@SuppressWarnings("unchecked")
	protected DoubleClickListener<E>[] buildDoubleClickListeners() {
		return new DoubleClickListener[0];
	}

	@SuppressWarnings("unchecked")
	protected SelectionChangeListener<E>[] buildSelectionChangeListeners() {
		return new SelectionChangeListener[0];
	}

	protected DisposeListener buildListBoxDisposeListener() {
		return new DisposeListener() {
			public void widgetDisposed(DisposeEvent event) {
				ListBoxModelAdapter.this.listBoxDisposed(event);
			}
			@Override
			public String toString() {
				return "list box dispose listener";
			}
		};
	}

	protected void synchronizeListBox() {
		this.synchronizeListBoxItems();
		this.synchronizeListBoxSelection();
	}


	// ********** string converter **********

	public void setStringConverter(StringConverter<E> stringConverter) {
		if (stringConverter == null) {
			throw new NullPointerException();
		}
		this.stringConverter = stringConverter;
		this.synchronizeListBox();
	}


	// ********** list **********

	/**
	 * Use the string converter to convert the specified item to a
	 * string that can be added to the list box.
	 */
	protected String convert(E item) {
		return this.stringConverter.convertToString(item);
	}

	/**
	 * Brute force synchronization of list box with the model list.
	 */
	protected void synchronizeListBoxItems() {
		if (this.listBox.isDisposed()) {
			return;
		}
		int len = this.listHolder.size();
		String[] items = new String[len];
		for (int i = 0; i < len; i++) {
			items[i] = this.convert(this.listHolder.get(i));
		}
		this.listBox.setItems(items);
	}

	/**
	 * The model has changed - synchronize the list box.
	 */
	protected void listItemsAdded(ListChangeEvent event) {
		if (this.listBox.isDisposed()) {
			return;
		}
		int i = event.index();
		for (ListIterator<E> stream = this.items(event); stream.hasNext(); ) {
			this.listBox.add(this.convert(stream.next()), i++);
		}
	}

	/**
	 * The model has changed - synchronize the list box.
	 */
	protected void listItemsRemoved(ListChangeEvent event) {
		if (this.listBox.isDisposed()) {
			return;
		}
		this.listBox.remove(event.index(), event.index() + event.itemsSize() - 1);
	}

	/**
	 * The model has changed - synchronize the list box.
	 */
	protected void listItemsMoved(ListChangeEvent event) {
		if (this.listBox.isDisposed()) {
			return;
		}
		int target = event.targetIndex();
		int source = event.sourceIndex();
		int len = event.moveLength();
		int loStart = Math.min(target, source);
		int hiStart = Math.max(target, source);
		// make a copy of the affected items...
		String[] subArray = CollectionTools.subArray(this.listBox.getItems(), loStart, hiStart + len - loStart);
		// ...move them around...
		subArray = CollectionTools.move(subArray, target - loStart, source - loStart, len);
		// ...and then put them back
		for (int i = 0; i < subArray.length; i++) {
			this.listBox.setItem(loStart + i, subArray[i]);
		}
	}

	/**
	 * The model has changed - synchronize the list box.
	 */
	protected void listItemsReplaced(ListChangeEvent event) {
		if (this.listBox.isDisposed()) {
			return;
		}
		int i = event.index();
		for (ListIterator<E> stream = this.items(event); stream.hasNext(); ) {
			this.listBox.setItem(i++, this.convert(stream.next()));
		}
	}

	/**
	 * The model has changed - synchronize the list box.
	 */
	protected void listCleared(ListChangeEvent event) {
		if (this.listBox.isDisposed()) {
			return;
		}
		this.listBox.removeAll();
	}

	/**
	 * The model has changed - synchronize the list box.
	 */
	protected void listChanged(ListChangeEvent event) {
		this.synchronizeListBoxItems();
	}

	// minimized unchecked code
	@SuppressWarnings("unchecked")
	protected ListIterator<E> items(ListChangeEvent event) {
		return ((ListIterator<E>) event.items());
	}


	// ********** selected items **********

	protected int indexOf(E item) {
		int len = this.listHolder.size();
		for (int i = 0; i < len; i++) {
			if (this.listHolder.get(i) == item) {
				return i;
			}
		}
		return -1;
	}

	protected void synchronizeListBoxSelection() {
		if (this.listBox.isDisposed()) {
			return;
		}
		int[] indices = new int[this.selectedItemsHolder.size()];
		int i = 0;
		for (Iterator<E> stream = this.selectedItemsHolder.iterator(); stream.hasNext(); ) {
			indices[i++] = this.indexOf(stream.next());
		}
		this.listBox.deselectAll();
		this.listBox.select(indices);
	}

	protected void selectedItemsAdded(CollectionChangeEvent event) {
		if (this.listBox.isDisposed()) {
			return;
		}
		int[] indices = new int[event.itemsSize()];
		int i = 0;
		for (Iterator<E> stream = this.items(event); stream.hasNext(); ) {
			indices[i++] = this.indexOf(stream.next());
		}
		this.listBox.select(indices);
	}

	protected void selectedItemsRemoved(CollectionChangeEvent event) {
		if (this.listBox.isDisposed()) {
			return;
		}
		int[] indices = new int[event.itemsSize()];
		int i = 0;
		for (Iterator<E> stream = this.items(event); stream.hasNext(); ) {
			indices[i++] = this.indexOf(stream.next());
		}
		this.listBox.deselect(indices);
	}

	protected void selectedItemsCleared(CollectionChangeEvent event) {
		if (this.listBox.isDisposed()) {
			return;
		}
		this.listBox.deselectAll();
	}

	protected void selectedItemsChanged(CollectionChangeEvent event) {
		this.synchronizeListBoxSelection();
	}

	// minimized unchecked code
	@SuppressWarnings("unchecked")
	protected Iterator<E> items(CollectionChangeEvent event) {
		return ((Iterator<E>) event.items());
	}


	// ********** list box events **********

	protected void listBoxSelectionChanged(SelectionEvent event) {
		if (this.selectionChangeListeners.length > 0) {
			@SuppressWarnings("unchecked")
			SelectionChangeEvent<E> scEvent = new SelectionChangeEvent(this, this.selectedItems());
			for (SelectionChangeListener<E> selectionChangeListener : this.selectionChangeListeners) {
				selectionChangeListener.selectionChanged(scEvent);
			}
		}
	}

	protected Collection<E> selectedItems() {
		if (this.listBox.isDisposed()) {
			return Collections.emptySet();
		}
		@SuppressWarnings("unchecked")
		ArrayList<E> selectedItems = new ArrayList(this.listBox.getSelectionCount());
		for (int selectionIndex : this.listBox.getSelectionIndices()) {
			selectedItems.add(this.listHolder.get(selectionIndex));
		}
		return selectedItems;
	}

	protected void listBoxDoubleClicked(SelectionEvent event) {
		if (this.listBox.isDisposed()) {
			return;
		}
		if (this.doubleClickListeners.length > 0) {
			// there should be only a single item selected during a double-click(?)
			E selection = this.listHolder.get(this.listBox.getSelectionIndex());
			@SuppressWarnings("unchecked")
			DoubleClickEvent<E> dcEvent = new DoubleClickEvent(this, selection);
			for (DoubleClickListener<E> doubleClickListener : this.doubleClickListeners) {
				doubleClickListener.doubleClick(dcEvent);
			}
		}
	}


	// ********** dispose **********

	protected void listBoxDisposed(DisposeEvent event) {
		// the list box is not yet "disposed" when we receive this event
		// so we can still remove our listeners
		this.listBox.removeDisposeListener(this.listBoxDisposeListener);
		this.listBox.removeSelectionListener(this.listBoxSelectionListener);
		this.selectedItemsHolder.removeCollectionChangeListener(CollectionValueModel.VALUES, this.selectedItemsChangeListener);
		this.listHolder.removeListChangeListener(ListValueModel.LIST_VALUES, this.listChangeListener);
	}


	// ********** standard methods **********

	@Override
	public String toString() {
		return StringTools.buildToStringFor(this, this.listHolder);
	}


	// ********** double click support **********

	public void addDoubleClickListener(DoubleClickListener<E> listener) {
		this.doubleClickListeners = CollectionTools.add(this.doubleClickListeners, listener);
	}

	public void removeDoubleClickListener(DoubleClickListener<E> listener) {
		this.doubleClickListeners = CollectionTools.remove(this.doubleClickListeners, listener);
	}

	public interface DoubleClickListener<E> {
		void doubleClick(DoubleClickEvent<E> event);
	}

	public static class DoubleClickEvent<E> extends EventObject {
		private final E selection;
		private static final long serialVersionUID = 1L;

		protected DoubleClickEvent(ListBoxModelAdapter<E> source, E selection) {
			super(source);
			if (selection == null) {
				throw new NullPointerException();
			}
			this.selection = selection;
		}

		@Override
		@SuppressWarnings("unchecked")
		public ListBoxModelAdapter<E> getSource() {
			return (ListBoxModelAdapter<E>) super.getSource();
		}

		public E selection() {
			return this.selection;
		}

	}


	// ********** selection support **********

	public void addSelectionChangeListener(SelectionChangeListener<E> listener) {
		this.selectionChangeListeners = CollectionTools.add(this.selectionChangeListeners, listener);
	}

	public void removeSelectionChangeListener(SelectionChangeListener<E> listener) {
		this.selectionChangeListeners = CollectionTools.remove(this.selectionChangeListeners, listener);
	}

	public interface SelectionChangeListener<E> {
		void selectionChanged(SelectionChangeEvent<E> event);
	}

	public static class SelectionChangeEvent<E> extends EventObject {
		private final Collection<E> selection;
		private static final long serialVersionUID = 1L;

		protected SelectionChangeEvent(ListBoxModelAdapter<E> source, Collection<E> selection) {
			super(source);
			if (selection == null) {
				throw new NullPointerException();
			}
			this.selection = selection;
		}

		@Override
		@SuppressWarnings("unchecked")
		public ListBoxModelAdapter<E> getSource() {
			return (ListBoxModelAdapter<E>) super.getSource();
		}

		public Iterator<E> selection() {
			return this.selection.iterator();
		}

	}

}

Back to the top