001    /*
002      Copyright (C) 2001-2002 Renaud Pawlak <renaud@aopsys.com>
003      
004      This program is free software; you can redistribute it and/or modify
005      it under the terms of the GNU Lesser General Public License as
006      published by the Free Software Foundation; either version 2 of the
007      License, or (at your option) any later version.
008    
009      This program is distributed in the hope that it will be useful, but
010      WITHOUT ANY WARRANTY; without even the implied warranty of
011      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
012      Lesser General Public License for more details.
013    
014      You should have received a copy of the GNU Lesser General Public
015      License along with this program; if not, write to the Free Software
016      Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
017      USA */
018    
019    package org.objectweb.jac.aspects.gui.swing;
020    
021    import java.awt.Dimension;
022    import javax.swing.JEditorPane;
023    import javax.swing.JScrollPane;
024    import org.objectweb.jac.aspects.gui.FieldEditor;
025    import org.objectweb.jac.core.rtti.FieldItem;
026    
027    /**
028     * A Swing editor component for multi-lines text values.
029     */
030    
031    public class TextEditor extends AbstractFieldEditor
032        implements FieldEditor 
033    {
034    
035        JEditorPane editor;
036        JScrollPane scrollPane;
037       
038        int defaultWidth = 400;
039        int defaultHeight = 400;
040    
041        /**
042         * Constructs a new text editor. */
043    
044        public TextEditor(Object substance, FieldItem field) {
045            super(substance,field);
046            init();
047        }
048    
049        public void init() {
050            editor=new JEditorPane();
051    
052            scrollPane=new JScrollPane(editor);
053            add(scrollPane);
054            scrollPane.setPreferredSize(new Dimension(defaultWidth,defaultHeight));
055            scrollPane.setMinimumSize(new Dimension(128,64));
056    
057            setMinimumSize(new Dimension(128,64));
058            setPreferredSize(new Dimension(defaultWidth,defaultHeight));
059    
060            ((JEditorPane)editor).addFocusListener(this);
061    
062            ((JEditorPane)editor).setContentType("html");
063        }
064     
065        // FieldEditor internalView
066        public void setValue(Object value) {
067            super.setValue(value);
068            if (value==null) 
069                ((JEditorPane)editor).setText("");
070            else if (value instanceof String)
071                ((JEditorPane)editor).setText((String)value);
072            else if (value instanceof byte[])
073                ((JEditorPane)editor).setText(new String((byte[])value));
074            else
075                throw new RuntimeException("Unhandled type "+value.getClass().getName());
076        }
077    
078        public Object getValue() {
079            Object value = ((JEditorPane)editor).getText();
080            if (type.getActualClass()==byte[].class)
081                value  = ((String)value).getBytes();
082            return value;
083        }
084    
085        public void setWidth(int width) {
086            defaultWidth = width;
087            scrollPane.setPreferredSize(new Dimension(defaultWidth, defaultHeight));
088        }
089    
090        public void setHeight(int height) {
091            defaultHeight = height;
092            scrollPane.setPreferredSize(new Dimension(defaultWidth, defaultHeight));
093        }
094    
095        public void onSetFocus(Object extraOption) {
096            loggerFocus.debug("TextEditor.onSetFocus "+extraOption);
097            requestFocus();
098            if (extraOption!=null && extraOption instanceof Integer) {
099                int line = ((Integer)extraOption).intValue();
100                String text = ((JEditorPane)editor).getText();
101                int index = 0;
102                int previndex = 0;
103                for (int i=0; i<line; i++) {
104                    previndex = index;
105                    index = text.indexOf('\n',index+1);
106                    loggerFocus.debug("TextEditor.onSetFocus "+previndex+","+index);
107                }
108                loggerFocus.debug("TextEditor.onSetFocus "+previndex+","+index);
109                ((JEditorPane)editor).setSelectionStart(previndex+1);
110                ((JEditorPane)editor).setSelectionEnd(index);
111            }
112        }
113    
114        /**
115         * Set the focus on the TextEditor
116         */
117        public void requestFocus() {
118            ((JEditorPane)editor).requestFocus();
119            loggerFocus.debug("focusing "+editor.getClass().getName());
120        }
121    }