1 /*
2 * File: Event.java
3 * Created: 22.10.2006 17:11:19
4 *
5 * Copyright 2006 Michal Burda.
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
20 */
21
22 package net.sf.webmancer.base;
23
24 /**
25 * @author Michal Burda
26 */
27 public class Event {
28 /**
29 * Target object name or <code>null</code>, if the event is broadcast.
30 */
31 private String target;
32
33 /**
34 * <code>true</code> if the event is handled already.
35 */
36 private boolean handled;
37
38 /**
39 * Creates new event that is adressed to target object <code>target</code>. A <code>null</code> target means a broadcast event.
40 *
41 * @param target The target object identification or <code>null</code> for broadcast event.
42 */
43 public Event(final String target) {
44 this.target = target;
45 this.handled = false;
46 }
47
48 /**
49 * Creates new broadcast event.
50 */
51 public Event() {
52 this(null);
53 }
54
55 /**
56 * Returns <code>true</code> if the event has been handled already. Broadcast events return always <code>false</code>.
57 *
58 * @return <code>true</code> if the event has been handled already.
59 */
60 public boolean isHandled() {
61 return this.handled;
62 }
63
64 /**
65 * Returns <code>true</code> if the current event is a broadcast event. Broadcast events do not have any concrete target.
66 *
67 * @return <code>true</code> if the current event is a broadcast event.
68 */
69 public boolean isBroadcast() {
70 return this.target == null;
71 }
72
73 /**
74 * Return the target object's name or <code>null</code>, if the current event is broadcast.
75 *
76 * @return the target object's name
77 */
78 public String getTarget() {
79 return this.target;
80 }
81
82 /**
83 * Set the handled flag to the event. Broadcast events cannot be set to handled.
84 *
85 * @param handled the handled flag
86 * @throws IllegalStateException if setting handled for a broadcast event
87 */
88 public void setHandled(final boolean handled) {
89 if (handled && isBroadcast()) {
90 throw new IllegalStateException("Broadcast event cannot be set to handled by any widget");
91 }
92 this.handled = handled;
93 }
94
95 /**
96 * @see java.lang.Object#toString()
97 */
98 @Override
99 public String toString() {
100 StringBuilder sb = new StringBuilder();
101 sb.append(this.getClass().getName());
102 sb.append("{target=");
103 sb.append(this.target);
104 sb.append(", handled=");
105 sb.append(this.handled);
106 sb.append("}");
107 return sb.toString();
108 }
109
110
111 }