1 31 32 package org.antlr.works.editor; 33 34 import org.antlr.works.components.grammar.CEditorGrammar; 35 import org.antlr.works.swing.Toolbar; 36 import org.antlr.works.utils.Console; 37 import org.antlr.xjlib.foundation.XJUtils; 38 39 import javax.swing.*; 40 import java.awt.*; 41 import java.awt.event.ActionEvent ; 42 import java.awt.event.ActionListener ; 43 import java.text.SimpleDateFormat ; 44 import java.util.Date ; 45 import java.util.HashMap ; 46 import java.util.Map ; 47 48 public class EditorConsole extends EditorTab implements Console { 49 50 protected CEditorGrammar editor; 51 52 protected JPanel panel; 53 protected JTextArea textArea; 54 55 protected SimpleDateFormat dateFormat = new SimpleDateFormat ("HH:mm:ss"); 56 protected Map <Thread ,Integer > modeByThread = new HashMap <Thread , Integer >(); 57 58 protected static EditorConsole current = null; 59 60 public static synchronized void setCurrent(EditorConsole console) { 61 current = console; 62 } 63 64 public static synchronized EditorConsole getCurrent() { 65 return current; 66 } 67 68 public EditorConsole(CEditorGrammar editor) { 69 this.editor = editor; 70 71 panel = new JPanel(new BorderLayout()); 72 Toolbar box = Toolbar.createHorizontalToolbar(); 73 74 JButton clear = new JButton("Clear All"); 75 clear.addActionListener(new ActionListener () { 76 public void actionPerformed(ActionEvent e) { 77 clear(); 78 } 79 }); 80 box.addElement(clear); 81 box.add(Box.createHorizontalGlue()); 82 83 panel.add(createTextArea(), BorderLayout.CENTER); 84 panel.add(box, BorderLayout.SOUTH); 85 } 86 87 public void setMode(int mode) { 88 modeByThread.put(Thread.currentThread(), mode); 89 } 90 91 public int getMode() { 92 Integer mode = modeByThread.get(Thread.currentThread()); 93 if(mode == null) 94 return Console.MODE_VERBOSE; 95 else 96 return mode; 97 } 98 99 public void makeCurrent() { 100 EditorConsole.setCurrent(this); 101 } 102 103 public Container getContainer() { 104 return panel; 105 } 106 107 public Container createTextArea() { 108 textArea = new JTextArea(); 109 JScrollPane textAreaScrollPane = new JScrollPane(textArea); 110 textAreaScrollPane.setWheelScrollingEnabled(true); 111 return textAreaScrollPane; 112 } 113 114 public void clear() { 115 textArea.setText(""); 116 editor.clearConsoleStatus(); 117 } 118 119 public synchronized void println(String s) { 120 println(s, Console.LEVEL_NORMAL); 121 } 122 123 public synchronized void println(String s, int level) { 124 print(s+"\n", level); 125 } 126 127 public synchronized void print(Throwable e) { 128 println(XJUtils.stackTrace(e), Console.LEVEL_ERROR); 129 } 130 131 public synchronized void print(final String s, final int level) { 132 if(!SwingUtilities.isEventDispatchThread()) { 133 SwingUtilities.invokeLater(new Runnable () { 134 public void run() { 135 print(s, level); 136 } 137 }); 138 } 139 140 String t = "["+dateFormat.format(new Date ())+"] "+s; 141 textArea.setText(textArea.getText()+t); 142 textArea.setCaretPosition(textArea.getText().length()); 143 System.out.println(s); 144 145 if(getMode() == Console.MODE_VERBOSE) { 146 editor.consolePrint(s, level); 147 } 148 } 149 150 public String getTabName() { 151 return "Console"; 152 } 153 154 public Component getTabComponent() { 155 return getContainer(); 156 } 157 158 } 159 | Popular Tags |