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.gui; 023 024import org.biojava.nbio.structure.align.gui.jmol.JmolPanel; 025 026import javax.swing.*; 027import java.awt.event.*; 028import java.util.ArrayList; 029import java.util.List; 030 031 032/** a utility class that listens to Ramsol script commands in the @link {@link BiojavaJmol} class 033 * 034 * @author Andreas Prlic 035 * @deprecated use org.biojava.nbio.structure.align.gui.jmol.RasmolCommandListener instead 036 */ 037@Deprecated 038public class RasmolCommandListener 039extends KeyAdapter 040implements ActionListener, 041MouseListener { 042 043 JTextField textfield; 044 JmolPanel jmolPanel; 045 046 List<String> history; 047 int historyPosition; 048 049 public RasmolCommandListener(JmolPanel panel, JTextField field){ 050 textfield = field; 051 jmolPanel = panel; 052 history = new ArrayList<String>(); 053 historyPosition = -2; // -2 = history = empty; 054 } 055 056 @Override 057 public void actionPerformed(ActionEvent event) { 058 /* 059 if ( spice.isLoading() ) { 060 logger.finest("loading data, please be patient"); 061 return ; 062 } 063 */ 064 String cmd = textfield.getText(); 065 jmolPanel.executeCmd(cmd); 066 textfield.setText(""); 067 068 // now comes history part: 069 070 // no need for history: 071 if ( cmd.equals("")) return; 072 073 // check last command in history 074 // if equivalent, don't add, 075 // otherwise add 076 if (history.size()>0){ 077 String txt=history.get(history.size()-1); 078 if (! txt.equals(cmd)) { 079 history.add(cmd); 080 } 081 } else { 082 // the first time always add 083 history.add(cmd); 084 } 085 historyPosition=history.size(); 086 087 088 } 089 090 @Override 091 public void mouseClicked(MouseEvent e){ 092 String cmd = textfield.getText(); 093 if ( cmd.equals("enter RASMOL like command...")){ 094 textfield.setText(""); 095 textfield.repaint(); 096 } 097 }; 098 099 100 @Override 101 public void mouseExited(MouseEvent e){}; 102 @Override 103 public void mouseReleased(MouseEvent e){}; 104 @Override 105 public void mousePressed(MouseEvent e){}; 106 107 @Override 108 public void mouseEntered(MouseEvent e){}; 109 110 /** takes care of the cursor up/down keys. triggers copying of stored 111 * commands into the current textfield 112 * 113 */ 114 115 116 @Override 117 public void keyReleased(KeyEvent e){ 118 119 int code = e.getKeyCode(); 120 //String s = e.getKeyText(code); 121 //System.out.println(s); 122 if (( code == KeyEvent.VK_UP ) || 123 ( code == KeyEvent.VK_KP_UP)) { 124 // go one back in history; 125 if ( historyPosition > 0){ 126 historyPosition= historyPosition-1; 127 } 128 } else if (( code == KeyEvent.VK_DOWN ) || 129 ( code == KeyEvent.VK_KP_DOWN)) { 130 if ( historyPosition < (history.size()-1) ){ 131 historyPosition++; 132 } else { 133 // clear command if at beginning of history 134 textfield.setText(""); 135 historyPosition=history.size(); 136 return; 137 } 138 } else if ( code == KeyEvent.VK_PAGE_UP) { 139 if ( historyPosition > 0) { 140 historyPosition = 0; 141 } 142 } else if ( code == KeyEvent.VK_PAGE_DOWN) { 143 if ( historyPosition >= 0) { 144 historyPosition = history.size()-1; 145 } 146 } else { 147 // some other key has been pressed, do nothing 148 return; 149 } 150 151 if ( historyPosition >= 0) { 152 String txt = history.get(historyPosition); 153 textfield.setText(txt); 154 } 155 156 157 } 158 159 160}