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    /**
030     * 8/16/2005   pvo  modified handleAddAction such that a message dialog appears if
031     *                  there is no span selected instructing the user to select a span
032     *                  of text before clicking the add button.
033     */
034    
035    package edu.uchsc.ccp.knowtator;
036    
037    import java.util.ArrayList;
038    import java.util.Collection;
039    import java.util.HashSet;
040    import java.util.List;
041    import java.util.Set;
042    
043    import javax.swing.JOptionPane;
044    
045    import edu.stanford.smi.protege.model.SimpleInstance;
046    import edu.stanford.smi.protege.widget.StringListWidget;
047    import edu.uchsc.ccp.knowtator.textsource.TextSourceAccessException;
048    
049    public class SpanWidget extends StringListWidget {
050            static final long serialVersionUID = 0;
051    
052            KnowtatorManager manager;
053    
054            KnowtatorProjectUtil kpu;
055    
056            AnnotationUtil annotationUtil;
057    
058            public void initialize() {
059                    super.initialize();
060                    manager = (KnowtatorManager) getKnowledgeBase().getClientInformation(Knowtator.KNOWTATOR_MANAGER);
061                    kpu = manager.getKnowtatorProjectUtil();
062                    annotationUtil = manager.getAnnotationUtil();
063            }
064    
065            protected void handleCreateAction() {
066                    handleAddAction();
067            }
068    
069            private List<Span> getIntersectingSpans(Span span, List<Span> spans) {
070                    List<Span> returnValues = new ArrayList<Span>();
071                    for (Span spn : spans) {
072                            if (spn.intersects(span)) {
073                                    returnValues.add(spn);
074                            }
075                    }
076                    return returnValues;
077            }
078    
079            protected void handleAddAction() {
080                    try {
081                            SimpleInstance annotation = (SimpleInstance) getInstance();
082                            List<Span> selectedSpans = manager.getSelectedSpans();
083    
084                            if (selectedSpans.size() > 0) {
085                                    List<Span> newSpans = new ArrayList<Span>();
086                                    Set<Span> retiredSpans = new HashSet<Span>();
087                                    List<Span> currentSpans = annotationUtil.getSpans(annotation);
088                                    for (Span selectedSpan : selectedSpans) {
089                                            List<Span> intersectingSpans = getIntersectingSpans(selectedSpan, currentSpans);
090                                            if (intersectingSpans.size() > 0) {
091                                                    Span mergedSpan = selectedSpan;
092                                                    for (Span intersectingSpan : intersectingSpans) {
093                                                            mergedSpan = Span.merge(mergedSpan, intersectingSpan);
094                                                    }
095                                                    newSpans.add(mergedSpan);
096                                                    retiredSpans.addAll(intersectingSpans);
097                                            } else {
098                                                    newSpans.add(selectedSpan);
099                                            }
100                                    }
101    
102                                    List<Span> spans = new ArrayList<Span>();
103                                    for (Span currentSpan : currentSpans) {
104                                            if (!retiredSpans.contains(currentSpan)) {
105                                                    spans.add(currentSpan);
106                                            }
107                                    }
108                                    spans.addAll(newSpans);
109                                    annotationUtil.setSpans(annotation, spans, null);
110                                    manager.refreshAnnotationsDisplay(true);
111                            } else {
112                                    JOptionPane.showMessageDialog(null,
113                                                    "Select a span of text in the text viewer before clicking the 'add span' button.",
114                                                    "Select Span First", JOptionPane.INFORMATION_MESSAGE);
115                            }
116                    } catch (InvalidSpanException ise) {
117                            JOptionPane.showMessageDialog(this, "Annotation has an invalid span value: " + ise.getMessage(),
118                                            "Invalid span", JOptionPane.ERROR_MESSAGE);
119                    } catch (TextSourceAccessException tsae) {
120                            JOptionPane.showMessageDialog(null, "There was a problem retrieving the text from the text source: "
121                                            + tsae.getMessage(), "Text Source Error", JOptionPane.ERROR_MESSAGE);
122                    }
123            }
124    
125            protected void handleRemoveAction(Collection instances) {
126                    try {
127                            super.handleRemoveAction(instances);
128                            annotationUtil.setSpans((SimpleInstance) getInstance(), annotationUtil.getSpans(
129                                            (SimpleInstance) getInstance(), true), null);
130                            manager.refreshAnnotationsDisplay(true);
131                    } catch (InvalidSpanException ise) {
132                            JOptionPane.showMessageDialog(this, "Annotation has an invalid span value: " + ise.getMessage(),
133                                            "Invalid span", JOptionPane.ERROR_MESSAGE);
134                    } catch (TextSourceAccessException tsae) {
135                            JOptionPane.showMessageDialog(null, "There was a problem retrieving the text from the text source: "
136                                            + tsae.getMessage(), "Text Source Error", JOptionPane.ERROR_MESSAGE);
137                    }
138    
139            }
140    
141            public int[] getSelectedIndices() {
142                    return getList().getSelectedIndices();
143            }
144    
145            public void setSelectedIndices(int[] selectedIndices) {
146                    getList().setSelectedIndices(selectedIndices);
147            }
148    
149            @Override
150            protected void handleViewAction(String str) {
151                    return;
152            }
153    
154    }