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

📄 programmereditordemo.java

📁 it is a basic java browser
💻 JAVA
📖 第 1 页 / 共 3 页
字号:
/* * A simple text editor that demonstrates the integration of the  * com.Ostermiller.Syntax Syntax Highlighting package with a text editor. * Copyright (C) 2001 Stephen Ostermiller  * http://ostermiller.org/contact.pl?regarding=Syntax+Highlighting * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the * GNU General Public License for more details. * * See COPYING.TXT for details. */ package com.Ostermiller.Syntax;import java.awt.*;import java.awt.event.*;import javax.swing.*;import javax.swing.text.*;import javax.swing.event.*;import java.io.*;import java.util.*;import com.Ostermiller.Syntax.Lexer.*;/** * A <a href="http://ostermiller.org/syntax/editor.html">demonstration text editor</a> * that uses syntax highlighting. */public class ProgrammerEditorDemo extends JFrame {    /**     * The place where the text is drawn.     */    protected JTextPane textPane;    /**     * the styled document that is the model for     * the textPane     */    protected HighLightedDocument document;    /**     * A reader wrapped around the document     * so that the document can be fed into     * the lexer.     */    protected DocumentReader documentReader;    /**     * The lexer that tells us what colors different     * words should be.     */    protected Lexer syntaxLexer;    /**     * A thread that handles the actual coloring.     */    protected Colorer colorer;    /**     * A lock for modifying the document, or for     * actions that depend on the document not being     * modified.     */    private Object doclock = new Object();    /**     * Create a new Demo     */    public ProgrammerEditorDemo() {        // initial set up that sets the title        super("Programmer's Editor Demonstration");        // Create the document model.        document = new HighLightedDocument();        // Create the text pane and configure it.        textPane = new JTextPane(document);        textPane.setCaretPosition(0);        textPane.setMargin(new Insets(5,5,5,5));        JScrollPane scrollPane = new JScrollPane(textPane);        // specify the initial size and location for the window.        scrollPane.setPreferredSize(new Dimension(620, 460));        setLocation(50, 50);        // Add the components to the frame.        JPanel contentPane = new JPanel(new BorderLayout());        contentPane.add(scrollPane, BorderLayout.CENTER);        setContentPane(contentPane);        // Set up the menu bar.        JMenu styleMenu = createStyleMenu();        JMenuBar mb = new JMenuBar();        mb.add(styleMenu);        setJMenuBar(mb);        // Make the window so that it can close the application        addWindowListener(new WindowAdapter() {            public void windowClosing(WindowEvent e) {                System.exit(0);            }            public void windowActivated(WindowEvent e) {                // focus magic                textPane.requestFocus();            }        });        // Start the thread that does the coloring        colorer = new Colorer();        colorer.start();        // Set up the hash table that contains the styles.        initStyles();        // create the new document.        documentReader = new DocumentReader(document);        // Put the initial text into the text pane and        // set it's initial coloring style.        initDocument();        // put it all together and show it.        pack();        setVisible(true);    }    /**     * Run the Syntax Highlighting as a separate thread.     * Things that need to be colored are messaged to the     * thread and put in a list.     */    private class Colorer extends Thread {        /**         * Keep a list of places in the file that it is safe to restart the         * highlighting.  This happens whenever the lexer reports that it has         * returned to its initial state.  Since this list needs to be sorted         * and we need to be able to retrieve ranges from it, it is stored in a         * balanced tree.         */        private TreeSet iniPositions = new TreeSet(new DocPositionComparator());        /**         * As we go through and remove invalid positions we will also be finding		 * new valid positions. 		 * Since the position list cannot be deleted from and written to at the same         * time, we will keep a list of the new positions and simply add it to the         * list of positions once all the old positions have been removed.         */    	private HashSet newPositions = new HashSet();        /**         * A simple wrapper representing something that needs to be colored.         * Placed into an object so that it can be stored in a Vector.         */        private class RecolorEvent {            public int position;            public int adjustment;            public RecolorEvent(int position, int adjustment){                this.position = position;                this.adjustment = adjustment;            }        }        /**         * Vector that stores the communication between the two threads.         */        private volatile Vector v = new Vector();        /**         * The amount of change that has occurred before the place in the         * document that we are currently highlighting (lastPosition).         */        private volatile int change = 0;        /**         * The last position colored         */        private volatile int lastPosition = -1;                private volatile boolean asleep = false;        /**         * When accessing the vector, we need to create a critical section.         * we will synchronize on this object to ensure that we don't get         * unsafe thread behavior.         */        private Object lock = new Object();        /**         * Tell the Syntax Highlighting thread to take another look at this         * section of the document.  It will process this as a FIFO.         * This method should be done inside a doclock.         */        public void color(int position, int adjustment){            // figure out if this adjustment effects the current run.            // if it does, then adjust the place in the document            // that gets highlighted.            if (position < lastPosition){                if (lastPosition < position - adjustment){                    change -= lastPosition - position;				} else {                	change += adjustment;                }            }            synchronized(lock){                v.add(new RecolorEvent(position, adjustment));                if (asleep){                    this.interrupt();                }            }        }        /**         * The colorer runs forever and may sleep for long         * periods of time.  It should be interrupted every         * time there is something for it to do.         */        public void run(){            int position = -1;            int adjustment = 0;            // if we just finish, we can't go to sleep until we            // ensure there is nothing else for us to do.            // use try again to keep track of this.            boolean tryAgain = false;            for (;;){  // forever                synchronized(lock){                    if (v.size() > 0){                        RecolorEvent re = (RecolorEvent)(v.elementAt(0));                        v.removeElementAt(0);                        position = re.position;                        adjustment = re.adjustment;                    } else {                        tryAgain = false;                        position = -1;                        adjustment = 0;                    }                }                if (position != -1){                    SortedSet workingSet;                    Iterator workingIt;                    DocPosition startRequest = new DocPosition(position);                    DocPosition endRequest = new DocPosition(position + ((adjustment>=0)?adjustment:-adjustment));                    DocPosition  dp;                    DocPosition dpStart = null;                    DocPosition dpEnd = null;                    // find the starting position.  We must start at least one                    // token before the current position                    try {                        // all the good positions before                        workingSet = iniPositions.headSet(startRequest);                        // the last of the stuff before                        dpStart = ((DocPosition)workingSet.last());                    } catch (NoSuchElementException x){                        // if there were no good positions before the requested start,                        // we can always start at the very beginning.                        dpStart = new DocPosition(0);                    }                    // if stuff was removed, take any removed positions off the list.                    if (adjustment < 0){                        workingSet = iniPositions.subSet(startRequest, endRequest);                        workingIt = workingSet.iterator();                        while (workingIt.hasNext()){                            workingIt.next();                            workingIt.remove();                        }                    }                    // adjust the positions of everything after the insertion/removal.                    workingSet = iniPositions.tailSet(startRequest);                    workingIt = workingSet.iterator();                    while (workingIt.hasNext()){                        ((DocPosition)workingIt.next()).adjustPosition(adjustment);                    }                    // now go through and highlight as much as needed                    workingSet = iniPositions.tailSet(dpStart);                    workingIt = workingSet.iterator();                    dp = null;                    if (workingIt.hasNext()){                        dp = (DocPosition)workingIt.next();                    }                    try {                        Token t;                        boolean done = false;                        dpEnd = dpStart;                        synchronized (doclock){                            // we are playing some games with the lexer for efficiency.                            // we could just create a new lexer each time here, but instead,                            // we will just reset it so that it thinks it is starting at the                            // beginning of the document but reporting a funny start position.                            // Reseting the lexer causes the close() method on the reader                            // to be called but because the close() method has no effect on the                            // DocumentReader, we can do this.                            syntaxLexer.reset(documentReader, 0, dpStart.getPosition(), 0);                            // After the lexer has been set up, scroll the reader so that it                            // is in the correct spot as well.                            documentReader.seek(dpStart.getPosition());                            // we will highlight tokens until we reach a good stopping place.                            // the first obvious stopping place is the end of the document.                            // the lexer will return null at the end of the document and wee                            // need to stop there.                            t = syntaxLexer.getNextToken();                        }                        newPositions.add(dpStart);                        while (!done && t != null){                            // this is the actual command that colors the stuff.                            // Color stuff with the description of the style matched                            // to the hash table that has been set up ahead of time.                            synchronized (doclock){                                if (t.getCharEnd() <= document.getLength()){                                    document.setCharacterAttributes(                                        t.getCharBegin() + change,                                        t.getCharEnd()-t.getCharBegin(),                                        getStyle(t.getDescription()),                                        true                                    );                                                                                                      // record the position of the last bit of text that we colored                                    dpEnd = new DocPosition(t.getCharEnd());                                }                                lastPosition = (t.getCharEnd() + change);                            }                            // The other more complicated reason for doing no more highlighting                            // is that all the colors are the same from here on out anyway.                            // We can detect this by seeing if the place that the lexer returned                            // to the initial state last time we highlighted is the same as the                            // place that returned to the initial state this time.                            // As long as that place is after the last changed text, everything                            // from there on is fine already.                            if (t.getState() == Token.INITIAL_STATE){                                //System.out.println(t);                                // look at all the positions from last time that are less than or                                // equal to the current position                                while (dp != null && dp.getPosition() <= t.getCharEnd()){                                    if (dp.getPosition() == t.getCharEnd() && dp.getPosition() >= endRequest.getPosition()){                                        // we have found a state that is the same										done = true;                                        dp = null;                                    } else if (workingIt.hasNext()){                                        // didn't find it, try again.                                        dp = (DocPosition)workingIt.next();                                    } else {                                        // didn't find it, and there is no more info from last                                        // time.  This means that we will just continue                                        // until the end of the document.                                        dp = null;                                    }                                }                                // so that we can do this check next time, record all the

⌨️ 快捷键说明

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