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

📄 outputpane.java

📁 SRI international 发布的OAA框架软件
💻 JAVA
字号:
/**
 * The contents of this file are subject to the OAA  Community Research
 * License Version 2.0 (the "License"); you may not use this file except
 * in compliance with the License. You may obtain a copy of the License
 * at http://www.ai.sri.com/~oaa/.  Software distributed under the License
 * is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, either
 * express or implied. See the License for the specific language governing
 * rights and limitations under the License.  Portions of the software are
 * Copyright (c) SRI International, 1999-2003.  All rights reserved.
 * "OAA" is a registered trademark, and "Open Agent Architecture" is a
 * trademark, of SRI International, a California nonprofit public benefit
 * corporation.
*/

package com.sri.oaa2.agt.startit;

import java.io.*;
import java.util.*;

import javax.swing.*;
import javax.swing.text.*;
import java.awt.*;
import java.awt.event.*;

public class OutputPane extends JTextPane {

  private LockableStyledDocument doc = new LockableStyledDocument();
  private SimpleAttributeSet[] outAttr;
  protected SimpleAttributeSet inverseAttr;

  private static final Calendar calendar = Calendar.getInstance();
  public int MaxNumLines = 500;
  private int[] lineLengths = new int[MaxNumLines];
  private long[] timeStamps = new long[MaxNumLines];
  private int firstLine = 0;
  private int numLines = 0;
  private MyPopup popup;

  private int tmp = 0;

  static private Color whitish = new Color(244,244,244);
  static {
    UIManager.put("TabbedPane.selected", whitish);
  }

  public OutputPane() {
    this(Color.black);
  }

  public OutputPane(Color c) {
    setEditable(false);
    setDocument(doc);
    setBackground(whitish);

    SimpleAttributeSet attr = new SimpleAttributeSet();
    StyleConstants.setForeground(attr, c);

    outAttr = new SimpleAttributeSet[Outputter.NUM_TYPES];

    inverseAttr = new SimpleAttributeSet(attr);
    StyleConstants.setBackground(inverseAttr, c);
    StyleConstants.setForeground(inverseAttr, Color.white);

    outAttr[Outputter.INFO] = new SimpleAttributeSet(attr);

    outAttr[Outputter.ERROR] = new SimpleAttributeSet(attr);
    StyleConstants.setBackground(outAttr[Outputter.ERROR], new Color(255, 220, 220));
    StyleConstants.setBold(outAttr[Outputter.ERROR], true);

    outAttr[Outputter.WARNING] = new SimpleAttributeSet(attr);
    StyleConstants.setBackground(outAttr[Outputter.WARNING], new Color(255, 255, 220));
    StyleConstants.setBold(outAttr[Outputter.WARNING], true);

    outAttr[Outputter.DEBUG] = new SimpleAttributeSet(attr);
    //StyleConstants.setBold(outAttr[Outputter.DEBUG], true);
    StyleConstants.setItalic(outAttr[Outputter.DEBUG], true);

    popup = new MyPopup();
    addMouseListener(new MouseAdapter() {
      public void mousePressed(MouseEvent e) {
	if (SwingUtilities.isRightMouseButton(e)) {
	  popup.show(e.getComponent(), e.getX()+3, e.getY()+3);
	}
      }
    });
  }

  public void appendLine(String text, int type) {
    appendLine(text, type, this);
  }

  /** appendLine with style from another OutputPane
   */
  public void appendLine(final String text, final int type, final OutputPane pane) {
    Runnable append = new Runnable() {
      public void run() {
	_appendLine(text, type, pane);
      }
    };
    SwingUtilities.invokeLater(append);
  }

  public void _appendLine(String text, int type, OutputPane pane) {
    long callTime = calendar.getTimeInMillis();

    if (numLines >= MaxNumLines) {
      try {
	doc.remove(0, lineLengths[firstLine]);
	numLines--;
	firstLine++;
	firstLine = firstLine % MaxNumLines;
      }
      catch (BadLocationException e) {System.out.println(e);}
    }

    doc.lock();
    try {
      int offset = 0;
      if (pane != this) {
	doc.insertString(doc.getLength(), "   ", pane.inverseAttr);
	doc.insertString(doc.getLength(), " ", pane.getAttr(type));
	offset = 4;
      }
      doc.insertString(doc.getLength(), text + "\n", pane.getAttr(type));
      setCaretPosition(doc.getLength());
      int lastLine = getLineNum(numLines);
      lineLengths[lastLine] = text.length()+1+offset;
      timeStamps[lastLine] = callTime;
      numLines++;
    }
    catch (BadLocationException e) {System.err.println(e);}
    doc.unlock();

    //if ((tmp++ % 50) == 0) {
    //  System.out.println("MEM: " + Runtime.getRuntime().totalMemory());
    //}
  }

  public void clear() {
    try {
      doc.remove(0, doc.getLength());
      firstLine = numLines = 0;
    }
    catch (BadLocationException e) {System.err.println(e);}
  }

  public SimpleAttributeSet getAttr(int type) {
    return outAttr[type];
  }

  public int getLineNum(int l) {
    return (firstLine + l) % MaxNumLines;
  }

  /** Makes the writeLock() and writeUnlock() methods accessible, so
   * we can do multiple insertString()s with different text attributes
   * in one line, and no other thread will write while we're in the
   * middle of the line
   */
  static public class LockableStyledDocument extends DefaultStyledDocument{
    public void lock() {writeLock();}
    public void unlock() {writeUnlock();}
  }

  private class MyPopup extends JPopupMenu {
    public MyPopup() {
      JMenuItem item;
      item = new JMenuItem("clear");
      item.addActionListener(new ActionListener() {
	public void actionPerformed(ActionEvent e) {
	  clear();
	}
      });
      add(item);
    }
  }

}

⌨️ 快捷键说明

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