From 3d6b4d8ad1b4ac3eddbd15fd1bdf90f4573ed2f7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jindra=20Pet=F8=EDk?= Date: Sun, 10 Jul 2011 13:29:34 +0200 Subject: [PATCH] AS3: Highlighting current line. --- .../asdec/abc/gui/ASMSourceEditorPane.java | 11 +- .../asdec/abc/gui/DecompiledEditorPane.java | 257 +++++++++--------- .../asdec/abc/gui/LineMarkedEditorPane.java | 67 +++++ .../com/jpexs/asdec/abc/gui/MainFrame.java | 10 +- 4 files changed, 211 insertions(+), 134 deletions(-) create mode 100644 trunk/src/com/jpexs/asdec/abc/gui/LineMarkedEditorPane.java diff --git a/trunk/src/com/jpexs/asdec/abc/gui/ASMSourceEditorPane.java b/trunk/src/com/jpexs/asdec/abc/gui/ASMSourceEditorPane.java index 56e979685..4640c118b 100644 --- a/trunk/src/com/jpexs/asdec/abc/gui/ASMSourceEditorPane.java +++ b/trunk/src/com/jpexs/asdec/abc/gui/ASMSourceEditorPane.java @@ -32,13 +32,13 @@ import java.io.IOException; import java.util.HashMap; -public class ASMSourceEditorPane extends JEditorPane { +public class ASMSourceEditorPane extends LineMarkedEditorPane { public ABC abc; public int bodyIndex; public ASMSourceEditorPane() { - + } public void setBodyIndex(int bodyIndex, ABC abc) { @@ -116,14 +116,15 @@ public class ASMSourceEditorPane extends JEditorPane { if (instrCount == pos+1) { break; } - lineStart = i; + lineStart = i+1; } } if (lineCnt == -1) { lineEnd = text.length() - 1; } - select(lineStart, lineEnd); - requestFocus(); + //select(lineStart, lineEnd); + setCaretPosition(lineStart); + //requestFocus(); } public void selectLine(int line) { diff --git a/trunk/src/com/jpexs/asdec/abc/gui/DecompiledEditorPane.java b/trunk/src/com/jpexs/asdec/abc/gui/DecompiledEditorPane.java index b938b8dc0..2b603e74b 100644 --- a/trunk/src/com/jpexs/asdec/abc/gui/DecompiledEditorPane.java +++ b/trunk/src/com/jpexs/asdec/abc/gui/DecompiledEditorPane.java @@ -15,7 +15,6 @@ * along with this program; if not, write to the Free Software * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ - package com.jpexs.asdec.abc.gui; import com.jpexs.asdec.Main; @@ -23,146 +22,148 @@ import com.jpexs.asdec.abc.ABC; import com.jpexs.asdec.abc.avm2.ConvertException; import com.jpexs.asdec.helpers.Highlighting; -import javax.swing.*; import java.awt.event.MouseEvent; import java.awt.event.MouseListener; import java.util.ArrayList; import java.util.HashMap; import java.util.List; +import javax.swing.event.CaretEvent; +import javax.swing.event.CaretListener; +public class DecompiledEditorPane extends LineMarkedEditorPane implements MouseListener,CaretListener { -public class DecompiledEditorPane extends JEditorPane implements MouseListener { + private List highlights = new ArrayList(); + private List traitHighlights = new ArrayList(); + private ABC abc; + private int classIndex; + public int lastTraitIndex = 0; - private List highlights = new ArrayList(); - private List traitHighlights = new ArrayList(); - private ABC abc; - private int classIndex; - public int lastTraitIndex = 0; + public void caretUpdate(CaretEvent e) { + getCaret().setVisible(true); + int pos = getCaretPosition(); + for (Highlighting th : traitHighlights) { + if ((pos >= th.startPos) && (pos < th.startPos + th.len)) { - private class BufferedClass { - - public String text; - public List highlights; - public List traitHighlights; - - public BufferedClass(String text, List highlights, List traitHighlights) { - this.text = text; - this.highlights = highlights; - this.traitHighlights = traitHighlights; - } - } - - private HashMap bufferedClasses = new HashMap(); - - public void gotoLastTrait() { - gotoTrait(lastTraitIndex); - } - - public void gotoTrait(int traitId) { - for (Highlighting th : traitHighlights) { - if (th.offset == traitId) { - setCaretPosition(th.startPos + th.len - 1); - setCaretPosition(th.startPos); - break; + int bi = abc.findBodyIndex(abc.findMethodIdByTraitId(classIndex, (int) th.offset)); + if (bi == -1) { + Main.abcMainFrame.sourceTextArea.setText(""); + break; } - } - int mi = abc.findMethodIdByTraitId(classIndex, traitId); - int bi = abc.findBodyIndex(mi); - if (bi == -1) { - Main.abcMainFrame.sourceTextArea.setText(""); - return; - } - if (Main.abcMainFrame.sourceTextArea.bodyIndex != bi) { - Main.abcMainFrame.sourceTextArea.setBodyIndex(bi, abc); - } - } - - public DecompiledEditorPane() { - /*setFont(new Font("Courier new", Font.PLAIN, 16)); - setBackground(new Color(0, 0, 0x80)); - setForeground(Color.white); - setCaretColor(Color.white);*/ - addMouseListener(this); - setEditable(false); - } - - public void setClassIndex(int index, ABC abc) { - setText("//Please wait..."); - - String hilightedCode = ""; - if (!bufferedClasses.containsKey(index)) { - hilightedCode = abc.classToString(index, true,false); - highlights = Highlighting.getInstrHighlights(hilightedCode); - traitHighlights = Highlighting.getTraitHighlights(hilightedCode); - hilightedCode = Highlighting.stripHilights(hilightedCode); - bufferedClasses.put(index, new BufferedClass(hilightedCode, highlights, traitHighlights)); - } else { - BufferedClass bc = bufferedClasses.get(index); - hilightedCode = bc.text; - highlights = bc.highlights; - traitHighlights = bc.traitHighlights; - } - setText(hilightedCode); - this.abc = abc; - classIndex = index; - } - - public void reloadClass() { - if (bufferedClasses.containsKey(classIndex)) { - bufferedClasses.remove(classIndex); - } - setClassIndex(classIndex, abc); - } - - public void setABC(ABC abc) { - this.abc = abc; - bufferedClasses.clear(); - setText(""); - } - - public void mouseClicked(MouseEvent e) { - } - - public void mousePressed(MouseEvent e) { - int pos = getCaretPosition(); - for (Highlighting th : traitHighlights) { - if ((pos >= th.startPos) && (pos < th.startPos + th.len)) { - - int bi = abc.findBodyIndex(abc.findMethodIdByTraitId(classIndex, (int) th.offset)); - if (bi == -1) { - Main.abcMainFrame.sourceTextArea.setText(""); - break; - } - lastTraitIndex = (int) th.offset; - if (Main.abcMainFrame.sourceTextArea.bodyIndex != bi) { - /*try { - abc.bodies[bi].code.clearCode(abc.constants, abc.bodies[bi]); - } catch (ConvertException ex) { - Logger.getLogger(DecompiledEditorPane.class.getName()).log(Level.SEVERE, null, ex); - }*/ - Main.abcMainFrame.sourceTextArea.setBodyIndex(bi, abc); - } - for (Highlighting h : highlights) { - if ((pos >= h.startPos) && (pos < h.startPos + h.len)) { - try { - Main.abcMainFrame.sourceTextArea.selectInstruction(abc.bodies[bi].code.adr2pos(h.offset)); - - } catch (ConvertException ex) { - } - break; - } - } + lastTraitIndex = (int) th.offset; + if (Main.abcMainFrame.sourceTextArea.bodyIndex != bi) { + /*try { + abc.bodies[bi].code.clearCode(abc.constants, abc.bodies[bi]); + } catch (ConvertException ex) { + Logger.getLogger(DecompiledEditorPane.class.getName()).log(Level.SEVERE, null, ex); + }*/ + Main.abcMainFrame.sourceTextArea.setBodyIndex(bi, abc); } - } + for (Highlighting h : highlights) { + if ((pos >= h.startPos) && (pos < h.startPos + h.len)) { + try { + Main.abcMainFrame.sourceTextArea.selectInstruction(abc.bodies[bi].code.adr2pos(h.offset)); - } + } catch (ConvertException ex) { + } + break; + } + } + } + } + } - public void mouseReleased(MouseEvent e) { - } + private class BufferedClass { - public void mouseEntered(MouseEvent e) { - } + public String text; + public List highlights; + public List traitHighlights; - public void mouseExited(MouseEvent e) { - } + public BufferedClass(String text, List highlights, List traitHighlights) { + this.text = text; + this.highlights = highlights; + this.traitHighlights = traitHighlights; + } + } + private HashMap bufferedClasses = new HashMap(); + + public void gotoLastTrait() { + gotoTrait(lastTraitIndex); + } + + public void gotoTrait(int traitId) { + for (Highlighting th : traitHighlights) { + if (th.offset == traitId) { + setCaretPosition(th.startPos + th.len - 1); + setCaretPosition(th.startPos); + break; + } + } + int mi = abc.findMethodIdByTraitId(classIndex, traitId); + int bi = abc.findBodyIndex(mi); + if (bi == -1) { + Main.abcMainFrame.sourceTextArea.setText(""); + return; + } + if (Main.abcMainFrame.sourceTextArea.bodyIndex != bi) { + Main.abcMainFrame.sourceTextArea.setBodyIndex(bi, abc); + } + } + + public DecompiledEditorPane() { + addMouseListener(this); + setEditable(false); + getCaret().setVisible(true); + addCaretListener(this); + } + + public void setClassIndex(int index, ABC abc) { + setText("//Please wait..."); + + String hilightedCode = ""; + if (!bufferedClasses.containsKey(index)) { + hilightedCode = abc.classToString(index, true, false); + highlights = Highlighting.getInstrHighlights(hilightedCode); + traitHighlights = Highlighting.getTraitHighlights(hilightedCode); + hilightedCode = Highlighting.stripHilights(hilightedCode); + bufferedClasses.put(index, new BufferedClass(hilightedCode, highlights, traitHighlights)); + } else { + BufferedClass bc = bufferedClasses.get(index); + hilightedCode = bc.text; + highlights = bc.highlights; + traitHighlights = bc.traitHighlights; + } + setText(hilightedCode); + this.abc = abc; + classIndex = index; + } + + public void reloadClass() { + if (bufferedClasses.containsKey(classIndex)) { + bufferedClasses.remove(classIndex); + } + setClassIndex(classIndex, abc); + } + + public void setABC(ABC abc) { + this.abc = abc; + bufferedClasses.clear(); + setText(""); + } + + public void mouseClicked(MouseEvent e) { + } + + public void mousePressed(MouseEvent e) { + + + } + + public void mouseReleased(MouseEvent e) { + } + + public void mouseEntered(MouseEvent e) { + } + + public void mouseExited(MouseEvent e) { + } } diff --git a/trunk/src/com/jpexs/asdec/abc/gui/LineMarkedEditorPane.java b/trunk/src/com/jpexs/asdec/abc/gui/LineMarkedEditorPane.java new file mode 100644 index 000000000..8cfdf413f --- /dev/null +++ b/trunk/src/com/jpexs/asdec/abc/gui/LineMarkedEditorPane.java @@ -0,0 +1,67 @@ +/* + * Copyright (C) 2011 Jindra + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + */ + +package com.jpexs.asdec.abc.gui; + +import java.awt.Color; +import java.awt.FontMetrics; +import java.awt.Graphics; +import javax.swing.JEditorPane; +import javax.swing.event.CaretEvent; +import javax.swing.event.CaretListener; +import javax.swing.text.Element; + +/** + * + * @author JPEXS + */ +public class LineMarkedEditorPane extends JEditorPane { + + int lastLine=-1; + public LineMarkedEditorPane() { + setOpaque(false); + addCaretListener(new CaretListener(){ + + public void caretUpdate(CaretEvent e) { + int caretPosition = getCaretPosition(); + Element root = getDocument().getDefaultRootElement(); + int currentLine = root.getElementIndex(caretPosition); + if(currentLine!=lastLine){ + lastLine=currentLine; + repaint(); + } + } + + }); + } + + @Override + public void paint(Graphics g) { + g.setColor(Color.white); + g.fillRect(0, 0, getWidth(),getHeight()); + FontMetrics fontMetrics = g.getFontMetrics(); + int lh = fontMetrics.getHeight(); + int a = fontMetrics.getAscent(); + int d=fontMetrics.getDescent(); + int h = a + d; + int rH = h; + g.setColor(new Color(0xee,0xee,0xee)); + g.fillRect(0, d+lh*lastLine-1, getWidth(),lh); + super.paint(g); + } +} diff --git a/trunk/src/com/jpexs/asdec/abc/gui/MainFrame.java b/trunk/src/com/jpexs/asdec/abc/gui/MainFrame.java index e024bc70c..4a0a3580c 100644 --- a/trunk/src/com/jpexs/asdec/abc/gui/MainFrame.java +++ b/trunk/src/com/jpexs/asdec/abc/gui/MainFrame.java @@ -32,7 +32,10 @@ import javax.swing.table.*; import java.awt.*; import java.awt.event.*; import java.io.IOException; +import java.util.ArrayList; import java.util.List; +import jsyntaxpane.actions.ComboCompletionAction; +import jsyntaxpane.syntaxkits.Flasm3SyntaxKit; public class MainFrame extends JFrame implements ActionListener, ItemListener { @@ -171,7 +174,12 @@ public class MainFrame extends JFrame implements ActionListener, ItemListener { JPanel rightPanel = new JPanel(); rightPanel.setLayout(new BorderLayout()); rightPanel.add(new JScrollPane(sourceTextArea), BorderLayout.CENTER); - sourceTextArea.setContentType("text/flasm3"); + sourceTextArea.setContentType("text/flasm3"); + ActionMap am=sourceTextArea.getActionMap(); + ComboCompletionAction cca=(ComboCompletionAction)am.get("combo-completion"); + List items=new ArrayList(); + items.add("ahoj"); + cca.setItems(items); JPanel buttonsPan = new JPanel(); buttonsPan.setLayout(new FlowLayout()); JButton verifyButton = new JButton("Verify");