KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > jac > aspects > gui > swing > AbstractCodeEditor


1 /*
2   Copyright (C) 2003 Laurent Martelli <laurent@aopsys.com>
3                      Renaud Pawlak <renaud@aopsys.com>
4
5   This program is free software; you can redistribute it and/or modify
6   it under the terms of the GNU Lesser General Public License as
7   published by the Free Software Foundation; either version 2 of the
8   License, or (at your option) any later version.
9
10   This program is distributed in the hope that it will be useful, but
11   WITHOUT ANY WARRANTY; without even the implied warranty of
12   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13   Lesser General Public License for more details.
14
15   You should have received a copy of the GNU Lesser General Public
16   License along with this program; if not, write to the Free Software
17   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
18   USA */

19
20 package org.objectweb.jac.aspects.gui.swing;
21
22 import java.awt.BorderLayout JavaDoc;
23 import java.awt.Dimension JavaDoc;
24 import java.awt.Dimension JavaDoc;
25 import java.awt.event.KeyAdapter JavaDoc;
26 import java.awt.event.KeyEvent JavaDoc;
27 import javax.swing.JScrollPane JavaDoc;
28 import org.objectweb.jac.aspects.gui.Length;
29 import org.objectweb.jac.aspects.gui.swing.EditorScrollPane;
30 import org.objectweb.jac.core.rtti.FieldItem;
31
32 /**
33  * A base class for source code editors
34  */

35 public abstract class AbstractCodeEditor extends AbstractFieldEditor
36 {
37     protected SHEditor editor;
38     JScrollPane JavaDoc scrollPane;
39    
40     public AbstractCodeEditor(Object JavaDoc substance, FieldItem field) {
41         super(substance,field);
42         
43         width = new Length("400px");
44         height = new Length("400px");
45
46         setLayout(new BorderLayout JavaDoc());
47         scrollPane = new EditorScrollPane();
48         editor = ((EditorScrollPane)scrollPane).editor;
49         add(BorderLayout.CENTER,scrollPane);
50         //scrollPane.setPreferredSize(new Dimension(defaultWidth,defaultHeight));
51
scrollPane.setMinimumSize(new Dimension JavaDoc(128,64));
52
53         setMinimumSize(new Dimension JavaDoc(128,64));
54         //setPreferredSize(new Dimension(defaultWidth,defaultHeight));
55

56         editor.addFocusListener(this);
57         editor.addKeyListener(
58             new KeyAdapter JavaDoc() {
59                     public void keyReleased(KeyEvent JavaDoc e) {
60                         if (e.isControlDown() && e.getKeyCode()==KeyEvent.VK_S) {
61                             loggerEvents.debug("saving text of "+getField());
62                             commit();
63                         }
64                     }
65                 });
66
67
68         init();
69     }
70
71     abstract protected void init();
72
73     // FieldEditor internalView
74
public void setValue(Object JavaDoc value) {
75         super.setValue(value);
76         if (value==null)
77             editor.setText("");
78         else if (value instanceof byte[])
79             editor.setText(new String JavaDoc((byte[])value));
80         else
81             editor.setText((String JavaDoc)value);
82     }
83
84     public Object JavaDoc getValue() {
85         if (type.getActualClass()==byte[].class)
86             return editor.getText().getBytes();
87         else
88             return editor.getText();
89     }
90
91     public void setSize(Length width, Length height) {
92         this.width = width;
93         this.height = height;
94         SwingUtils.setSize(scrollPane, width, height);
95     }
96
97     public void onSetFocus(Object JavaDoc extraOption) {
98         loggerFocus.debug("AbstactEditor.onSetFocus "+extraOption);
99         requestFocus();
100         if (extraOption instanceof Integer JavaDoc) {
101             int line = ((Integer JavaDoc)extraOption).intValue();
102             String JavaDoc text = editor.getText();
103             int index = 0;
104             int previndex = 0;
105             for (int i=0; i<line; i++) {
106                 previndex = index;
107                 index = text.indexOf('\n',index+1);
108                 loggerFocus.debug("AbstactEditor.onSetFocus "+previndex+","+index);
109             }
110             if (index==-1) {
111                 index = text.length()-1;
112             }
113             editor.gotoLine(line);
114             editor.setSelectionStart(previndex+1);
115             editor.setSelectionEnd(index+1);
116             loggerFocus.debug("AbstactEditor.onSetFocus "+previndex+","+index);
117         }
118     }
119
120     /**
121      * Set the focus on the TextEditor
122      */

123     public void requestFocus() {
124         editor.requestFocus();
125         loggerFocus.debug("focusing "+editor.getClass().getName());
126     }
127
128 }
129
130
Popular Tags