📄 zeditor.java
字号:
/*
* Copyright 2002 EZCell , Inc. All rights reserved.
* Version 1.0.
* Author W.John
*/
package ezcell;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.FontMetrics;
import java.awt.Graphics;
import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.util.Vector;
import javax.swing.JComponent;
import javax.swing.JTextField;
/**
* DOCUMENT ME!
*
* @version 1.00
* @author W.John
*/
public class ZEditor extends JTextField implements KeyListener, FocusListener {
private ZSheetState modal;
private int row;
private int col;
private String text;
private ZRect loc;
private Vector listeners = new Vector();
ZEditor() {
super(2);
addKeyListener(this);
addFocusListener(this);
}
/**
*
* @return
*/
public boolean isChanged() {
return !getText().equals(text);
}
/**
*
* @return
*/
public int getCol() {
return col;
}
/**
*
* @return
*/
public boolean isManagingFocus() {
return true; // hack
}
/**
*
* @param modal
*/
public void setModal(ZSheetState modal) {
this.modal = modal;
}
/**
*
* @return
*/
public ZSheetState getModal() {
return modal;
}
/**
*
* @return
*/
public int getRow() {
return row;
}
/**
*
* @return
*/
public void autoResize() {
Graphics g = this.getGraphics();
FontMetrics fm = g.getFontMetrics(g.getFont());
Dimension newValue = super.getSize();
newValue.width = Math.max(fm.stringWidth(getText()) + 15, loc.getWidth());
if(newValue.width > loc.getWidth() )
this.setSize(newValue);
}
/**
*
* @param lst
*/
public void addListener(ZEditorListener lst) {
listeners.add(lst);
}
/**
*
* @param evt
*/
public void focusGained(FocusEvent evt) {
}
/**
*
* @param evt
*/
public void focusLost(FocusEvent evt) {
endEdit();
}
/**
*
* @param row
* @param col
* @param text
* @param loc
*/
public void init(int row, int col, String text, ZRect loc) {
this.row = row;
this.col = col;
this.text = text;
this.setText(text);
this.loc = loc;
}
/**
*
* @param e
*/
public void keyPressed(KeyEvent e) {
switch (e.getKeyCode()) {
case KeyEvent.VK_LEFT:
case KeyEvent.VK_RIGHT:
case KeyEvent.VK_UP:
case KeyEvent.VK_DOWN:
if (text == "") {
e.consume();
getParent().requestFocus();
sendKey(getParent(), e.getKeyCode(), e.getKeyChar());
}
break;
case KeyEvent.VK_TAB:
case KeyEvent.VK_ENTER:
e.consume();
getParent().requestFocus();
sendKey(getParent(), e.getKeyCode(), e.getKeyChar());
break;
case KeyEvent.VK_ESCAPE:
text = getText();
getParent().requestFocus();
break;
}
autoResize();
}
/**
*
* @param e
*/
public void keyReleased(KeyEvent e) {
}
/**
*
* @param e
*/
public void keyTyped(KeyEvent e) {
}
/**
*
* @param lst
*/
public void removeListener(ZEditorListener lst) {
listeners.remove(lst);
}
/**
*
* @param comp
* @param vk_code
* @param keyChar
*/
public static void sendKey(Component comp, int vk_code, char keyChar) {
try {
comp.dispatchEvent(new KeyEvent(comp, KeyEvent.KEY_PRESSED, System.currentTimeMillis(), 0, vk_code, keyChar));
Thread.sleep(1);
comp.dispatchEvent(new KeyEvent(comp, KeyEvent.KEY_RELEASED, System.currentTimeMillis(), 0, vk_code, keyChar));
Thread.sleep(1);
} catch (InterruptedException ex) {
}
}
/**
*
* @param comp
*/
public void showIn(JComponent comp) {
if (this.getParent() != comp) {
comp.add(this);
}
setBounds(loc.left, loc.top, loc.getWidth(), loc.getHeight());
setVisible(true);
requestFocus();
}
/**
*/
private void endEdit() {
for (int i = 0; i < listeners.size(); i++) {
ZEditorListener lst = (ZEditorListener) listeners.get(i);
lst.editFinished(this);
}
}
}
/**
* DOCUMENT ME!
*
* @version $Revision$
* @author $author$
*/
interface ZEditorListener {
/**
*
* @param editor
*/
public void editFinished(ZEditor editor);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -