KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > util > explorer > swing > gui > lib > DefaultDialogConsole


1 /*====================================================================
2
3 Objectweb Explorer Framework
4 Copyright (C) 2000-2004 INRIA - USTL - LIFL - GOAL
5 Contact: openccm@objectweb.org
6
7 This library is free software; you can redistribute it and/or
8 modify it under the terms of the GNU Lesser General Public
9 License as published by the Free Software Foundation; either
10 version 2.1 of the License, or any later version.
11
12 This library is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 Lesser General Public License for more details.
16
17 You should have received a copy of the GNU Lesser General Public
18 License along with this library; if not, write to the Free Software
19 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
20 USA
21
22 Initial developer(s): Jerome Moroy, Philippe Merle.
23 Contributor(s): ______________________________________.
24
25 ====================================================================*/

26
27 package org.objectweb.util.explorer.swing.gui.lib;
28
29 import java.util.Calendar JavaDoc;
30 import java.util.GregorianCalendar JavaDoc;
31
32 import java.awt.Color JavaDoc;
33 import java.awt.Dimension JavaDoc;
34 import javax.swing.JDialog JavaDoc;
35 import javax.swing.JTextArea JavaDoc;
36 import javax.swing.Box JavaDoc;
37 import javax.swing.JButton JavaDoc;
38 import javax.swing.AbstractAction JavaDoc;
39
40 import org.objectweb.util.explorer.swing.gui.api.Console;
41
42
43 import java.awt.event.ActionEvent JavaDoc;
44
45 /**
46  * This class defines a graphical console.
47  *
48  * @author <a HREF="mailto:Jerome.Moroy@lifl.fr">Jerome Moroy</a>
49  *
50  * @version 0.1
51  */

52 public class DefaultDialogConsole
53     extends JDialog JavaDoc
54     implements Console {
55
56     //==================================================================
57
//
58
// Internal states.
59
//
60
//==================================================================
61

62     protected JTextArea JavaDoc textArea_ = null;
63
64     protected Calendar JavaDoc calendar_ = null;
65
66     //==================================================================
67
//
68
// Constructor.
69
//
70
//==================================================================
71

72     /**
73      * Default constructor
74      */

75     public DefaultDialogConsole(String JavaDoc title, boolean isModal){
76         super();
77         
78         setModal(isModal);
79         setTitle(title);
80         getContentPane().add(createBox());
81         setDefaultCloseOperation(javax.swing.WindowConstants.HIDE_ON_CLOSE);
82         //setResizable(false);
83
pack();
84
85         // Centers the frame
86
Dimension JavaDoc screenSize = java.awt.Toolkit.getDefaultToolkit().getScreenSize();
87         setLocation((screenSize.width - getWidth()) / 2, (screenSize.height - getHeight()) / 2);
88     }
89     
90     //==================================================================
91
//
92
// Internal methods.
93
//
94
//==================================================================
95

96     /**
97      * Adds the buttons into the buttons panel.
98      * @param buttonBox The panel which will contain the buttons.
99      */

100     public void addButtons(Box JavaDoc buttonBox){
101         buttonBox.add(new JButton JavaDoc(new ClearAction()));
102         buttonBox.add(Box.createHorizontalStrut(10));
103         buttonBox.add(new JButton JavaDoc(new HideAction()));
104     }
105
106     /**
107      * Creates the console box.
108      */

109     protected Box JavaDoc createBox(){
110         Box JavaDoc mainBox = Box.createVerticalBox();
111
112         mainBox.add(Box.createVerticalGlue());
113         
114         textArea_ = new JTextArea JavaDoc(25,40);
115         textArea_.setBorder(javax.swing.BorderFactory.createLineBorder(Color.black));
116         textArea_.setEditable(false);
117         textArea_.setLineWrap(true);
118
119         mainBox.add(new javax.swing.JScrollPane JavaDoc(textArea_));
120
121         mainBox.add(Box.createVerticalStrut(10));
122
123         Box JavaDoc buttonBox = Box.createHorizontalBox();
124         buttonBox.add(Box.createHorizontalGlue());
125         addButtons(buttonBox);
126         buttonBox.add(Box.createHorizontalGlue());
127         mainBox.add(buttonBox);
128
129         mainBox.add(Box.createVerticalStrut(10));
130         mainBox.add(Box.createVerticalGlue());
131
132         return mainBox;
133         
134     }
135
136     protected String JavaDoc getNumber(int num){
137         return (num>9)?("" + num):("0" + num);
138     }
139
140     protected String JavaDoc getCurrentDate(){
141         calendar_ = new GregorianCalendar JavaDoc();
142         StringBuffer JavaDoc theDate = new StringBuffer JavaDoc();
143         theDate.append(calendar_.get(Calendar.YEAR));
144         theDate.append("/");
145         theDate.append(getNumber(calendar_.get(Calendar.MONTH) + 1));
146         theDate.append("/");
147         theDate.append(getNumber(calendar_.get(Calendar.DAY_OF_MONTH)));
148         theDate.append(" - ");
149         theDate.append((calendar_.get(Calendar.AM_PM)==0)?"AM":"PM");
150         theDate.append(" ");
151         theDate.append(getNumber(calendar_.get(Calendar.HOUR)));
152         theDate.append(":");
153         theDate.append(getNumber(calendar_.get(Calendar.MINUTE)));
154         theDate.append(":");
155         theDate.append(getNumber(calendar_.get(Calendar.SECOND)));
156         return theDate.toString();
157     }
158     
159     //==================================================================
160
//
161
// Public methods for Console interface.
162
//
163
//==================================================================
164

165     /**
166      * Adds a message into the console.
167      * @param message The message to display.
168      */

169     public void add(String JavaDoc message){
170         textArea_.append("[" + getCurrentDate() + "] ");
171         textArea_.append(message);
172     }
173
174     /**
175      * Clears the contains of the console.
176      */

177     public void clear(){
178         textArea_.setText("");
179     }
180
181     /**
182      * Shows the console.
183      */

184     public void show(){
185         super.show();
186     }
187
188     /**
189      * Hides the console.
190      */

191     public void hide(){
192         super.hide();
193     }
194
195     //==================================================================
196
//
197
// Protected classes for button actions.
198
//
199
//==================================================================
200

201     protected class ClearAction
202         extends AbstractAction JavaDoc {
203
204         public ClearAction(){
205             super("Clear");
206         }
207
208         public void actionPerformed(ActionEvent JavaDoc e){
209             clear();
210         }
211
212     }
213
214     protected class HideAction
215         extends AbstractAction JavaDoc {
216
217         public HideAction(){
218             super("Close");
219         }
220
221         public void actionPerformed(ActionEvent JavaDoc e){
222             hide();
223         }
224     }
225
226 }
227
Popular Tags