View Javadoc

1   /**********************************************
2    * Copyright (C) 2011 Lukas Laag
3    * This file is part of svgreal.
4    * 
5    * svgreal is free software: you can redistribute it and/or modify
6    * it under the terms of the GNU General Public License as published by
7    * the Free Software Foundation, either version 3 of the License, or
8    * (at your option) any later version.
9    * 
10   * svgreal is distributed in the hope that it will be useful,
11   * but WITHOUT ANY WARRANTY; without even the implied warranty of
12   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13   * GNU General Public License for more details.
14   * 
15   * You should have received a copy of the GNU General Public License
16   * along with svgreal.  If not, see http://www.gnu.org/licenses/
17   **********************************************/
18  package org.vectomatic.svg.edit.client.gxt.widget;
19  
20  import java.util.Collections;
21  import java.util.HashMap;
22  import java.util.HashSet;
23  import java.util.Map;
24  import java.util.Set;
25  
26  import com.extjs.gxt.ui.client.data.ModelData;
27  import com.extjs.gxt.ui.client.widget.grid.GridSelectionModel;
28  
29  /**
30   * Subclass to GridSelectionModel which allows the grid to be null
31   * @author laaglu
32   * @param <M>
33   * The grid model class.
34   */
35  public class GridSelectionModelExt<M extends ModelData> extends GridSelectionModel<M> {
36  	  @Override
37  	  protected void onLastFocusChanged(M oldFocused, M newFocused) {
38  		  if (grid != null) {
39  			  super.onLastFocusChanged(oldFocused, newFocused);
40  		  }
41  	  }
42  	  @Override
43  	  protected void onSelectChange(M model, boolean select) {
44  		  if (grid != null) {
45  			  super.onSelectChange(model, select);
46  		  }
47  	  }
48  	  
49  	  public int getCount() {
50  		  return selected.size();
51  	  }
52  	  
53  	  public int getSelectedIndex() {
54  		  return (lastSelected != null) ? getIndex(lastSelected) : -1;
55  	  }
56  	  
57  	  public int getIndex(M model) {
58  		  for (int i = 0, size = listStore.getCount(); i < size; i++) {
59  			  if (listStore.getAt(i) == model) {
60  				  return i;
61  			  }
62  		  }
63  		  return -1;
64  	  }
65  	  
66  	  public Set<Integer> getSelectedIndices() {
67  		  if (listStore == null) {
68  			  return Collections.<Integer>emptySet();
69  		  }
70  		  Map<M, Integer> modelToIndex = new HashMap<M, Integer>();
71  		  for (int i = 0, size = listStore.getCount(); i < size; i++) {
72  			  modelToIndex.put(listStore.getAt(i), i);
73  		  }
74  		  Set<Integer> indices = new HashSet<Integer>();
75  		  for (M m : selected) {
76  			  indices.add(modelToIndex.get(m));
77  		  }
78  		  return indices;
79  	  }
80  }