View Javadoc
1   /*
2    * Copyright (c) 2005, The JUNG Authors
3    * All rights reserved.
4    * 
5    * This software is open-source under the BSD license; see either "license.txt"
6    * or https://github.com/jrtom/jung/blob/master/LICENSE for a description.
7    * 
8    *
9    * Created on Mar 28, 2005
10   */
11  package edu.uci.ics.jung.visualization.picking;
12  
13  import java.awt.event.ItemEvent;
14  import java.util.ArrayList;
15  import java.util.Collection;
16  import java.util.Collections;
17  import java.util.LinkedHashSet;
18  import java.util.List;
19  import java.util.Set;
20  
21  /**
22   * Maintains the state of what has been 'picked' in the graph.
23   * The <code>Sets</code> are constructed so that their iterators
24   * will traverse them in the order in which they are picked.
25   * 
26   * @author Tom Nelson 
27   * @author Joshua O'Madadhain
28   * 
29   */
30  public class MultiPickedState<T> extends AbstractPickedState<T> implements PickedState<T> {
31      /**
32       * the 'picked' vertices
33       */
34      protected Set<T> picked = new LinkedHashSet<T>();
35      
36      public boolean pick(T v, boolean state) {
37          boolean prior_state = this.picked.contains(v);
38          if (state) {
39              picked.add(v);
40              if(prior_state == false) {
41                  fireItemStateChanged(new ItemEvent(this, ItemEvent.ITEM_STATE_CHANGED,
42                          v, ItemEvent.SELECTED));
43              }
44  
45          } else {
46              picked.remove(v);
47              if(prior_state == true) {
48                  fireItemStateChanged(new ItemEvent(this, ItemEvent.ITEM_STATE_CHANGED,
49                      v, ItemEvent.DESELECTED));
50              }
51  
52          }
53          return prior_state;
54      }
55  
56      public void clear() {
57          Collection<T> unpicks = new ArrayList<T>(picked);
58          for(T v : unpicks) {
59              pick(v, false);
60          }
61          picked.clear();
62  
63      }
64  
65      public Set<T> getPicked() {
66          return Collections.unmodifiableSet(picked);
67      }
68      
69      public boolean isPicked(T e) {
70          return picked.contains(e);
71      }
72  
73      /**
74       * for the ItemSelectable interface contract
75       */
76      @SuppressWarnings("unchecked")
77      public T[] getSelectedObjects() {
78          List<T> list = new ArrayList<T>(picked);
79          return (T[])list.toArray();
80      }
81      
82  }