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.Rectangle;
031    import java.awt.event.MouseAdapter;
032    import java.awt.event.MouseEvent;
033    import java.awt.event.MouseListener;
034    import java.util.ArrayList;
035    import java.util.Collection;
036    import java.util.Collections;
037    import java.util.List;
038    
039    import javax.swing.JMenuItem;
040    import javax.swing.JPopupMenu;
041    
042    import edu.stanford.smi.protege.model.Cls;
043    import edu.stanford.smi.protege.model.Frame;
044    import edu.stanford.smi.protege.model.KnowledgeBase;
045    import edu.stanford.smi.protege.model.SimpleInstance;
046    import edu.stanford.smi.protege.ui.FrameComparator;
047    import edu.stanford.smi.protege.util.ComponentUtilities;
048    import edu.stanford.smi.protege.util.SelectableList;
049    import edu.stanford.smi.protege.util.SimpleListModel;
050    import edu.uchsc.ccp.knowtator.AnnotationUtil;
051    import edu.uchsc.ccp.knowtator.BrowserTextUtil;
052    import edu.uchsc.ccp.knowtator.KnowtatorManager;
053    import edu.uchsc.ccp.knowtator.KnowtatorProjectUtil;
054    import edu.uchsc.ccp.knowtator.MentionUtil;
055    import edu.uchsc.ccp.knowtator.ProjectSettings;
056    import edu.uchsc.ccp.knowtator.TextSourceUtil;
057    import edu.uchsc.ccp.knowtator.event.EventHandler;
058    import edu.uchsc.ccp.knowtator.event.RefreshAnnotationsDisplayListener;
059    import edu.uchsc.ccp.knowtator.event.SelectedAnnotationChangeEvent;
060    import edu.uchsc.ccp.knowtator.event.SelectedAnnotationChangeListener;
061    import edu.uchsc.ccp.knowtator.event.SelectedClsChangeEvent;
062    import edu.uchsc.ccp.knowtator.event.SelectedClsChangeListener;
063    
064    public class AnnotationSchemaInstancesList extends SelectableList implements SelectedAnnotationChangeListener,
065                    RefreshAnnotationsDisplayListener {
066            KnowtatorManager manager;
067    
068            KnowledgeBase kb;
069    
070            KnowtatorProjectUtil kpu;
071    
072            AnnotationUtil annotationUtil;
073    
074            MentionUtil mentionUtil;
075    
076            TextSourceUtil textSourceUtil;
077    
078            BrowserTextUtil browserTextUtil;
079    
080            ColorFrameRenderer renderer;
081    
082            JPopupMenu popupMenu;
083    
084            public AnnotationSchemaInstancesList(KnowtatorManager manager) {
085                    super();
086                    initialize(manager);
087            }
088    
089            private void initialize(KnowtatorManager manager) {
090                    this.manager = manager;
091                    setModel(new SimpleListModel());
092    
093                    kb = manager.getKnowledgeBase();
094                    kpu = manager.getKnowtatorProjectUtil();
095                    renderer = manager.getRenderer();
096                    annotationUtil = manager.getAnnotationUtil();
097                    mentionUtil = manager.getMentionUtil();
098                    textSourceUtil = manager.getTextSourceUtil();
099                    browserTextUtil = manager.getBrowserTextUtil();
100    
101                    setCellRenderer(renderer);
102    
103                    popupMenu = new JPopupMenu();
104    
105                    addMouseListener(createSelectableListener());
106                    EventHandler.getInstance().addSelectedClsChangeListener(new SelectedClsChangeListener() {
107                            public void clsSelectionChanged(SelectedClsChangeEvent scce) {
108                                    Cls selectedCls = AnnotationSchemaInstancesList.this.manager.getSelectedCls();
109                                    ArrayList instances = new ArrayList(selectedCls.getDirectInstances());
110                                    Collections.sort(instances, new FrameComparator());
111                                    if (ProjectSettings.getShowInstances(getKb().getProject())) {
112                                            ((SimpleListModel) (AnnotationSchemaInstancesList.this.getModel())).setValues(instances);
113                                    }
114                            }
115                    });
116    
117                    EventHandler.getInstance().addSelectedAnnotationChangeListener(this);
118                    EventHandler.getInstance().addRefreshAnnotationsDisplayListener(this);
119    
120            }
121    
122            private KnowledgeBase getKb() {
123                    return kb;
124            }
125    
126            private MouseListener createSelectableListener() {
127                    return new MouseAdapter() {
128    
129                            public void mouseClicked(MouseEvent event) {
130                                    if (event.isPopupTrigger())
131                                            return;
132                                    Collection selection = getSelection();
133    
134                                    if (selection.size() == 1) {
135                                            final Frame selectedFrame = (Frame) selection.iterator().next();
136    
137                                            int index = locationToIndex(event.getPoint());
138                                            Rectangle cellBounds = getCellBounds(index, index);
139                                            if (!cellBounds.contains(event.getPoint()))
140                                                    return;
141    
142                                            popupMenu.removeAll();
143                                            JMenuItem createAnnotationMenuItem = AnnotationSchemaMenuItemFactory.createAnnotationMenuItem(
144                                                            manager, selectedFrame, textSourceUtil);
145                                            if (createAnnotationMenuItem != null) {
146                                                    popupMenu.add(createAnnotationMenuItem);
147                                                    popupMenu.addSeparator();
148                                                    JMenuItem fastAnnotateMenuItem = AnnotationSchemaMenuItemFactory.createFastAnnotateMenuItem(
149                                                                    manager, selectedFrame);
150                                                    popupMenu.add(fastAnnotateMenuItem);
151                                                    if (manager.isFastAnnotateMode() && manager.fastAnnotateToolBarContains(selectedFrame)) {
152                                                            JMenuItem removeItem = AnnotationSchemaMenuItemFactory.createRemoveClsFromToolbarMenuItem(
153                                                                            manager, selectedFrame);
154                                                            popupMenu.add(removeItem);
155                                                    }
156    
157                                            } else {
158                                                    popupMenu.add(new JMenuItem(
159                                                                    "The current filter does not allow creation of annotations of this type."));
160                                            }
161                                            List<JMenuItem> selectAnnotationMenuItems = AnnotationSchemaMenuItemFactory
162                                                            .createSelectAnnotationMenuItems(manager, selectedFrame, AnnotationSchemaInstancesList.this);
163                                            if (selectAnnotationMenuItems != null && selectAnnotationMenuItems.size() > 0)
164                                                    popupMenu.addSeparator();
165                                            for (JMenuItem selectAnnotationMenuItem : selectAnnotationMenuItems) {
166                                                    popupMenu.add(selectAnnotationMenuItem);
167                                            }
168    
169                                            if (popupMenu.getSubElements() != null && popupMenu.getSubElements().length > 0)
170                                                    popupMenu.show(event.getComponent(), event.getX() + 10, event.getY());
171                                    }
172                            }
173                    };
174            }
175    
176            public void annotationSelectionChanged(SelectedAnnotationChangeEvent sace) {
177                    SimpleInstance annotation = sace.getSelectedAnnotation();
178                    if (annotation == null)
179                            return;
180                    SimpleInstance selectedMention = annotationUtil.getMention(annotation);
181    
182                    mentionUtil.initializeSlotMentions(selectedMention);
183    
184                    SimpleInstance mentionInstance = mentionUtil.getMentionInstance(selectedMention);
185                    if (mentionInstance != null) {
186                            ComponentUtilities.setSelectedValue(this, mentionInstance);
187                    } else
188                            clearSelection();
189            }
190    
191            public void refreshAnnotationsDisplay(boolean scrollToSelection) {
192                    validate();
193                    repaint();
194            }
195    }