KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > thoughtriver > open > vectorvisuals > ui > SelectionManager


1 /*
2  * SelectionManager.java
3  *
4  * Created on 13 June 2003, 01:16
5  */

6
7 package com.thoughtriver.open.vectorvisuals.ui;
8
9 import java.util.*;
10
11 import com.thoughtriver.open.vectorvisuals.*;
12
13 /**
14  * The <CODE>SelectionManager</CODE> is responsible for recording listeners
15  * that have registered to receive <CODE>SelectionEvent</CODE>s, and for
16  * distributing those events to the listeners when they occur.
17  *
18  * @author Brandon Franklin
19  * @version $Date: 2006/11/25 09:00:37 $
20  */

21 public class SelectionManager {
22
23     /** The singleton instance of <CODE>SelectionManager</CODE> */
24     static private SelectionManager singleton = null;
25
26     /**
27      * Returns the shared instance of <CODE>SelectionManager</CODE>, which is
28      * useful if a unified selection management approach is desired.
29      *
30      * @return the <CODE>SelectionManager</CODE> singleton
31      */

32     static synchronized public SelectionManager getSharedInstance() {
33
34         if (singleton == null) {
35             singleton = new SelectionManager();
36         }
37
38         return singleton;
39     }
40
41     /** The <CODE>List</CODE> of listeners to be notified of selection changes */
42     private List<SelectionListener> listeners = null;
43
44     /** The currently selected <CODE>VisualObject</CODE> */
45     private VisualObject selection = null;
46
47     /**
48      * Creates a new instance of <CODE>SelectionManager</CODE> and prepares it
49      * for use.
50      */

51     public SelectionManager() {
52         listeners = new LinkedList<SelectionListener>();
53     }
54
55     /**
56      * Adds a <CODE>SelectionListener</CODE> to receive notification when
57      * <CODE>VisualObject</CODE>s are selected and deselected.
58      *
59      * @param listener the new listener to add
60      */

61     public void addSelectionListener(final SelectionListener listener) {
62         listeners.add(listener);
63     }
64
65     /**
66      * Removes a <CODE>SelectionListener</CODE> from receiving notification
67      * when <CODE>VisualObject</CODE>s are selected and deselected.
68      *
69      * @param listener the listener to remove
70      */

71     public void removeSelectionListener(final SelectionListener listener) {
72         listeners.remove(listener);
73     }
74
75     /**
76      * Notifies the <CODE>SelectionManager</CODE> that a <CODE>VisualObject</CODE>
77      * has been selected, so the appropriate listeners should be notified. If
78      * the selection is simply being cleared (no new object is being selected)
79      * the parameter will be null.
80      *
81      * @param obj the <CODE>VisualObject</CODE> that has been selected, or
82      * null if selection is being cleared
83      */

84     synchronized public void setSelectedObject(final VisualObject obj) {
85
86         VisualObject oldSelection = getSelectedObject();
87         VisualObject newSelection = obj;
88
89         // Determine if a change has occurred
90
if (oldSelection == newSelection) {
91             return;
92         }
93         boolean transition = ((oldSelection != null) && (newSelection != null)) ? true : false;
94
95         // Notify of deselection
96
SelectionEvent event = null;
97         if (oldSelection != null) {
98             event = new SelectionEvent(oldSelection, false, transition);
99
100             for (SelectionListener listener : listeners) {
101                 listener.selectionChanged(event);
102             }
103         }
104         selection = newSelection;
105
106         // Notify of selection
107
if (newSelection != null) {
108             event = new SelectionEvent(newSelection, true, transition);
109             for (SelectionListener listener : listeners) {
110                 listener.selectionChanged(event);
111             }
112         }
113
114     }
115
116     /**
117      * Returns the <CODE>VisualObject</CODE> that is currently selected.
118      *
119      * @return the <CODE>VisualObject</CODE> that is currently selected
120      */

121     public VisualObject getSelectedObject() {
122         return selection;
123     }
124
125 }
126
Popular Tags