KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > genimen > djeneric > ui > DjCodeEditor


1 /*
2  * Copyright (c) 2001-2005 by Genimen BV (www.genimen.com) All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, is permitted provided that the following conditions are met: -
6  * Redistributions of source code must retain the above copyright notice, this
7  * list of conditions and the following disclaimer. - Redistributions in binary
8  * form must reproduce the above copyright notice, this list of conditions and
9  * the following disclaimer in the documentation and/or other materials
10  * provided with the distribution. - All advertising materials mentioning
11  * features or use of this software must display the following acknowledgment:
12  * "This product includes Djeneric." - Products derived from this software may
13  * not be called "Djeneric" nor may "Djeneric" appear in their names without
14  * prior written permission of Genimen BV. - Redistributions of any form
15  * whatsoever must retain the following acknowledgment: "This product includes
16  * Djeneric."
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER AND CONTRIBUTORS "AS IS"
19  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21  * ARE DISCLAIMED. IN NO EVENT SHALL GENIMEN BV, DJENERIC.ORG, OR CONTRIBUTORS
22  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28  * POSSIBILITY OF SUCH DAMAGE.
29  */

30 package com.genimen.djeneric.ui;
31
32 import java.awt.BorderLayout JavaDoc;
33 import java.awt.Color JavaDoc;
34 import java.awt.Dimension JavaDoc;
35 import java.awt.Rectangle JavaDoc;
36 import java.awt.event.InputEvent JavaDoc;
37 import java.awt.event.KeyEvent JavaDoc;
38 import java.awt.event.MouseEvent JavaDoc;
39
40 import javax.swing.BorderFactory JavaDoc;
41 import javax.swing.JComponent JavaDoc;
42 import javax.swing.JLabel JavaDoc;
43 import javax.swing.JOptionPane JavaDoc;
44 import javax.swing.JPanel JavaDoc;
45 import javax.swing.JScrollPane JavaDoc;
46 import javax.swing.JTextArea JavaDoc;
47 import javax.swing.SwingConstants JavaDoc;
48 import javax.swing.border.BevelBorder JavaDoc;
49 import javax.swing.border.Border JavaDoc;
50 import javax.swing.event.CaretEvent JavaDoc;
51 import javax.swing.text.BadLocationException JavaDoc;
52 import javax.swing.undo.UndoManager JavaDoc;
53
54 import com.genimen.djeneric.language.Messages;
55 import com.genimen.djeneric.util.DjLogger;
56 import com.genimen.djeneric.util.DjStringReplacer;
57
58 public class DjCodeEditor extends JPanel JavaDoc
59 {
60   private static final long serialVersionUID = 1L;
61   public static final String JavaDoc SEPARATOR = " \t\n\r,.!*()-+=[]:;\"<>/";
62   final UndoManager JavaDoc _undomanager = new UndoManager JavaDoc();
63
64   BorderLayout JavaDoc borderLayout1 = new BorderLayout JavaDoc();
65   JTextArea JavaDoc _editor = new JTextArea JavaDoc();
66   JPanel JavaDoc jPanel1 = new JPanel JavaDoc();
67   JPanel JavaDoc jPanel2 = new JPanel JavaDoc();
68   BorderLayout JavaDoc borderLayout2 = new BorderLayout JavaDoc();
69   Border JavaDoc border1;
70   JLabel JavaDoc _lblRow = new JLabel JavaDoc();
71   JLabel JavaDoc jLabel1 = new JLabel JavaDoc();
72   JLabel JavaDoc _lblColumn = new JLabel JavaDoc();
73   JScrollPane JavaDoc jScrollPane1 = new JScrollPane JavaDoc();
74   JPanel JavaDoc _buttonPanel = new JPanel JavaDoc();
75   BorderLayout JavaDoc borderLayout3 = new BorderLayout JavaDoc();
76   JLabel JavaDoc _lblStatus = new JLabel JavaDoc();
77   Color JavaDoc _normalColor = _lblStatus.getForeground();
78   String JavaDoc _orgStatusMessage = "";
79
80   public DjCodeEditor()
81   {
82     try
83     {
84       jbInit();
85       getEditor().addKeyListener(new CodeEditor__editor_keyAdapter(this));
86       getEditor().getDocument().addUndoableEditListener(this._undomanager);
87       _undomanager.setLimit(50000);
88
89     }
90     catch (Exception JavaDoc e)
91     {
92       DjLogger.log(e);
93     }
94   }
95
96   public UndoManager JavaDoc getUndomanager()
97   {
98     return _undomanager;
99   }
100
101   public void clear()
102   {
103     _editor.setText("");
104   }
105
106   public Object JavaDoc getDisplayedValue()
107   {
108     return _editor.getText();
109   }
110
111   public JComponent JavaDoc getFocussableComponent()
112   {
113     return _editor;
114   }
115
116   public JTextArea JavaDoc getEditor()
117   {
118     return _editor;
119   }
120
121   protected JPanel JavaDoc getButtonPanel()
122   {
123     return _buttonPanel;
124   }
125
126   private void jbInit() throws Exception JavaDoc
127   {
128     _editor.setFont(new java.awt.Font JavaDoc("Monospaced", 0, 12));
129     _editor.addCaretListener(new CodeEditor__editor_caretAdapter(this));
130     border1 = BorderFactory.createBevelBorder(BevelBorder.LOWERED, Color.white, Color.white, new Color JavaDoc(109, 109, 110),
131                                               new Color JavaDoc(156, 156, 158));
132     this.setLayout(borderLayout1);
133     jPanel1.setLayout(borderLayout2);
134     jPanel2.setBorder(border1);
135     jPanel2.setPreferredSize(new Dimension JavaDoc(70, 21));
136     jPanel2.setLayout(null);
137     _lblRow.setHorizontalAlignment(SwingConstants.TRAILING);
138     _lblRow.setText("1");
139     _lblRow.setBounds(new Rectangle JavaDoc(-1, 4, 30, 15));
140     jLabel1.setText(":");
141     jLabel1.setBounds(new Rectangle JavaDoc(33, 4, 3, 15));
142     _lblColumn.setText("1");
143     _lblColumn.setBounds(new Rectangle JavaDoc(42, 4, 30, 15));
144     _buttonPanel.setLayout(borderLayout3);
145     _lblStatus.setText("");
146     _lblStatus.addMouseListener(new DjCodeEditor__lblStatus_mouseAdapter(this));
147     this.add(jPanel1, BorderLayout.SOUTH);
148     jPanel1.add(jPanel2, BorderLayout.EAST);
149     jPanel2.add(_lblColumn, null);
150     jPanel2.add(_lblRow, null);
151     jPanel2.add(jLabel1, null);
152     jPanel1.add(_buttonPanel, BorderLayout.CENTER);
153     _buttonPanel.add(_lblStatus, BorderLayout.WEST);
154     this.add(jScrollPane1, BorderLayout.CENTER);
155     jScrollPane1.getViewport().add(_editor, null);
156   }
157
158   void _editor_caretUpdate(CaretEvent JavaDoc e)
159   {
160     int idx = e.getDot();
161     try
162     {
163       int line = _editor.getLineOfOffset(idx);
164       int column = idx - _editor.getLineStartOffset(line);
165       _lblRow.setText(String.valueOf(line + 1));
166       _lblColumn.setText(String.valueOf(column + 1));
167     }
168     catch (BadLocationException JavaDoc e1)
169     {
170       DjLogger.log(e1);
171     }
172   }
173
174   private void wordRight(boolean keepSelection)
175   {
176     String JavaDoc code = getEditor().getText();
177     int idx = getEditor().getCaretPosition();
178     int orgIdx = getEditor().getSelectionStart();
179
180     if (idx >= code.length() - 1) return;
181
182     if (SEPARATOR.indexOf(code.charAt(idx)) != -1)
183     {
184       idx = skipSeparators(code, idx);
185     }
186     else
187     {
188       idx = skipCode(code, idx);
189     }
190     getEditor().setCaretPosition(idx);
191     if (keepSelection)
192     {
193       getEditor().setSelectionStart(orgIdx);
194       getEditor().setSelectionEnd(idx);
195     }
196   }
197
198   private int skipCode(String JavaDoc code, int idx)
199   {
200
201     while (idx < code.length() - 1 && SEPARATOR.indexOf(code.charAt(idx)) == -1)
202       idx++;
203     return idx;
204   }
205
206   private int skipSeparators(String JavaDoc code, int idx)
207   {
208     while (idx < code.length() - 1 && SEPARATOR.indexOf(code.charAt(idx)) != -1)
209       idx++;
210     return idx;
211   }
212
213   private void wordLeft(boolean keepSelection)
214   {
215     String JavaDoc code = getEditor().getText();
216     int idx = getEditor().getCaretPosition() - 1;
217     if (idx < 0) return;
218
219     int orgIdx = getEditor().getSelectionStart();
220
221     if (SEPARATOR.indexOf(code.charAt(idx)) != -1)
222     {
223       idx = backupSeparators(code, idx);
224     }
225     else
226     {
227       idx = backupCode(code, idx);
228     }
229     getEditor().setCaretPosition(idx);
230     if (keepSelection)
231     {
232       getEditor().setSelectionStart(orgIdx);
233       getEditor().setSelectionEnd(idx);
234     }
235   }
236
237   private int backupCode(String JavaDoc code, int idx)
238   {
239
240     while (idx > 0 && SEPARATOR.indexOf(code.charAt(idx)) == -1)
241       idx--;
242     if (SEPARATOR.indexOf(code.charAt(idx)) != -1) idx++;
243     return idx;
244   }
245
246   private int backupSeparators(String JavaDoc code, int idx)
247   {
248     while (idx > 0 && SEPARATOR.indexOf(code.charAt(idx)) != -1)
249       idx--;
250     if (SEPARATOR.indexOf(code.charAt(idx)) == -1) idx++;
251     return idx;
252   }
253
254   protected void editorKeypressed(KeyEvent JavaDoc e)
255   {
256     if ((e.getKeyCode() == KeyEvent.VK_RIGHT) && ((e.getModifiers() & InputEvent.CTRL_MASK) != 0))
257     {
258       wordRight(e.isShiftDown());
259       e.consume();
260     }
261     if ((e.getKeyCode() == KeyEvent.VK_LEFT) && ((e.getModifiers() & InputEvent.CTRL_MASK) != 0))
262     {
263       wordLeft(e.isShiftDown());
264       e.consume();
265     }
266     if (e.getKeyCode() == KeyEvent.VK_Z)
267     {
268       if (e.getModifiersEx() == KeyEvent.CTRL_DOWN_MASK)
269       {
270         if (_undomanager.canUndo())
271         {
272           _undomanager.undo();
273         }
274
275       }
276     }
277     if (e.getKeyCode() == KeyEvent.VK_Y)
278     {
279       if (e.getModifiersEx() == KeyEvent.CTRL_DOWN_MASK)
280       {
281         if (_undomanager.canRedo())
282         {
283           _undomanager.redo();
284         }
285
286       }
287     }
288   }
289
290   public String JavaDoc getText()
291   {
292     return _editor.getText();
293   }
294
295   public void setText(String JavaDoc t)
296   {
297     _editor.setText(t);
298     getUndomanager().discardAllEdits();
299   }
300
301   public void setCaretPosition(int position)
302   {
303     _editor.setCaretPosition(position);
304   }
305
306   public int getLineEndOffset(int line) throws BadLocationException JavaDoc
307   {
308     return _editor.getLineEndOffset(line);
309   }
310
311   public int getLineOfOffset(int offset) throws BadLocationException JavaDoc
312   {
313     return _editor.getLineOfOffset(offset);
314   }
315
316   public int getLineStartOffset(int line) throws BadLocationException JavaDoc
317   {
318     return _editor.getLineStartOffset(line);
319   }
320
321   public int getCaretPosition()
322   {
323     return _editor.getCaretPosition();
324   }
325
326   public void insert(String JavaDoc str, int pos)
327   {
328     _editor.insert(str, pos);
329   }
330
331   public void setStatusMessage(String JavaDoc msg)
332   {
333     setStatusMessage(msg, true);
334   }
335
336   public void setStatusMessage(String JavaDoc msg, boolean informative)
337   {
338     if (msg == null) msg = Messages.getString("global.Nullpointer");
339     _orgStatusMessage = msg;
340
341     if (!informative) _lblStatus.setForeground(Color.red);
342     else _lblStatus.setForeground(_normalColor);
343
344     DjStringReplacer sr = new DjStringReplacer(msg);
345     sr.replace("\n", " ");
346     sr.replace("\r", "");
347     sr.replace(" ", " ");
348     _lblStatus.setText(sr.toString());
349   }
350
351   void _lblStatus_mouseClicked(MouseEvent JavaDoc e)
352   {
353     if (e.getClickCount() > 1)
354     {
355       JOptionPane.showMessageDialog(this, _orgStatusMessage);
356     }
357   }
358
359   public void requestFocus()
360   {
361     _editor.requestFocus();
362   }
363
364   public DjEditorPositionInfo getEditorPositionInfo()
365   {
366     return new DjEditorPositionInfo(getEditor(), false);
367   }
368
369   public int getCaretLine() throws BadLocationException JavaDoc
370   {
371     return _editor.getLineOfOffset(_editor.getCaretPosition());
372   }
373
374   public int getCaretColumn() throws BadLocationException JavaDoc
375   {
376     int idx = _editor.getCaretPosition();
377     int line = _editor.getLineOfOffset(idx);
378     return idx - _editor.getLineStartOffset(line);
379   }
380
381   public void setStatusMessage(Throwable JavaDoc t)
382   {
383     DjLogger.log(t);
384     setStatusMessage(t.getMessage(), false);
385   }
386
387 }
388
389 class CodeEditor__editor_caretAdapter implements javax.swing.event.CaretListener JavaDoc
390 {
391   DjCodeEditor adaptee;
392
393   CodeEditor__editor_caretAdapter(DjCodeEditor adaptee)
394   {
395     this.adaptee = adaptee;
396   }
397
398   public void caretUpdate(CaretEvent JavaDoc e)
399   {
400     adaptee._editor_caretUpdate(e);
401   }
402 }
403
404 class CodeEditor__editor_keyAdapter extends java.awt.event.KeyAdapter JavaDoc
405 {
406   DjCodeEditor adaptee;
407
408   CodeEditor__editor_keyAdapter(DjCodeEditor adaptee)
409   {
410     this.adaptee = adaptee;
411   }
412
413   public void keyPressed(KeyEvent JavaDoc e)
414   {
415     adaptee.editorKeypressed(e);
416   }
417 }
418
419 class DjCodeEditor__lblStatus_mouseAdapter extends java.awt.event.MouseAdapter JavaDoc
420 {
421   DjCodeEditor adaptee;
422
423   DjCodeEditor__lblStatus_mouseAdapter(DjCodeEditor adaptee)
424   {
425     this.adaptee = adaptee;
426   }
427
428   public void mouseClicked(MouseEvent JavaDoc e)
429   {
430     adaptee._lblStatus_mouseClicked(e);
431   }
432 }
Popular Tags