KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > zanthan > sequence > SequenceFrame


1 package com.zanthan.sequence;
2
3 import java.awt.*;
4 import java.awt.event.*;
5 import javax.swing.*;
6
7 import java.io.*;
8
9 /**
10  * Displays a sequence diagram for each thread. When this exits, the
11  * application exits.
12  */

13 public class SequenceFrame extends JFrame {
14
15     private JTabbedPane tabbedPane = new JTabbedPane();
16     
17     /**
18      * Constructs a new frame.
19      */

20     public SequenceFrame() {
21         super("dynaop SEQUENCE");
22         
23         // position window.
24
Dimension size = Toolkit.getDefaultToolkit().getScreenSize();
25         int width = size.width / 2;
26         int height = size.height / 2;
27         pack();
28         setSize(new Dimension(width, height));
29         
30         // center window on screen.
31
setLocation(
32             (size.width - width) / 2,
33             (size.height - height) / 2
34         );
35         
36         // handle close button.
37
setDefaultCloseOperation(DISPOSE_ON_CLOSE);
38         addWindowListener(new WindowAdapter() {
39             public void windowClosed(WindowEvent e) {
40                 System.exit(0);
41             }
42         });
43
44         // add tabbed pane.
45
getContentPane().add(this.tabbedPane);
46
47         // make visible.
48
setVisible(true);
49     }
50
51     /**
52      * Update thread local diagram.
53      */

54     public void update(String JavaDoc s) {
55         //System.out.println("* " + s);
56

57         Diagram diagram = (Diagram) localDiagram.get();
58         diagram.out.write(s);
59         String JavaDoc code = diagram.out.toString();
60         diagram.display.init(code);
61         diagram.model.setText(code, this);
62     }
63
64     /**
65      * Thread local diagram.
66      */

67     private ThreadLocal JavaDoc localDiagram = new ThreadLocal JavaDoc() {
68         
69             
70         protected Object JavaDoc initialValue() {
71             // lazily create diagrams and tabs.
72
final Diagram diagram = new Diagram();
73
74             JPanel panel = new JPanel();
75             panel.setLayout(new BorderLayout());
76
77             JToolBar toolBar = new JToolBar();
78             toolBar.setFloatable(false);
79             toolBar.add(new JButton(new SaveAsAction(diagram.model)));
80             toolBar.add(new JButton(new ExportAction(diagram.display)));
81             
82             panel.add(toolBar, BorderLayout.NORTH);
83             panel.add(new JScrollPane(diagram.display,
84                 JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,
85                 JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED),
86                 BorderLayout.CENTER
87             );
88             
89             SequenceFrame.this.tabbedPane.addTab(
90                 Thread.currentThread().getName(),
91                 panel
92             );
93             
94             return diagram;
95         }
96     };
97    
98     /**
99      * Objects for a diagram.
100      */

101     private class Diagram {
102         Display display = new Display(null);
103         Model model = new Model();
104         StringWriter out = new StringWriter();
105     }
106     
107 }
108
Popular Tags