1 18 19 package org.objectweb.jac.aspects.gui.swing; 20 21 import java.awt.Dimension ; 22 import javax.swing.JEditorPane ; 23 import javax.swing.JScrollPane ; 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 31 32 public class TextEditor extends AbstractFieldEditor 33 implements FieldEditor 34 { 35 36 JEditorPane editor; 37 JScrollPane scrollPane; 38 39 41 42 public TextEditor(Object substance, FieldItem field) { 43 super(substance,field); 44 init(); 45 } 46 47 public void init() { 48 editor = new JEditorPane (); 49 scrollPane = new JScrollPane (editor); 50 add(scrollPane); 51 52 ((JEditorPane )editor).addFocusListener(this); 53 ((JEditorPane )editor).setContentType("html"); 54 } 55 56 public void setValue(Object value) { 58 super.setValue(value); 59 if (value==null) 60 ((JEditorPane )editor).setText(""); 61 else if (value instanceof String ) 62 ((JEditorPane )editor).setText((String )value); 63 else if (value instanceof byte[]) 64 ((JEditorPane )editor).setText(new String ((byte[])value)); 65 else 66 throw new RuntimeException ("Unhandled type "+value.getClass().getName()); 67 } 68 69 public Object getValue() { 70 Object value = ((JEditorPane )editor).getText(); 71 if (type.getActualClass()==byte[].class) 72 value = ((String )value).getBytes(); 73 return value; 74 } 75 76 public void onSetFocus(Object extraOption) { 77 loggerFocus.debug("TextEditor.onSetFocus "+extraOption); 78 requestFocus(); 79 if (extraOption!=null && extraOption instanceof Integer ) { 80 int line = ((Integer )extraOption).intValue(); 81 String text = ((JEditorPane )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 )editor).setSelectionStart(previndex+1); 91 ((JEditorPane )editor).setSelectionEnd(index); 92 } 93 } 94 95 98 public void requestFocus() { 99 ((JEditorPane )editor).requestFocus(); 100 loggerFocus.debug("focusing "+editor.getClass().getName()); 101 } 102 } 103 | Popular Tags |