View Javadoc

1   /*
2    * Copyright (c) 2005, the JUNG Project and the Regents of the University of
3    * California All rights reserved.
4    * 
5    * This software is open-source under the BSD license; see either "license.txt"
6    * or http://jung.sourceforge.net/license.txt 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      /**
37       * @see PickedState#pick(ArchetypeVertex, boolean)
38       */
39      public boolean pick(T v, boolean state) {
40          boolean prior_state = this.picked.contains(v);
41          if (state) {
42              picked.add(v);
43              if(prior_state == false) {
44                  fireItemStateChanged(new ItemEvent(this, ItemEvent.ITEM_STATE_CHANGED,
45                          v, ItemEvent.SELECTED));
46              }
47  
48          } else {
49              picked.remove(v);
50              if(prior_state == true) {
51                  fireItemStateChanged(new ItemEvent(this, ItemEvent.ITEM_STATE_CHANGED,
52                      v, ItemEvent.DESELECTED));
53              }
54  
55          }
56          return prior_state;
57      }
58  
59      /**
60       * @see edu.uci.ics.jung.visualization.picking.PickedState#clearPickedVertices()
61       */
62      public void clear() {
63          Collection<T> unpicks = new ArrayList<T>(picked);
64          for(T v : unpicks) {
65              pick(v, false);
66          }
67          picked.clear();
68  
69      }
70  
71      /**
72       * @see edu.uci.ics.jung.visualization.picking.PickedState#getPickedEdges()
73       */
74      public Set<T> getPicked() {
75          return Collections.unmodifiableSet(picked);
76      }
77      
78      /**
79       * @see edu.uci.ics.jung.visualization.picking.PickedState#isPicked(ArchetypeEdge)
80       */
81      public boolean isPicked(T e) {
82          return picked.contains(e);
83      }
84  
85      /**
86       * for the ItemSelectable interface contract
87       */
88      @SuppressWarnings("unchecked")
89      public T[] getSelectedObjects() {
90          List<T> list = new ArrayList<T>(picked);
91          return (T[])list.toArray();
92      }
93      
94  }