1 26 package org.objectweb.util.browser.gui.lib; 27 28 import java.util.Calendar ; 29 import java.util.GregorianCalendar ; 30 31 import java.awt.Color ; 32 import java.awt.Dimension ; 33 import javax.swing.JDialog ; 34 import javax.swing.JTextArea ; 35 import javax.swing.Box ; 36 import javax.swing.JButton ; 37 import javax.swing.AbstractAction ; 38 39 import org.objectweb.util.browser.gui.api.Console; 40 41 import java.awt.event.ActionEvent ; 42 43 50 public class DefaultDialogConsole 51 extends JDialog 52 implements Console { 53 54 60 protected JTextArea textArea_ = null; 61 62 protected Calendar calendar_ = null; 63 64 70 73 public DefaultDialogConsole(String 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 pack(); 82 83 Dimension screenSize = java.awt.Toolkit.getDefaultToolkit().getScreenSize(); 85 setLocation((screenSize.width - getWidth()) / 2, (screenSize.height - getHeight()) / 2); 86 } 87 88 94 98 public void addButtons(Box buttonBox){ 99 buttonBox.add(new JButton (new ClearAction())); 100 buttonBox.add(Box.createHorizontalStrut(10)); 101 buttonBox.add(new JButton (new HideAction())); 102 } 103 104 107 protected Box createBox(){ 108 Box mainBox = Box.createVerticalBox(); 109 110 mainBox.add(Box.createVerticalGlue()); 111 112 textArea_ = new JTextArea (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 (textArea_)); 118 119 mainBox.add(Box.createVerticalStrut(10)); 120 121 Box 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 getNumber(int num){ 135 return (num>9)?("" + num):("0" + num); 136 } 137 138 protected String getCurrentDate(){ 139 calendar_ = new GregorianCalendar (); 140 StringBuffer theDate = new StringBuffer (); 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 163 167 public void add(String message){ 168 textArea_.append("[" + getCurrentDate() + "] "); 169 textArea_.append(message); 170 } 171 172 175 public void clear(){ 176 textArea_.setText(""); 177 } 178 179 182 public void show(){ 183 super.show(); 184 } 185 186 189 public void hide(){ 190 super.hide(); 191 } 192 193 199 protected class ClearAction 200 extends AbstractAction { 201 202 public ClearAction(){ 203 super("Clear"); 204 } 205 206 public void actionPerformed(ActionEvent e){ 207 clear(); 208 } 209 210 } 211 212 protected class HideAction 213 extends AbstractAction { 214 215 public HideAction(){ 216 super("Close"); 217 } 218 219 public void actionPerformed(ActionEvent e){ 220 hide(); 221 } 222 } 223 224 } 225 | Popular Tags |