KickJava   Java API By Example, From Geeks To Geeks.

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


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

18
19 package org.objectweb.jac.aspects.gui.swing;
20
21 import java.awt.Dimension JavaDoc;
22 import javax.swing.JEditorPane JavaDoc;
23 import javax.swing.JScrollPane JavaDoc;
24 import org.objectweb.jac.aspects.gui.FieldEditor;
25 import org.objectweb.jac.aspects.gui.Length;
26 import org.objectweb.jac.core.rtti.FieldItem;
27
28 /**
29  * A Swing editor component for multi-lines text values.
30  */

31
32 public class TextEditor extends AbstractFieldEditor
33     implements FieldEditor
34 {
35
36     JEditorPane JavaDoc editor;
37     JScrollPane JavaDoc scrollPane;
38    
39     /**
40      * Constructs a new text editor. */

41
42     public TextEditor(Object JavaDoc substance, FieldItem field) {
43         super(substance,field);
44         init();
45     }
46
47     public void init() {
48         editor = new JEditorPane JavaDoc();
49         scrollPane = new JScrollPane JavaDoc(editor);
50         add(scrollPane);
51
52         ((JEditorPane JavaDoc)editor).addFocusListener(this);
53         ((JEditorPane JavaDoc)editor).setContentType("html");
54     }
55
56     // FieldEditor internalView
57
public void setValue(Object JavaDoc value) {
58         super.setValue(value);
59         if (value==null)
60             ((JEditorPane JavaDoc)editor).setText("");
61         else if (value instanceof String JavaDoc)
62             ((JEditorPane JavaDoc)editor).setText((String JavaDoc)value);
63         else if (value instanceof byte[])
64             ((JEditorPane JavaDoc)editor).setText(new String JavaDoc((byte[])value));
65         else
66             throw new RuntimeException JavaDoc("Unhandled type "+value.getClass().getName());
67     }
68
69     public Object JavaDoc getValue() {
70         Object JavaDoc value = ((JEditorPane JavaDoc)editor).getText();
71         if (type.getActualClass()==byte[].class)
72             value = ((String JavaDoc)value).getBytes();
73         return value;
74     }
75
76     public void onSetFocus(Object JavaDoc extraOption) {
77         loggerFocus.debug("TextEditor.onSetFocus "+extraOption);
78         requestFocus();
79         if (extraOption!=null && extraOption instanceof Integer JavaDoc) {
80             int line = ((Integer JavaDoc)extraOption).intValue();
81             String JavaDoc text = ((JEditorPane JavaDoc)editor).getText();
82             int index = 0;
83             int previndex = 0;
84             for (int i=0; i<line; i++) {
85                 previndex = index;
86                 index = text.indexOf('\n',index+1);
87                 loggerFocus.debug("TextEditor.onSetFocus "+previndex+","+index);
88             }
89             loggerFocus.debug("TextEditor.onSetFocus "+previndex+","+index);
90             ((JEditorPane JavaDoc)editor).setSelectionStart(previndex+1);
91             ((JEditorPane JavaDoc)editor).setSelectionEnd(index);
92         }
93     }
94
95     /**
96      * Set the focus on the TextEditor
97      */

98     public void requestFocus() {
99         ((JEditorPane JavaDoc)editor).requestFocus();
100         loggerFocus.debug("focusing "+editor.getClass().getName());
101     }
102 }
103
Popular Tags