📄 highlighttextpane.java
字号:
style = doc.getStyle("Types");
for (int i = 0; i < typeList.size(); i++) {
dcl = (String) typeList.get(i);
dcl = "\\b" + dcl + "\\b";
highlightDoc(dcl, style);
}
setCaretPosition(0);
}
public void showJava(String idl) {
Style style = doc.getStyle("NormalJava");
print(idl, style);
style = doc.getStyle("Comments");
String dcl = "(//.*?$)|(/\\*.*?\\*/)";
highlightDoc(dcl, style);
style = doc.getStyle("Numbers");
dcl = "\\b" + "\\d+" + "\\b";
highlightDoc(dcl, style);
// style = doc.getStyle("KeyWords");
// for (int i = 0; i < kwList.size(); i++) {
// dcl = (String) kwList.get(i);
// dcl = "\\b" + dcl + "\\b";
// highlightDoc(dcl, style);
// }
style = doc.getStyle("KeyWords");
for (int i = 0; i < javaKwList.size(); i++) {
dcl = (String) javaKwList.get(i);
dcl = "\\b" + dcl + "\\b";
highlightDoc(dcl, style);
}
int pos = idl.indexOf(" // ------------------End----------------------");
if (pos < 1)
pos = 1;
setCaretPosition(pos - 1);
setCaretColor(Color.RED);
getCaret().setVisible(true);
setTabs(this, 25);
requestFocus();
}
public void updateJava(int start, int size, boolean isComm) {
int i_s = start;
int i_e = start + size;
try {
if (isComm) {
int ii = doc.getText(0, start).lastIndexOf("/*");
if (ii >= 0) {
// update start value
i_s = ii;
}
int jj = doc.getText(i_e, doc.getLength() - i_e).indexOf("*/");
if (jj >= 0) {
i_e = i_e + jj + 2;
}
}
} catch (Exception ex) {
// ex.printStackTrace();
LOG.severe(ex.getMessage());
}
// update the start & size
start = i_s;
size = i_e - start;
// System.out.println("i_s = " + i_s + " i_e = " + i_e + "
// start = " + start + " size = "
// + size);
// System.out.println("----Second---B--");
// try {
// System.out.println(doc.getText(start, size));
// } catch (BadLocationException e) {
// // TODO Auto-generated catch block
// e.printStackTrace();
// }
// System.out.println("----Second---E--");
// resume to normal text
Style style = doc.getStyle("NormalJava");
doc.setCharacterAttributes(start, size, style, false);
// doc.setCharacterAttributes(0, doc.getLength(), style,
// false);
style = doc.getStyle("Comments");
String dcl = "(//.*?$)|(/\\*.*?\\*/)";
highlightDoc(dcl, style, start, size);
// highlightDoc(dcl, style);
style = doc.getStyle("Numbers");
dcl = "\\b" + "\\d+" + "\\b";
highlightDoc(dcl, style, start, size);
// highlightDoc(dcl, style);
style = doc.getStyle("KeyWords");
for (int i = 0; i < javaKwList.size(); i++) {
dcl = (String) javaKwList.get(i);
dcl = "\\b" + dcl + "\\b";
highlightDoc(dcl, style, start, size);
// highlightDoc(dcl, style);
}
}
private void highlightDoc(String patter_dcl, Style style) {
try {
Pattern p = Pattern.compile(patter_dcl, Pattern.DOTALL + Pattern.MULTILINE);
Matcher m = p.matcher(doc.getText(0, doc.getLength()));
while (m.find()) {
doc.setCharacterAttributes(m.start(), m.end() - m.start(), style, false);
}
} catch (Exception e) {
LOG.severe(e.getMessage());
}
}
private void highlightDoc(String patter_dcl, Style style, int pos, int len) {
try {
Pattern p = Pattern.compile(patter_dcl, Pattern.DOTALL + Pattern.MULTILINE);
Matcher m = p.matcher(doc.getText(pos, len));
while (m.find()) {
doc.setCharacterAttributes(pos + m.start(), m.end() - m.start(), style, false);
}
} catch (Exception e) {
LOG.severe(e.getMessage());
}
}
public void print(String input) {
// Append to document.
try {
doc.remove(0, doc.getLength());
doc.insertString(0, input, null);
} catch (Exception e) {
LOG.severe(e.getMessage());
}
}
public void print(String input, Style style) {
// Append to document.
try {
doc.remove(0, doc.getLength());
doc.insertString(0, input, style);
} catch (Exception e) {
LOG.severe(e.getMessage());
}
}
public void append(String input) {
// Append to document.
try {
doc.insertString(doc.getLength(), input, null);
setCaretPosition(doc.getLength());
} catch (Exception e) {
LOG.severe(e.getMessage());
}
}
public synchronized void appendln(String input) {
// Append to document.
try {
checkSize();
doc.insertString(doc.getLength(), input + "\n", doc.getStyle("Normal"));
setCaretPosition(doc.getLength());
} catch (Exception e) {
LOG.severe(e.getMessage());
}
}
public synchronized void appendln(String input, String s) {
// Append to document.
try {
checkSize();
int pos = doc.getLength();
doc.insertString(pos, input + "\n", doc.getStyle(s));
// doc.setCharacterAttributes(pos, input.length(),
// doc.getStyle(s),
// false);
setCaretPosition(doc.getLength());
} catch (Exception e) {
LOG.severe(e.getMessage());
}
}
public void print(String input, String style) {
// Append to document.
try {
doc.remove(0, doc.getLength());
doc.insertString(0, input, doc.getStyle(style));
} catch (Exception e) {
LOG.severe(e.getMessage());
}
}
public void append(String input, String s) {
// Append to document.
try {
checkSize();
doc.insertString(doc.getLength(), input, doc.getStyle(s));
} catch (Exception e) {
LOG.severe(e.getMessage());
}
}
private void checkSize() {
try {
while (doc.getLength() > 2000000) {
doc.remove(0, 700000);
}
} catch (Exception e) {
LOG.severe(e.getMessage());
}
}
public void clear() {
try {
doc.remove(0, doc.getLength());
} catch (Exception e) {
LOG.severe(e.getMessage());
}
}
public void setTabs(JTextPane textPane, int tabWidth) {
// FontMetrics fm =
// textPane.getFontMetrics(textPane.getFont());
// int charWidth = fm.charWidth('w');
// int tabWidth = charWidth * charactersPerTab;
TabStop[] tabs = new TabStop[10];
for (int j = 0; j < tabs.length; j++) {
int tab = j + 1;
tabs[j] = new TabStop(tab * tabWidth);
}
TabSet tabSet = new TabSet(tabs);
SimpleAttributeSet attributes = new SimpleAttributeSet();
StyleConstants.setTabSet(attributes, tabSet);
int length = textPane.getDocument().getLength();
textPane.getStyledDocument().setParagraphAttributes(0, length, attributes, false);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -