KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > infoglue > cms > plugins > codeeditor > TextAreaInputListener


1 /* ===============================================================================
2  *
3  * Part of the InfoGlue Content Management Platform (www.infoglue.org)
4  *
5  * ===============================================================================
6  *
7  * Copyright (C)
8  *
9  * This program is free software; you can redistribute it and/or modify it under
10  * the terms of the GNU General Public License version 2, as published by the
11  * Free Software Foundation. See the file LICENSE.html for more information.
12  *
13  * This program is distributed in the hope that it will be useful, but WITHOUT
14  * ANY WARRANTY, including the implied warranty of MERCHANTABILITY or FITNESS
15  * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License along with
18  * this program; if not, write to the Free Software Foundation, Inc. / 59 Temple
19  * Place, Suite 330 / Boston, MA 02111-1307 / USA.
20  *
21  * ===============================================================================
22  */

23  
24 package org.infoglue.cms.plugins.codeeditor;
25
26 import java.awt.Point JavaDoc;
27 import java.awt.event.KeyAdapter JavaDoc;
28 import java.awt.event.KeyEvent JavaDoc;
29 import java.util.StringTokenizer JavaDoc;
30
31 public class TextAreaInputListener extends KeyAdapter JavaDoc
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 JavaDoc e)
43     {
44         //System.out.println("Event:" + e.getKeyChar() + ":" + e.getKeyCode());
45
//System.out.println("this.isControlKeyActive" + this.isControlKeyActive);
46

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.getText();
55
String JavaDoc text = this.infoGlueTextArea.getDocument().getText(this.infoGlueTextArea.getDocument().getStartPosition().getOffset(), this.infoGlueTextArea.getDocument().getEndPosition().getOffset());
56                 //System.out.println("Text1:" + this.infoGlueTextArea.getText().length());
57
//System.out.println("Text2:" + this.infoGlueTextArea.getDocument().getText(0, this.infoGlueTextArea.getDocument().getLength()).length());
58

59                 StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
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 JavaDoc keyword = sb.toString();
77                 //System.out.println("keyword:" + keyword + ": Length=" + keyword.length());
78
if(keyword.equals("$templateLogic") || keyword.equals("$!templateLogic") || keyword.equals("${templateLogic") || keyword.equals("$!{templateLogic"))
79                 {
80                     //System.out.println("Trigger codeHelper...");
81
int x = 0;
82                     int y = 0;
83                     try
84                     {
85                         Point JavaDoc caretPosition = this.infoGlueTextArea.getCaret().getMagicCaretPosition();
86                         //System.out.println("caretPosition:" + caretPosition);
87

88                         x = caretPosition.x;
89                         y = caretPosition.y;
90                     }
91                     catch(Exception JavaDoc ex)
92                     {
93                         ex.printStackTrace();
94                     }
95                     
96                     this.infoGlueTextArea.openTemplateLogicCodeHelper(x, y);
97                     this.isControlKeyActive = false;
98                 }
99             }
100             catch(Exception JavaDoc ex)
101             {
102                 ex.printStackTrace();
103             }
104             //System.out.println("The component with focus is:" + KeyboardFocusManager.getCurrentKeyboardFocusManager().getFocusOwner());
105
}
106         else if(e.getKeyCode() == 52 && this.isControlKeyActive) //$-sign
107
{
108             int x = 0;
109             int y = 0;
110             try
111             {
112                 Point JavaDoc caretPosition = this.infoGlueTextArea.getCaret().getMagicCaretPosition();
113                 //System.out.println("caretPosition:" + caretPosition);
114

115                 x = caretPosition.x;
116                 y = caretPosition.y;
117             }
118             catch(Exception JavaDoc 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 JavaDoc caretPosition = this.infoGlueTextArea.getCaret().getMagicCaretPosition();
134                 //System.out.println("caretPosition:" + caretPosition);
135

136                 x = caretPosition.x;
137                 y = caretPosition.y;
138             }
139             catch(Exception JavaDoc ex)
140             {
141                 ex.printStackTrace();
142             }
143             
144             this.infoGlueTextArea.openVelocityTemplateCodeHelper(x, y);
145         }
146         else if(key == '\t')
147         {
148             String JavaDoc selectedText = this.infoGlueTextArea.getSelectedText();
149             //int startIndex = this.infoGlueTextArea.getSelectionStart();
150
//int endIndex = this.infoGlueTextArea.getSelectionEnd();
151

152             //System.out.println("selectedText:" + selectedText);
153
if(selectedText != null)
154             {
155                 String JavaDoc linebreak = System.getProperty("line.separator");
156                 //System.out.println("linebreak:" + linebreak);
157
StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
158                 StringTokenizer JavaDoc st = new StringTokenizer JavaDoc(selectedText, linebreak, true);
159                 while (st.hasMoreTokens())
160                 {
161                     String JavaDoc row = st.nextToken();
162                     //System.out.println("Row:" + row);
163
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             //System.out.println("this.isControlKeyActive" + this.isControlKeyActive);
180
if(this.isControlKeyActive)
181             {
182                 //System.out.println("Save text");
183
this.infoGlueTextArea.controller.executeSave(this.infoGlueTextArea.getText());
184             }
185         }
186         else if(e.getKeyCode() == KeyEvent.VK_SHIFT)
187         {
188             //System.out.println("Shift is pressed");
189
this.isShiftKeyActive = true;
190         }
191         else if(e.getKeyCode() == KeyEvent.VK_CONTROL)
192         {
193             //System.out.println("Control is pressed");
194
this.isControlKeyActive = true;
195         }
196         
197         //this.infoGlueTextArea.setCharacterAttributes(this.infoGlueTextArea.getParagraphAttributes(), false);
198
}
199     
200     
201     public void keyReleased(KeyEvent JavaDoc e)
202     {
203         //System.out.println("Event:" + e.getKeyChar() + ":" + e.getKeyCode());
204

205         char key = e.getKeyChar();
206         
207         if(e.getKeyCode() == KeyEvent.VK_SHIFT)
208         {
209             //System.out.println("Shift is released");
210
this.isShiftKeyActive = false;
211         }
212         else if(e.getKeyCode() == KeyEvent.VK_CONTROL)
213         {
214             //System.out.println("Control is released");
215
this.isControlKeyActive = false;
216         }
217     }
218     
219     
220 }
Popular Tags