1 2 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 ; 33 import java.net.InetAddress ; 34 import java.net.UnknownHostException ; 35 import java.util.*; 36 37 import org.apache.commons.logging.Log; 38 import org.apache.commons.logging.LogFactory; 39 40 45 46 public class Target extends VisualElement { 47 private static Log log = LogFactory.getLog(Target.class); 48 49 final private String name; 51 private MultiValueMap registered_components = new MultiValueMap(); 52 53 final private MultiValueMap events = new MultiValueMap(); 57 58 63 public Target(final String name) throws AlgorithmException { 65 if (name == null) throw new AlgorithmException("name is null"); 66 this.name = name; 67 } 68 69 75 public void registerComponent(final BasicComponent component, final Class clazz) { 77 synchronized (registered_components) { 78 if (!registered_components.containsValue(clazz, component)) 79 registered_components.put(clazz, component); 80 } 81 } 82 83 88 public void unregisterComponent(final BasicComponent component, final Class clazz) { 90 synchronized (registered_components) { 91 if (registered_components.remove(clazz, component) == null) 92 log.error("component was not registered"); 93 } 94 } 95 96 101 public EventGeneric getLastEvent(Class 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 117 public List<EventGeneric> getEvents(final Date begin, final Date end, Class 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 163 public String getName() { 164 return name; 165 } 166 167 172 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 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 clazz) { 195 synchronized (events) { 196 events.remove(clazz); 197 } 198 } 199 200 205 @SuppressWarnings ("unchecked") 207 public Set<Class > getEventTypesInUse() { 208 synchronized (events) { 209 return new HashSet<Class >(events.keySet()); 211 } 212 } 213 214 219 public void disposed() { 221 super.disposed(); 222 getGUI().dropTargetInstance(this); 223 } 224 225 230 public boolean canAddTarget(final VisualElement parent) { 231 if (parent != null) synchronized (getGUI().getGUICreated()) { 232 if (getGUI().getGUICreated()[0] == true) { 233 if (parent.equals(getGUI().getVisualThisHost())) return false; 235 236 if (parent.equals(getGUI().getVisualTransientAll())) return false; 238 239 if (parent.equals(getGUI().getVisualTransientNetworks()) || getGUI().getVisualTransientNetworks().getChildren().contains(parent)) 241 return false; 242 243 if (parent.getChildren().contains(this)) return false; 245 246 if (parent.equals(this)) return false; 248 249 if (parent.getAllParents(getClass()).contains(this)) return false; 251 } 252 } 253 254 return true; 255 } 256 257 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 281 public boolean equals(final Object 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 294 public int hashCode() { 296 return getName().hashCode(); 297 } 298 } 299 | Popular Tags |