KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > proactive > examples > StandardFrame


1 /*
2 * ################################################################
3 *
4 * ProActive: The Java(TM) library for Parallel, Distributed,
5 * Concurrent computing with Security and Mobility
6 *
7 * Copyright (C) 1997-2002 INRIA/University of Nice-Sophia Antipolis
8 * Contact: proactive-support@inria.fr
9 *
10 * This library is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU Lesser General Public
12 * License as published by the Free Software Foundation; either
13 * version 2.1 of the License, or any later version.
14 *
15 * This library is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 * Lesser General Public License for more details.
19 *
20 * You should have received a copy of the GNU Lesser General Public
21 * License along with this library; if not, write to the Free Software
22 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
23 * USA
24 *
25 * Initial developer(s): The ProActive Team
26 * http://www.inria.fr/oasis/ProActive/contacts.html
27 * Contributor(s):
28 *
29 * ################################################################
30 */

31 package org.objectweb.proactive.examples;
32
33 import java.text.*;
34 import javax.swing.*;
35 import javax.swing.text.*;
36 import javax.swing.border.*;
37
38 import org.apache.log4j.Logger;
39
40 /**
41  * Title:
42  * Description:
43  * Copyright: Copyright (c) 2001
44  * Company: Inria
45  * @author Lionel Mestre
46  * @author Roland Bertuli
47  * @version 1.1
48  */

49
50 public abstract class StandardFrame extends javax.swing.JFrame JavaDoc {
51     
52     static Logger logger = Logger.getLogger(StandardFrame.class.getName());
53
54   protected final static int MESSAGE_ZONE_HEIGHT = 250;
55   protected String JavaDoc name;
56   protected int width;
57   protected int height;
58   protected DateFormat dateFormat = new SimpleDateFormat("HH:mm:ss");
59   protected transient JTextPane messageArea;
60   protected transient Style regularStyle;
61   protected javax.swing.JSplitPane JavaDoc verticalSplitPane;
62
63
64   //
65
// -- CONSTRUCTORS -----------------------------------------------
66
//
67

68   public StandardFrame(String JavaDoc name, int width, int height) {
69     super(name);
70     this.name = name;
71     init(width, height);
72   }
73
74   public StandardFrame(String JavaDoc name) {
75     super(name);
76     this.name = name;
77   }
78
79   public StandardFrame(){}
80
81   //
82
// -- PUBLIC METHODS -----------------------------------------------
83
//
84

85   public void receiveMessage(final String JavaDoc message) {
86     final String JavaDoc date = dateFormat.format(new java.util.Date JavaDoc());
87     final String JavaDoc threadName = Thread.currentThread().getName();
88     SwingUtilities.invokeLater(new Runnable JavaDoc() {
89       public void run() {
90       Document doc = messageArea.getDocument();
91     try {
92       doc.insertString(doc.getLength(), date + " (" + threadName
93                + ")\n => " + message + "\n", regularStyle);
94     }
95     catch (Exception JavaDoc e) {
96       logger.error("Couldn't insert initial text.");
97     }
98       }
99     });
100   }
101
102   
103   //Method with possibility of color text
104
public void receiveMessage(final String JavaDoc message, final java.awt.Color JavaDoc color) {
105     final String JavaDoc date = dateFormat.format(new java.util.Date JavaDoc());
106     final String JavaDoc threadName = Thread.currentThread().getName();
107     final Style s = messageArea.addStyle("colored", regularStyle);
108     StyleConstants.setForeground(s, color);
109     javax.swing.SwingUtilities.invokeLater(new Runnable JavaDoc() {
110       public void run() {
111         Document doc = messageArea.getDocument();
112     try {
113       doc.insertString(doc.getLength(), date + " (" + threadName + ")\n => ",
114                regularStyle);
115       doc.insertString(doc.getLength(), message + "\n", s);
116     }
117     catch (Exception JavaDoc e) {
118       logger.error("Couldn't insert initial text.");
119     }
120       }
121     });
122   }
123
124   
125   
126   //
127
// -- PROTECTED METHODS -----------------------------------------------
128
//
129

130   protected void init(int width, int height) {
131     initFrame(name, width, height);
132     setVisible(true);
133     start();
134   }
135   
136   
137   protected abstract void start();
138
139
140   protected abstract javax.swing.JPanel JavaDoc createRootPanel();
141
142
143   protected javax.swing.JPanel JavaDoc createMessageZonePanel(final javax.swing.JTextPane JavaDoc area) {
144     Style styleDef = StyleContext.getDefaultStyleContext().getStyle(StyleContext.DEFAULT_STYLE);
145     regularStyle = area.addStyle("regular", styleDef);
146     area.setFont(new java.awt.Font JavaDoc("SansSerif", java.awt.Font.PLAIN, 10));
147     
148     JPanel panel = new javax.swing.JPanel JavaDoc(new java.awt.BorderLayout JavaDoc());
149     javax.swing.border.TitledBorder JavaDoc border = new javax.swing.border.TitledBorder JavaDoc("Messages");
150     panel.setBorder(border);
151     javax.swing.JPanel JavaDoc topPanel = new javax.swing.JPanel JavaDoc(new java.awt.BorderLayout JavaDoc());
152     // clear log button
153
javax.swing.JButton JavaDoc clearLogButton = new javax.swing.JButton JavaDoc("clear messages");
154     clearLogButton.addActionListener(new java.awt.event.ActionListener JavaDoc() {
155       public void actionPerformed(java.awt.event.ActionEvent JavaDoc e) {
156         area.setText("");
157       }
158     });
159     topPanel.add(clearLogButton, java.awt.BorderLayout.WEST);
160     panel.add(topPanel, java.awt.BorderLayout.NORTH);
161     javax.swing.JScrollPane JavaDoc pane = new javax.swing.JScrollPane JavaDoc(area);
162     panel.add(pane, java.awt.BorderLayout.CENTER);
163     return panel;
164   }
165   
166   
167   //
168
// -- PRIVATE METHODS -----------------------------------------------
169
//
170

171   private void initFrame(String JavaDoc name, int width, int height) {
172     java.awt.Container JavaDoc c = getContentPane();
173     c.setLayout(new java.awt.GridLayout JavaDoc(1, 1));
174
175     // create topPanel
176
JPanel topPanel = new JPanel(new java.awt.GridLayout JavaDoc(1, 1));
177     TitledBorder border = new javax.swing.border.TitledBorder JavaDoc(name);
178     topPanel.setBorder(border);
179     topPanel.add(createRootPanel());
180
181     // create bottom Panel
182
messageArea = new javax.swing.JTextPane JavaDoc();
183     messageArea.setEditable(false);
184     javax.swing.JPanel JavaDoc bottomPanel = createMessageZonePanel(messageArea);
185
186     // create an vertical split Panel
187
verticalSplitPane = new JSplitPane(JSplitPane.VERTICAL_SPLIT);
188     verticalSplitPane.setDividerLocation(height);
189     verticalSplitPane.setTopComponent(topPanel);
190     verticalSplitPane.setBottomComponent(bottomPanel);
191     c.add(verticalSplitPane);
192
193     setSize(width, height + MESSAGE_ZONE_HEIGHT);
194     setLocation(30, 30);
195     addWindowListener(new java.awt.event.WindowAdapter JavaDoc() {
196       public void windowClosing(java.awt.event.WindowEvent JavaDoc e) {
197         System.exit(0);
198       }
199     });
200   }
201
202
203 }
204
Popular Tags