Source code: javatools/swing/frame/MessageSpy.java
1 /*
2 * MessageSpy.java
3 *
4 * Created on 19 aprile 2003, 10.29
5 Javatools (modified version) - Some useful general classes.
6 Copyright (C) 2002-2003 Chris Bitmead (original) Antonio Petrelli (modified)
7
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2 of the License, or
11 (at your option) any later version.
12
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
17
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21
22 Contact me at: brenmcguire@users.sourceforge.net
23 */
24
25 package javatools.swing.frame;
26
27 import java.util.*;
28 import javatools.swing.MoreSwingUtilities;
29 import javatools.util.RunningRunnable;
30
31 /**
32 *
33 * @author Antonio Petrelli
34 * @version 0.2.0
35 */
36 public class MessageSpy extends javax.swing.JFrame implements RunningRunnable {
37
38 /** Creates new form MessageSpy */
39 public MessageSpy() {
40 initComponents();
41 titles = new LinkedList();
42 texts = new LinkedList();
43 cboFontSize.setSelectedItem(Integer.toString(txaText.getFont().getSize()));
44 exitOnClose = false;
45 }
46
47 public void wakeUp() {
48 showText(pos);
49 }
50
51 public void add(String title, String text) {
52 titles.add(title);
53 texts.add(text);
54 pos = titles.size() - 1;
55 MoreSwingUtilities.invokeWakingUp(this);
56 }
57
58 public void setExitOnClose(boolean value) {
59 exitOnClose = value;
60 }
61
62 /** This method is called from within the constructor to
63 * initialize the form.
64 * WARNING: Do NOT modify this code. The content of this method is
65 * always regenerated by the Form Editor.
66 */
67 private void initComponents() {//GEN-BEGIN:initComponents
68 tlbButtons = new javax.swing.JToolBar();
69 butBack = new javax.swing.JButton();
70 butForward = new javax.swing.JButton();
71 butClear = new javax.swing.JButton();
72 cboFontSize = new javax.swing.JComboBox();
73 panText = new javax.swing.JPanel();
74 lblTitle = new javax.swing.JLabel();
75 scpText = new javax.swing.JScrollPane();
76 txaText = new javax.swing.JTextArea();
77
78 setTitle("Message Spy");
79 addWindowListener(new java.awt.event.WindowAdapter() {
80 public void windowClosing(java.awt.event.WindowEvent evt) {
81 exitForm(evt);
82 }
83 });
84
85 butBack.setIcon(new javax.swing.ImageIcon(getClass().getResource("/res/back.png")));
86 butBack.setEnabled(false);
87 butBack.addActionListener(new java.awt.event.ActionListener() {
88 public void actionPerformed(java.awt.event.ActionEvent evt) {
89 butBackActionPerformed(evt);
90 }
91 });
92
93 tlbButtons.add(butBack);
94
95 butForward.setIcon(new javax.swing.ImageIcon(getClass().getResource("/res/forward.png")));
96 butForward.setEnabled(false);
97 butForward.addActionListener(new java.awt.event.ActionListener() {
98 public void actionPerformed(java.awt.event.ActionEvent evt) {
99 butForwardActionPerformed(evt);
100 }
101 });
102
103 tlbButtons.add(butForward);
104
105 butClear.setIcon(new javax.swing.ImageIcon(getClass().getResource("/res/editdelete.png")));
106 butClear.addActionListener(new java.awt.event.ActionListener() {
107 public void actionPerformed(java.awt.event.ActionEvent evt) {
108 butClearActionPerformed(evt);
109 }
110 });
111
112 tlbButtons.add(butClear);
113
114 cboFontSize.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "3", "5", "8", "10", "12", "14", "18", "24", "36", "48" }));
115 cboFontSize.addActionListener(new java.awt.event.ActionListener() {
116 public void actionPerformed(java.awt.event.ActionEvent evt) {
117 cboFontSizeActionPerformed(evt);
118 }
119 });
120
121 tlbButtons.add(cboFontSize);
122
123 getContentPane().add(tlbButtons, java.awt.BorderLayout.NORTH);
124
125 panText.setLayout(new java.awt.BorderLayout());
126
127 lblTitle.setText("This label will change");
128 panText.add(lblTitle, java.awt.BorderLayout.NORTH);
129
130 scpText.setPreferredSize(new java.awt.Dimension(420, 420));
131 txaText.setFont(new java.awt.Font("Courier", 0, 12));
132 scpText.setViewportView(txaText);
133
134 panText.add(scpText, java.awt.BorderLayout.CENTER);
135
136 getContentPane().add(panText, java.awt.BorderLayout.CENTER);
137
138 pack();
139 }//GEN-END:initComponents
140
141 private void cboFontSizeActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cboFontSizeActionPerformed
142 // Add your handling code here:
143 try {
144 txaText.setFont(new java.awt.Font("Courier", 0, Integer.parseInt((String) cboFontSize.getSelectedItem())));
145 }
146 catch (NumberFormatException e) {
147 System.out.println(e.getMessage());
148 }
149 }//GEN-LAST:event_cboFontSizeActionPerformed
150
151 private void butClearActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_butClearActionPerformed
152 // Add your handling code here:
153 titles.clear();
154 texts.clear();
155 butBack.setEnabled(false);
156 butForward.setEnabled(false);
157 txaText.setText("");
158 lblTitle.setText("No message");
159 }//GEN-LAST:event_butClearActionPerformed
160
161 private void butForwardActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_butForwardActionPerformed
162 // Add your handling code here:
163 pos++;
164 showText(pos);
165 }//GEN-LAST:event_butForwardActionPerformed
166
167 private void butBackActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_butBackActionPerformed
168 // Add your handling code here:
169 pos--;
170 showText(pos);
171 }//GEN-LAST:event_butBackActionPerformed
172
173 /** Exit the Application */
174 private void exitForm(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_exitForm
175 if (exitOnClose)
176 System.exit(0);
177 }//GEN-LAST:event_exitForm
178
179
180 // Variables declaration - do not modify//GEN-BEGIN:variables
181 private javax.swing.JPanel panText;
182 private javax.swing.JTextArea txaText;
183 private javax.swing.JButton butForward;
184 private javax.swing.JButton butBack;
185 private javax.swing.JLabel lblTitle;
186 private javax.swing.JScrollPane scpText;
187 private javax.swing.JButton butClear;
188 private javax.swing.JToolBar tlbButtons;
189 private javax.swing.JComboBox cboFontSize;
190 // End of variables declaration//GEN-END:variables
191
192 private List titles;
193 private List texts;
194 private int pos;
195 private boolean exitOnClose;
196
197 private void showText(int pos) {
198 boolean showBack, showForward;
199
200 lblTitle.setText(((String) titles.get(pos)) + ": " + Integer.toString(pos+1) + " of " +
201 Integer.toString(titles.size()));
202 txaText.setText((String) texts.get(pos));
203 showBack = true;
204 showForward = true;
205 if (pos == 0) {
206 showBack = false;
207 }
208 if (pos == titles.size() - 1) {
209 showForward = false;
210 }
211 butBack.setEnabled(showBack);
212 butForward.setEnabled(showForward);
213 }
214 }