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.ui;
029    
030    import java.awt.GridBagConstraints;
031    import java.awt.GridBagLayout;
032    import java.awt.Insets;
033    import java.awt.event.ActionEvent;
034    import java.awt.event.ActionListener;
035    import java.util.List;
036    
037    import javax.swing.BorderFactory;
038    import javax.swing.JButton;
039    import javax.swing.JLabel;
040    import javax.swing.JOptionPane;
041    import javax.swing.JPanel;
042    import javax.swing.border.EtchedBorder;
043    
044    import edu.stanford.smi.protege.model.SimpleInstance;
045    import edu.stanford.smi.protege.util.ComponentUtilities;
046    import edu.uchsc.ccp.knowtator.InvalidSpanException;
047    import edu.uchsc.ccp.knowtator.KnowtatorManager;
048    import edu.uchsc.ccp.knowtator.Span;
049    import edu.uchsc.ccp.knowtator.SpanUtil;
050    import edu.uchsc.ccp.knowtator.textsource.TextSourceAccessException;
051    
052    public class SpanEditPanel extends JPanel implements ActionListener {
053            static final long serialVersionUID = 0;
054    
055            KnowtatorManager manager;
056    
057            AnnotationInstanceDisplay annotationDisplay;
058    
059            SpanUtil spanUtil;
060    
061            JButton growLeftAnnotationButton;
062    
063            JButton shrinkLeftAnnotationButton;
064    
065            JButton shrinkRightAnnotationButton;
066    
067            JButton growRightAnnotationButton;
068    
069            public SpanEditPanel(KnowtatorManager manager, AnnotationInstanceDisplay annotationDisplay) {
070                    super(new GridBagLayout());
071                    this.manager = manager;
072                    this.annotationDisplay = annotationDisplay;
073                    this.spanUtil = manager.getSpanUtil();
074                    initialize();
075            }
076    
077            private void initialize() {
078                    growLeftAnnotationButton = new JButton(ComponentUtilities.loadImageIcon(SpanEditPanel.class,
079                                    "/edu/uchsc/ccp/knowtator/images/extend_left.gif"));
080                    growLeftAnnotationButton.setActionCommand(SpanUtil.GROW_ANNOTATION_LEFT);
081                    growLeftAnnotationButton.setBorder(BorderFactory.createEtchedBorder(EtchedBorder.LOWERED));
082                    growLeftAnnotationButton.setToolTipText("Grow annotation on left side");
083                    growLeftAnnotationButton.setBorder(null);
084                    shrinkLeftAnnotationButton = new JButton(ComponentUtilities.loadImageIcon(SpanEditPanel.class,
085                                    "/edu/uchsc/ccp/knowtator/images/shrink_left.gif"));
086                    shrinkLeftAnnotationButton.setActionCommand(SpanUtil.SHRINK_ANNOTATION_LEFT);
087                    shrinkLeftAnnotationButton.setBorder(BorderFactory.createEtchedBorder(EtchedBorder.LOWERED));
088                    shrinkLeftAnnotationButton.setToolTipText("Shrink annotation on left side");
089                    shrinkLeftAnnotationButton.setBorder(null);
090                    shrinkRightAnnotationButton = new JButton(ComponentUtilities.loadImageIcon(SpanEditPanel.class,
091                                    "/edu/uchsc/ccp/knowtator/images/shrink_right.gif"));
092                    shrinkRightAnnotationButton.setActionCommand(SpanUtil.SHRINK_ANNOTATION_RIGHT);
093                    shrinkRightAnnotationButton.setBorder(BorderFactory.createEtchedBorder(EtchedBorder.LOWERED));
094                    shrinkRightAnnotationButton.setToolTipText("Shrink annotation on right side");
095                    shrinkRightAnnotationButton.setBorder(null);
096                    growRightAnnotationButton = new JButton(ComponentUtilities.loadImageIcon(SpanEditPanel.class,
097                                    "/edu/uchsc/ccp/knowtator/images/extend_right.gif"));
098                    growRightAnnotationButton.setActionCommand(SpanUtil.GROW_ANNOTATION_RIGHT);
099                    growRightAnnotationButton.setBorder(BorderFactory.createEtchedBorder(EtchedBorder.LOWERED));
100                    growRightAnnotationButton.setToolTipText("Grow annotation on right side");
101                    growRightAnnotationButton.setBorder(null);
102    
103                    growLeftAnnotationButton.addActionListener(this);
104                    shrinkLeftAnnotationButton.addActionListener(this);
105                    growRightAnnotationButton.addActionListener(this);
106                    shrinkRightAnnotationButton.addActionListener(this);
107    
108                    add(new JLabel("span edit:"), new GridBagConstraints(0, 0, 1, 1, 0, 0, GridBagConstraints.EAST,
109                                    GridBagConstraints.NONE, new Insets(2, 2, 2, 2), 2, 2));
110                    add(growLeftAnnotationButton, new GridBagConstraints(1, 0, 1, 1, 0, 0, GridBagConstraints.CENTER,
111                                    GridBagConstraints.NONE, new Insets(2, 2, 2, 2), 2, 2));
112                    add(shrinkLeftAnnotationButton, new GridBagConstraints(2, 0, 1, 1, 0, 0, GridBagConstraints.CENTER,
113                                    GridBagConstraints.NONE, new Insets(2, 2, 2, 2), 2, 2));
114                    add(shrinkRightAnnotationButton, new GridBagConstraints(3, 0, 1, 1, 0, 0, GridBagConstraints.CENTER,
115                                    GridBagConstraints.NONE, new Insets(2, 2, 2, 2), 2, 2));
116                    add(growRightAnnotationButton, new GridBagConstraints(4, 0, 1, 1, 0, 0, GridBagConstraints.CENTER,
117                                    GridBagConstraints.NONE, new Insets(2, 2, 2, 2), 2, 2));
118    
119            }
120    
121            public void actionPerformed(ActionEvent event) {
122                    String command = event.getActionCommand();
123    
124                    if (command.equals(SpanUtil.GROW_ANNOTATION_LEFT) || command.equals(SpanUtil.GROW_ANNOTATION_RIGHT)
125                                    || command.equals(SpanUtil.SHRINK_ANNOTATION_LEFT) || command.equals(SpanUtil.SHRINK_ANNOTATION_RIGHT)) {
126                            try {
127                                    List<Span> selectedSpans = annotationDisplay.getSelectedSpans();
128                                    SimpleInstance selectedAnnotation = manager.getSelectedAnnotation();
129                                    
130                                    if((event.getModifiers() & ActionEvent.CTRL_MASK) > 0)
131                                            command = command+"_WORD";
132                                    
133                                    if (selectedSpans.size() > 0) {
134                                            int[] selectedSpanIndices = annotationDisplay.getSelectedSpanIndices();
135                                            spanUtil.editSpans(selectedSpans, selectedAnnotation, command);
136                                            annotationDisplay.setSelectedSpanIndices(selectedSpanIndices);
137                                    }
138                            } catch (InvalidSpanException ise) {
139                                    JOptionPane.showMessageDialog(this, "Annotation has an invalid span value: " + ise.getMessage(),
140                                                    "Invalid span", JOptionPane.ERROR_MESSAGE);
141                            } catch (TextSourceAccessException tsae) {
142                                    JOptionPane.showMessageDialog(null, "There was a problem retrieving the text from the text source: "
143                                                    + tsae.getMessage(), "Text Source Error", JOptionPane.ERROR_MESSAGE);
144                            }
145                    }
146            }
147    }