📄 syntaxdocument.java
字号:
for (int x = 0; x < getTabSize(); x++) { tab = tab + " "; } while (st.hasMoreTokens()) { String token = st.nextToken(); if (token.indexOf("\t") == 0) modifiedString = modifiedString + tab; else modifiedString = modifiedString + token; } return modifiedString; } /** * Loads a file and assigns the file text to the document. * @param String fileName - the name of the file. */ public String LoadDocument(String fileName) { String fileData = ""; try { // Read data FileInputStream fis = new FileInputStream (fileName); int size = fis.available(); byte[] bytes = new byte [size]; fis.read (bytes); fileData = new String (bytes); fis.close (); } catch (IOException e) {} if (!fileData.equals("")) { // remove tabs and replace them with spaces String tab = ""; // Create a standard tab from spaces for (int x = 0; x < tabSize; x++) tab = tab + " "; String data = ""; // Parse the document by line StringTokenizer st = new StringTokenizer(fileData, "\n", true); while (st.hasMoreTokens()) { String str = st.nextToken(); // and now by tabs per line StringTokenizer tabs = new StringTokenizer(str, "\t"); while (tabs.hasMoreTokens()) { String noTabs = tabs.nextToken(); String check = str.substring(0, str.indexOf(noTabs)); String theTab = ""; for (int x = 0; x < check.length(); x ++) theTab = theTab + tab; data = data + theTab + noTabs; } } fileData = data; } return fileData; } /** * Converts the spaces in the document to tabs. A certain amount of * white space in the document is considered to be the size of a tab, such as * three or four consecutive space characters. This routine replaces that that * with the actual tab character. * @return String text - text with proper tabs. */ public String ConvertToTabs() { String fileData = thePane.getText(); // Convert the spaces into tabs String tab = ""; // Create a standard tab from spaces for (int x = 0; x < tabSize; x++) tab = tab + " "; String data = ""; // Parse the document by line StringTokenizer st = new StringTokenizer(fileData, "\n", true); while (st.hasMoreTokens()) { String str = st.nextToken(); int foundtab = 0; int pos = 0; while (foundtab > -1) { foundtab = str.indexOf(tab, pos); if (foundtab > -1) { String tmpstr = str.substring(0, foundtab); str = str.substring(foundtab + tab.length()); str = tmpstr + "\t" + str; pos = foundtab + 1; } } data = data + str; } fileData = data; return fileData; } /** * Converts the spaces in the document to tabs. A certain amount of * white space in the document is considered to be the size of a tab, such as * three or four consecutive space characters. This routine replaces that that * with the actual tab character. * @param String text - the text to parse. * @param int size - tab size. * @return String text - the text with proper tabs. */ private String ConvertToTabs(String text, int size) { String fileData = text; // Convert the spaces into tabs String tab = ""; // Create a standard tab from spaces for (int x = 0; x < size; x++) tab = tab + " "; String data = ""; // Parse the document by line StringTokenizer st = new StringTokenizer(fileData, "\n", true); while (st.hasMoreTokens()) { String str = st.nextToken(); int foundtab = 0; int pos = 0; while (foundtab > -1) { foundtab = str.indexOf(tab, pos); if (foundtab > -1) { String tmpstr = str.substring(0, foundtab); str = str.substring(foundtab + tab.length()); str = tmpstr + "\t" + str; pos = foundtab + 1; } } data = data + str; } fileData = data; return fileData; } /** * Save the document. Converts the spaces in to tabs before saving. * @param String fileName - the name of the file to write too. */ public void SaveDocument(String fileName) { String fileData = thePane.getText(); // Convert the spaces into tabs String tab = ""; // Create a standard tab from spaces for (int x = 0; x < tabSize; x++) tab = tab + " "; String data = ""; // Parse the document by line StringTokenizer st = new StringTokenizer(fileData, "\n", true); while (st.hasMoreTokens()) { String str = st.nextToken(); int foundtab = 0; int pos = 0; while (foundtab > -1) { foundtab = str.indexOf(tab, pos); if (foundtab > -1) { String tmpstr = str.substring(0, foundtab); str = str.substring(foundtab + tab.length()); str = tmpstr + "\t" + str; pos = foundtab + 1; } } data = data + str; } fileData = data; // Save the file try { FileOutputStream DataOut = new FileOutputStream(fileName); DataOut.write(fileData.getBytes()); DataOut.close(); } catch (IOException e) {} } /** * Save the document. Converts the spaces in to tabs before saving. * @param WAPFiles f) */ public void SaveDocument(WAPFiles f) { String fileData = f.getFileText(); // Convert the spaces into tabs String tab = ""; // Create a standard tab from spaces for (int x = 0; x < tabSize; x++) tab = tab + " "; String data = ""; // Parse the document by line StringTokenizer st = new StringTokenizer(fileData, "\n", true); while (st.hasMoreTokens()) { String str = st.nextToken(); int foundtab = 0; int pos = 0; while (foundtab > -1) { foundtab = str.indexOf(tab, pos); if (foundtab > -1) { String tmpstr = str.substring(0, foundtab); str = str.substring(foundtab + tab.length()); str = tmpstr + "\t" + str; pos = foundtab + 1; } } data = data + str; } fileData = data; // Save the file try { String fileName = f.getFilePath() + f.getFileName(); // Save the data FileOutputStream DataOut = new FileOutputStream(fileName); DataOut.write(fileData.getBytes()); DataOut.close(); } catch (IOException e) {} } /** * Gets the tab size used by the document. * @return int size. */ public int getTabSize() { return tabSize; } /** * Changes the KeywordFinder to reflect the type of file and version of the dtd. */ public KeywordFinder changeKeywordFinder() { String txt = ""; try { txt = this.getText(0, this.getLength() - 1); } catch (BadLocationException indxerr) {} String cfname = getConversionFileName(txt); if (cfname.length() > 0) wordFinder = new KeywordFinder(cfname); else wordFinder = null; return wordFinder; } /** * Gets the status of the insert button. * @return boolean status - true if insert enabled. */ public boolean getInsertStatus() { return insertStatus; } /** * Sets the insert status. * @param boolean status - set too true to enable insert. */ public void setInsertStatus(boolean status) { insertStatus = status; } /** * Sets the color and type of caret based on the insert status of the document. */ public void setCaret() { int pos = thePane.getCaretPosition(); if (insertStatus) { DefaultCaret dc = new DefaultCaret(); dc.setBlinkRate(500); thePane.setCaret(dc); } else { OverwriteCaret oc = new OverwriteCaret(); oc.setBlinkRate(500); thePane.setCaret(oc); } thePane.setCaretPosition(pos); thePane.repaint(); } /** * Gets the state of the document (modified or not). * @return boolean state. */ public boolean getDocStatus() { return modified; } /** * Sets the state of the document (modified or not). * @param boolean state. */ public void setDocStatus(boolean status) { modified = status; } /** * Gets the attribute vector used by the document. * @return Vector attribVector. */ public Vector getAttribVector() { return attribVector; } /** Inner class - Listens for input and allows processing of the input once * it is entered into the document. Left this class just in case there * might be some use for it in the future. */ class SyntaxDocListener implements DocumentListener { /** Constructor - creates a new instance of DocumentListener.*/ public SyntaxDocListener() { super(); } /** A custom implementation of changedUpdate * param DocumentEvent e */ public void changedUpdate(DocumentEvent e) { modified = true; } /** A custom implementation of insertUpdate * param DocumentEvent e */ public void insertUpdate(DocumentEvent e) { } /** A custom implementation of removeUpdate * param DocumentEvent e */ public void removeUpdate(DocumentEvent e) { } } /** Inner class - used for custom carets. */ class OverwriteCaret extends DefaultCaret { /** draw the caret */ public void paint(Graphics g) { if (!isVisible()) return; try { JTextComponent c = getComponent(); int dot = getDot(); Rectangle r = c.modelToView(dot); g.setColor(c.getCaretColor()); g.fillRect(r.x, r.y, 7, r.height - 1); String str = thePane.getText().substring(thePane.getCaretPosition(), thePane.getCaretPosition() + 1); if (str.equals("\n")) str = ""; g.setColor(thePane.getBackground()); g.drawString(str, r.x, r.y + r.height - 2); } catch (BadLocationException e) {} catch (StringIndexOutOfBoundsException strerr) {} } /** specify the size of the caret for redrawing * and do repaint() -- this is called when the * caret moves. */ protected synchronized void damage(Rectangle r) { if (r == null) return; x = r.x; y = r.y; width = 7; height = r.height - 1; repaint(); } } /** * Inner class - document specific undo listener. */ protected class DocUndoListener implements UndoableEditListener { int ActiveFile = -1; /** * Sets the active file to ling undo manager too. * @param int a - the active file number. */ public void setActiveFile(int a) { ActiveFile = a; } /** * Overridden for custom functionality. */ public void undoableEditHappened(UndoableEditEvent e) { if (ActiveFile > -1) { String edit = e.getEdit().toString(); if (edit.indexOf("InsertUndo") > -1) { try { undoManager.addEdit(e.getEdit()); } catch (IndexOutOfBoundsException be) { // set statusbar message } } } } }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -