001: package com.zanthan.sequence;
002:
003: import java.awt.*;
004: import java.awt.event.*;
005: import javax.swing.*;
006:
007: import java.io.*;
008:
009: /**
010: * Displays a sequence diagram for each thread. When this exits, the
011: * application exits.
012: */
013: public class SequenceFrame extends JFrame {
014:
015: private JTabbedPane tabbedPane = new JTabbedPane();
016:
017: /**
018: * Constructs a new frame.
019: */
020: public SequenceFrame() {
021: super ("dynaop SEQUENCE");
022:
023: // position window.
024: Dimension size = Toolkit.getDefaultToolkit().getScreenSize();
025: int width = size.width / 2;
026: int height = size.height / 2;
027: pack();
028: setSize(new Dimension(width, height));
029:
030: // center window on screen.
031: setLocation((size.width - width) / 2,
032: (size.height - height) / 2);
033:
034: // handle close button.
035: setDefaultCloseOperation(DISPOSE_ON_CLOSE);
036: addWindowListener(new WindowAdapter() {
037: public void windowClosed(WindowEvent e) {
038: System.exit(0);
039: }
040: });
041:
042: // add tabbed pane.
043: getContentPane().add(this .tabbedPane);
044:
045: // make visible.
046: setVisible(true);
047: }
048:
049: /**
050: * Update thread local diagram.
051: */
052: public void update(String s) {
053: //System.out.println("* " + s);
054:
055: Diagram diagram = (Diagram) localDiagram.get();
056: diagram.out.write(s);
057: String code = diagram.out.toString();
058: diagram.display.init(code);
059: diagram.model.setText(code, this );
060: }
061:
062: /**
063: * Thread local diagram.
064: */
065: private ThreadLocal localDiagram = new ThreadLocal() {
066:
067: protected Object initialValue() {
068: // lazily create diagrams and tabs.
069: final Diagram diagram = new Diagram();
070:
071: JPanel panel = new JPanel();
072: panel.setLayout(new BorderLayout());
073:
074: JToolBar toolBar = new JToolBar();
075: toolBar.setFloatable(false);
076: toolBar.add(new JButton(new SaveAsAction(diagram.model)));
077: toolBar.add(new JButton(new ExportAction(diagram.display)));
078:
079: panel.add(toolBar, BorderLayout.NORTH);
080: panel.add(new JScrollPane(diagram.display,
081: JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,
082: JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED),
083: BorderLayout.CENTER);
084:
085: SequenceFrame.this .tabbedPane.addTab(Thread.currentThread()
086: .getName(), panel);
087:
088: return diagram;
089: }
090: };
091:
092: /**
093: * Objects for a diagram.
094: */
095: private class Diagram {
096: Display display = new Display(null);
097: Model model = new Model();
098: StringWriter out = new StringWriter();
099: }
100:
101: }
|