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
029 package edu.uchsc.ccp.knowtator.ui;
030
031 import java.util.ArrayList;
032 import java.util.Collection;
033 import java.util.List;
034
035 import edu.stanford.smi.protege.model.Project;
036 import edu.stanford.smi.protege.model.SimpleInstance;
037 import edu.stanford.smi.protege.ui.InstanceDisplay;
038 import edu.stanford.smi.protege.util.CollectionUtilities;
039 import edu.stanford.smi.protege.widget.ClsWidget;
040 import edu.stanford.smi.protege.widget.SlotWidget;
041 import edu.uchsc.ccp.knowtator.InvalidSpanException;
042 import edu.uchsc.ccp.knowtator.KnowtatorProjectUtil;
043 import edu.uchsc.ccp.knowtator.Span;
044 import edu.uchsc.ccp.knowtator.SpanWidget;
045 import edu.uchsc.ccp.knowtator.event.EventHandler;
046 import edu.uchsc.ccp.knowtator.event.SelectedAnnotationChangeEvent;
047 import edu.uchsc.ccp.knowtator.event.SelectedAnnotationChangeListener;
048
049 public class AnnotationInstanceDisplay extends InstanceDisplay implements SelectedAnnotationChangeListener
050
051 {
052 KnowtatorProjectUtil kpu;
053
054 Project project;
055
056 public AnnotationInstanceDisplay(KnowtatorProjectUtil kpu, Project project) {
057 super(project);
058 this.kpu = kpu;
059 this.project = project;
060 EventHandler.getInstance().addSelectedAnnotationChangeListener(this);
061 }
062
063 public AnnotationInstanceDisplay(KnowtatorProjectUtil kpu, Project project, boolean showHeader,
064 boolean showHeaderLabel) {
065 super(project, showHeader, showHeaderLabel);
066 this.kpu = kpu;
067 this.project = project;
068 EventHandler.getInstance().addSelectedAnnotationChangeListener(this);
069 }
070
071 /**
072 * Returns a list Span objects corresponding to the span strings selected in
073 * the span widget If there are no spans selected, then the first span
074 * listed in the span widget is returned.
075 */
076 public List<Span> getSelectedSpans() throws InvalidSpanException {
077 List<Span> spans = new ArrayList<Span>();
078 ClsWidget clsWidget = getFirstClsWidget(); // getCurrentClsWidget();
079 SlotWidget slotWidget = clsWidget.getSlotWidget(kpu.getAnnotationSpanSlot());
080 SpanWidget spanWidget = (SpanWidget) slotWidget;
081
082 // spanWidget.addSelectionListener(listener)
083
084 try {
085 Collection<String> spanStrings = (Collection<String>) spanWidget.getSelection();
086 for (String spanString : spanStrings) {
087 Span span = Span.parseSpan(spanString);
088 spans.add(span);
089 }
090 if (spans.size() == 0) {
091 String spanString = (String) CollectionUtilities.getFirstItem(spanWidget.getValues());
092 if (spanString != null) {
093 Span span = Span.parseSpan(spanString);
094 spans.add(span);
095 }
096 }
097 } catch (ClassCastException cce) {
098 throw new InvalidSpanException(cce.getMessage());
099 }
100 return spans;
101 }
102
103 public int[] getSelectedSpanIndices() throws InvalidSpanException {
104 ClsWidget clsWidget = getFirstClsWidget(); // getCurrentClsWidget();
105 SlotWidget slotWidget = clsWidget.getSlotWidget(kpu.getAnnotationSpanSlot());
106 SpanWidget spanWidget = (SpanWidget) slotWidget;
107 return spanWidget.getSelectedIndices();
108 }
109
110 public void setSelectedSpanIndices(int[] selectedSpanIndices) {
111 ClsWidget clsWidget = getFirstClsWidget(); // getCurrentClsWidget();
112 SlotWidget slotWidget = clsWidget.getSlotWidget(kpu.getAnnotationSpanSlot());
113 SpanWidget spanWidget = (SpanWidget) slotWidget;
114 spanWidget.setSelectedIndices(selectedSpanIndices);
115 }
116
117 public void annotationSelectionChanged(SelectedAnnotationChangeEvent sace) {
118 SimpleInstance annotation = sace.getSelectedAnnotation();
119 setInstance(annotation);
120 }
121 }