package art.servers.gui.components; import art.library.gui.FlatGUI; import art.library.gui.flat.FlatButton; import art.library.gui.flat.FlatScrollPane; import art.library.gui.flat.FlatToggleButton; import art.library.gui.flat.FlatWindow; import art.library.model.transactions.traces.Note; import art.library.model.transactions.traces.Trace; import art.library.utils.resources.Resources; import art.library.utils.synchro.Mutex; import art.servers.Shared; import java.awt.BorderLayout; import java.awt.Color; import java.awt.Dimension; import java.awt.Insets; import java.awt.Toolkit; import java.awt.datatransfer.Clipboard; import java.awt.datatransfer.StringSelection; import java.text.SimpleDateFormat; import java.util.ArrayList; import java.util.List; import java.util.Timer; import java.util.TimerTask; import javax.swing.JScrollPane; import javax.swing.JTextPane; import javax.swing.text.DefaultStyledDocument; import javax.swing.text.Element; import javax.swing.text.Style; import javax.swing.text.StyleConstants; import javax.swing.text.StyleContext; public class PanelTraces extends PanelGeneric { public static final int SCREEN_RESET = 0; public static final int SCREEN_BRIGHT = 1; public static final int SCREEN_DIM = 2; public static final int SCREEN_BLINK = 3; public static final int SCREEN_UNDERLINE = 4; public static final int SCREEN_REVERSE = 7; public static final int SCREEN_HIDDEN = 8; public static final int SCREEN_BLACK = 0; public static final int SCREEN_RED = 1; public static final int SCREEN_GREEN = 2; public static final int SCREEN_YELLOW = 3; public static final int SCREEN_BLUE = 4; public static final int SCREEN_MAGENTA = 5; public static final int SCREEN_CYAN = 6; public static final int SCREEN_WHITE = 7; protected Color myblack = new Color(35,35,35); protected FlatScrollPane scrollPane = new FlatScrollPane(); protected DefaultStyledDocument document = new DefaultStyledDocument(); protected JTextPane textpane = new JTextPane(document); protected StyleContext context = new StyleContext(); protected Style style = context.getStyle(StyleContext.DEFAULT_STYLE); protected boolean autoscroll = true; protected int totalLines = 1000; protected FlatToggleButton buttonView = null; protected FlatButton buttonCopy = null; protected FlatButton buttonTrash = null; protected FlatToggleButton buttonAutoscroll = null; protected FlatWindow window = null; protected List traces = new ArrayList(); protected Mutex mutex = new Mutex(); public PanelTraces(FlatWindow window, int totalLines) { this.window = window; this.totalLines = totalLines; this. initialise(); } public PanelTraces(FlatWindow window) { this.window = window; initialise(); } public void selection() { try { window.addButtonSeparator(); window.addButtonMenu(buttonView); window.addButtonMenu(buttonCopy); window.addButtonMenu(buttonTrash); window.addButtonMenu(buttonAutoscroll); } catch (Exception e) { } } public void deselection() { try { window.removeComponentMenu(buttonView); window.removeComponentMenu(buttonCopy); window.removeComponentMenu(buttonTrash); window.removeComponentMenu(buttonAutoscroll); } catch (Exception e) { } } protected void initialise() { buttonView = new FlatToggleButton(Shared.getMessage("&View")); buttonView.setIcon(Resources.getResourceURL("data/art.library.server/icons/24x24/list.png")); buttonView.setPreferredSize(new Dimension(buttonView.getStringWidth(), 0)); buttonView.setPreferredSize(new Dimension(buttonView.getStringWidth(), 0)); buttonView.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { view(buttonView.isSelected()); } }); buttonCopy = new FlatButton(Shared.getMessage("&Copy")); buttonCopy.setIcon(Resources.getResourceURL("data/art.library.server/icons/24x24/copy.png")); buttonCopy.setPreferredSize(new Dimension(buttonCopy.getStringWidth(), 0)); buttonCopy.setPreferredSize(new Dimension(buttonCopy.getStringWidth(), 0)); buttonCopy.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { copyToClipboard(); } }); buttonTrash = new FlatButton(Shared.getMessage("Clea&r")); buttonTrash.setIcon(Resources.getResourceURL("data/art.library.server/icons/24x24/clear.png")); buttonTrash.setPreferredSize(new Dimension(buttonTrash.getStringWidth(), 0)); buttonTrash.setPreferredSize(new Dimension(buttonTrash.getStringWidth(), 0)); buttonTrash.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { clear(); } }); buttonAutoscroll = new FlatToggleButton(Shared.getMessage("&Autoscroll")); buttonAutoscroll.setSelected(true); buttonAutoscroll.setIcon(Resources.getResourceURL("data/art.library.server/icons/24x24/escalator-down.png")); buttonAutoscroll.setPreferredSize(new Dimension(buttonAutoscroll.getStringWidth(), 0)); buttonAutoscroll.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { autoscroll(buttonAutoscroll.isSelected()); } }); document = new DefaultStyledDocument(); textpane = new JTextPane(document); //textpane.setContentType("text/html;charset=UTF-8"); textpane.setEditable(false); textpane.setEditable(false); textpane.setBackground(myblack); textpane.setMargin(new Insets(10,10,10,10)); StyleConstants.setSpaceAbove(style, 10); StyleConstants.setSpaceBelow(style, 10); StyleConstants.setFontSize(style, 12); StyleConstants.setFontFamily(style, "Monospaced"); StyleConstants.setForeground(style, Color.white); StyleConstants.setBackground(style, myblack); scrollPane.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED); scrollPane.getViewport().setBackground(Color.black); scrollPane.getViewport().add(textpane); this.getLook().background = FlatGUI.lookDesign.getColor("button.color.rollover.background"); this.setLayout(new BorderLayout()); this.add(scrollPane, BorderLayout.CENTER); scrollPane.setVisible(false); TimerTask timerTask = new TimerTask() { public void run() { reload(); } }; Timer timer = new Timer(); timer.scheduleAtFixedRate(timerTask, 0, 1000); } public void addTrace(Object object) { mutex.lockWrite(); { traces.add(object); } mutex.releaseWrite(); } public void reload() { try { List localtraces = new ArrayList(); mutex.lockWrite(); { localtraces.addAll(this.traces); this.traces.clear(); } mutex.releaseWrite(); SimpleDateFormat formato1 = new SimpleDateFormat(Shared.getMessage("dd/MM/yyyy HH:mm:ss.SSS")); for(Object object : localtraces) { String result = ""; if (object instanceof Trace) { Trace trace = (Trace)object; result = result + color(SCREEN_RESET, SCREEN_WHITE, SCREEN_BLACK); result = result + formato1.format(trace.timestamp) + "\n"; switch (trace.type) { case Trace.TRACE_NONE: result = result = result + color(SCREEN_RESET, SCREEN_WHITE, SCREEN_BLACK); break; case Trace.TRACE_INFORMATION: result = result = result + color(SCREEN_RESET, SCREEN_CYAN, SCREEN_BLACK); break; case Trace.TRACE_WARNING: result = result + color(SCREEN_RESET, SCREEN_YELLOW, SCREEN_BLACK); break; case Trace.TRACE_ERROR: result = result + color(SCREEN_RESET, SCREEN_RED, SCREEN_BLACK); break; case Trace.TRACE_CORRECT: result = result + color(SCREEN_RESET, SCREEN_RED, SCREEN_BLACK); break; } result = result + "{\n"; if (trace.sourceComputer != null) result = result + " " + Shared.getMessage("Source") + " : " + trace.sourceComputer + "\n"; if (trace.username != null) result = result + " " + Shared.getMessage("Username") + " : " + trace.username + "\n"; if (trace.profile != null) result = result + " " + Shared.getMessage("Profile") + " : " + trace.profile + "\n"; if (trace.service != null) result = result + " " + Shared.getMessage("Service") + " : " + trace.service + "\n"; if (trace.action != null) result = result + " " + Shared.getMessage("Action") + " : " + trace.action + "\n"; if (trace.resource != null) result = result + " " + Shared.getMessage("Resource") + " : " + trace.resource + "\n"; if (trace.result != null) result = result + " " + Shared.getMessage("Result") + " : " + trace.result + "\n"; if (trace.stack != null) result = result + " " + Shared.getMessage("Stack") + " : " + trace.stack + "\n"; result = result + "}\n"; newLine(result); } else if (object instanceof Note) { Note note = (Note)object; result = result + color(SCREEN_RESET, SCREEN_WHITE, SCREEN_BLACK); result = result + formato1.format(note.timestamp) + " : "; switch (note.type) { case Trace.TRACE_NONE: result = result = result + color(SCREEN_RESET, SCREEN_WHITE, SCREEN_BLACK); break; case Trace.TRACE_INFORMATION: result = result = result + color(SCREEN_RESET, SCREEN_CYAN, SCREEN_BLACK); break; case Trace.TRACE_WARNING: result = result + color(SCREEN_RESET, SCREEN_YELLOW, SCREEN_BLACK); break; case Trace.TRACE_ERROR: result = result + color(SCREEN_RESET, SCREEN_RED, SCREEN_BLACK); break; case Trace.TRACE_CORRECT: result = result + color(SCREEN_RESET, SCREEN_GREEN, SCREEN_BLACK); break; } result = result + note.message; newLine(result); } else { result = result + color(SCREEN_RESET, SCREEN_WHITE, SCREEN_BLACK); result = result + formato1.format(System.currentTimeMillis()) + " : "; result = result + color(SCREEN_RESET, SCREEN_CYAN, SCREEN_BLACK); result = result + object.toString().trim(); newLine(result); } } } catch (Exception e) { } } public void view(boolean value) { scrollPane.setVisible(value); } public void autoscroll(boolean value) { this.autoscroll = value; } public void copyToClipboard() { try { Clipboard clpbrd = Toolkit.getDefaultToolkit ().getSystemClipboard (); clpbrd.setContents (new StringSelection (textpane.getDocument().getText(0, textpane.getDocument().getLength())), null); } catch (Exception e) { } } public void clear() { try { document.remove(0,document.getLength()); } catch (Exception e) { } } public void newLine(String text) { try { List lmessage = getMessages(text); for (Message message : lmessage) { if (message.foreground != null) StyleConstants.setForeground(style, message.foreground); if (message.background != null) StyleConstants.setBackground(style, message.background); document.insertString(document.getLength(), message.text, style); } document.insertString(document.getLength(), "\n", style); if (autoscroll == true) { textpane.setCaretPosition(textpane.getDocument().getLength()); } if (totalLines > 0) { Element root = textpane.getDocument().getDefaultRootElement(); while (root.getElementCount() > totalLines) { Element first = root.getElement(0); textpane.getDocument().remove(first.getStartOffset(), first.getEndOffset()); } } } catch (Exception e) { } } protected List getMessages(String text) { byte[] data = text.getBytes(); List lmessage = new ArrayList(); int position = 0; while (position >= 0) position = nextMessage(data, position, lmessage); return lmessage; } protected int nextMessage(byte[] data, int position, List lmessage) { for (int i=0; i ESC[attributes;foreground;brackground // // ESC : 27 // // attributes // SCREEN_RESET 0 // SCREEN_BRIGHT 1 // SCREEN_DIM 2 // SCREEN_BLINK 3 // SCREEN_UNDERLINE 4 // SCREEN_REVERSE 7 // SCREEN_HIDDEN 8 // // colors // SCREEN_BLACK 0 // SCREEN_RED 1 // SCREEN_GREEN 2 // SCREEN_YELLOW 3 // SCREEN_BLUE 4 // SCREEN_MAGENTA 5 // SCREEN_CYAN 6 // SCREEN_WHITE 7 protected class Message { public String text; public Color foreground = null; public Color background = null; public Message(String control, String text) { this.text = text; if (control.length() == 10) { foreground = color(Integer.parseInt(control.substring(4,6)) - 30); background = color(Integer.parseInt(control.substring(4,6)) - 40); } } protected Color color(int color) { switch (color) { case 0 : return myblack; case 1 : return Color.red; case 2 : return Color.green; case 3 : return Color.yellow; case 4 : return Color.blue; case 5 : return Color.magenta; case 6 : return Color.cyan; case 7 : return Color.white; } return myblack; } } protected static String color (int attributes, int foreground, int background) { return "\033[" + attributes + ";" + (foreground + 30) + ";" + (background + 40) + "m"; } }