📄 xtree.java
字号:
/* * 08/15/2001 - 14:05:44 * * XTree.java - A tree used for XInsert system * Copyright (C) 1999-2000 Romain Guy * Portion copyright (C) 2000 Richard Lowe * romain.guy@jext.org * http://www.jext.org * * 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 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. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */package org.jext.xinsert;import java.io.*;import java.util.*;import java.awt.*;import java.awt.event.*;import javax.swing.*;import javax.swing.tree.*;import javax.swing.event.*;import javax.swing.text.Document;import javax.swing.text.BadLocationException;import org.jext.*;import org.jext.dawn.*;import org.jext.event.*;import org.jext.scripting.dawn.Run;import org.jext.gui.JextCheckBox;import org.jext.gui.JextHighlightButton;import org.jext.misc.Indent;import org.jext.xml.XInsertReader;public class XTree extends JPanel implements TreeSelectionListener, ActionListener, Runnable, JextListener{ private static Vector inserts; private String file, currentMode; private JTree tree; private JextFrame parent; /** * This contains the complete content of the files. It's never associated to the JTree: * associateXTreeToMode reads it and chooses the right nodes. * @see associateXTreeToMode */ private DefaultTreeModel treeModel; private JextHighlightButton expand, collapse, reload; private JextCheckBox carriageReturn, executeScript, textSurrounding; // nested submenus private int rootIndex; private XTreeNode root; private Stack menuStack = null; private XTreeObject xtreeObj = null; public void addMenu(String nodeName, String modes) { xtreeObj = new XTreeObject(new XTreeNode(nodeName, modes), 0); if (menuStack.empty()) { treeModel.insertNodeInto(xtreeObj.getXTreeNode(), root, rootIndex); rootIndex++; } else { XTreeObject obj = (XTreeObject) menuStack.peek(); treeModel.insertNodeInto(xtreeObj.getXTreeNode(), obj.getXTreeNode(), obj.getIndex()); obj.incrementIndex(); } menuStack.push(xtreeObj); } public void closeMenu() { try { xtreeObj = (XTreeObject) menuStack.pop(); } catch (Exception e) { xtreeObj = null; } } public void addInsert(String nodeName, String content, int type) { inserts.addElement(new XTreeItem(content, type)); XTreeNode node = new XTreeNode(nodeName, null, inserts.size()); if (xtreeObj == null) { treeModel.insertNodeInto(node, root, rootIndex); ++rootIndex; } else { XTreeObject obj = (XTreeObject) menuStack.peek(); treeModel.insertNodeInto(node, obj.getXTreeNode(), obj.getIndex()); obj.incrementIndex(); } } public XTree(JextFrame parent, String file) { super(); this.parent = parent; parent.addJextListener(this); setLayout(new BorderLayout()); root = new XTreeNode("XInsert"); treeModel = new DefaultTreeModel(root); tree = new JTree(treeModel); tree.addTreeSelectionListener(this); tree.putClientProperty("JTree.lineStyle", "Angled"); if (!Jext.getBooleanProperty("useSkin")) tree.setCellRenderer(new XTreeCellRenderer()); init(file); String icons = Jext.getProperty("jext.look.icons"); JToolBar pane = new JToolBar();//new BorderLayout()); pane.setFloatable(false); pane.add(collapse = new JextHighlightButton(Jext.getProperty("xtree.collapse.button"), Utilities.getIcon("images/button_collapse" + icons + ".gif", Jext.class)));//, //BorderLayout.NORTH); collapse.setMnemonic(Jext.getProperty("xtree.collapse.mnemonic").charAt(0)); collapse.addActionListener(this); pane.add(expand = new JextHighlightButton(Jext.getProperty("xtree.expand.button"), Utilities.getIcon("images/button_expand" + icons + ".gif", Jext.class)));//, //BorderLayout.CENTER); expand.setMnemonic(Jext.getProperty("xtree.expand.mnemonic").charAt(0)); expand.addActionListener(this); pane.add(reload = new JextHighlightButton(Jext.getProperty("xtree.reload.button"), Utilities.getIcon("images/menu_reload" + icons + ".gif", Jext.class)));//, //BorderLayout.SOUTH); reload.setMnemonic(Jext.getProperty("xtree.reload.mnemonic").charAt(0)); reload.addActionListener(this); add(pane, BorderLayout.NORTH); JScrollPane s = new JScrollPane(tree); s.setBorder(null); add(s, BorderLayout.CENTER); JPanel optionPane = new JPanel(new BorderLayout()); optionPane.add(carriageReturn = new JextCheckBox(Jext.getProperty("xtree.carriage.label")), BorderLayout.NORTH); carriageReturn.setSelected(Jext.getBooleanProperty("carriage")); carriageReturn.addActionListener(this); optionPane.add(executeScript = new JextCheckBox(Jext.getProperty("xtree.execute.label")), BorderLayout.CENTER); executeScript.setSelected(Jext.getBooleanProperty("execute")); if (Jext.getProperty("execute") == null) executeScript.setSelected(true); executeScript.addActionListener(this); optionPane.add(textSurrounding = new JextCheckBox(Jext.getProperty("xtree.surrounding.label")), BorderLayout.SOUTH); textSurrounding.setSelected(Jext.getBooleanProperty("surrounding")); if (Jext.getProperty("surrounding") == null) textSurrounding.setSelected(true); textSurrounding.addActionListener(this); add(optionPane, BorderLayout.SOUTH); } private void init(String file) { init(file, true); } private void init(String file, boolean useThread) { this.file = file; if (useThread) { Thread x = new Thread(this, "--XTree builder thread"); x.start(); } else run(); } public void stop() { } //this loads the main file, while loadLocalFiles thinks to local files. public void run() { inserts = new Vector(200); menuStack = new Stack(); rootIndex = 0; if (XInsertReader.read(this, Jext.class.getResourceAsStream(file), file)) { loadLocalFiles(); SwingUtilities.invokeLater(new Runnable() { public void run() { associateXTreeToMode(false); tree.expandRow(0); tree.setRootVisible(false); tree.setShowsRootHandles(true); file = null; } }); } } private void loadLocalFiles() { String dir = Jext.SETTINGS_DIRECTORY + "xinsert" + File.separator; String inserts[] = Utilities.getWildCardMatches(dir, "*.insert.xml", false); if (inserts == null) return; try { String fileName; for (int i = 0; i < inserts.length; i++) { fileName = dir + inserts[i]; if (XInsertReader.read(this, new FileInputStream(fileName), fileName)) { String[] args = { inserts[i] }; System.out.println(Jext.getProperty("xtree.loaded", args)); } } } catch (FileNotFoundException fnfe) { } } public void jextEventFired(JextEvent evt) { int what = evt.getWhat(); if (what == JextEvent.SYNTAX_MODE_CHANGED || what == JextEvent.TEXT_AREA_SELECTED || what == JextEvent.OPENING_WINDOW) { associateXTreeToMode(); } } private void associateXTreeToMode() { associateXTreeToMode(true); } /** * This method builds a copy of treeModel containing only nodes relevant to current * mode, and then uses the new TreeModel for the tree. * Must be called by the event-dispatching thread! */ private void associateXTreeToMode(boolean checkColorizingMode) { JextTextArea textArea = parent.getTextArea(); if (textArea == null) return; String mode = textArea.getColorizingMode(); //if we must reload(at user's request or when loading is finished), we do this even when //mode has not changed. if (checkColorizingMode && mode.equals(currentMode)) return; int index = 0; XTreeNode _root = new XTreeNode("XInsert"); DefaultTreeModel _model = new DefaultTreeModel(_root); for (int i = 0; i < root.getChildCount(); i++) { XTreeNode child = (XTreeNode) root.getChildAt(i); if (child.isAssociatedToMode(mode)) { child.setParent(null); if (child.isPermanent()) _root.add(child); else { if (child.toString().equalsIgnoreCase(mode)) _root.insert(child, 0); else _root.insert(child, index); index++; } }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -