📄 testframe.java
字号:
/**
*
*/
package flow.graph.gui.syntax.action;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Graphics;
import java.awt.Point;
import java.awt.Rectangle;
import java.awt.datatransfer.Clipboard;
import java.awt.datatransfer.ClipboardOwner;
import java.awt.datatransfer.DataFlavor;
import java.awt.datatransfer.StringSelection;
import java.awt.datatransfer.Transferable;
import java.awt.datatransfer.UnsupportedFlavorException;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.io.IOException;
import java.util.HashMap;
import java.util.Set;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import javax.swing.AbstractAction;
import javax.swing.Action;
import javax.swing.BorderFactory;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JFrame;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.JPopupMenu;
import javax.swing.JRadioButton;
import javax.swing.JScrollPane;
import javax.swing.JTabbedPane;
import javax.swing.JTextArea;
import javax.swing.JTextPane;
import javax.swing.KeyStroke;
import javax.swing.LookAndFeel;
import javax.swing.SwingUtilities;
import javax.swing.TransferHandler;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;
import javax.swing.event.UndoableEditEvent;
import javax.swing.event.UndoableEditListener;
import javax.swing.plaf.ComponentUI;
import javax.swing.plaf.metal.DefaultMetalTheme;
import javax.swing.plaf.metal.MetalLookAndFeel;
import javax.swing.text.AttributeSet;
import javax.swing.text.BadLocationException;
import javax.swing.text.DefaultEditorKit;
import javax.swing.text.DefaultStyledDocument;
import javax.swing.text.Document;
import javax.swing.text.Element;
import javax.swing.text.MutableAttributeSet;
import javax.swing.text.SimpleAttributeSet;
import javax.swing.text.StyleConstants;
import javax.swing.text.StyledDocument;
import javax.swing.text.StyledEditorKit;
import javax.swing.undo.CannotRedoException;
import javax.swing.undo.CannotUndoException;
import javax.swing.undo.UndoManager;
import javax.swing.undo.UndoableEdit;
import org.jgraph.graph.DefaultEdge;
import org.jgraph.graph.DefaultGraphCell;
import flow.graph.gui.graph.GraphManager;
import flow.graph.gui.graph.lua.gui.CaseForm;
import flow.graph.gui.syntax.MyEditorKit;
import flow.graph.gui.tools.JNormalButton;
import flow.graph.gui.tools.SimpleButton;
import flow.graph.gui.tree.HilighterCodeStatusBar;
import flow.graph.test.test.UndoStyleFrame.RedoAct;
import flow.graph.test.test.UndoStyleFrame.UndoAct;
/**
* A class illustrating running line number count on JTextPane. Nothing
is painted on the pane itself,
* but a separate JPanel handles painting the line numbers.<br>
*
* @author Daniel Sj?blom<br>
* Created on Mar 3, 2004<br>
* Copyright (c) 2004<br>
* @version 1.0<br>
*/
public class TestFrame extends JPanel{
// for this simple experiment, we keep the pane + scrollpane as members.
public final static int BIT_SIZE = 8;
public final static int BIT_SIZE_SPACE = 6;
NonWrappingTextPane pane;
public JScrollPane scrollPane;
public HilighterCodeStatusBar staturBar;
private int numberLength = 1;
protected UndoAct undoAction = new UndoAct(); // an Action for undo
protected RedoAct redoAction = new RedoAct(); // an Action for redo
private Action copyAction = TransferHandler.getCopyAction();
private Action pasteAction = TransferHandler.getPasteAction();
private Action cutAction = TransferHandler.getCutAction();
class NonWrappingTextPane extends JTextPane{
//Document doc = getDocument();
public NonWrappingTextPane(){
}
public boolean getScrollableTracksViewportWidth() {
Component parent = getParent();
ComponentUI ui = getUI();
return parent != null ? (ui.getPreferredSize(this).width <= parent.getSize().width) : true;
}
}
public TestFrame(){
super();
setMaximumSize(new Dimension(BIT_SIZE_SPACE+BIT_SIZE, BIT_SIZE_SPACE+BIT_SIZE));
setPreferredSize(new Dimension(BIT_SIZE_SPACE+BIT_SIZE, BIT_SIZE_SPACE+BIT_SIZE));//每个数字占8个单位
setMinimumSize(new Dimension(BIT_SIZE_SPACE+BIT_SIZE, BIT_SIZE_SPACE+BIT_SIZE));
//we need to override paint so that the linenumbers stay in sync
pane = new NonWrappingTextPane(){
public void paint(Graphics g){
super.paint(g);
TestFrame.this.repaint();
}
};
pane.getDocument().addUndoableEditListener(undoAction);
pane.getDocument().addUndoableEditListener(redoAction);
//pane.setEditorKit(new highlightKit());
pane.setEditorKit(new MyEditorKit());
scrollPane = new JScrollPane(pane);
staturBar = new HilighterCodeStatusBar();
//setBackground(new Color(250, 250, 250));
//setBackground(new Color)
this.setBorder(BorderFactory.createLineBorder(Color.LIGHT_GRAY, 1));
pane.addMouseListener(new MouseListener(){
public void mouseClicked(MouseEvent e) {
// TODO Auto-generated method stub
}
public void mousePressed(MouseEvent e) {
// TODO Auto-generated method stub
if(SwingUtilities.isRightMouseButton(e)){
//鼠标右键
JPopupMenu menu = new JPopupMenu();
JMenuItem allItem = new JMenuItem("全选", new ImageIcon(GraphManager.class.getResource("images/collapse.gif")));
allItem.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
//System.out.println("aalaksdjflksadf");
//new CaseForm((DefaultGraphCell)port.getParent(), (DefaultEdge)cell, graph);
pane.selectAll();
}});
menu.add(allItem);
menu.addSeparator();
JMenuItem undoItem = new JMenuItem("撤销", new ImageIcon(GraphManager.class.getResource("images/undo.gif")));
undoItem.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
//System.out.println("aalaksdjflksadf");
undoAction.actionPerformed(e);
}});
menu.add(undoAction);
JMenuItem redoItem = new JMenuItem("重做", new ImageIcon(GraphManager.class.getResource("images/redo.gif")));
redoItem.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
//System.out.println("aalaksdjflksadf");
redoAction.actionPerformed(e);
}});
menu.add(redoAction);
menu.addSeparator();
JMenuItem cutItem = new JMenuItem("剪切", new ImageIcon(GraphManager.class.getResource("images/cut.gif")));
cutItem.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
cutAction.actionPerformed(e);
}});
menu.add(cutItem);
JMenuItem copyItem = new JMenuItem("拷贝", new ImageIcon(GraphManager.class.getResource("images/copy.gif")));
copyItem.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
copyAction.actionPerformed(e);
}});
menu.add(copyItem);
JMenuItem pasteItem = new JMenuItem("粘贴", new ImageIcon(GraphManager.class.getResource("images/paste.gif")));
pasteItem.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
pasteAction.actionPerformed(e);
}});
menu.add(pasteItem);
menu.show(pane, e.getX(), e.getY());
}
}
public void mouseReleased(MouseEvent e) {
// TODO Auto-generated method stub
}
public void mouseEntered(MouseEvent e) {
// TODO Auto-generated method stub
}
public void mouseExited(MouseEvent e) {
// TODO Auto-generated method stub
}});
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -