Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(1477)

Side by Side Diff: chrome/android/java/src/org/chromium/chrome/browser/widget/selection/SelectionDelegate.java

Issue 2789363003: Implement single-selection mode for SelectionDelegate. (Closed)
Patch Set: Address feedback from Michael Created 3 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « no previous file | chrome/android/java_sources.gni » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright 2016 The Chromium Authors. All rights reserved. 1 // Copyright 2016 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 package org.chromium.chrome.browser.widget.selection; 5 package org.chromium.chrome.browser.widget.selection;
6 6
7 import org.chromium.base.ObserverList; 7 import org.chromium.base.ObserverList;
8 8
9 import java.util.ArrayList; 9 import java.util.ArrayList;
10 import java.util.HashSet; 10 import java.util.HashSet;
11 import java.util.List; 11 import java.util.List;
12 import java.util.Set; 12 import java.util.Set;
13 13
14 /** 14 /**
15 * A generic delegate used to keep track of selected items. 15 * A generic delegate used to keep track of selected items.
16 * @param <E> The type of the selectable items this delegate interacts with. 16 * @param <E> The type of the selectable items this delegate interacts with.
17 */ 17 */
18 public class SelectionDelegate<E> { 18 public class SelectionDelegate<E> {
19 // True if the SelectionDelegate should only support a single item being sel ected at a time.
20 private boolean mSingleSelection;
Theresa 2017/04/04 16:36:40 nit: mIsSingleSelection
19 21
20 /** 22 /**
21 * Observer interface to be notified of selection changes. 23 * Observer interface to be notified of selection changes.
22 */ 24 */
23 public interface SelectionObserver<E> { 25 public interface SelectionObserver<E> {
24 /** 26 /**
25 * Called when the set of selected items has changed. 27 * Called when the set of selected items has changed.
26 * @param selectedItems The list of currently selected items. An empty l ist indicates there 28 * @param selectedItems The list of currently selected items. An empty l ist indicates there
27 * is no selection. 29 * is no selection.
28 */ 30 */
29 public void onSelectionStateChange(List<E> selectedItems); 31 void onSelectionStateChange(List<E> selectedItems);
30 } 32 }
31 33
32 private Set<E> mSelectedItems = new HashSet<>(); 34 private Set<E> mSelectedItems = new HashSet<>();
33 private ObserverList<SelectionObserver<E>> mObservers = new ObserverList<>() ; 35 private ObserverList<SelectionObserver<E>> mObservers = new ObserverList<>() ;
34 36
35 /** 37 /**
38 * Sets the mode of this SelectionDelegate to single-selection.
39 */
40 public void setSingleSelectionMode() {
41 mSingleSelection = true;
42 }
43
44 /**
36 * Toggles the selected state for the given item. 45 * Toggles the selected state for the given item.
37 * @param item The item to toggle. 46 * @param item The item to toggle.
38 * @return Whether the item is selected. 47 * @return Whether the item is selected.
39 */ 48 */
40 public boolean toggleSelectionForItem(E item) { 49 public boolean toggleSelectionForItem(E item) {
41 if (mSelectedItems.contains(item)) mSelectedItems.remove(item); 50 if (mSelectedItems.contains(item)) {
42 else mSelectedItems.add(item); 51 mSelectedItems.remove(item);
52 } else {
53 if (mSingleSelection) mSelectedItems.clear();
54 mSelectedItems.add(item);
55 }
43 56
44 notifyObservers(); 57 notifyObservers();
45 58
46 return isItemSelected(item); 59 return isItemSelected(item);
47 } 60 }
48 61
49 /** 62 /**
50 * True if the item is selected. False otherwise. 63 * True if the item is selected. False otherwise.
51 * @param item The item. 64 * @param item The item.
52 * @return Whether the item is selected. 65 * @return Whether the item is selected.
(...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after
92 public void removeObserver(SelectionObserver<E> observer) { 105 public void removeObserver(SelectionObserver<E> observer) {
93 mObservers.removeObserver(observer); 106 mObservers.removeObserver(observer);
94 } 107 }
95 108
96 private void notifyObservers() { 109 private void notifyObservers() {
97 List<E> selectedItems = getSelectedItems(); 110 List<E> selectedItems = getSelectedItems();
98 for (SelectionObserver<E> observer : mObservers) { 111 for (SelectionObserver<E> observer : mObservers) {
99 observer.onSelectionStateChange(selectedItems); 112 observer.onSelectionStateChange(selectedItems);
100 } 113 }
101 } 114 }
102
103 } 115 }
OLDNEW
« no previous file with comments | « no previous file | chrome/android/java_sources.gni » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698