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.BorderLayout;
031 import java.awt.Component;
032 import java.awt.Dimension;
033 import java.awt.Point;
034 import java.awt.event.MouseEvent;
035 import java.awt.event.MouseMotionListener;
036 import java.util.ArrayList;
037 import java.util.Collection;
038 import java.util.Collections;
039 import java.util.List;
040
041 import javax.swing.JComponent;
042 import javax.swing.JList;
043 import javax.swing.JScrollPane;
044 import javax.swing.ListCellRenderer;
045 import javax.swing.ListSelectionModel;
046 import javax.swing.event.ListSelectionEvent;
047 import javax.swing.event.ListSelectionListener;
048
049 import org.apache.log4j.Logger;
050
051 import edu.stanford.smi.protege.model.SimpleInstance;
052 import edu.stanford.smi.protege.ui.ListFinder;
053 import edu.stanford.smi.protege.util.Assert;
054 import edu.stanford.smi.protege.util.ComponentFactory;
055 import edu.stanford.smi.protege.util.ComponentUtilities;
056 import edu.uchsc.ccp.knowtator.AnnotationUtil;
057 import edu.uchsc.ccp.knowtator.DisplayColors;
058 import edu.uchsc.ccp.knowtator.KnowtatorManager;
059
060 /**
061 * The following code was copied and modified from
062 * edu.stanford.smi.protege.ui.DisplayUtilities written by the Protege team.
063 */
064
065 public class AnnotationPicker extends JComponent implements MouseMotionListener, ListSelectionListener {
066 static final long serialVersionUID = 0;
067
068 Logger logger = Logger.getLogger(AnnotationPicker.class);
069
070 private JList _list;
071
072 List<SimpleInstance> annotations;
073
074 int mouseOverIndex = -1;
075
076 ListCellRenderer renderer;
077
078 KnowtatorManager manager;
079
080 DisplayColors displayColors;
081
082 AnnotationUtil annotationUtil;
083
084 public AnnotationPicker(KnowtatorManager manager, List<SimpleInstance> annotations, boolean allowMultipleSelection) {
085 this.manager = manager;
086 displayColors = manager.getDisplayColors();
087 annotationUtil = manager.getAnnotationUtil();
088
089 renderer = manager.getRenderer();
090
091 this.annotations = annotations;
092 // Collections.sort(slotList, new FrameComparator());
093 _list = ComponentFactory.createList(ModalDialog.getCloseAction(this));
094 _list.setListData(annotations.toArray());
095 _list.setCellRenderer(renderer);
096 _list.addMouseMotionListener(this);
097 _list.addListSelectionListener(this);
098 if (!allowMultipleSelection)
099 _list.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
100 setLayout(new BorderLayout());
101 add(new JScrollPane(_list), BorderLayout.CENTER);
102 add(new ListFinder(_list, "Find"), BorderLayout.SOUTH);
103 setPreferredSize(new Dimension(300, 300));
104 }
105
106 public Collection getSelection() {
107 return ComponentUtilities.getSelection(_list);
108 }
109
110 public void mouseDragged(MouseEvent e) {
111 }
112
113 public void mouseMoved(MouseEvent e) {
114 if (_list.getSelectedIndex() != -1)
115 return;
116 Point p = e.getPoint();
117 int index = _list.locationToIndex(p);
118 if (index != mouseOverIndex) {
119 mouseOverIndex = index;
120 SimpleInstance annotation = (SimpleInstance) annotations.get(index);
121 manager.getTextPane().highlightAnnotationTemp(annotation);
122 }
123 }
124
125 public void valueChanged(ListSelectionEvent arg0) {
126 int index = _list.getSelectedIndex();
127 if (index != mouseOverIndex) {
128 mouseOverIndex = index;
129 SimpleInstance annotation = (SimpleInstance) annotations.get(index);
130 manager.getTextPane().highlightAnnotationTemp(annotation);
131 }
132 }
133
134 public static List<SimpleInstance> pickAnnotationsFromCollection(Component component, KnowtatorManager manager,
135 List<SimpleInstance> annotations, String label) {
136 return pickAnnotationsFromCollection(component, manager, annotations, label, true);
137 }
138
139 public static List<SimpleInstance> pickAnnotationsFromCollection(Component component, KnowtatorManager manager,
140 List<SimpleInstance> annotations, String label, boolean allowMultipleSelection) {
141 AnnotationPicker panel = new AnnotationPicker(manager, annotations, allowMultipleSelection);
142
143 Collection pickedAnnotations = Collections.EMPTY_LIST;
144 int result = ModalDialog.showDialog(component, panel, label, ModalDialog.MODE_OK_CANCEL, null);
145 switch (result) {
146 case ModalDialog.OPTION_OK:
147 pickedAnnotations = panel.getSelection();
148 break;
149 case ModalDialog.OPTION_CANCEL:
150 break;
151 default:
152 Assert.fail("bad result: " + result);
153 }
154 List<SimpleInstance> returnValues = new ArrayList<SimpleInstance>();
155 for (Object obj : pickedAnnotations) {
156 returnValues.add((SimpleInstance) obj);
157 }
158 return returnValues;
159 }
160
161 }