📄 programmereditordemo.java
字号:
// initial states from this time. newPositions.add(dpEnd); } synchronized (doclock){ t = syntaxLexer.getNextToken(); } } // remove all the old initial positions from the place where // we started doing the highlighting right up through the last // bit of text we touched. workingIt = iniPositions.subSet(dpStart, dpEnd).iterator(); while (workingIt.hasNext()){ workingIt.next(); workingIt.remove(); } // Remove all the positions that are after the end of the file.: workingIt = iniPositions.tailSet(new DocPosition(document.getLength())).iterator(); while (workingIt.hasNext()){ workingIt.next(); workingIt.remove(); } // and put the new initial positions that we have found on the list. iniPositions.addAll(newPositions); newPositions.clear(); /*workingIt = iniPositions.iterator(); while (workingIt.hasNext()){ System.out.println(workingIt.next()); } System.out.println("Started: " + dpStart.getPosition() + " Ended: " + dpEnd.getPosition());*/ } catch (IOException x){ } synchronized (doclock){ lastPosition = -1; change = 0; } // since we did something, we should check that there is // nothing else to do before going back to sleep. tryAgain = true; } asleep = true; if (!tryAgain){ try { sleep (0xffffff); } catch (InterruptedException x){ } } asleep = false; } } } /** * Color or recolor the entire document */ public void colorAll(){ color(0, document.getLength()); } /** * Color a section of the document. * The actual coloring will start somewhere before * the requested position and continue as long * as needed. * * @param position the starting point for the coloring. * @param adjustment amount of text inserted or removed * at the starting point. */ public void color(int position, int adjustment){ colorer.color(position, adjustment); } /** * Create the style menu. * * @return the style menu. */ private JMenu createStyleMenu() { ActionListener actionListener = new ActionListener(){ public void actionPerformed(ActionEvent e){ if (e.getActionCommand().equals("Java")){ syntaxLexer = new JavaLexer(documentReader); colorAll(); } else if (e.getActionCommand().equals("C/C++")){ syntaxLexer = new CLexer(documentReader); colorAll(); } else if (e.getActionCommand().equals("LaTeX")){ syntaxLexer = new LatexLexer(documentReader); colorAll(); } else if (e.getActionCommand().equals("SQL")){ syntaxLexer = new SQLLexer(documentReader); colorAll(); } else if (e.getActionCommand().equals("Java Properties")){ syntaxLexer = new PropertiesLexer(documentReader); colorAll(); } else if (e.getActionCommand().equals("HTML (Simple)")){ syntaxLexer = new HTMLLexer(documentReader); colorAll(); } else if (e.getActionCommand().equals("HTML (Complex)")){ syntaxLexer = new HTMLLexer1(documentReader); colorAll(); } else if (e.getActionCommand().equals("Gray Out")){ SimpleAttributeSet style = new SimpleAttributeSet(); StyleConstants.setFontFamily(style, "Monospaced"); StyleConstants.setFontSize(style, 12); StyleConstants.setBackground(style, Color.gray); StyleConstants.setForeground(style, Color.black); StyleConstants.setBold(style, false); StyleConstants.setItalic(style, false); document.setCharacterAttributes(0, document.getLength(), style, true); } } }; JMenu menu = new JMenu("Style"); ButtonGroup group = new ButtonGroup(); JRadioButtonMenuItem item; item = new JRadioButtonMenuItem("Java", true); group.add(item); item.addActionListener(actionListener); menu.add(item); item = new JRadioButtonMenuItem("C/C++", false); group.add(item); item.addActionListener(actionListener); menu.add(item); item = new JRadioButtonMenuItem("HTML (Simple)", false); group.add(item); item.addActionListener(actionListener); menu.add(item); item = new JRadioButtonMenuItem("HTML (Complex)", false); group.add(item); item.addActionListener(actionListener); menu.add(item); item = new JRadioButtonMenuItem("LaTeX", false); group.add(item); item.addActionListener(actionListener); menu.add(item); item = new JRadioButtonMenuItem("SQL", false); group.add(item); item.addActionListener(actionListener); menu.add(item); item = new JRadioButtonMenuItem("Java Properties", false); group.add(item); item.addActionListener(actionListener); menu.add(item); JMenuItem i = new JMenuItem("Gray Out"); i.addActionListener(actionListener); menu.add(i); return menu; } /** * Initialize the document with some default text and set * they initial type of syntax highlighting. */ private void initDocument() { String initString = ( "/**\n" + " * Simple common test program.\n" + " */\n" + "public class HelloWorld {\n" + " public static void main(String[] args) {\n" + " // Display the greeting.\n" + " System.out.println(\"Hello World!\");\n" + " }\n" + "}\n" ); syntaxLexer = new JavaLexer(documentReader); try { document.insertString(document.getLength(), initString, getStyle("text")); } catch (BadLocationException ble) { System.err.println("Couldn't insert initial text."); } } /** * Run the demo. * * @param args ignored */ public static void main(String[] args) { // create the demo ProgrammerEditorDemo frame = new ProgrammerEditorDemo(); } /** * A hash table containing the text styles. * Simple attribute sets are hashed by name (String) */ private Hashtable styles = new Hashtable(); /** * retrieve the style for the given type of text. * * @param styleName the label for the type of text ("tag" for example) * or null if the styleName is not known. * @return the style */ private SimpleAttributeSet getStyle(String styleName){ return ((SimpleAttributeSet)styles.get(styleName)); } /** * Create the styles and place them in the hash table. */ private void initStyles(){ SimpleAttributeSet style; style = new SimpleAttributeSet(); StyleConstants.setFontFamily(style, "Monospaced"); StyleConstants.setFontSize(style, 12); StyleConstants.setBackground(style, Color.white); StyleConstants.setForeground(style, Color.black); StyleConstants.setBold(style, false); StyleConstants.setItalic(style, false); styles.put("body", style); style = new SimpleAttributeSet(); StyleConstants.setFontFamily(style, "Monospaced"); StyleConstants.setFontSize(style, 12); StyleConstants.setBackground(style, Color.white); StyleConstants.setForeground(style, Color.blue); StyleConstants.setBold(style, true); StyleConstants.setItalic(style, false); styles.put("tag", style); style = new SimpleAttributeSet(); StyleConstants.setFontFamily(style, "Monospaced"); StyleConstants.setFontSize(style, 12); StyleConstants.setBackground(style, Color.white); StyleConstants.setForeground(style, Color.blue); StyleConstants.setBold(style, false); StyleConstants.setItalic(style, false); styles.put("endtag", style); style = new SimpleAttributeSet(); StyleConstants.setFontFamily(style, "Monospaced"); StyleConstants.setFontSize(style, 12); StyleConstants.setBackground(style, Color.white); StyleConstants.setForeground(style, Color.black); StyleConstants.setBold(style, false); StyleConstants.setItalic(style, false); styles.put("reference", style); style = new SimpleAttributeSet(); StyleConstants.setFontFamily(style, "Monospaced"); StyleConstants.setFontSize(style, 12); StyleConstants.setBackground(style, Color.white); StyleConstants.setForeground(style, new Color(0xB03060)/*Color.maroon*/); StyleConstants.setBold(style, true); StyleConstants.setItalic(style, false); styles.put("name", style); style = new SimpleAttributeSet(); StyleConstants.setFontFamily(style, "Monospaced"); StyleConstants.setFontSize(style, 12); StyleConstants.setBackground(style, Color.white); StyleConstants.setForeground(style, new Color(0xB03060)/*Color.maroon*/); StyleConstants.setBold(style, false); StyleConstants.setItalic(style, true); styles.put("value", style); style = new SimpleAttributeSet(); StyleConstants.setFontFamily(style, "Monospaced"); StyleConstants.setFontSize(style, 12); StyleConstants.setBackground(style, Color.white); StyleConstants.setForeground(style, Color.black); StyleConstants.setBold(style, true); StyleConstants.setItalic(style, false); styles.put("text", style); style = new SimpleAttributeSet(); StyleConstants.setFontFamily(style, "Monospaced"); StyleConstants.setFontSize(style, 12); StyleConstants.setBackground(style, Color.white); StyleConstants.setForeground(style, Color.blue); StyleConstants.setBold(style, false); StyleConstants.setItalic(style, false); styles.put("reservedWord", style); style = new SimpleAttributeSet(); StyleConstants.setFontFamily(style, "Monospaced"); StyleConstants.setFontSize(style, 12); StyleConstants.setBackground(style, Color.white); StyleConstants.setForeground(style, Color.black); StyleConstants.setBold(style, false); StyleConstants.setItalic(style, false); styles.put("identifier", style); style = new SimpleAttributeSet(); StyleConstants.setFontFamily(style, "Monospaced"); StyleConstants.setFontSize(style, 12); StyleConstants.setBackground(style, Color.white); StyleConstants.setForeground(style, new Color(0xB03060)/*Color.maroon*/); StyleConstants.setBold(style, false); StyleConstants.setItalic(style, false); styles.put("literal", style); style = new SimpleAttributeSet(); StyleConstants.setFontFamily(style, "Monospaced"); StyleConstants.setFontSize(style, 12); StyleConstants.setBackground(style, Color.white); StyleConstants.setForeground(style, new Color(0x000080)/*Color.navy*/); StyleConstants.setBold(style, false); StyleConstants.setItalic(style, false); styles.put("separator", style); style = new SimpleAttributeSet(); StyleConstants.setFontFamily(style, "Monospaced"); StyleConstants.setFontSize(style, 12); StyleConstants.setBackground(style, Color.white); StyleConstants.setForeground(style, Color.black); StyleConstants.setBold(style, true); StyleConstants.setItalic(style, false); styles.put("operator", style); style = new SimpleAttributeSet(); StyleConstants.setFontFamily(style, "Monospaced"); StyleConstants.setFontSize(style, 12); StyleConstants.setBackground(style, Color.white); StyleConstants.setForeground(style, Color.green.darker()); StyleConstants.setBold(style, false); StyleConstants.setItalic(style, false); styles.put("comment", style); style = new SimpleAttributeSet(); StyleConstants.setFontFamily(style, "Monospaced"); StyleConstants.setFontSize(style, 12); StyleConstants.setBackground(style, Color.white); StyleConstants.setForeground(style, new Color(0xA020F0).darker()/*Color.purple*/); StyleConstants.setBold(style, false); StyleConstants.setItalic(style, false); styles.put("preprocessor", style); style = new SimpleAttributeSet(); StyleConstants.setFontFamily(style, "Monospaced"); StyleConstants.setFontSize(style, 12); StyleConstants.setBackground(style, Color.white); StyleConstants.setForeground(style, Color.black); StyleConstants.setBold(style, false); StyleConstants.setItalic(style, false); styles.put("whitespace", style);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -