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    package edu.uchsc.ccp.knowtator.textsource;
030    
031    import java.awt.Component;
032    import java.awt.FlowLayout;
033    import java.awt.event.ActionEvent;
034    import java.awt.event.ActionListener;
035    
036    import javax.swing.JButton;
037    import javax.swing.JLabel;
038    import javax.swing.JPanel;
039    import javax.swing.JToolBar;
040    import javax.swing.border.TitledBorder;
041    
042    import edu.stanford.smi.protege.model.Project;
043    import edu.stanford.smi.protege.util.ComponentUtilities;
044    import edu.uchsc.ccp.knowtator.TextSourceUtil;
045    
046    public class TextSourceSelector extends JPanel implements ActionListener, TextSourceChangeListener,
047                    TextSourceCollectionChangeListener {
048            JPanel panel;
049    
050            JLabel textSourceCollectionLabel;
051    
052            JButton previousTextSourceButton;
053    
054            JButton nextTextSourceButton;
055    
056            JButton selectTextSourceButton;
057    
058            JButton findTextSourceButton;
059    
060            JButton openTextSourceCollectionButton;
061    
062            TitledBorder border;
063    
064            TextSourceUtil textSourceUtil;
065    
066            Project project;
067    
068            Component parent;
069    
070            JToolBar toolBar;
071    
072            /** Creates a new instance of TextSourceSelector */
073            public TextSourceSelector(Project project, TextSourceUtil textSourceUtil, JToolBar toolBar, Component parent) {
074                    super(new FlowLayout());
075    
076                    this.project = project;
077                    this.textSourceUtil = textSourceUtil;
078                    this.toolBar = toolBar;
079                    this.parent = parent;
080    
081                    textSourceUtil.addTextSourceChangeListener(this);
082                    textSourceUtil.addTextSourceCollectionChangeListener(this);
083    
084                    textSourceCollectionLabel = new JLabel("Text Source Collection: ");
085    
086                    panel = new JPanel();
087                    previousTextSourceButton = new JButton(ComponentUtilities.loadImageIcon(TextSourceSelector.class,
088                                    "/edu/uchsc/ccp/knowtator/images/prev.gif"));
089                    previousTextSourceButton.addActionListener(this);
090                    previousTextSourceButton.setEnabled(false);
091                    previousTextSourceButton.setToolTipText("previous text source");
092                    nextTextSourceButton = new JButton(ComponentUtilities.loadImageIcon(TextSourceSelector.class,
093                                    "/edu/uchsc/ccp/knowtator/images/next.gif"));
094                    nextTextSourceButton.addActionListener(this);
095                    nextTextSourceButton.setEnabled(false);
096                    nextTextSourceButton.setToolTipText("next text source");
097                    openTextSourceCollectionButton = new JButton(ComponentUtilities.loadImageIcon(TextSourceSelector.class,
098                                    "/edu/uchsc/ccp/knowtator/images/Open24.gif"));
099                    openTextSourceCollectionButton.setToolTipText("Open text source collection");
100                    openTextSourceCollectionButton.addActionListener(this);
101                    selectTextSourceButton = new JButton(ComponentUtilities.loadImageIcon(TextSourceSelector.class,
102                                    "/edu/uchsc/ccp/knowtator/images/History24.gif"));
103                    selectTextSourceButton.addActionListener(this);
104                    selectTextSourceButton.setEnabled(false);
105                    selectTextSourceButton.setToolTipText("Select text source from list");
106                    findTextSourceButton = new JButton(ComponentUtilities.loadImageIcon(TextSourceSelector.class,
107                                    "/edu/uchsc/ccp/knowtator/images/Find24.gif"));
108                    findTextSourceButton.addActionListener(this);
109                    findTextSourceButton.setEnabled(false);
110                    findTextSourceButton.setToolTipText("Search for text source");
111    
112                    // panel.add(previousTextSourceButton);
113                    // panel.add(nextTextSourceButton);
114                    // panel.add(selectTextSourceButton);
115                    // panel.add(findTextSourceButton);
116                    // panel.add(openTextSourceCollectionButton);
117    
118                    toolBar.add(textSourceCollectionLabel);
119                    toolBar.addSeparator();
120                    toolBar.add(previousTextSourceButton);
121                    toolBar.addSeparator();
122                    toolBar.add(selectTextSourceButton);
123                    toolBar.addSeparator();
124                    toolBar.add(nextTextSourceButton);
125    
126                    toolBar.addSeparator();
127                    toolBar.add(findTextSourceButton);
128                    toolBar.addSeparator();
129                    toolBar.add(openTextSourceCollectionButton);
130    
131                    // setLayout(new GridLayout(1,1));
132                    // add(panel);
133    
134                    // border =
135                    // BorderFactory.createTitledBorder("Text source collection: ");
136                    // setBorder(border);
137    
138            }
139    
140            public void dispose() {
141                    toolBar.remove(textSourceCollectionLabel);
142                    toolBar.remove(previousTextSourceButton);
143                    toolBar.remove(nextTextSourceButton);
144                    toolBar.remove(selectTextSourceButton);
145                    toolBar.remove(findTextSourceButton);
146                    toolBar.remove(openTextSourceCollectionButton);
147    
148                    // remove the separators.
149                    toolBar.remove(toolBar.getComponent(toolBar.getComponentCount() - 1));
150                    toolBar.remove(toolBar.getComponent(toolBar.getComponentCount() - 1));
151                    toolBar.remove(toolBar.getComponent(toolBar.getComponentCount() - 1));
152                    toolBar.remove(toolBar.getComponent(toolBar.getComponentCount() - 1));
153                    toolBar.remove(toolBar.getComponent(toolBar.getComponentCount() - 1));
154    
155                    previousTextSourceButton.removeActionListener(this);
156                    nextTextSourceButton.removeActionListener(this);
157                    selectTextSourceButton.removeActionListener(this);
158                    findTextSourceButton.removeActionListener(this);
159                    openTextSourceCollectionButton.removeActionListener(this);
160    
161                    panel = null;
162                    textSourceCollectionLabel = null;
163                    previousTextSourceButton = null;
164                    nextTextSourceButton = null;
165                    selectTextSourceButton = null;
166                    findTextSourceButton = null;
167                    openTextSourceCollectionButton = null;
168                    border = null;
169    
170                    textSourceUtil.removeTextSourceChangeListener(this);
171                    textSourceUtil.removeTextSourceCollectionChangeListener(this);
172    
173                    project = null;
174                    toolBar = null;
175            }
176    
177            public void textSourceChanged(TextSourceChangeEvent event) {
178                    TextSource textSource = event.getTextSource();
179                    if (textSource != null) {
180                            textSourceUtil.getTextSourceInstance(textSource, true);
181                            int textSourceIndex = textSourceUtil.getCurrentTextSourceCollection().getIndex(textSource);
182                            previousTextSourceButton.setEnabled(textSourceIndex > 0);
183                            nextTextSourceButton
184                                            .setEnabled(textSourceIndex < (textSourceUtil.getCurrentTextSourceCollection().size() - 1));
185                    }
186            }
187    
188            public void textSourceCollectionChanged(TextSourceCollectionChangeEvent event) {
189                    TextSourceCollection tsc = event.getTextSourceCollection();
190                    // border.setTitle("Text source collection: "+ tsc.getName());
191                    textSourceCollectionLabel.setText("Text source collection: " + tsc.getName());
192                    selectTextSourceButton.setEnabled(true);
193                    findTextSourceButton.setEnabled(true);
194            }
195    
196            public void actionPerformed(ActionEvent actionEvent) {
197                    Object source = actionEvent.getSource();
198                    if (source == openTextSourceCollectionButton) {
199                            TextSourceCollection selectedTSC = textSourceUtil.selectTextSourceCollection(parent);
200                            if (selectedTSC != null)
201                                    textSourceUtil.setCurrentTextSourceCollection(selectedTSC);
202                    } else if (source == nextTextSourceButton) {
203                            TextSource textSource = textSourceUtil.getCurrentTextSource();
204                            TextSourceCollection tsc = textSourceUtil.getCurrentTextSourceCollection();
205                            int textSourceIndex = tsc.getIndex(textSource);
206                            try {
207                                    textSourceUtil.setCurrentTextSource(tsc.get(textSourceIndex + 1));
208                            } catch (TextSourceAccessException tsae) {
209                                    tsae.printStackTrace();
210                            }
211                    } else if (source == previousTextSourceButton) {
212                            TextSource textSource = textSourceUtil.getCurrentTextSource();
213                            TextSourceCollection tsc = textSourceUtil.getCurrentTextSourceCollection();
214                            int textSourceIndex = tsc.getIndex(textSource);
215                            try {
216                                    textSourceUtil.setCurrentTextSource(tsc.get(textSourceIndex - 1));
217                            } catch (TextSourceAccessException tsae) {
218                                    tsae.printStackTrace();
219                            }
220                    } else if (source == selectTextSourceButton) {
221                            TextSource textSource = textSourceUtil.getCurrentTextSourceCollection().select(parent);
222                            textSourceUtil.setCurrentTextSource(textSource);
223                    } else if (source == findTextSourceButton) {
224                            TextSource textSource = textSourceUtil.getCurrentTextSourceCollection().find(parent);
225                            textSourceUtil.setCurrentTextSource(textSource);
226                    }
227            }
228    }