📄 minieditor.java
字号:
dsMatchCase = new Checkbox("Match Case");
ds.getContentPane().add(dsLoop);
ds.getContentPane().add(dsMatchCase);
ds.getContentPane().add(dsMessage1);
ds.getContentPane().add(dsMessage2);
ds.setLocation(120, 120);
ds.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
dispose();
FindStartPos = 0;
}
});
ds.setSize(250, 110);
ds.setVisible(true);
} else if (ae.getSource() == bs) {
int a = 0, b = 0;
String str1, str2, str3, str4, strA, strB;
str1 = ta.getText();
str2 = str1.toLowerCase();
str3 = tfs.getText();
str4 = str3.toLowerCase();
if (dsMatchCase.getState()) {
strA = str1;
strB = str3;
} else {
strA = str2;
strB = str4;
}
a = strA.indexOf(strB, FindStartPos);
if (a > -1) {
ta.setCaretPosition(a);
b = tfs.getText().length();
ta.select(a, a + b);
FindStartPos = a + b;
foundCount++;
dsMessage2.setText(foundCount + "");
} else {
if (dsLoop.getState()) {
JOptionPane.showMessageDialog(null, "End of file.",
"Find result", 1);
FindStartPos = 0;
} else {
JOptionPane.showMessageDialog(null, "End of file.",
"Find result", 1);
}
foundCount = 0;
}
} else if (ae.getSource() == menuEditReplace) {
JDialog dr = new JDialog(this, "Replace", true);
dr.getContentPane().setLayout(new GridBagLayout());
GridBagConstraints gbc = new GridBagConstraints();
gbc.gridx = 0;
gbc.gridy = 0;
gbc.gridwidth = 2;
gbc.gridheight = 1;
// gbc.fill = gbc.HORIZONTAL; gbc.anchor = gbc.CENTER;
Panel p1 = new Panel();
dr.getContentPane().add(p1);
// drMatchCase = new Checkbox("Match Case");
// gbc.gridx = 1;
// gbc.gridy = 1;
// gbc.gridwidth = 1;
// gbc.gridheight =1;
// dr.getContentPane().add(drMatchCase);
gbc.gridx = 2;
gbc.gridy = 0;
gbc.gridwidth = 1;
gbc.gridheight = 1;
// gbc.fill = gbc.NONE; gbc.anchor = gbc.EAST;
Panel p2 = new Panel();
p1.setLayout(new GridLayout(5, 1));
p2.setLayout(new GridLayout(4, 1));
dr.getContentPane().add(p2);
JLabel drMessage1 = new JLabel("Replace: ");
JLabel drMessage2 = new JLabel("With: ");
drMatchCase = new Checkbox("Match Case");
tfro = new TextField(15);
tfrn = new TextField(15);
p1.add(drMessage1);
p1.add(tfro);
p1.add(drMessage2);
p1.add(tfrn);
p1.add(drMatchCase);
brf = new Button("Find");
// brf.addActionListener(this);
brf.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent brfe) {
int a = 0, b = 0;
String str1, str2, str3, str4, strA, strB;
str1 = ta.getText();
str2 = str1.toLowerCase();
str3 = tfro.getText();
str4 = str3.toLowerCase();
if (drMatchCase.getState()) {
strA = str1;
strB = str3;
} else {
strA = str2;
strB = str4;
}
a = strA.indexOf(strB, FindStartPos);
if (a > -1) {
ta.setCaretPosition(a);
b = tfro.getText().length();
ta.select(a, a + b);
FindStartPos = a + b;
foundCount++;
} else {
JOptionPane.showMessageDialog(null, "End of file.",
"Result", 1);
foundCount = 0;
}
}
});
brr = new Button("Replace");
brr.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent brre) {
if (tfrn.getText().length() == 0
&& ta.getSelectedText() != null)
ta.replaceSelection("");
if (tfrn.getText().length() > 0
&& ta.getSelectedText() != null)
ta.replaceSelection(tfrn.getText());
int a = 0, b = 0;
String str1, str2, str3, str4, strA, strB;
str1 = ta.getText();
str2 = str1.toLowerCase();
str3 = tfro.getText();
str4 = str3.toLowerCase();
if (drMatchCase.getState()) {
strA = str1;
strB = str3;
} else {
strA = str2;
strB = str4;
}
a = strA.indexOf(strB, FindStartPos);
if (a > -1) {
ta.setCaretPosition(a);
b = tfro.getText().length();
ta.select(a, a + b);
FindStartPos = a + b;
foundCount++;
} else {
JOptionPane.showMessageDialog(null, "End of file.",
"Result", 1);
foundCount = 0;
}
}
});
brra = new Button("Replace All");
brra.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent brrae) {
int a = 0;
while (a > -1) {
int b = 0;
String str1, str2, str3, str4, strA, strB;
str1 = ta.getText();
str2 = str1.toLowerCase();
str3 = tfro.getText();
str4 = str3.toLowerCase();
if (drMatchCase.getState()) {
strA = str1;
strB = str3;
} else {
strA = str2;
strB = str4;
}
a = strA.indexOf(strB, FindStartPos);
if (a > -1) {
ta.setCaretPosition(a);
b = tfro.getText().length();
ta.select(a, a + b);
FindStartPos = a + b;
foundCount++;
} else {
JOptionPane.showMessageDialog(null, "End of file.",
"Result", 1);
foundCount = 0;
}
if (tfrn.getText().length() == 0
&& ta.getSelectedText() != null)
ta.replaceSelection("");
if (tfrn.getText().length() > 0
&& ta.getSelectedText() != null)
ta.replaceSelection(tfrn.getText());
}
}
});
Button brc = new Button("Cancel");
brc.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent brce) {
dispose();
}
});
p2.add(brf);
p2.add(brr);
p2.add(brra);
p2.add(brc);
dr.setResizable(false);
dr.setLocation(120, 120);
dr.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
dispose();
FindStartPos = 0;
}
});
dr.setSize(220, 138);
dr.setVisible(true);
} else if (ae.getSource() == menuEditGoTo) {
JDialog dg = new JDialog(this, "Line Number", true);
dg.getContentPane().setLayout(new FlowLayout());
final JTextField dgtf = new JTextField(4);
Button dgOk = new Button("Go To");
dgOk.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent brce) {
int totalLine = ta.getLineCount();
int[] lineNumber = new int[totalLine + 1];
String s = ta.getText();
int pos = 0, t = 0;
while (true) {
pos = s.indexOf('\12', pos);
if (pos == -1)
break;
lineNumber[t++] = pos++;
}
int gt = 1;
try {
gt = Integer.parseInt(dgtf.getText());
} catch (NumberFormatException efe) {
JOptionPane.showMessageDialog(null,
"Please enter an integer.", "Input", 1);
}
if (gt < 2 || gt >= totalLine) {
if (gt < 2)
ta.setCaretPosition(0);
else
ta.setCaretPosition(s.length());
} else
ta.setCaretPosition(lineNumber[gt - 2] + 1);
dispose();
}
});
Button dgCancel = new Button("Cancel");
dgCancel.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent brce) {
dispose();
}
});
dg.getContentPane().add(dgtf);
dg.getContentPane().add(dgOk);
dg.getContentPane().add(dgCancel);
dg.setResizable(false);
dg.setLocation(120, 120);
dg.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
dispose();
}
});
dg.setSize(180, 60);
dg.setVisible(true);
} else if (ae.getSource() == menuEditSelectAll) {
ta.selectAll();
} else if (ae.getSource() == menuViewFont) {
MenuFont mf = new MenuFont(this, true);
ta.setFont(mf.myLayout(ta.getFont()));
} else if (ae.getSource() == menuViewColor) {
MenuColor mc = new MenuColor(this, true);
Color[] fbgc = new Color[2];
fbgc = mc.myLayout(ta.getForeground(), ta.getBackground());
ta.setForeground(fbgc[0]);
ta.setBackground(fbgc[1]);
ta.setCaretColor(fbgc[0]);
} else if (ae.getSource() == menuViewClassic) {
if (menuViewClassic.getState()) {
ta.setForeground(new Color(0, 255, 0));
ta.setBackground(new Color(45, 0, 45));
ta.setFont(new Font("Serif", Font.BOLD, 16));
ta.setCaretColor(new Color(0, 255, 0));
} else {
ta.setForeground(defaultForeground);
ta.setBackground(defaultBackground);
ta.setFont(defaultFont);
ta.setCaretColor(defaultCaretColor);
}
} else if (ae.getSource() == menuViewStatus) {
if (menuViewStatus.getState()) {
showStatus();
}
} else if (ae.getSource() == menuViewWordWrap) {
if (menuViewWordWrap.getState()) {
ta.setLineWrap(true);
} else {
ta.setLineWrap(false);
}
} else if (ae.getSource() == menuViewDoubleSpace) {
int pos = 0, t = 0;
String str = ta.getText();
while (true) {
pos = str.indexOf('\15', pos);
if (pos == -1)
break;
str = str.substring(0, pos) + '\15' + '\12'
+ str.substring(pos);
pos = pos + 3;
}
ta.setText(str);
} else if (ae.getSource() == menuHelpAbout) {
dl = new JDialog(this, "About MiniEditor", true);
dl.getContentPane().setLayout(new GridLayout(3, 3));
dl.setBackground(new Color(212, 208, 200));
Button bOk = new Button("OK");
bOk.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent bOke) {
dispose();
}
});
Label ver = new Label("Version .9");
Label null1 = new Label();
Label null2 = new Label();
Label null3 = new Label();
Label null4 = new Label();
Label null5 = new Label();
Label null6 = new Label();
Label null7 = new Label();
dl.getContentPane().add(null1);
dl.getContentPane().add(ver);
dl.getContentPane().add(null2);
dl.getContentPane().add(null3);
dl.getContentPane().add(bOk);
dl.getContentPane().add(null4);
dl.getContentPane().add(null5);
dl.getContentPane().add(null6);
dl.getContentPane().add(null7);
bOk.addActionListener(this);
dl.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
dispose();
}
});
dl.setLocation(120, 120);
dl.setResizable(false);
dl.setSize(200, 80);
dl.setVisible(true);
}
}// end of ActionListener
// DocumentListener
public void removeUpdate(DocumentEvent e) {
String s;
s = fileStatus.getText();
if (!s.endsWith("*") & beginTextListener & !isNewFile) {
fileStatus.setText("*");
}
menuEditUndo.setEnabled(true);
}
public void insertUpdate(DocumentEvent e) {
String s;
s = fileStatus.getText();
if (!s.endsWith("*") & beginTextListener & !isNewFile) {
fileStatus.setText("*");
}
menuEditUndo.setEnabled(true);
}
public void changedUpdate(DocumentEvent e) {
String s;
s = fileStatus.getText();
if (!s.endsWith("*") & beginTextListener & !isNewFile) {
fileStatus.setText("*");
}
menuEditUndo.setEnabled(true);
}
// end of DocumentListener
// Caretlistener
public void caretUpdate(CaretEvent e) {
if (menuViewStatus.getState())
showStatus();
}// end of Caretlistener
// KeyListener
public void keyPressed(KeyEvent e) {
if (e.getKeyCode() == '\10') {
BACKSPACE = true;
}
// if(menuViewStatus.getState())
// showStatus();
}
public void keyReleased(KeyEvent e) {
if (e.getKeyCode() == 155) { // ESCAPE = 155
if (INSERTMODE)
INSERTMODE = false;
else
INSERTMODE = true;
}
if (menuViewStatus.getState())
showStatus();
}
public void keyTyped(KeyEvent e) {
beginTextListener = true;
isNewFile = false;
if (!BACKSPACE) {
if (!INSERTMODE) {
int pos = ta.getCaretPosition();
char c = ta.getText().charAt(pos);
if (c == '\12') {
} else if (c == '\15') {
} else {
ta.replaceRange("", pos, pos + 1);
}
}
}
BACKSPACE = false;
}// end of KeyListener
void showStatus() {
int rows, cols, from, current, to, fileSize;
rows = cols = from = current = 0;
to = ta.getCaretPosition();
fileSize = 0;
String str = ta.getText();
cols = to - str.substring(0, to).lastIndexOf(10);
fileSize = str.length();
String mode;
if (INSERTMODE) {
mode = "INSERT";
} else {
mode = "OVERLAY";
}
try {
rows = ta.getLineOfOffset(to) + 1;
} catch (BadLocationException ble) {
}
statusRow.setText("row: " + rows);
statusCol.setText("col: " + cols);
statusMode.setText("mode: " + mode);
statusSize.setText("size: " + fileSize);
// fileStatus.setText("file status: ");
}
int wordLocation(String str, int pos, boolean isToRight) {
char c;
if (isToRight) {
c = str.charAt(pos);
while (true) {
if (c < 48)
break;
else if (c > 57 & c < 65)
break;
else if (c > 90 & c < 97)
break;
else if (c > 122)
break;
pos++;
c = str.charAt(pos);
}
return pos--;
} else {
pos--;
c = str.charAt(pos);
while (true) {
if (c < 48)
break;
else if (c > 57 & c < 65)
break;
else if (c > 90 & c < 97)
break;
else if (c > 122)
break;
pos--;
c = str.charAt(pos);
}
return pos++;
}
}
class UndoHandler implements UndoableEditListener {
public void undoableEditHappened(UndoableEditEvent uee) {
undo.addEdit(uee.getEdit());
}
}
}// end of class MiniEditor
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -