⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 syntaxdocument.java

📁 WAP ide 代码
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
package wapide;import javax.swing.*;import javax.swing.text.*;import javax.swing.event.*;import javax.swing.undo.*;import java.awt.*;import java.lang.*;import java.util.*;import java.io.*;/* * This class defines a custom document which is used by the IDE frame class. * It has built-in support for syntax highlighting. * Copyright (c) 2003 * @author Mark Busman * @version 1.0 * For License and contact information see WAPIDE.java */public class SyntaxDocument extends DefaultStyledDocument {  /**   * The JTextPane used as the text editor.   */  private JTextPane thePane;  /**   * The KeywordFinder used by the document.   */  private KeywordFinder wordFinder;  /**   * Attribute Style used by document for syntax highlighting.   */  private MutableAttributeSet style0;  /**   * Attribute Style used by document for syntax highlighting.   */  private MutableAttributeSet style1;  /**   * Attribute Style used by document for syntax highlighting.   */  private MutableAttributeSet style2;  /**   * Attribute Style used by document for syntax highlighting.   */  private MutableAttributeSet style3;  /**   * Attribute Style used by document for syntax highlighting.   */  private MutableAttributeSet style4;  /**   * Attribute Style used by document for syntax highlighting.   */  private MutableAttributeSet style5;  /**   * Document settings.   */  private Vector settings;  /**   * Tab size used by document, default is 2.  Tabs in the document are made up   * of spaces.   */  private int tabSize = 2;  /**   * Syntax highlighting status.   */  private boolean highlight = true;  /**   * Smart Indent status.   */  private boolean smartIndent = true;  /**   * Insert status.   */  private boolean insertStatus = true;  /**   * Doc Status.   */  private boolean modified = false;  /**   * Attribs used by document's syntax highlighting routines.   */  private Vector attribVector = new Vector();  /**   * The document specific UndoManager.   */  protected UndoManager undoManager = new UndoManager();  /**   * The listerner for undo/redo events.   */  private DocUndoListener undoListener = new DocUndoListener();  /**   * Empty constructor.   */  public SyntaxDocument() {  }  /** Constructor - creates a new Syntax Document.   *  @param JTextPane pane - the TextPane this class is used with.   *  @param int docType - the type of document created and being worked on,   *  (1) wml, (2) si, (3) sl.   */  public SyntaxDocument(JTextPane pane, int docType, Vector Settings) {    super();    settings = Settings;    thePane = pane;    this.addDocumentListener(new SyntaxDocListener());    this.addUndoableEditListener(undoListener);    wordFinder = new KeywordFinder(docType);  }  /** Overrided method @see Document.insertString(). Overriden to provide the   *  syntax highlighthing since this cannot be done through the document   *  listeners.   * @param int offset - the starting point of the string insertion.   * @param String str - the String to be inserted.   * @param AttributeSet a - the text formating attributes to apply, normally a predefined style.   */  public void insertString(int offs, String str, AttributeSet a) throws BadLocationException {    // Check to see if insert is on, and if not, remove a Character 1st    // before inserting the new character.    if (!insertStatus)      remove(offs, 1);    // Check to see if a new line (\n) character is being entered    if (str == "\n") {      if (smartIndent) {        //find out how many tabs in the previous line        int tabs = checkPreviousTabs(offs);        String temp = "";        for (int y = 0; y < tabs; y++)          for (int x = 0; x < tabSize; x++)            temp = temp + " ";        str = "\n" + temp;      }    }    // Check to see if a tab is being inserted    if (str == "\t") { // if so replace the tab with the appropriate amount of spaces      String temp = "";      for (int x = 0; x < tabSize; x++)        temp = temp + " ";      str = temp;    }    // insert the text into the document    super.insertString(offs, str, a);    // highlight text accordingly    if (highlight)      if (!(wordFinder == null))        checkWord();    else {      checkWord(offs + 1);    }  }  /** Overrided method @see Document.removeString(). Overriden to provide the   *  syntax highlighthing since this cannot be done through the document   *  listeners.   * @param int offset - the starting point of the string insertion.   * @param String len - the length of the String to be inserted.   */  public void remove(int offs, int len) throws BadLocationException {    // check to see if a tab is being deleted    int pos = offs;    try {      String compare = thePane.getText().substring(pos, pos + len);      if (compare.equals(" ")) {        String TabString = "";        for (int i = 0; i < tabSize; i++)          TabString = TabString + " ";        int startpos = offs - tabSize + 1;        pos++;        compare = thePane.getText().substring(startpos, pos);        if (compare.equals(TabString)) {          // is it at the end of the tab          if (offs == (startpos + tabSize - 1)) { // then erase the whole tab            offs = startpos; // and adjust the cursor position accordingly            len = tabSize;          }        }      }      // delete the text from the document      super.remove(offs, len);      // highlight text accordingly      if (highlight)        if (!(wordFinder == null))          checkWord();      else        checkWord(offs);    }    catch (StringIndexOutOfBoundsException strerr) {}  }  /**   * Checks the amount of tabs on the previous line(s).   * @param int offs - the offset in the document.   */  private int checkPreviousTabs(int offs) {    String theText = thePane.getText();    StringTokenizer st = new StringTokenizer(theText, "\n");    int totalLineCount = st.countTokens();    int previousLine = 0;    String [] tokenizedString = new String[totalLineCount];    int i = 0;    try {      // put the document into a string array, seperated by line      while (st.hasMoreTokens()) {        tokenizedString[i] = st.nextToken();        i++;      }      // check what line we are on now      int pos = -1;      while (pos < offs) {        pos = theText.indexOf("\n", pos + 1);        if (pos > -1)           previousLine++;        else          pos = offs;      }      String theLine = tokenizedString[previousLine];      // now count the tabs at the start of the line      String tab = "";      for (int x = 0; x < tabSize; x++)          tab = tab + " ";      st = new StringTokenizer(theLine, tab);      String temp = st.nextToken();      pos = theLine.indexOf(temp);      String theTabs = theLine.substring(0, pos);      int tabNumber = theTabs.length() / tabSize;      return tabNumber;    }    catch (ArrayIndexOutOfBoundsException e) {      return 0;    }    catch (NoSuchElementException el) {      return 0;    }  }  /**   * checks to see if the word is a special keyword and if so   * formats the text accordingly.   */  private void checkWord() {    this.setCharacterAttributes(0, thePane.getText().length(), style0, true);    Vector v = wordFinder.scanDocument(thePane.getText());    attribVector = v;    MutableAttributeSet style = new SimpleAttributeSet();    if (v.size() > 0) {      int loop = v.size() / 3;      for (int l = 0; l < loop; l++) {        Integer applyStyle = (Integer)v.get(l * 3 + 2);        switch (applyStyle.intValue()) {          case 0: // plain text            style = style0;            break;          case 1: // a wap tag            style = style1;            break;          case 2: // a wap attribute            style = style2;            break;          case 3: // a special tag            style = style3;            break;          case 4: // comment            style = style4;            break;          case 5: // string            style = style5;            break;        }        Integer start = (Integer)v.get(l * 3);        Integer end = (Integer)v.get(l * 3 + 1);        this.setCharacterAttributes(start.intValue(), end.intValue(), style, true);      }    }  }  /**   * Checks if the word is a keyword and formats text accordingly.\   * @param int offs - the offset in the document.   */  private void checkWord(int offs) {      this.setCharacterAttributes(0, thePane.getText().length(), style0, true);  }  /**   * Updates the settings for the document.   * @param Vector Settings.   */  public void UpdateSettings(Vector Settings) {    settings = Settings;    style0 = new SimpleAttributeSet();    StyleConstants.setForeground(style0, (Color) settings.get(5));    style1 = new SimpleAttributeSet();    StyleConstants.setForeground(style1, (Color) settings.get(9));    style2 = new SimpleAttributeSet();    StyleConstants.setForeground(style2, (Color) settings.get(8));    style3 = new SimpleAttributeSet();    StyleConstants.setForeground(style3, (Color) settings.get(3));    style4 = new SimpleAttributeSet();    StyleConstants.setForeground(style4, (Color) settings.get(2));    style5 = new SimpleAttributeSet();    StyleConstants.setForeground(style5, (Color) settings.get(6));    String temptabsize = (String) settings.get(10);    try {      int oldtabsize = tabSize;      tabSize = Integer.parseInt(temptabsize);      if (oldtabsize != tabSize)        AdjustTabs(oldtabsize);    }    catch (NumberFormatException e) {}    JCheckBox tempbox = (JCheckBox) settings.get(13);    highlight = tempbox.isSelected();    tempbox = (JCheckBox) settings.get(11);    smartIndent = tempbox.isSelected();    AdjustHighlight();  }  /**   * Adjsuts the tabs in the document if a change is made thru the   * Settings dialog of the IDEFrame class.   */  private void AdjustTabs(int oldsize) {    String tempPane = ConvertToTabs(thePane.getText(), oldsize);    thePane.setEnabled(false);    String tab = new String();    for (int i = 0; i < oldsize; i++)      tab = tab + " ";    String newTab = new String();    for (int i = 0; i < tabSize; i++)      newTab = newTab + " ";    String adjustedText = new String();    StringTokenizer st = new StringTokenizer(tempPane, "\t");    while (st.hasMoreTokens()) {        adjustedText = adjustedText + st.nextToken() + newTab;    }    try {      adjustedText = adjustedText.substring(0,adjustedText.length() - tabSize);    }    catch (StringIndexOutOfBoundsException e) { }    thePane.setEnabled(true);    thePane.setText(adjustedText);  }  /** A helper method to determine what file to call */  private String getConversionFileName(String data) {    String fname = "";    //locate !DOCTYPE    int startPos = data.indexOf("!DOCTYPE");    if (startPos > -1) {      int endPos = data.indexOf(">", startPos);      if (endPos > 0) {        String type = data.substring(startPos, endPos);        String loc = "http://www.wapforum.org/DTD/";        startPos = type.indexOf(loc);        if (startPos > -1) {          // need to change this to look for xml 1st and then dtd          endPos = type.indexOf(".xml", startPos + loc.length());          if (endPos > -1)            fname = type.substring(startPos + loc.length(), endPos);          else {            endPos = type.indexOf(".dtd", startPos + loc.length());            if (endPos > -1)              fname = type.substring(startPos + loc.length(), endPos);          }        }      }    }    return fname;  }  /**   * Adjusts the syntax highlighting feature, truns it either on or off   * based on settings made.   */  private void AdjustHighlight() {    if (highlight)      checkWord();    else      checkWord(0);  }  /**   * Sets the Active file for the UndoLister object.   * @param int a - the active file number.   */  public void setActiveFile(int a) {    undoListener.setActiveFile(a);  }  /**   * Undoes a text edit event.   */  public void undo() {    undoManager.undo();  }  /**   * Redoes a text edit event.   */  public void redo() {    undoManager.redo();  }  /**   * Resets the undo manager, removing all edits it stored.   */  public void resetUndoManager() {    undoManager.discardAllEdits();  }  /**   * Helper method for removing the \t (tab) character in documents   * that used it.  It reformats the document to use spaces.   */  public String removeTabChar(String s) {    StringTokenizer st = new StringTokenizer(s, "\t", true);    String tab = "";    String modifiedString = "";

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -