1 31 32 33 package org.antlr.works.navigation; 34 35 import org.antlr.works.components.grammar.CEditorGrammar; 36 import org.antlr.works.syntax.element.ElementRule; 37 import org.antlr.works.utils.OverlayObject; 38 import org.antlr.xjlib.appkit.frame.XJFrameInterface; 39 40 import javax.swing.*; 41 import javax.swing.border.BevelBorder ; 42 import javax.swing.event.DocumentEvent ; 43 import javax.swing.event.DocumentListener ; 44 import java.awt.*; 45 import java.awt.event.KeyAdapter ; 46 import java.awt.event.KeyEvent ; 47 import java.awt.event.MouseAdapter ; 48 import java.awt.event.MouseEvent ; 49 import java.util.List ; 50 51 public class GoToRule extends OverlayObject { 52 53 public JTextField ruleNameField; 54 public JList matchingRuleList; 55 public DefaultListModel matchingRuleListModel; 56 public JScrollPane matchingRuleScrollPane; 57 public CEditorGrammar editor; 58 59 public static final int VISIBLE_MATCHING_RULES = 15; 60 61 public GoToRule(CEditorGrammar editor, XJFrameInterface parentFrame, JComponent parentComponent) { 62 super(parentFrame, parentComponent); 63 this.editor = editor; 64 } 65 66 public JComponent overlayCreateInterface() { 67 JPanel panel = new JPanel(new BorderLayout()); 68 panel.add(new JLabel("Go To Rule:"), BorderLayout.NORTH); 69 70 ruleNameField = new JTextField(); 71 ruleNameField.addKeyListener(new TextFieldKeyAdapter()); 72 ruleNameField.getDocument().addDocumentListener(new TextFieldDocumentListener()); 73 74 matchingRuleListModel = new DefaultListModel(); 75 76 matchingRuleList = new JList(matchingRuleListModel); 77 matchingRuleList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION); 78 matchingRuleList.setBackground(new Color(235, 244, 254)); 79 matchingRuleList.setPrototypeCellValue("This is a rule name g"); 80 matchingRuleList.addKeyListener(new ListKeyAdapter()); 81 82 matchingRuleList.addMouseListener(new MouseAdapter () { 84 @Override 85 public void mouseClicked(MouseEvent mouseEvent) { 86 if(mouseEvent.getClickCount() == 2) { 87 goToRule(); 88 hide(); 89 } 90 } 91 }); 92 93 matchingRuleScrollPane = new JScrollPane(matchingRuleList, JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED, JScrollPane.HORIZONTAL_SCROLLBAR_NEVER); 94 matchingRuleScrollPane.setBorder(BorderFactory.createBevelBorder(BevelBorder.RAISED)); 95 matchingRuleScrollPane.setVisible(false); 96 parentFrame.getLayeredPane().add(matchingRuleScrollPane, JLayeredPane.MODAL_LAYER); 97 98 panel.add(ruleNameField, BorderLayout.SOUTH); 99 100 return panel; 101 } 102 103 public boolean overlayWillDisplay() { 104 ruleNameField.setText(""); 105 SwingUtilities.invokeLater(new Runnable () { 107 public void run() { 108 ruleNameField.requestFocusInWindow(); 109 } 110 }); 111 updateAutoCompletionList(); 112 return true; 113 } 114 115 public void hide() { 116 super.hide(); 117 matchingRuleScrollPane.setVisible(false); 118 } 119 120 public void resize() { 121 super.resize(); 122 resizeMatchingRules(); 123 } 124 125 public void resizeMatchingRules() { 126 Rectangle r = content.getBounds(); 127 int height = matchingRuleList.getFixedCellHeight(); 128 int size = matchingRuleListModel.size(); 129 if(size > 0) { 130 height = height*Math.min(VISIBLE_MATCHING_RULES, size)+5; 131 matchingRuleScrollPane.setBounds(r.x, r.y+r.height, r.width, height); 132 } 133 } 134 135 public void updateAutoCompletionList() { 136 matchingRuleListModel.removeAllElements(); 137 138 List <String > rules = editor.rules.getRulesStartingWith(ruleNameField.getText().toLowerCase()); 139 if(rules.isEmpty()) { 140 matchingRuleScrollPane.setVisible(false); 141 ruleNameField.setForeground(Color.red); 142 return; 143 } else { 144 ruleNameField.setForeground(Color.black); 145 } 146 147 for (String rule : rules) { 148 matchingRuleListModel.addElement(rule); 149 } 150 matchingRuleList.setSelectedIndex(0); 151 152 resizeMatchingRules(); 153 matchingRuleScrollPane.setVisible(true); 154 } 155 156 public void selectNextListElement(int direction) { 157 int index = matchingRuleList.getSelectedIndex(); 158 index += direction; 159 index = Math.min(Math.max(0, index), matchingRuleListModel.size()-1); 160 161 matchingRuleList.setSelectedIndex(index); 162 matchingRuleList.scrollRectToVisible(matchingRuleList.getCellBounds(index, index)); 163 } 164 165 public void goToRule() { 166 if(matchingRuleListModel.isEmpty()) 167 return; 168 169 int index = matchingRuleList.getSelectedIndex(); 170 if(index >= 0) { 171 ElementRule rule = editor.rules.selectRuleNameInTree((String )matchingRuleListModel.get(index)); 172 if(rule != null) { 173 editor.goToHistoryRememberCurrentPosition(); 174 editor.rules.goToRule(rule); 175 } 176 } 177 } 178 179 public class ListKeyAdapter extends KeyAdapter { 180 181 public void keyPressed(KeyEvent e) { 182 if(e.isConsumed()) 183 return; 184 185 if(!content.isVisible()) 186 return; 187 188 switch(e.getKeyCode()) { 189 case KeyEvent.VK_ESCAPE: 190 hide(); 191 e.consume(); 192 break; 193 194 case KeyEvent.VK_ENTER: 195 goToRule(); 196 hide(); 197 e.consume(); 198 break; 199 200 case KeyEvent.VK_UP: 201 selectNextListElement(-1); 202 e.consume(); 203 break; 204 205 case KeyEvent.VK_DOWN: 206 selectNextListElement(1); 207 e.consume(); 208 break; 209 } 210 } 211 } 212 213 public class TextFieldKeyAdapter extends KeyAdapter { 214 215 public void keyPressed(KeyEvent e) { 216 if(e.isConsumed()) 217 return; 218 219 if(!content.isVisible()) 220 return; 221 222 switch(e.getKeyCode()) { 223 case KeyEvent.VK_ESCAPE: 224 hide(); 225 e.consume(); 226 break; 227 228 case KeyEvent.VK_ENTER: 229 goToRule(); 230 hide(); 231 e.consume(); 232 break; 233 234 case KeyEvent.VK_UP: 235 selectNextListElement(-1); 236 e.consume(); 237 break; 238 239 case KeyEvent.VK_DOWN: 240 selectNextListElement(1); 241 e.consume(); 242 break; 243 } 244 } 245 } 246 247 public class TextFieldDocumentListener implements DocumentListener { 248 249 public void insertUpdate(DocumentEvent event) { 250 updateAutoCompletionList(); 251 } 252 253 public void removeUpdate(DocumentEvent event) { 254 updateAutoCompletionList(); 255 } 256 257 public void changedUpdate(DocumentEvent event) { 258 updateAutoCompletionList(); 259 } 260 } 261 262 } 263 | Popular Tags |