1 19 20 package org.objectweb.jac.aspects.gui.swing; 21 22 import java.awt.BorderLayout ; 23 import java.awt.Dimension ; 24 import java.awt.Dimension ; 25 import java.awt.event.KeyAdapter ; 26 import java.awt.event.KeyEvent ; 27 import javax.swing.JScrollPane ; 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 35 public abstract class AbstractCodeEditor extends AbstractFieldEditor 36 { 37 protected SHEditor editor; 38 JScrollPane scrollPane; 39 40 public AbstractCodeEditor(Object substance, FieldItem field) { 41 super(substance,field); 42 43 width = new Length("400px"); 44 height = new Length("400px"); 45 46 setLayout(new BorderLayout ()); 47 scrollPane = new EditorScrollPane(); 48 editor = ((EditorScrollPane)scrollPane).editor; 49 add(BorderLayout.CENTER,scrollPane); 50 scrollPane.setMinimumSize(new Dimension (128,64)); 52 53 setMinimumSize(new Dimension (128,64)); 54 56 editor.addFocusListener(this); 57 editor.addKeyListener( 58 new KeyAdapter () { 59 public void keyReleased(KeyEvent 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 public void setValue(Object value) { 75 super.setValue(value); 76 if (value==null) 77 editor.setText(""); 78 else if (value instanceof byte[]) 79 editor.setText(new String ((byte[])value)); 80 else 81 editor.setText((String )value); 82 } 83 84 public Object 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 extraOption) { 98 loggerFocus.debug("AbstactEditor.onSetFocus "+extraOption); 99 requestFocus(); 100 if (extraOption instanceof Integer ) { 101 int line = ((Integer )extraOption).intValue(); 102 String 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 123 public void requestFocus() { 124 editor.requestFocus(); 125 loggerFocus.debug("focusing "+editor.getClass().getName()); 126 } 127 128 } 129 130 | Popular Tags |