18579ea343f8d4c74b44d5b5cb2df3ef7552b2f6eshowardpackage autotest.common.table;
28579ea343f8d4c74b44d5b5cb2df3ef7552b2f6eshoward
38579ea343f8d4c74b44d5b5cb2df3ef7552b2f6eshowardimport autotest.common.JSONArrayList;
4b9c6617bdc063b3b4aa760a0a45190ee069139fdshowardimport autotest.common.table.DataSource.DefaultDataCallback;
5b9c6617bdc063b3b4aa760a0a45190ee069139fdshowardimport autotest.common.table.DataSource.Query;
68579ea343f8d4c74b44d5b5cb2df3ef7552b2f6eshoward
78579ea343f8d4c74b44d5b5cb2df3ef7552b2f6eshowardimport com.google.gwt.json.client.JSONArray;
8b9c6617bdc063b3b4aa760a0a45190ee069139fdshowardimport com.google.gwt.json.client.JSONNumber;
98579ea343f8d4c74b44d5b5cb2df3ef7552b2f6eshowardimport com.google.gwt.json.client.JSONObject;
108579ea343f8d4c74b44d5b5cb2df3ef7552b2f6eshoward
11b9c6617bdc063b3b4aa760a0a45190ee069139fdshowardimport java.util.List;
12b9c6617bdc063b3b4aa760a0a45190ee069139fdshowardimport java.util.Set;
13b9c6617bdc063b3b4aa760a0a45190ee069139fdshoward
148579ea343f8d4c74b44d5b5cb2df3ef7552b2f6eshowardpublic class DynamicTableSelectionManager extends SelectionManager {
15b9c6617bdc063b3b4aa760a0a45190ee069139fdshoward    /**
16b9c6617bdc063b3b4aa760a0a45190ee069139fdshoward     * see deselectFiltered()
17b9c6617bdc063b3b4aa760a0a45190ee069139fdshoward     */
18b9c6617bdc063b3b4aa760a0a45190ee069139fdshoward    private final DefaultDataCallback deselectFilteredCallback = new DefaultDataCallback() {
19b9c6617bdc063b3b4aa760a0a45190ee069139fdshoward        @Override
20b9c6617bdc063b3b4aa760a0a45190ee069139fdshoward        public void onQueryReady(Query query) {
21b9c6617bdc063b3b4aa760a0a45190ee069139fdshoward            query.getPage(null, null, null, this);
22b9c6617bdc063b3b4aa760a0a45190ee069139fdshoward        }
23b9c6617bdc063b3b4aa760a0a45190ee069139fdshoward
24b9c6617bdc063b3b4aa760a0a45190ee069139fdshoward        @Override
254879914c122f4ed97eae3b08c5af1930fd75b39dshoward        public void handlePage(List<JSONObject> data) {
26b9c6617bdc063b3b4aa760a0a45190ee069139fdshoward            // use set intersection to get objects from the selected object set, rather than using
27b9c6617bdc063b3b4aa760a0a45190ee069139fdshoward            // the objects returned by the server.  we have to do this because ArrayDataSource uses
28b9c6617bdc063b3b4aa760a0a45190ee069139fdshoward            // object identity and not value equality of JSONObjects.
294879914c122f4ed97eae3b08c5af1930fd75b39dshoward            Set<JSONObject> filteredRows = new JSONObjectSet<JSONObject>(data);
30b9c6617bdc063b3b4aa760a0a45190ee069139fdshoward            Set<JSONObject> rowsToRemove = new JSONObjectSet<JSONObject>(getSelectedObjects());
31b9c6617bdc063b3b4aa760a0a45190ee069139fdshoward            rowsToRemove.retainAll(filteredRows);
32b9c6617bdc063b3b4aa760a0a45190ee069139fdshoward            deselectObjects(rowsToRemove);
33b9c6617bdc063b3b4aa760a0a45190ee069139fdshoward        }
34b9c6617bdc063b3b4aa760a0a45190ee069139fdshoward    };
35b9c6617bdc063b3b4aa760a0a45190ee069139fdshoward
368579ea343f8d4c74b44d5b5cb2df3ef7552b2f6eshoward    private DynamicTable attachedDynamicTable;
378579ea343f8d4c74b44d5b5cb2df3ef7552b2f6eshoward
388579ea343f8d4c74b44d5b5cb2df3ef7552b2f6eshoward    public DynamicTableSelectionManager(DynamicTable table, boolean selectOnlyOne) {
398579ea343f8d4c74b44d5b5cb2df3ef7552b2f6eshoward        super(table, selectOnlyOne);
408579ea343f8d4c74b44d5b5cb2df3ef7552b2f6eshoward        attachedDynamicTable = table;
418579ea343f8d4c74b44d5b5cb2df3ef7552b2f6eshoward    }
428579ea343f8d4c74b44d5b5cb2df3ef7552b2f6eshoward
438579ea343f8d4c74b44d5b5cb2df3ef7552b2f6eshoward
448579ea343f8d4c74b44d5b5cb2df3ef7552b2f6eshoward    @Override
458579ea343f8d4c74b44d5b5cb2df3ef7552b2f6eshoward    /**
468579ea343f8d4c74b44d5b5cb2df3ef7552b2f6eshoward     * Select all objects covering all pages, not just the currently displayed page in the table.
478579ea343f8d4c74b44d5b5cb2df3ef7552b2f6eshoward     */
488579ea343f8d4c74b44d5b5cb2df3ef7552b2f6eshoward    public void selectAll() {
49b9c6617bdc063b3b4aa760a0a45190ee069139fdshoward        attachedDynamicTable.getCurrentQuery().getPage(null, null, null, new DefaultDataCallback() {
50b9c6617bdc063b3b4aa760a0a45190ee069139fdshoward            @Override
514879914c122f4ed97eae3b08c5af1930fd75b39dshoward            public void handlePage(List<JSONObject> data) {
524879914c122f4ed97eae3b08c5af1930fd75b39dshoward                selectObjects(data);
538579ea343f8d4c74b44d5b5cb2df3ef7552b2f6eshoward            }
548579ea343f8d4c74b44d5b5cb2df3ef7552b2f6eshoward        });
558579ea343f8d4c74b44d5b5cb2df3ef7552b2f6eshoward    }
56b9c6617bdc063b3b4aa760a0a45190ee069139fdshoward
57b9c6617bdc063b3b4aa760a0a45190ee069139fdshoward    @Override
58b9c6617bdc063b3b4aa760a0a45190ee069139fdshoward    public void onSelectNone() {
59b9c6617bdc063b3b4aa760a0a45190ee069139fdshoward        deselectFiltered();
60b9c6617bdc063b3b4aa760a0a45190ee069139fdshoward    }
61b9c6617bdc063b3b4aa760a0a45190ee069139fdshoward
62b9c6617bdc063b3b4aa760a0a45190ee069139fdshoward    /**
63b9c6617bdc063b3b4aa760a0a45190ee069139fdshoward     * Only deselect items included in the current filters.
64b9c6617bdc063b3b4aa760a0a45190ee069139fdshoward     */
65b9c6617bdc063b3b4aa760a0a45190ee069139fdshoward    private void deselectFiltered() {
66b9c6617bdc063b3b4aa760a0a45190ee069139fdshoward        if (!attachedDynamicTable.isAnyUserFilterActive()) {
67b9c6617bdc063b3b4aa760a0a45190ee069139fdshoward            deselectAll();
68b9c6617bdc063b3b4aa760a0a45190ee069139fdshoward            return;
69b9c6617bdc063b3b4aa760a0a45190ee069139fdshoward        }
70b9c6617bdc063b3b4aa760a0a45190ee069139fdshoward
71b9c6617bdc063b3b4aa760a0a45190ee069139fdshoward        JSONObject params = attachedDynamicTable.getCurrentQuery().getParams();
72b9c6617bdc063b3b4aa760a0a45190ee069139fdshoward        params.put("id__in", selectedIdList());
73b9c6617bdc063b3b4aa760a0a45190ee069139fdshoward        attachedDynamicTable.getDataSource().query(params, deselectFilteredCallback);
74b9c6617bdc063b3b4aa760a0a45190ee069139fdshoward    }
75b9c6617bdc063b3b4aa760a0a45190ee069139fdshoward
76b9c6617bdc063b3b4aa760a0a45190ee069139fdshoward    private JSONArray selectedIdList() {
77b9c6617bdc063b3b4aa760a0a45190ee069139fdshoward        JSONArrayList<JSONNumber> idList = new JSONArrayList<JSONNumber>();
78b9c6617bdc063b3b4aa760a0a45190ee069139fdshoward        for (JSONObject object : getSelectedObjects()) {
79b9c6617bdc063b3b4aa760a0a45190ee069139fdshoward            JSONNumber id = object.get("id").isNumber();
80b9c6617bdc063b3b4aa760a0a45190ee069139fdshoward            assert id != null;
81b9c6617bdc063b3b4aa760a0a45190ee069139fdshoward            idList.add(id);
82b9c6617bdc063b3b4aa760a0a45190ee069139fdshoward        }
83b9c6617bdc063b3b4aa760a0a45190ee069139fdshoward        return idList.getBackingArray();
84b9c6617bdc063b3b4aa760a0a45190ee069139fdshoward    }
858579ea343f8d4c74b44d5b5cb2df3ef7552b2f6eshoward}
86