package abstrasy.consoleui;
import java.awt.FontMetrics;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.event.ActionEvent;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import javax.swing.AbstractAction;
import javax.swing.Action;
import javax.swing.Timer;
import javax.swing.text.SimpleAttributeSet;
import javax.swing.text.StyleConstants;
import javax.swing.text.TabSet;
import javax.swing.text.TabStop;
/**
* Abstrasy Interpreter
*
* Copyright : Copyright (c) 2006-2012, Luc Bruninx.
*
* Concédée sous licence EUPL, version 1.1 uniquement (la «Licence»).
*
* Vous ne pouvez utiliser la présente oeuvre que conformément à la Licence.
* Vous pouvez obtenir une copie de la Licence à l’adresse suivante:
*
* http://www.osor.eu/eupl
*
* Sauf obligation légale ou contractuelle écrite, le logiciel distribué sous
* la Licence est distribué "en l’état", SANS GARANTIES OU CONDITIONS QUELLES
* QU’ELLES SOIENT, expresses ou implicites.
*
* Consultez la Licence pour les autorisations et les restrictions
* linguistiques spécifiques relevant de la Licence.
*
*
* @author Luc Bruninx
* @version 1.0
*/
public class EditorTextArea extends NoWrapTextPane {
public EditorTextArea() {
try {
jbInit();
}
catch (Exception e) {
e.printStackTrace();
}
}
EditorTextArea mySelf = this;
transient Action comAction = new AbstractAction() {
public void actionPerformed(ActionEvent e) {
mySelf.comTimer.stop();
try {
((MyDocument) mySelf.getDocument()).colorize();
}
catch (Exception ex) {
ex.printStackTrace();
}
mySelf.comTimer.start();
}
};
Timer comTimer = new Timer(MyDocument.changedelay / 2, comAction);
public void paintComponent(Graphics g) {
Graphics2D g2 = (Graphics2D) g;
//Object antialias = g2.getRenderingHint(RenderingHints.KEY_ANTIALIASING);
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
super.paintComponent(g);
}
public void setTabSize(int charactersPerTab) {
FontMetrics fm = this.getFontMetrics(this.getFont());
int charWidth = fm.charWidth('w');
int tabWidth = charWidth * charactersPerTab;
TabStop[] tabs = new TabStop[36];
for (int j = 0; j < tabs.length; j++) {
int tab = j + 1;
tabs[j] = new TabStop(tab * tabWidth);
}
TabSet tabSet = new TabSet(tabs);
SimpleAttributeSet attributes = new SimpleAttributeSet();
StyleConstants.setTabSet(attributes, tabSet);
int length = this.getDocument().getLength();
this.getStyledDocument().setParagraphAttributes(0, length, attributes, false);
}
// assuming this method is defined inside your JTextPane
// and getLineStartOffset should return -1, if the row doesn't exist
public int getLineStartOffset(int row) {
String[] st = this.getText().split("\n");
int offset = 0;
int i = 0;
while ((row > 0) && (i < st.length)) {
offset += (st[i++].length() + 1);
row--;
}
if (row != 0) {
offset = -1;
}
return offset;
}
public int getLineOfOffset(int pos) {
String[] st = this.getText().split("\n");
int offset1 = 0;
int offset2 = 0;
int i = 0;
while ((pos > 0) && (i < st.length)) {
offset1 = offset2;
offset2 = offset1 + (st[i].length() + 1);
if ((offset1 <= pos) && (pos <= offset2)) {
pos = -1;
}
else {
i++;
}
}
if (i == st.length) {
i = -1;
}
return i;
}
public void forceColorize() {
((MyDocument) this.getDocument()).parse(this.getText(), 0);
}
private void jbInit() throws Exception {
this.addKeyListener(new KeyAdapter() {
public void keyTyped(KeyEvent e) {
this_keyTyped(e);
}
});
}
public void setText(String source){
String _source_= replaceCSeq(source,"\t"," ");
super.setText(_source_);
}
public void copy(){
//this.setText(this.getText());
super.copy();
}
public void cut(){
//this.setText(this.getText());
super.cut();
}
public void paste(){
super.paste();
int ed=this.getCaretPosition();
if(ed>0){
this.setText(this.getText());
}
this.setCaretPosition(ed);
}
public boolean findNext(String tx){
int ed=this.getCaretPosition();
int index=-1;
if(ed>=0){
try{
index=this.getText().indexOf(tx, ed);
this.setCaretPosition(index);
this.setSelectionStart(index);
this.setSelectionEnd(index+tx.length());
}
catch(Exception e){
index=-1;
}
}
if(index>=0){return true;} else{ return false; }
}
private static final String replaceCSeq(String src, String fndChar, String rSeq) {
StringBuffer rs;
StringBuffer rs2;
CharSequence ps1;
CharSequence ps2;
rs = new StringBuffer((src == null ? "": src));
int i = rs.indexOf(fndChar);
while (i >= 0) {
if (i == 0) {
ps1 = null;
}
else {
ps1 = rs.subSequence(0, i);
}
if (i >= rs.length() - fndChar.length()) {
ps2 = null;
}
else {
int ti = i + fndChar.length();
ps2 = rs.subSequence(ti, rs.length());
}
//System.out.println();
rs2 = new StringBuffer();
if (ps1 != null) {
rs2 = rs2.append(ps1);
}
rs2 = rs2.append(rSeq);
if (ps2 != null) {
rs2 = rs2.append(ps2);
}
rs = rs2;
i = rs.indexOf(fndChar, i + rSeq.length());
}
return rs.toString();
}
private void this_keyTyped(KeyEvent e) {
if (e.getKeyChar() == '\t') {
int ed=this.getCaretPosition();
if(ed>0){
int sd=ed-1;
this.select(sd,ed);
this.replaceSelection(" ");
}
}
}
}