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