Subversion Repositories svnkaklik

Rev

Details | Last modification | View Log

Rev Author Line No. Line
157 kaklik 1
/*
2
    AVRcamVIEW: A PC application to test out the functionallity of the
3
     AVRcam real-time image processing engine.
4
    Copyright (C) 2004    Brent A. Taylor
5
 
6
    This program is free software; you can redistribute it and/or
7
    modify it under the terms of the GNU General Public
8
    License as published by the Free Software Foundation; either
9
    version 2 of the License, or (at your option) any later version.
10
 
11
    This program is distributed in the hope that it will be useful,
12
    but WITHOUT ANY WARRANTY; without even the implied warranty of
13
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14
    General Public License for more details.
15
 
16
    You should have received a copy of the GNU General Public
17
    License along with this program; if not, write to the Free Software
18
    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19
 
20
   For more information on the AVRcamVIEW, please contact:
21
 
22
   taylorba@comcast.net
23
 
24
   or go to www.jrobot.net for more details regarding the system.
25
*/
26
 
27
package avr.connection;
28
 
29
import javax.swing.event.*;
30
 
31
import avr.connection.event.*;
32
 
33
/***********************************************************************
34
 * Defines a Connection that fires events describing if the connection
35
 * was opened or closed.
36
 */
37
public abstract class AbstractConnection implements Connection {
38
 
39
   /**
40
    * The list of registered event listeners.
41
    */
42
   private EventListenerList listenerList;
43
 
44
   /**
45
    * Boolean to test if the connection is connected (true) or
46
    * disconnected (false).
47
    */
48
   private boolean connected;
49
 
50
   /**
51
    * Initialize this object and set connected to false.
52
    */
53
   protected AbstractConnection() {
54
      listenerList = new EventListenerList();
55
      connected = false;
56
   }
57
 
58
   /**
59
    * Set connected to true and fire a connected event.
60
    */
61
   protected void setConnected() {
62
      setConnected(true);
63
      fireConnectedEvent(this);
64
   }
65
 
66
   /**
67
    * Set connected to false and fire a disconnected event.
68
    */
69
   protected void setDisconnected() {
70
      setConnected(false);
71
      fireDisconnectedEvent(this);
72
   }
73
 
74
   /**
75
    * Set this connected variable to the given value.
76
    * @param connected true if this connection is connected, false otherwise.
77
    */
78
   private void setConnected(boolean connected) {
79
      this.connected = connected;
80
   }
81
 
82
   /**
83
    * Tells whether or not this connection is connected.
84
    * @return true if, and only if, this connection is connected.
85
    */
86
   public boolean isConnected() {
87
      return connected;
88
   }
89
 
90
   /**
91
    * Adds a listener to the list that's notified each time a connect or
92
    * disconnect occurs.
93
    * @param cl the ConnectionListener
94
    */
95
   public void addConnectionListener(ConnectionListener cl) {
96
      listenerList.add(ConnectionListener.class, cl);
97
   }
98
 
99
   /**
100
    * Removes a listener to the list that's notified each time a connect or
101
    * disconnect occurs.
102
    * @param cl the ConnectionListener
103
    */
104
   public void removeConnectionListener(ConnectionListener cl) {
105
      listenerList.remove(ConnectionListener.class, cl);
106
   }
107
 
108
   public void removeAllConnectionListeners() {
109
      ConnectionListener[] listeners = getConnectionListeners();
110
      for(int i = 0; i < listeners.length; i++) {
111
         removeConnectionListener(listeners[i]);
112
      }
113
   }
114
 
115
   /**
116
    * Returns an array of all the connection listeners registered on
117
    * this connection.
118
    * @return all of this connection's ConnectionListeners or
119
    * an empty array if no connection listeners are currently registered
120
    */
121
   public ConnectionListener[] getConnectionListeners() {
122
      return (ConnectionListener[])
123
                listenerList.getListeners(ConnectionListener.class);
124
   }
125
 
126
   /**
127
    * Notifies all listeners that a connection has been established.
128
    * @param source This connection.
129
    */
130
   protected void fireConnectedEvent(Object source) {
131
      ConnectionListener[] listeners = getConnectionListeners();
132
 
133
      ConnectionEvent event = null;
134
 
135
      for(int i = 0; i < listeners.length; i++) {
136
         if(event == null) {
137
            event = new ConnectionEvent(this);
138
         }
139
         listeners[i].connected(event);
140
      }
141
   }
142
 
143
   /**
144
    * Notifies all listeners that a connection has been destroyed.
145
    * @param source This connection.
146
    */
147
   protected void fireDisconnectedEvent(Object source) {
148
      ConnectionListener[] listeners = getConnectionListeners();
149
 
150
      ConnectionEvent event = null;
151
 
152
      for(int i = 0; i < listeners.length; i++) {
153
         if(event == null) {
154
            event = new ConnectionEvent(this);
155
         }
156
         listeners[i].disconnected(event);
157
      }
158
   }
159
 
160
}