Mercurial > projects > dwt2
comparison org.eclipse.jface.databinding/src/org/eclipse/jface/internal/databinding/viewers/SelectionProviderMultipleSelectionObservableList.d @ 78:0a55d2d5a946
Added file for databinding
author | Frank Benoit <benoit@tionex.de> |
---|---|
date | Tue, 14 Apr 2009 11:35:29 +0200 |
parents | |
children | 6be48cf9f95c |
comparison
equal
deleted
inserted
replaced
76:f05e6e8b2f2d | 78:0a55d2d5a946 |
---|---|
1 /******************************************************************************* | |
2 * Copyright (c) 2007 Peter Centgraf 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 * Peter Centgraf - initial API and implementation, bug 124683 | |
10 * Boris Bokowski, IBM Corporation - initial API and implementation | |
11 *******************************************************************************/ | |
12 module org.eclipse.jface.internal.databinding.viewers.SelectionProviderMultipleSelectionObservableList; | |
13 | |
14 import java.lang.all; | |
15 | |
16 import java.util.ArrayList; | |
17 import java.util.Collections; | |
18 import java.util.List; | |
19 | |
20 import org.eclipse.core.databinding.observable.Diffs; | |
21 import org.eclipse.core.databinding.observable.Realm; | |
22 import org.eclipse.core.databinding.observable.list.ListDiff; | |
23 import org.eclipse.core.databinding.observable.list.WritableList; | |
24 import org.eclipse.jface.viewers.ISelection; | |
25 import org.eclipse.jface.viewers.ISelectionChangedListener; | |
26 import org.eclipse.jface.viewers.ISelectionProvider; | |
27 import org.eclipse.jface.viewers.IStructuredSelection; | |
28 import org.eclipse.jface.viewers.SelectionChangedEvent; | |
29 import org.eclipse.jface.viewers.StructuredSelection; | |
30 | |
31 /** | |
32 * Observes multiple-selection of an {@link ISelectionProvider}. | |
33 * | |
34 * @since 1.2 | |
35 */ | |
36 public class SelectionProviderMultipleSelectionObservableList : | |
37 WritableList { | |
38 | |
39 protected ISelectionProvider selectionProvider; | |
40 protected bool handlingSelection; | |
41 protected bool updating; | |
42 protected SelectionListener selectionListener = new SelectionListener(); | |
43 | |
44 class SelectionListener : ISelectionChangedListener { | |
45 public void selectionChanged(SelectionChangedEvent event) { | |
46 if (updating) { | |
47 return; | |
48 } | |
49 handlingSelection = true; | |
50 try { | |
51 updateWrappedList(new ArrayList(getSelectionList(event.getSelection()))); | |
52 } finally { | |
53 handlingSelection = false; | |
54 } | |
55 } | |
56 } | |
57 | |
58 /** | |
59 * Create a new observable list based on the current selection of the given | |
60 * selection provider. Assumes that the selection provider provides | |
61 * structured selections. | |
62 * | |
63 * @param realm | |
64 * @param selectionProvider | |
65 * @param elementType | |
66 */ | |
67 public this(Realm realm, | |
68 ISelectionProvider selectionProvider, Object elementType) { | |
69 super(realm, new ArrayList(getSelectionList(selectionProvider)), elementType); | |
70 this.selectionProvider = selectionProvider; | |
71 selectionProvider.addSelectionChangedListener(selectionListener); | |
72 } | |
73 | |
74 protected void fireListChange(ListDiff diff) { | |
75 if (handlingSelection) { | |
76 super.fireListChange(diff); | |
77 } else { | |
78 // this is a bit of a hack - we are changing the diff to match the order | |
79 // of elements returned by the selection provider after we've set the | |
80 // selection. | |
81 updating = true; | |
82 try { | |
83 List oldList = getSelectionList(selectionProvider); | |
84 selectionProvider | |
85 .setSelection(new StructuredSelection(wrappedList)); | |
86 wrappedList = new ArrayList(getSelectionList(selectionProvider)); | |
87 super.fireListChange(Diffs.computeListDiff(oldList, wrappedList)); | |
88 } finally { | |
89 updating = false; | |
90 } | |
91 } | |
92 } | |
93 | |
94 protected static List getSelectionList(ISelectionProvider selectionProvider) { | |
95 if (selectionProvider is null) { | |
96 throw new IllegalArgumentException(); | |
97 } | |
98 return getSelectionList(selectionProvider.getSelection()); | |
99 } | |
100 | |
101 protected static List getSelectionList(ISelection sel) { | |
102 if (null !is cast(IStructuredSelection)sel) { | |
103 return (cast(IStructuredSelection) sel).toList(); | |
104 } | |
105 return Collections.EMPTY_LIST; | |
106 } | |
107 | |
108 public synchronized void dispose() { | |
109 selectionProvider.removeSelectionChangedListener(selectionListener); | |
110 selectionProvider = null; | |
111 super.dispose(); | |
112 } | |
113 } |