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.Component;
031    import java.awt.event.ActionEvent;
032    import java.awt.event.ActionListener;
033    import java.awt.event.MouseAdapter;
034    import java.awt.event.MouseEvent;
035    import java.util.ArrayList;
036    import java.util.Collections;
037    import java.util.Comparator;
038    import java.util.List;
039    
040    import javax.swing.JMenuItem;
041    
042    import edu.stanford.smi.protege.model.Frame;
043    import edu.stanford.smi.protege.model.Instance;
044    import edu.stanford.smi.protege.model.SimpleInstance;
045    import edu.uchsc.ccp.knowtator.KnowtatorManager;
046    import edu.uchsc.ccp.knowtator.Span;
047    import edu.uchsc.ccp.knowtator.TextSourceUtil;
048    import edu.uchsc.ccp.knowtator.textsource.TextSourceAccessException;
049    
050    public class AnnotationSchemaMenuItemFactory {
051            public static final int SELECTION_ITEMS_COUNT_THRESHOLD = 20;
052    
053            public static JMenuItem createAnnotationMenuItem(final KnowtatorManager manager, final Frame selectedFrame,
054                            TextSourceUtil textSourceUtil) {
055                    boolean create = manager.getFilterUtil().isClsLicensedByFilter(manager.getSelectedFilter(), selectedFrame);
056    
057                    if (create) {
058                            List<Span> selectedSpans = manager.getSelectedSpans();
059                            String annotationText = "<html>create <b>" + selectedFrame.getBrowserText() + "</b> annotation</html>";
060                            if (selectedSpans != null && selectedSpans.size() != 0) {
061                                    try {
062                                            String selectedText = textSourceUtil.getCurrentTextSource().getText(selectedSpans);
063                                            if (selectedText.length() > 30) {
064                                                    selectedText = selectedText.substring(0, 30) + "...";
065                                            }
066                                            annotationText = "<html>create <b>" + selectedFrame.getBrowserText() + "</b> annotation with <b>"
067                                                            + selectedText + "</b></html>";
068                                    } catch (TextSourceAccessException tsae) {
069                                            tsae.printStackTrace();
070                                    }
071                            }
072    
073                            JMenuItem menuItem = new JMenuItem(annotationText);
074                            menuItem.addActionListener(new ActionListener() {
075                                    public void actionPerformed(ActionEvent actionEvent) {
076                                            manager.createAnnotation((Instance) selectedFrame);
077                                    }
078                            });
079                            return menuItem;
080                    }
081                    return null;
082            }
083    
084            public static JMenuItem createFastAnnotateMenuItem(final KnowtatorManager manager, final Frame selectedFrame) {
085                    Frame fastAnnotateFrame = manager.getFastAnnotateFrame();
086                    JMenuItem fastAnnotateMenuItem = null;
087                    fastAnnotateMenuItem = new JMenuItem("<html>fast annotate with <b>" + selectedFrame.getBrowserText()
088                                    + "</b></html>");
089                    fastAnnotateMenuItem.addActionListener(new ActionListener() {
090                            public void actionPerformed(ActionEvent actionEvent) {
091                                    manager.startFastAnnotate(selectedFrame);
092                            }
093                    });
094    
095                    return fastAnnotateMenuItem;
096            }
097    
098            /**
099             * Creates and returns the menu item that will remove the selected class
100             * (button) from the fast annotation toolbar.
101             * 
102             * @param manager
103             * @param selectedFrame
104             *            The class that will be removed from the fast annotate toolbar.
105             * 
106             * @return The menu item that will remove the class from the toolbar
107             */
108            public static JMenuItem createRemoveClsFromToolbarMenuItem(final KnowtatorManager manager, final Frame selectedFrame) {
109                    JMenuItem removeClsMenuItem = new JMenuItem("<html>remove <b>" + selectedFrame.getBrowserText()
110                                    + "</b> from fast annotate toolbar</html>");
111                    removeClsMenuItem.addActionListener(new ActionListener() {
112                            public void actionPerformed(ActionEvent actionEvent) {
113                                    manager.removeFastAnnotateFrame(selectedFrame);
114                            }
115                    });
116                    return removeClsMenuItem;
117            }
118    
119            public static List<JMenuItem> createSelectAnnotationMenuItems(final KnowtatorManager manager,
120                            final Frame selectedFrame, final Component parent) {
121                    List<JMenuItem> selectAnnotationMenuItems = new ArrayList<JMenuItem>();
122    
123                    final List<SimpleInstance> clsAnnotations = new ArrayList<SimpleInstance>(manager
124                                    .getCurrentAnnotationsForFrame(selectedFrame));
125    
126                    boolean showMoreItem = false;
127    
128                    if (clsAnnotations != null && clsAnnotations.size() > 0) {
129                            // SimpleInstance selectedAnnotation =
130                            // manager.getLastSelectedAnnotation();
131    
132                            Comparator<SimpleInstance> annotationComparator = manager.getSpanUtil().comparator(
133                                            manager.getBrowserTextUtil().comparator());
134                            Collections.sort(clsAnnotations, annotationComparator);
135    
136                            List<SimpleInstance> filteredAnnotations = new ArrayList<SimpleInstance>();
137    
138                            boolean belowVisibleRange = false;
139                            for (SimpleInstance annotation : clsAnnotations) {
140                                    if (manager.isAnnotationVisible(annotation)) {
141                                            filteredAnnotations.add(annotation);
142                                            belowVisibleRange = true;
143                                    } else if (belowVisibleRange)
144                                            filteredAnnotations.add(annotation);
145                            }
146    
147                            //this could happen if none of the annotations are visible - e.g. because they are all spanless 
148                            //or above the visible range.  We will only add the spanless annotations here because we do not want the 
149                            //screen jumping around on us.  
150                            if(filteredAnnotations.size() == 0) {
151                                    for (SimpleInstance annotation : clsAnnotations) {
152                                            List<Span> spans = manager.getAnnotationUtil().getSpans(annotation);
153                                            if(spans == null || spans.size() == 0) {
154                                                    filteredAnnotations.add(annotation);
155                                            }
156                                    }
157                            }
158                            
159                            filteredAnnotations = filteredAnnotations.subList(0, Math.min(filteredAnnotations.size(),
160                                            SELECTION_ITEMS_COUNT_THRESHOLD));
161                            if (filteredAnnotations.size() < clsAnnotations.size())
162                                    showMoreItem = true;
163    
164                            JMenuItem selectAnnotationMenuItem;
165    
166                            if (showMoreItem) {
167                                    selectAnnotationMenuItem = new JMenuItem("more ...");
168                                    selectAnnotationMenuItem.addActionListener(new ActionListener() {
169                                            public void actionPerformed(ActionEvent actionEvent) {
170                                                    List<SimpleInstance> chosenAnnotations = AnnotationPicker.pickAnnotationsFromCollection(parent,
171                                                                    manager, clsAnnotations, "select annotation of type '" + selectedFrame + "'");
172                                                    if (chosenAnnotations.size() > 0)
173                                                            manager.setSelectedAnnotation(chosenAnnotations.get(0));
174                                            }
175                                    });
176                                    selectAnnotationMenuItems.add(selectAnnotationMenuItem);
177                            }
178    
179                            // TODO add the 20 closest annotations to the most recently selected
180                            // annotation.
181                            for (int i = 0; i < filteredAnnotations.size(); i++) {
182                                    final SimpleInstance annotation = filteredAnnotations.get(i);
183                                    selectAnnotationMenuItem = new JMenuItem("select: "
184                                                    + manager.getBrowserTextUtil().getBrowserText(annotation, 30));
185                                    selectAnnotationMenuItem.addActionListener(new ActionListener() {
186                                            public void actionPerformed(ActionEvent actionEvent) {
187                                                    manager.setSelectedAnnotation(annotation);
188                                            }
189                                    });
190                                    selectAnnotationMenuItem.addMouseListener(new MouseAdapter() {
191                                            public void mouseEntered(MouseEvent event) {
192                                                    manager.getTextPane().highlightAnnotationTemp(annotation);
193                                                    manager.setNotifyText(manager.getBrowserTextUtil().getBrowserText(annotation));
194                                            }
195    
196                                            public void mouseExited(MouseEvent event) {
197                                                    manager.refreshAnnotationsDisplay(true);
198                                            }
199                                    });
200                                    selectAnnotationMenuItems.add(selectAnnotationMenuItem);
201                            }
202                    }
203                    return selectAnnotationMenuItems;
204            }
205    }
206    
207    // if(selectedAnnotation != null)
208    // {
209    // Comparator<SimpleInstance> positionComparator =
210    // manager.getPositionComparator(selectedAnnotation);
211    // Collections.sort(clsAnnotations, positionComparator);
212    // while(clsAnnotations.size() > SELECTION_ITEMS_COUNT_THRESHOLD)
213    // clsAnnotations.remove(SELECTION_ITEMS_COUNT_THRESHOLD);
214    // }
215