001/*
002 *                    BioJava development code
003 *
004 * This code may be freely distributed and modified under the
005 * terms of the GNU Lesser General Public Licence.  This should
006 * be distributed with the code.  If you do not have a copy,
007 * see:
008 *
009 *      http://www.gnu.org/copyleft/lesser.html
010 *
011 * Copyright for this code is held jointly by the individual
012 * authors.  These should be listed in @author doc comments.
013 *
014 * For more information on the BioJava project and its aims,
015 * or to join the biojava-l mailing list, visit the home page
016 * at:
017 *
018 *      http://www.biojava.org/
019 *
020 * created at Oct 27, 2007
021 */
022package org.biojava.nbio.structure.align.gui.jmol;
023
024import org.biojava.nbio.structure.gui.BiojavaJmol;
025
026import javax.swing.*;
027import java.awt.event.*;
028import java.util.ArrayList;
029import java.util.List;
030
031
032
033
034/** a utility class that listens to Ramsol script commands in the @link {@link BiojavaJmol} class
035 *
036 * @author Andreas Prlic
037 *
038 */
039public class RasmolCommandListener
040extends KeyAdapter
041implements ActionListener,
042MouseListener {
043
044        JTextField textfield;
045        JmolPanel jmolPanel;
046
047        List<String> history;
048        int historyPosition;
049
050        public RasmolCommandListener(JmolPanel panel, JTextField field){
051                textfield = field;
052                jmolPanel = panel;
053                history = new ArrayList<String>();
054                historyPosition = -2; // -2 = history = empty;
055        }
056
057        @Override
058        public void actionPerformed(ActionEvent event) {
059                /*
060                if ( spice.isLoading() ) {
061                    logger.finest("loading data, please be patient");
062                    return ;
063                }
064                 */
065                String cmd = textfield.getText();
066                jmolPanel.executeCmd(cmd);
067                textfield.setText("");
068
069                // now comes history part:
070
071                // no need for history:
072                if ( cmd.equals("")) return;
073
074                // check last command in history
075                // if equivalent, don't add,
076                // otherwise add
077                if (history.size()>0){
078                        String txt=history.get(history.size()-1);
079                        if (! txt.equals(cmd)) {
080                                history.add(cmd);
081                        }
082                } else {
083                        // the first time always add
084                        history.add(cmd);
085                }
086                historyPosition=history.size();
087
088
089        }
090
091        @Override
092        public void  mouseClicked(MouseEvent e){
093                String cmd = textfield.getText();
094                if ( cmd.equals(StructureAlignmentJmol.COMMAND_LINE_HELP)){
095                        textfield.setText("");
096                        textfield.repaint();
097                }
098        };
099
100
101        @Override
102        public void  mouseExited(MouseEvent e){};
103        @Override
104        public void  mouseReleased(MouseEvent e){};
105        @Override
106        public void  mousePressed(MouseEvent e){};
107
108        @Override
109        public void  mouseEntered(MouseEvent e){};
110
111        /** takes care of the cursor up/down keys. triggers copying of stored
112         * commands into the current textfield
113         *
114         */
115
116
117        @Override
118        public void keyReleased(KeyEvent e){
119
120                int code = e.getKeyCode();
121                //String s = e.getKeyText(code);
122                //System.out.println(s);
123                if (( code == KeyEvent.VK_UP ) ||
124                                ( code == KeyEvent.VK_KP_UP)) {
125                        // go one back in history;
126                        if ( historyPosition > 0){
127                                historyPosition= historyPosition-1;
128                        }
129                } else if (( code == KeyEvent.VK_DOWN ) ||
130                                ( code == KeyEvent.VK_KP_DOWN)) {
131                        if ( historyPosition < (history.size()-1) ){
132                                historyPosition++;
133                        } else {
134                                // clear command if at beginning of history
135                                textfield.setText("");
136                                historyPosition=history.size();
137                                return;
138                        }
139                } else if ( code == KeyEvent.VK_PAGE_UP) {
140                        if ( historyPosition > 0) {
141                                historyPosition = 0;
142                        }
143                } else if ( code == KeyEvent.VK_PAGE_DOWN) {
144                        if ( historyPosition >= 0) {
145                                historyPosition = history.size()-1;
146                        }
147                } else {
148                        // some other key has been pressed, do nothing
149                        return;
150                }
151
152                if ( historyPosition >= 0) {
153                        String txt = history.get(historyPosition);
154                        textfield.setText(txt);
155                }
156
157
158        }
159
160
161}