001 /* 002 * The contents of this file are subject to the Mozilla Public 003 * License Version 1.1 (the "License"); you may not use this file 004 * except in compliance with the License. You may obtain a copy of 005 * the License at http://www.mozilla.org/MPL/ 006 * 007 * Software distributed under the License is distributed on an "AS 008 * IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or 009 * implied. See the License for the specific language governing 010 * rights and limitations under the License. 011 * 012 * The Original Code is Knowtator. 013 * 014 * The Initial Developer of the Original Code is University of Colorado. 015 * Copyright (C) 2005-2008. All Rights Reserved. 016 * 017 * Knowtator was developed by the Center for Computational Pharmacology 018 * (http://compbio.uchcs.edu) at the University of Colorado Health 019 * Sciences Center School of Medicine with support from the National 020 * Library of Medicine. 021 * 022 * Current information about Knowtator can be obtained at 023 * http://knowtator.sourceforge.net/ 024 * 025 * Contributor(s): 026 * Philip V. Ogren <philip@ogren.info> (Original Author) 027 */ 028 package edu.uchsc.ccp.knowtator.event; 029 030 import java.util.ArrayList; 031 import java.util.List; 032 033 import org.apache.log4j.Logger; 034 035 import edu.stanford.smi.protege.model.Cls; 036 import edu.stanford.smi.protege.model.Frame; 037 import edu.stanford.smi.protege.model.Instance; 038 import edu.stanford.smi.protege.model.KnowledgeBase; 039 import edu.stanford.smi.protege.model.SimpleInstance; 040 import edu.uchsc.ccp.knowtator.AnnotationUtil; 041 import edu.uchsc.ccp.knowtator.BrowserTextUtil; 042 import edu.uchsc.ccp.knowtator.FilterUtil; 043 import edu.uchsc.ccp.knowtator.KnowtatorManager; 044 import edu.uchsc.ccp.knowtator.KnowtatorProjectUtil; 045 import edu.uchsc.ccp.knowtator.MentionUtil; 046 import edu.uchsc.ccp.knowtator.Span; 047 import edu.uchsc.ccp.knowtator.TextSourceUtil; 048 049 public class EventHandler { 050 List<SelectedSpanChangeListener> selectedSpanChangeListeners; 051 052 List<SelectedAnnotationChangeListener> selectedAnnotationChangeListeners; 053 054 List<SelectedClsChangeListener> selectedClsChangeListeners; 055 056 List<AnnotationCreatedListener> annotationCreatedListeners; 057 058 List<CurrentAnnotationsChangedListener> currentAnnotationsChangedListeners; 059 060 List<RefreshAnnotationsDisplayListener> refreshAnnotationsDisplayListeners; 061 062 List<FastAnnotateListener> fastAnnotateListeners; 063 064 List<FilterChangedListener> filterChangedListeners; 065 066 List<NotifyTextChangeListener> notifyTextChangeListeners; 067 068 List<SpanEditListener> spanEditListeners; 069 070 KnowledgeBase kb; 071 072 KnowtatorManager manager; 073 074 AnnotationUtil annotationUtil; 075 076 MentionUtil mentionUtil; 077 078 KnowtatorProjectUtil kpu; 079 080 TextSourceUtil textSourceUtil; 081 082 FilterUtil filterUtil; 083 084 BrowserTextUtil browserTextUtil; 085 086 Logger logger = Logger.getLogger(EventHandler.class); 087 088 private static EventHandler instance = null; 089 090 public static EventHandler getInstance() { 091 if (instance == null) { 092 instance = new EventHandler(); 093 } 094 return instance; 095 } 096 097 protected EventHandler() { 098 selectedSpanChangeListeners = new ArrayList<SelectedSpanChangeListener>(); 099 selectedAnnotationChangeListeners = new ArrayList<SelectedAnnotationChangeListener>(); 100 selectedClsChangeListeners = new ArrayList<SelectedClsChangeListener>(); 101 annotationCreatedListeners = new ArrayList<AnnotationCreatedListener>(); 102 currentAnnotationsChangedListeners = new ArrayList<CurrentAnnotationsChangedListener>(); 103 refreshAnnotationsDisplayListeners = new ArrayList<RefreshAnnotationsDisplayListener>(); 104 fastAnnotateListeners = new ArrayList<FastAnnotateListener>(); 105 filterChangedListeners = new ArrayList<FilterChangedListener>(); 106 notifyTextChangeListeners = new ArrayList<NotifyTextChangeListener>(); 107 spanEditListeners = new ArrayList<SpanEditListener>(); 108 } 109 110 public void setKnowtatorManager(KnowtatorManager manager) { 111 this.manager = manager; 112 kb = manager.getKnowledgeBase(); 113 mentionUtil = manager.getMentionUtil(); 114 annotationUtil = manager.getAnnotationUtil(); 115 kpu = manager.getKnowtatorProjectUtil(); 116 textSourceUtil = manager.getTextSourceUtil(); 117 filterUtil = manager.getFilterUtil(); 118 browserTextUtil = manager.getBrowserTextUtil(); 119 } 120 121 public void addRefreshAnnotationsDisplayListener(RefreshAnnotationsDisplayListener refreshSpanHighlightsListener) { 122 refreshAnnotationsDisplayListeners.add(refreshSpanHighlightsListener); 123 } 124 125 public void removeRefreshSpanHighlightsListener(RefreshAnnotationsDisplayListener refreshSpanHighlightsListener) { 126 refreshAnnotationsDisplayListeners.remove(refreshSpanHighlightsListener); 127 } 128 129 public void fireRefreshAnnotationsDisplay(boolean scrollToSelection) { 130 logger.debug(""); 131 132 for (RefreshAnnotationsDisplayListener refreshAnnotationsDisplayListener : refreshAnnotationsDisplayListeners) { 133 refreshAnnotationsDisplayListener.refreshAnnotationsDisplay(scrollToSelection); 134 } 135 } 136 137 public void addCurrentAnnotationsChangedListener(CurrentAnnotationsChangedListener currentAnnotationsChangedListener) { 138 currentAnnotationsChangedListeners.add(currentAnnotationsChangedListener); 139 } 140 141 public void removeCurrentAnnotationsChangedListener( 142 CurrentAnnotationsChangedListener currentAnnotationsChangedListener) { 143 currentAnnotationsChangedListeners.remove(currentAnnotationsChangedListener); 144 } 145 146 public void fireCurrentAnnotationsChanged() { 147 logger.debug(""); 148 List<SimpleInstance> filteredAnnotations = manager.getCurrentFilteredAnnotations(); 149 List<SimpleInstance> partiallyFilteredAnnotations = manager.getCurrentPartiallyFilteredAnnotations(); 150 151 CurrentAnnotationsChangeEvent cace = new CurrentAnnotationsChangeEvent(manager, filteredAnnotations, 152 partiallyFilteredAnnotations); 153 for (CurrentAnnotationsChangedListener cacl : currentAnnotationsChangedListeners) { 154 cacl.currentAnnotationsChanged(cace); 155 } 156 } 157 158 public void addSelectedSpanChangeListener(SelectedSpanChangeListener selectedSpanChangeListener) { 159 selectedSpanChangeListeners.add(selectedSpanChangeListener); 160 } 161 162 public void removeSelectedSpanChangeListener(SelectedSpanChangeListener selectedSpanChangeListener) { 163 selectedSpanChangeListeners.remove(selectedSpanChangeListener); 164 } 165 166 /** 167 * The preferred way to update the currently selected spans is via 168 * KnowtatorManager.setSelectedSpans. Please use that method instead. 169 * 170 * @param selectedSpans 171 * @see KnowtatorManager#setSelectedSpans(List) 172 */ 173 public void fireSelectedSpanChanged(List<Span> selectedSpans) { 174 if (logger.isDebugEnabled()) { 175 StringBuffer sb = new StringBuffer(); 176 for (Span span : selectedSpans) 177 sb.append(" " + span.getStart() + "|" + span.getEnd()); 178 logger.debug(sb.toString()); 179 } 180 181 SelectedSpanChangeEvent ssce = new SelectedSpanChangeEvent(manager, selectedSpans); 182 for (SelectedSpanChangeListener sscl : selectedSpanChangeListeners) { 183 sscl.spanSelectionChanged(ssce); 184 } 185 } 186 187 public void addSelectedAnnotationChangeListener(SelectedAnnotationChangeListener selectedAnnotationChangeListener) { 188 selectedAnnotationChangeListeners.add(selectedAnnotationChangeListener); 189 } 190 191 public void removeSelectedAnnotationChangeListener(SelectedAnnotationChangeListener selectedAnnotationChangeListener) { 192 selectedAnnotationChangeListeners.remove(selectedAnnotationChangeListener); 193 } 194 195 /** 196 * The preferred way of updating the currently selected annotation is via 197 * KnowtatorManager.setSelectedAnnotation(). Please use that method instead. 198 * 199 * @param selectedAnnotation 200 * @see KnowtatorManager#setSelectedAnnotationSet(SimpleInstance) 201 */ 202 public void fireSelectedAnnotationChanged(SimpleInstance selectedAnnotation) { 203 logger.debug("selected annotation=\"" + browserTextUtil.getBrowserText(selectedAnnotation, 100) + "\""); 204 SelectedAnnotationChangeEvent sace = new SelectedAnnotationChangeEvent(manager, selectedAnnotation); 205 for (SelectedAnnotationChangeListener sacl : selectedAnnotationChangeListeners) { 206 sacl.annotationSelectionChanged(sace); 207 } 208 } 209 210 public void addSelectedClsChangeListener(SelectedClsChangeListener selectedClsChangeListener) { 211 selectedClsChangeListeners.add(selectedClsChangeListener); 212 } 213 214 public void removeSelectedClsChangeListener(SelectedClsChangeListener selectedClsChangeListener) { 215 selectedClsChangeListeners.remove(selectedClsChangeListener); 216 } 217 218 /** 219 * The preferred way of updating the currently selected cls is via 220 * KnowtatorManager.setSelectedCls(). Please use that method instead. 221 * 222 * @param selectedCls 223 * @see KnowtatorManager#setSelectedCls(Cls) 224 */ 225 public void fireSelectedClsChanged(Cls selectedCls) { 226 logger.debug("selected cls = \"" + selectedCls.getBrowserText() + "\""); 227 SelectedClsChangeEvent scce = new SelectedClsChangeEvent(manager, selectedCls); 228 for (SelectedClsChangeListener sccl : selectedClsChangeListeners) { 229 sccl.clsSelectionChanged(scce); 230 } 231 } 232 233 public void addAnnotationCreatedListener(AnnotationCreatedListener annotationCreatedListener) { 234 annotationCreatedListeners.add(annotationCreatedListener); 235 } 236 237 public void removeAnnotationCreatedListener(AnnotationCreatedListener annotationCreatedListener) { 238 annotationCreatedListeners.remove(annotationCreatedListener); 239 } 240 241 /** 242 * The preferred way of creating an annotation is via 243 * KnowtatorManager.createAnnotation. Please use that method instead. 244 * 245 * @param createdAnnotation 246 * @see KnowtatorManager#createAnnotation(Instance) 247 */ 248 public void fireAnnotationCreated(SimpleInstance createdAnnotation) { 249 logger.debug("created annotation = \"" + browserTextUtil.getBrowserText(createdAnnotation, 100) + "\""); 250 AnnotationCreatedEvent event = new AnnotationCreatedEvent(manager, createdAnnotation); 251 for (AnnotationCreatedListener acl : annotationCreatedListeners) { 252 acl.annotationCreated(event); 253 } 254 } 255 256 public void addFastAnnotateListener(FastAnnotateListener fastAnnotateListener) { 257 fastAnnotateListeners.add(fastAnnotateListener); 258 } 259 260 public void removeFastAnnotateListener(FastAnnotateListener fastAnnotateListener) { 261 fastAnnotateListeners.remove(fastAnnotateListener); 262 } 263 264 public void fireFastAnnotateStart() { 265 logger.debug(""); 266 for (FastAnnotateListener fastAnnotateListener : fastAnnotateListeners) { 267 fastAnnotateListener.fastAnnotateStart(); 268 } 269 } 270 271 public void fireFastAnnotateQuit() { 272 logger.debug(""); 273 for (FastAnnotateListener fastAnnotateListener : fastAnnotateListeners) { 274 fastAnnotateListener.fastAnnotateQuit(); 275 } 276 } 277 278 public void fireFastAnnotateClsChange() { 279 logger.debug(""); 280 for (FastAnnotateListener fastAnnotateListener : fastAnnotateListeners) { 281 fastAnnotateListener.fastAnnotateClsChange(); 282 } 283 } 284 285 public void fireFastAnnotateAddCls(Frame frame) { 286 logger.debug("Adding class: " + frame + " to fast annotate tool bar"); 287 for (FastAnnotateListener fastAnnotateListener : fastAnnotateListeners) { 288 fastAnnotateListener.fastAnnotateAddCls(frame); 289 } 290 } 291 292 public void fireFastAnnotateRemoveCls(Frame frame) { 293 logger.debug("Removing class: " + frame + " from fast annotate tool bar"); 294 for (FastAnnotateListener fastAnnotateListener : fastAnnotateListeners) { 295 fastAnnotateListener.fastAnnotateRemoveCls(frame); 296 } 297 } 298 299 public void addFilterChangedListener(FilterChangedListener filterChangedListener) { 300 filterChangedListeners.add(filterChangedListener); 301 } 302 303 public void removeFilterChangedListener(FilterChangedListener filterChangedListener) { 304 filterChangedListeners.remove(filterChangedListener); 305 } 306 307 public void fireFilterChanged(SimpleInstance filter, boolean consensusMode) { 308 logger.debug(" consensus mode = " + consensusMode); 309 FilterChangedEvent event = new FilterChangedEvent(manager, filter, consensusMode); 310 for (FilterChangedListener filterChangedListener : filterChangedListeners) { 311 filterChangedListener.filterChanged(event); 312 } 313 } 314 315 public void addNotifyTextChangeListener(NotifyTextChangeListener notifyTextChangeListener) { 316 notifyTextChangeListeners.add(notifyTextChangeListener); 317 } 318 319 public void removeNotifyTextChangeListner(NotifyTextChangeListener notifyTextChangeListener) { 320 notifyTextChangeListeners.remove(notifyTextChangeListener); 321 } 322 323 public void fireNotifyTextChanged(String notifyText) { 324 logger.debug(notifyText); 325 for (NotifyTextChangeListener listener : notifyTextChangeListeners) { 326 listener.notifyTextChanged(notifyText); 327 } 328 } 329 330 public void addSpanEditListener(SpanEditListener spanEditListener) { 331 spanEditListeners.add(spanEditListener); 332 } 333 334 public void removeSpanEditListner(SpanEditListener spanEditListener) { 335 spanEditListeners.remove(spanEditListener); 336 } 337 338 public void fireSpanEditted(SimpleInstance annotation) { 339 logger.debug(manager.getBrowserTextUtil().getBrowserText(annotation)); 340 SpanEditEvent see = new SpanEditEvent(manager, annotation); 341 for (SpanEditListener listener : spanEditListeners) { 342 listener.spanEditted(see); 343 } 344 } 345 346 public void dispose() { 347 selectedSpanChangeListeners.clear(); 348 selectedAnnotationChangeListeners.clear(); 349 selectedClsChangeListeners.clear(); 350 annotationCreatedListeners.clear(); 351 currentAnnotationsChangedListeners.clear(); 352 refreshAnnotationsDisplayListeners.clear(); 353 fastAnnotateListeners.clear(); 354 filterChangedListeners.clear(); 355 notifyTextChangeListeners.clear(); 356 } 357 358 }