📄 jhightlightlinetextpane.java
字号:
public void mousePressed(MouseEvent e) {
// TODO Auto-generated method stub
if(SwingUtilities.isRightMouseButton(e)){
//鼠标右键
JPopupMenu menu = new JPopupMenu();
JMenuItem allItem = new JMenuItem("全选 (CTRL+a)", 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");
pane.getUndoAction().actionPerformed(e);
}});
menu.add(undoItem);
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");
pane.getRedoAction().actionPerformed(e);
}});
menu.add(redoItem);
*/
menu.addSeparator();
JMenuItem cutItem = new JMenuItem("剪切 (CTRL+x)", new ImageIcon(GraphManager.class.getResource("images/cut.gif")));
cutItem.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
pane.getCutAction().actionPerformed(e);
}});
menu.add(cutItem);
JMenuItem copyItem = new JMenuItem("拷贝 (CTRL+c)", new ImageIcon(GraphManager.class.getResource("images/copy.gif")));
copyItem.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
pane.getCopyAction().actionPerformed(e);
}});
menu.add(copyItem);
JMenuItem pasteItem = new JMenuItem("粘贴 (CTRL+v)", new ImageIcon(GraphManager.class.getResource("images/paste.gif")));
pasteItem.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
pane.getPasteAction().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
}});
}
public void setEditable1(boolean able){
pane.setEditable(able);
}
public void setBackground1(Color color){
pane.setBackground(color);
}
/*
public void paint(Graphics g){
super.paint(g);
// We need to properly convert the points to match the viewport
// Read docs for viewport
int start =pane.viewToModel(scrollPane.getViewport().getViewPosition()); //starting pos in document
int end =pane.viewToModel(new Point(
scrollPane.getViewport().getViewPosition().x + pane.getWidth(),
scrollPane.getViewport().getViewPosition().y + pane.getHeight()));
// end pos in doc
// translate offsets to lines
Document doc = pane.getDocument();
int startline = doc.getDefaultRootElement().getElementIndex(start) ;
int endline = doc.getDefaultRootElement().getElementIndex(end);
int fontHeight = g.getFontMetrics(pane.getFont()).getHeight(); // fontheight
for (int line = startline, y = 0; line <= endline; line++, y +=fontHeight){
g.drawString(Integer.toString(line), 0, y);
}
}*/
public void paint(Graphics g){
super.paint(g);
//System.out.println("font="+pane.getFont());
// We need to properly convert the points to match the viewport
// Read docs for viewport
int start = pane.viewToModel(scrollPane.getViewport().getViewPosition()); //starting pos in document
int end = pane.viewToModel(new Point(
scrollPane.getViewport().getViewPosition().x + pane.getWidth(),
scrollPane.getViewport().getViewPosition().y + pane.getHeight()));
// end pos in doc
// translate offsets to lines
Document doc = pane.getDocument();
int startline = doc.getDefaultRootElement().getElementIndex(start) + 1;
int endline = doc.getDefaultRootElement().getElementIndex(end) + 1;
int fontHeight = g.getFontMetrics(pane.getFont()).getHeight();
int fontDesc = g.getFontMetrics(pane.getFont()).getDescent();
int starting_y = -1;
try{
starting_y = pane.modelToView(start).y - scrollPane.getViewport().getViewPosition().y + fontHeight - fontDesc;
}
catch (BadLocationException e1){
e1.printStackTrace();
}
int testline = 0;
//System.out.println(starting_y);
g.setColor(Color.GRAY);
for (int line = startline, y = starting_y; line <= endline; y += fontHeight, line++){
//System.out.println("line="+line+",y="+y);
testline=line;
g.drawString(Integer.toString(line), 0, y);
}
int bit = (testline+"").length();
if(bit > numberLength){
numberLength = bit;
setMaximumSize(new Dimension(BIT_SIZE_SPACE+BIT_SIZE*bit, BIT_SIZE_SPACE+BIT_SIZE*bit));
setPreferredSize(new Dimension(BIT_SIZE_SPACE+BIT_SIZE*bit, BIT_SIZE_SPACE+BIT_SIZE*bit));//每个数字占8个单位
setMinimumSize(new Dimension(BIT_SIZE_SPACE+BIT_SIZE*bit, BIT_SIZE_SPACE+BIT_SIZE*bit));
updateUI();
}
else if(bit < numberLength){
numberLength = bit;
setMaximumSize(new Dimension(BIT_SIZE_SPACE+BIT_SIZE*bit, BIT_SIZE_SPACE+BIT_SIZE*bit));
setPreferredSize(new Dimension(BIT_SIZE_SPACE+BIT_SIZE*bit, BIT_SIZE_SPACE+BIT_SIZE*bit));//每个数字占8个单位
setMinimumSize(new Dimension(BIT_SIZE_SPACE+BIT_SIZE*bit, BIT_SIZE_SPACE+BIT_SIZE*bit));
updateUI();
}
Element root = doc.getDefaultRootElement();
//取光标当前行
int cursorPos = pane.getCaretPosition();
//当前光标的位置
int line = root.getElementIndex(cursorPos);
int all = root.getElementIndex(doc.getLength());
staturBar.updateStatus((all+1)+"", (line+1)+"", doc.getLength()+"");
}
private String getSpace(int n){
String str = "";
for(int i=0;i<n;i++)
str += " ";
return str;
}
public String getText(){
return pane.getText();
}
public void setText(String s){
pane.setText(s);
}
public static void main(String[] args){
//JHightLightLineTextPane.configureUI();
com.nilo.plaf.nimrod.NimRODLookAndFeel nf = new com.nilo.plaf.nimrod.NimRODLookAndFeel();
com.nilo.plaf.nimrod.NimRODTheme nt = new com.nilo.plaf.nimrod.NimRODTheme();
com.nilo.plaf.nimrod.NimRODLookAndFeel.setCurrentTheme( nt);
try {
UIManager.setLookAndFeel( nf);
} catch (UnsupportedLookAndFeelException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
JFrame frame = new JFrame("HightLight Edit test...");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().setLayout(new BorderLayout());
final JHightLightLineTextPane nr = new JHightLightLineTextPane();
final JTextArea textCode = new JTextArea();
JTabbedPane tab = new JTabbedPane();
JPanel pane = new JPanel(new BorderLayout());
pane.add(nr, BorderLayout.WEST);
pane.add(nr.scrollPane, BorderLayout.CENTER);
/*
tab.addMouseListener(new MouseListener(){
public void mouseClicked(MouseEvent e) {
System.out.println("mouseClicked---------");
// TODO Auto-generated method stub
//System.out.println(((ParseCodeTab)e.getComponent()).getTitleAt(((ParseCodeTab)e.getComponent()).getSelectedIndex()));
//e.getComponent().setForeground(Color.BLACK);
if(((JTabbedPane)e.getComponent()).getSelectedIndex() == 0){
nr.setText(textCode.getText());
}
}
public void mousePressed(MouseEvent e) {
System.out.println("mousePressed---------");
// TODO Auto-generated method stub
}
public void mouseReleased(MouseEvent e) {
System.out.println("mouseReleased---------");
// TODO Auto-generated method stub
}
public void mouseEntered(MouseEvent e) {
System.out.println("mouseEntered---------");
// TODO Auto-generated method stub
}
public void mouseExited(MouseEvent e) {
System.out.println("mouseExited---------");
// TODO Auto-generated method stub
}
});*/
tab.add("代码测试", pane);
tab.add("代码预览", new JScrollPane(textCode));
JPanel buttonPanel = new JPanel(new FlowLayout());
JNormalButton exitButton = new JNormalButton("关 闭");
exitButton.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
System.exit(0);
}});
buttonPanel.add(exitButton);
frame.getContentPane().add(tab, BorderLayout.CENTER);
frame.getContentPane().add(buttonPanel, BorderLayout.SOUTH);
frame.pack();
frame.setSize(new Dimension(400, 400));
frame.show();
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -