1 23 24 package org.infoglue.cms.plugins.codeeditor; 25 26 import java.awt.Point ; 27 import java.awt.event.KeyAdapter ; 28 import java.awt.event.KeyEvent ; 29 import java.util.StringTokenizer ; 30 31 public class TextAreaInputListener extends KeyAdapter 32 { 33 private InfoGlueTextArea infoGlueTextArea = null; 34 private boolean isControlKeyActive = false; 35 private boolean isShiftKeyActive = false; 36 37 public TextAreaInputListener(InfoGlueTextArea infoGlueTextArea) 38 { 39 this.infoGlueTextArea = infoGlueTextArea; 40 } 41 42 public void keyPressed(KeyEvent e) 43 { 44 47 char key = e.getKeyChar(); 48 49 if(key == '.') 50 { 51 try 52 { 53 int endPosition = this.infoGlueTextArea.getCaretPosition() - 1; 54 String text = this.infoGlueTextArea.getDocument().getText(this.infoGlueTextArea.getDocument().getStartPosition().getOffset(), this.infoGlueTextArea.getDocument().getEndPosition().getOffset()); 56 59 StringBuffer sb = new StringBuffer (); 60 int position = endPosition; 61 while(true && position > -1) 62 { 63 char c = text.charAt(position); 64 if(c == '$' || c == ' ') 65 { 66 sb.insert(0, c); 67 break; 68 } 69 else 70 { 71 sb.insert(0, c); 72 } 73 position--; 74 } 75 76 String keyword = sb.toString(); 77 if(keyword.equals("$templateLogic") || keyword.equals("$!templateLogic") || keyword.equals("${templateLogic") || keyword.equals("$!{templateLogic")) 79 { 80 int x = 0; 82 int y = 0; 83 try 84 { 85 Point caretPosition = this.infoGlueTextArea.getCaret().getMagicCaretPosition(); 86 88 x = caretPosition.x; 89 y = caretPosition.y; 90 } 91 catch(Exception ex) 92 { 93 ex.printStackTrace(); 94 } 95 96 this.infoGlueTextArea.openTemplateLogicCodeHelper(x, y); 97 this.isControlKeyActive = false; 98 } 99 } 100 catch(Exception ex) 101 { 102 ex.printStackTrace(); 103 } 104 } 106 else if(e.getKeyCode() == 52 && this.isControlKeyActive) { 108 int x = 0; 109 int y = 0; 110 try 111 { 112 Point caretPosition = this.infoGlueTextArea.getCaret().getMagicCaretPosition(); 113 115 x = caretPosition.x; 116 y = caretPosition.y; 117 } 118 catch(Exception ex) 119 { 120 ex.printStackTrace(); 121 } 122 123 this.infoGlueTextArea.openVelocityVariableTemplateCodeHelper(x, y); 124 this.isControlKeyActive = false; 125 126 } 127 else if(key == '#') 128 { 129 int x = 0; 130 int y = 0; 131 try 132 { 133 Point caretPosition = this.infoGlueTextArea.getCaret().getMagicCaretPosition(); 134 136 x = caretPosition.x; 137 y = caretPosition.y; 138 } 139 catch(Exception ex) 140 { 141 ex.printStackTrace(); 142 } 143 144 this.infoGlueTextArea.openVelocityTemplateCodeHelper(x, y); 145 } 146 else if(key == '\t') 147 { 148 String selectedText = this.infoGlueTextArea.getSelectedText(); 149 152 if(selectedText != null) 154 { 155 String linebreak = System.getProperty("line.separator"); 156 StringBuffer sb = new StringBuffer (); 158 StringTokenizer st = new StringTokenizer (selectedText, linebreak, true); 159 while (st.hasMoreTokens()) 160 { 161 String row = st.nextToken(); 162 if(this.isShiftKeyActive) 164 { 165 row = row.replaceFirst("" + '\t', ""); 166 sb.append(row); 167 } 168 else 169 { 170 sb.append('\t' + row); 171 } 172 } 173 174 this.infoGlueTextArea.replaceSelection(sb.toString()); 175 } 176 } 177 else if(e.getKeyCode() == 83) 178 { 179 if(this.isControlKeyActive) 181 { 182 this.infoGlueTextArea.controller.executeSave(this.infoGlueTextArea.getText()); 184 } 185 } 186 else if(e.getKeyCode() == KeyEvent.VK_SHIFT) 187 { 188 this.isShiftKeyActive = true; 190 } 191 else if(e.getKeyCode() == KeyEvent.VK_CONTROL) 192 { 193 this.isControlKeyActive = true; 195 } 196 197 } 199 200 201 public void keyReleased(KeyEvent e) 202 { 203 205 char key = e.getKeyChar(); 206 207 if(e.getKeyCode() == KeyEvent.VK_SHIFT) 208 { 209 this.isShiftKeyActive = false; 211 } 212 else if(e.getKeyCode() == KeyEvent.VK_CONTROL) 213 { 214 this.isControlKeyActive = false; 216 } 217 } 218 219 220 } | Popular Tags |