1 26 27 package org.objectweb.util.explorer.swing.gui.lib; 28 29 import java.util.Calendar ; 30 import java.util.GregorianCalendar ; 31 32 import java.awt.Color ; 33 import java.awt.Dimension ; 34 import javax.swing.JDialog ; 35 import javax.swing.JTextArea ; 36 import javax.swing.Box ; 37 import javax.swing.JButton ; 38 import javax.swing.AbstractAction ; 39 40 import org.objectweb.util.explorer.swing.gui.api.Console; 41 42 43 import java.awt.event.ActionEvent ; 44 45 52 public class DefaultDialogConsole 53 extends JDialog 54 implements Console { 55 56 62 protected JTextArea textArea_ = null; 63 64 protected Calendar calendar_ = null; 65 66 72 75 public DefaultDialogConsole(String 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 pack(); 84 85 Dimension screenSize = java.awt.Toolkit.getDefaultToolkit().getScreenSize(); 87 setLocation((screenSize.width - getWidth()) / 2, (screenSize.height - getHeight()) / 2); 88 } 89 90 96 100 public void addButtons(Box buttonBox){ 101 buttonBox.add(new JButton (new ClearAction())); 102 buttonBox.add(Box.createHorizontalStrut(10)); 103 buttonBox.add(new JButton (new HideAction())); 104 } 105 106 109 protected Box createBox(){ 110 Box mainBox = Box.createVerticalBox(); 111 112 mainBox.add(Box.createVerticalGlue()); 113 114 textArea_ = new JTextArea (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 (textArea_)); 120 121 mainBox.add(Box.createVerticalStrut(10)); 122 123 Box 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 getNumber(int num){ 137 return (num>9)?("" + num):("0" + num); 138 } 139 140 protected String getCurrentDate(){ 141 calendar_ = new GregorianCalendar (); 142 StringBuffer theDate = new StringBuffer (); 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 165 169 public void add(String message){ 170 textArea_.append("[" + getCurrentDate() + "] "); 171 textArea_.append(message); 172 } 173 174 177 public void clear(){ 178 textArea_.setText(""); 179 } 180 181 184 public void show(){ 185 super.show(); 186 } 187 188 191 public void hide(){ 192 super.hide(); 193 } 194 195 201 protected class ClearAction 202 extends AbstractAction { 203 204 public ClearAction(){ 205 super("Clear"); 206 } 207 208 public void actionPerformed(ActionEvent e){ 209 clear(); 210 } 211 212 } 213 214 protected class HideAction 215 extends AbstractAction { 216 217 public HideAction(){ 218 super("Close"); 219 } 220 221 public void actionPerformed(ActionEvent e){ 222 hide(); 223 } 224 } 225 226 } 227 | Popular Tags |