KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > util > browser > gui > lib > DefaultDialogConsole


1 /*====================================================================
2
3 OpenCCM: The Open CORBA Component Model Platform
4 Copyright (C) 2000-2003 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.
23 Contributor(s): ______________________________________.
24
25 ====================================================================*/

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

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

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

70     /**
71      * Default constructor
72      */

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

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

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

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

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

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

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

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

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

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