KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > fenyo > gnetwatch > targets > Target


1
2 /*
3  * GNetWatch
4  * Copyright 2006, 2007 Alexandre Fenyo
5  * gnetwatch@fenyo.net
6  *
7  * This file is part of GNetWatch.
8  *
9  * GNetWatch is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * GNetWatch is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with GNetWatch; if not, write to the Free Software
21  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
22  */

23
24 package net.fenyo.gnetwatch.targets;
25
26 import net.fenyo.gnetwatch.*;
27 import net.fenyo.gnetwatch.GUI.*;
28 import net.fenyo.gnetwatch.data.*;
29
30 import org.apache.commons.collections.map.*;
31
32 import java.net.Inet4Address JavaDoc;
33 import java.net.InetAddress JavaDoc;
34 import java.net.UnknownHostException JavaDoc;
35 import java.util.*;
36
37 import org.apache.commons.logging.Log;
38 import org.apache.commons.logging.LogFactory;
39
40 /**
41  * Target is the base class for every types of targets.
42  * @author Alexandre Fenyo
43  * @version $Id: Target.java,v 1.31 2007/03/12 05:04:15 fenyo Exp $
44  */

45
46 public class Target extends VisualElement {
47   private static Log log = LogFactory.getLog(Target.class);
48
49   final private String JavaDoc name; // not null
50

51   private MultiValueMap registered_components = new MultiValueMap();
52
53   // maps a class of events to instances of this class
54
// note that this class is not synchronized
55
// the MultiValueMap decorates an ArrayList by defaults
56
final private MultiValueMap events = new MultiValueMap();
57
58   /**
59    * Constructor.
60    * @param name target name.
61    * @throws AlgorithmException exception.
62    */

63   // GUI thread
64
public Target(final String JavaDoc name) throws AlgorithmException {
65     if (name == null) throw new AlgorithmException("name is null");
66     this.name = name;
67   }
68
69   /**
70    * Informs this target that this component is interested in this type of events.
71    * @param component component to register.
72    * @param clazz type of events.
73    * @return void.
74    */

75   // AWT thread
76
public void registerComponent(final BasicComponent component, final Class JavaDoc clazz) {
77     synchronized (registered_components) {
78       if (!registered_components.containsValue(clazz, component))
79         registered_components.put(clazz, component);
80     }
81   }
82
83   /**
84    * Unregister a component.
85    * @param component component to unregister.
86    * @param clazz type of events.
87    */

88   // AWT thread
89
public void unregisterComponent(final BasicComponent component, final Class JavaDoc clazz) {
90     synchronized (registered_components) {
91       if (registered_components.remove(clazz, component) == null)
92         log.error("component was not registered");
93     }
94   }
95
96   /**
97    * Returns the last event.
98    * @param clazz type of events.
99    * @return EventGeneric last event.
100    */

101   public EventGeneric getLastEvent(Class JavaDoc clazz) {
102     synchronized (events) {
103       final ArrayList<EventGeneric> events_of_this_class =
104         (ArrayList<EventGeneric>) events.getCollection(clazz);
105       if (events_of_this_class == null) return null;
106       return events_of_this_class.get(events_of_this_class.size() - 1);
107     }
108   }
109
110   /**
111    * Returns events from the first BEFORE begin (or at begin) to the last AFTER end (or at end).
112    * @param begin start time.
113    * @param end end time.
114    * @param clazz type of events.
115    * @return List<EventGeneric> list of selected events.
116    */

117   // AWT thread
118
public List<EventGeneric> getEvents(final Date begin, final Date end, Class JavaDoc clazz) {
119     final List<EventGeneric> selected_events = new LinkedList<EventGeneric>();
120
121     synchronized (events) {
122       final ArrayList<EventGeneric> events_of_this_class =
123         (ArrayList<EventGeneric>) events.getCollection(clazz);
124       if (events_of_this_class == null) return selected_events;
125
126       EventGeneric just_before = null, just_after = null;
127       for (final EventGeneric event : events_of_this_class) {
128         if (event.getDate().after(begin) && event.getDate().before(end) ||
129             event.getDate().equals(begin) || event.getDate().equals(end))
130           selected_events.add(event);
131         if ((event.getDate().before(begin) || event.getDate().equals(begin)) &&
132             (just_before == null || event.getDate().after(just_before.getDate())))
133             just_before = event;
134         if ((event.getDate().after(end) || event.getDate().equals(end)) &&
135             (just_after == null || event.getDate().before(just_after.getDate())))
136           just_after = event;
137       }
138
139       if (just_before != null || just_after != null) {
140         for (final EventGeneric event : events_of_this_class) {
141           if (just_before != null && just_before.getDate().equals(event.getDate()) &&
142               !selected_events.contains(event)) selected_events.add(event);
143           if (just_after != null && just_after.getDate().equals(event.getDate()) &&
144               !selected_events.contains(event)) selected_events.add(event);
145         }
146       }
147
148       Collections.sort(selected_events, new Comparator<EventGeneric>() {
149         public int compare(final EventGeneric o1, final EventGeneric o2) {
150           return o1.getDate().compareTo(o2.getDate());
151         }
152       });
153
154       return selected_events;
155     }
156   }
157
158   /**
159    * Returns the name of this target.
160    * @param none.
161    * @return String target name.
162    */

163   public String JavaDoc getName() {
164     return name;
165   }
166
167   /**
168    * Adds a new event.
169    * @param event event to add.
170    * @return void.
171    */

172   // Queue thread
173
public void addEvent(final EventGeneric event) {
174     final List<BasicComponent> components = new ArrayList<BasicComponent>();
175
176     synchronized (registered_components) {
177       if (registered_components.containsKey(event.getClass()))
178         for (final BasicComponent component : (ArrayList<BasicComponent>)
179             registered_components.getCollection(event.getClass()))
180           components.add(component);
181     }
182     for (final BasicComponent component : components) component.updateValues(event);
183
184     synchronized (events) {
185       final boolean is_new_event_class = !events.containsKey(event.getClass());
186       events.put(event.getClass(), event);
187       // max of 300 events per type and per target
188
if (events.getCollection(event.getClass()).size() > 300)
189         ((ArrayList) events.getCollection(event.getClass())).remove(0);
190       if (is_new_event_class == true) getGUI().informTargetHasNewEventClass(this);
191     }
192   }
193
194   public void removeEvents(final Class JavaDoc clazz) {
195     synchronized (events) {
196     events.remove(clazz);
197     }
198   }
199
200   /**
201    * Returns the event types of events associated to this target.
202    * @param none.
203    * @return Set<Class> event types.
204    */

205   // GUI thread
206
@SuppressWarnings JavaDoc("unchecked")
207   public Set<Class JavaDoc> getEventTypesInUse() {
208     synchronized (events) {
209       // returns a copy of the set of keys
210
return new HashSet<Class JavaDoc>(events.keySet());
211     }
212   }
213
214   /**
215    * Called when this target is disposed.
216    * @param none.
217    * @return void.
218    */

219   // GUI thread
220
public void disposed() {
221     super.disposed();
222     getGUI().dropTargetInstance(this);
223   }
224
225   /**
226    * Checks that this target can be attached to a specific parent element.
227    * @param parent parent element.
228    * @return true if this target can be attached to this parent element.
229    */

230   public boolean canAddTarget(final VisualElement parent) {
231     if (parent != null) synchronized (getGUI().getGUICreated()) {
232       if (getGUI().getGUICreated()[0] == true) {
233         // group "local host" is readonly
234
if (parent.equals(getGUI().getVisualThisHost())) return false;
235
236         // group "every host" is readonly
237
if (parent.equals(getGUI().getVisualTransientAll())) return false;
238
239         // group "networks" and its childs are readonly
240
if (parent.equals(getGUI().getVisualTransientNetworks()) || getGUI().getVisualTransientNetworks().getChildren().contains(parent))
241           return false;
242
243         // avoid twins
244
if (parent.getChildren().contains(this)) return false;
245
246         // avoid self reference
247
if (parent.equals(this)) return false;
248
249         // avoid loops
250
if (parent.getAllParents(getClass()).contains(this)) return false;
251       }
252     }
253
254     return true;
255   }
256
257   /**
258    * Attaches this target to a specific parent element.
259    * @param gui current GUI instance.
260    * @param parent parent element.
261    * @return true if this target has been succesfully attached to this parent element.
262    */

263   public boolean addTarget(final GUI gui, final VisualElement parent) {
264     initialize(gui);
265
266     if (!canAddTarget(parent)) return false;
267     if (parent != null && !parent.canManageThisChild(this)) return false;
268
269     if (parent != null) {
270       getGUI().getCanonicalInstance(this).setParent(getGUI(), parent);
271       if (getGUI().getCanonicalInstance(this) == this) return true;
272     }
273     return false;
274   }
275
276   /**
277    * Compares two targets.
278    * @param o target to compare to.
279    * @return true if the targets are equal.
280    */

281   // any thread
282
public boolean equals(final Object JavaDoc o) {
283     if (this == o) return true;
284     if ((o == null) || (o.getClass() != getClass())) return false;
285     final Target target = (Target) o;
286     return getName().equals(target.getName());
287   }
288
289   /**
290    * Returns the hashcode for this target.
291    * @param none.
292    * @return int hashcode.
293    */

294   // any thread
295
public int hashCode() {
296     return getName().hashCode();
297   }
298 }
299
Popular Tags