Source code: com/imagero/evtmgr/ActionManager.java
1 /*
2 * Copyright (c) imagero Andrey Kuznetsov. All Rights Reserved.
3 * http://jgui.imagero.com
4 *
5 * This program 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 2, or (at your option)
8 * any later version.
9 *
10 * This program 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 this program; if not, write to the Free Software
17 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18 */
19
20 package com.imagero.evtmgr;
21
22 import java.awt.event.*;
23 import java.util.Vector;
24
25 /**
26 * simple manager for adding/removing ActionListeners and
27 * dispatch ActionEvents to listeners
28 * <br>
29 *
30 * <pre>
31 * //create private key
32 * private Object key = new Object();
33 * ActionManager manager = new ActionManager(key);
34 *
35 * //access ActionManager
36 * public ActionManager getActionManager() {
37 * return manager;
38 * }
39 *
40 * //adding listeners
41 * getActionManager().addActionListener(myActionListener);
42 *
43 * //fire actionPerformed
44 * manager.sendAction(key, myActionEvent);
45 * </pre>
46 *
47 * @author Andrei Kouznetsov
48 */
49 public class ActionManager {
50 Vector listeners;
51 Object key;
52
53 /**
54 * create new ActionManager
55 * @param key only who knows this key can dispatch ActionEvents
56 * @see #fireAction(java.lang.Object key, java.awt.event.ActionEvent e)
57 */
58 public ActionManager(Object key) {
59 if(key == null) {
60 throw new NullPointerException("key can't be null");
61 }
62 this.key = key;
63 this.listeners = new Vector();
64 }
65
66 /**
67 * adds ActionListener to listener list
68 */
69 public void addActionListener(ActionListener listener) {
70 if(listener == null) {
71 throw new NullPointerException();
72 }
73 if (!listeners.contains(listener)) {
74 this.listeners.addElement(listener);
75 }
76 }
77
78 /**
79 * removes ActionListener from listener list
80 */
81 public void removeActionListener(ActionListener listener) {
82 if(listener == null) {
83 throw new NullPointerException();
84 }
85 this.listeners.removeElement(listener);
86 }
87
88 private void checkKey(Object key) {
89 if (key != this.key) {
90 throw new IllegalArgumentException("wrong key");
91 }
92 }
93
94 /**
95 * calls actionPerformed(ActionEvent e) for all listeners
96 * only who knows the key (creator) can call this
97 */
98 public void fireAction(Object key, ActionEvent e) {
99 if(key != this.key) {
100 throw new IllegalArgumentException();
101 }
102
103 for(int i = 0; i < this.listeners.size(); i++) {
104 ActionListener listener = (ActionListener)this.listeners.elementAt(i);
105 listener.actionPerformed(e);
106 }
107 }
108
109 public void setKey(Object key, Object newKey) {
110 if(this.key == key) {
111 this.key = newKey;
112 }
113 }
114 }