1 3 27 28 30 package de.qfs.apps.qflog.logview; 31 32 34 import java.awt.Color ; 35 import java.awt.GridBagLayout ; 36 import java.awt.GridBagConstraints ; 37 import java.awt.BorderLayout ; 38 import java.awt.event.ActionEvent ; 39 import java.awt.event.ActionListener ; 40 import java.awt.event.FocusEvent ; 41 import java.awt.event.FocusListener ; 42 import java.awt.event.KeyAdapter ; 43 import java.awt.event.KeyEvent ; 44 45 import java.text.SimpleDateFormat ; 46 47 import java.util.Date ; 48 49 import javax.swing.BorderFactory ; 50 import javax.swing.Icon ; 51 import javax.swing.JLabel ; 52 import javax.swing.JPanel ; 53 import javax.swing.JScrollPane ; 54 import javax.swing.JTextArea ; 55 import javax.swing.KeyStroke ; 56 import javax.swing.border.Border ; 57 58 import de.qfs.lib.gui.Borders; 59 import de.qfs.lib.gui.SwingUtil; 60 import de.qfs.lib.log.Log; 61 import de.qfs.lib.log.LogEntry; 62 import de.qfs.lib.log.Logger; 63 import de.qfs.lib.util.MapResourceBundle; 64 65 67 73 public class LogDetailView extends JPanel 74 { 75 77 80 private final static Logger logger = new Logger (LogDetailView.class); 81 82 85 private static SimpleDateFormat df = new SimpleDateFormat ("HH:mm:ss.SSS"); 86 87 90 private final static String iconNames[] = { 91 "logTableView.icon.err", "logTableView.icon.errDetail", 92 "logTableView.icon.wrn", "logTableView.icon.wrnDetail", 93 "logTableView.icon.msg", "logTableView.icon.msgDetail", 94 "logTableView.icon.mtd", "logTableView.icon.mtdDetail", 95 "logTableView.icon.dbg", "logTableView.icon.dbgDetail"}; 96 97 100 private final static Icon [] icons = new Icon [iconNames.length]; 101 102 105 JLabel level; 106 107 110 JLabel time; 111 112 115 JLabel thread; 116 117 120 JLabel clazz; 121 122 125 JLabel method; 126 127 130 MessageView detail; 131 132 135 private boolean logging; 136 137 140 private transient MapResourceBundle resources; 141 142 144 146 149 public LogDetailView () 150 { 151 } 152 153 155 157 169 public final void setLoggingEnabled(boolean enable) 170 { 171 logging = enable; 172 } 173 174 176 178 181 protected void init() 182 { 183 if (logging && logger.level >= Log.MTD) { 184 logger.log(Log.MTD, "init()", ""); 185 } 186 187 initIcons(); 188 189 setLayout(new BorderLayout ()); 190 191 JPanel top = new JPanel (); 192 top.setLayout(new BorderLayout ()); 193 top.setBorder(null); 194 195 String title = resources.getString("logTableView.detail.title", 196 "Detail"); 197 JLabel titleLabel = new JLabel (title); 198 titleLabel.setBorder(BorderFactory.createEtchedBorder()); 199 top.add(titleLabel, BorderLayout.NORTH); 200 201 add(top, BorderLayout.NORTH); 202 203 JPanel center = new JPanel (); 204 center.setBorder(BorderFactory.createCompoundBorder 205 (BorderFactory.createLoweredBevelBorder(), 206 BorderFactory.createEmptyBorder(5, 5, 5, 5))); 207 208 GridBagLayout layout = new GridBagLayout (); 209 center.setLayout(layout); 210 211 GridBagConstraints cons = new GridBagConstraints (); 212 213 level = new JLabel (" "); 214 cons.gridx = 0; 215 cons.gridy = 0; 216 cons.gridwidth = 1; 217 cons.weightx = 1.0; 218 cons.weighty = 0; 219 cons.anchor = GridBagConstraints.NORTHWEST; 220 layout.setConstraints(level, cons); 221 center.add(level); 222 223 time = new JLabel (" "); 224 cons.gridx = 1; 225 cons.gridy = 0; 226 cons.gridwidth = 1; 227 cons.weightx = 1.0; 228 cons.weighty = 0; 229 cons.anchor = GridBagConstraints.NORTHWEST; 230 layout.setConstraints(time, cons); 231 center.add(time); 232 233 thread = new JLabel (" "); 234 cons.gridx = 2; 235 cons.gridy = 0; 236 cons.gridwidth = 2; 237 cons.weightx = 2.0; 238 cons.weighty = 0; 239 cons.anchor = GridBagConstraints.NORTHWEST; 240 layout.setConstraints(thread, cons); 241 center.add(thread); 242 243 clazz = new JLabel (" "); 244 cons.gridx = 0; 245 cons.gridy = 1; 246 cons.gridwidth = 2; 247 cons.weightx = 2.0; 248 cons.weighty = 0; 249 cons.anchor = GridBagConstraints.NORTHWEST; 250 layout.setConstraints(clazz, cons); 251 center.add(clazz); 252 253 method = new JLabel (" "); 254 cons.gridx = 2; 255 cons.gridy = 1; 256 cons.gridwidth = 2; 257 cons.weightx = 2.0; 258 cons.weighty = 0; 259 cons.anchor = GridBagConstraints.NORTHWEST; 260 layout.setConstraints(method, cons); 261 center.add(method); 262 263 detail = new MessageView (""); 264 cons.gridx = 0; 265 cons.gridy = 2; 266 cons.gridwidth = 4; 267 cons.weightx = 4.0; 268 cons.weighty = 1.0; 269 cons.fill = GridBagConstraints.BOTH; 270 cons.anchor = GridBagConstraints.NORTHWEST; 271 layout.setConstraints(detail, cons); 272 center.add(detail); 273 274 add(center, BorderLayout.CENTER); 275 } 276 277 280 283 public void cleanup() 284 { 285 if (logging && logger.level >= Log.MTD) { 286 logger.log(Log.MTD, "cleanup()", ""); 287 } 288 289 SwingUtil.cleanup(this); 290 291 detail.cleanup(); 292 detail = null; 293 294 resources = null; 295 } 296 297 300 303 public void finalize() 304 { 305 if (logging && logger.level >= Log.MTD) { 306 logger.log(Log.MTD, "finalize()", ""); 307 } 308 } 309 310 312 314 319 public void setResources(MapResourceBundle rb) 320 { 321 resources = rb; 322 } 323 324 327 332 public void setEntry(LogEntry entry) 333 { 334 if (entry != null) { 335 Icon icon = null; 336 if (icons[0] != null 337 && entry.getLevel() >= 0 338 && entry.getLevel() < icons.length) { 339 icon = icons[entry.getLevel() - 1]; 340 } 341 level.setIcon(icon); 342 level.setText(icon == null ? "" + entry.getLevel() : null); 343 time.setText(df.format (new Date (entry.getTimestamp()))); 344 thread.setText(entry.getThread()); 345 method.setText(entry.getMethod()); 346 clazz.setText(entry.getClazz()); 347 detail.getTextArea().setText(entry.getMessage()); 348 detail.getTextArea().setCaretPosition(0); 349 } else { 350 level.setText(" "); 351 time.setText(" "); 352 thread.setText(" "); 353 method.setText(" "); 354 clazz.setText(" "); 355 detail.getTextArea().setText(" "); 356 } 357 } 358 359 361 363 366 private void initIcons() 367 { 368 if (icons[0] == null && resources != null) { 369 for (int i = 0; i < icons.length; i++) { 370 icons[i] = resources.getIcon(iconNames[i], null); 371 } 372 } 373 } 374 375 377 379 381 384 private final static Logger mvLogger = new Logger (MessageView.class); 385 386 388 392 private class MessageView 393 extends JScrollPane 394 implements FocusListener 395 { 396 398 401 private Border defaultBorder; 402 403 406 private Border focusBorder; 407 408 411 private JTextArea textArea; 412 413 416 private KeyAdapter scrollListener = new KeyAdapter () { 417 public void keyPressed (KeyEvent e) { 418 if (e.getKeyCode() == e.VK_UP && 419 e.getModifiers() == 0) { 420 SwingUtil.scrollDownLine(MessageView.this); 421 e.consume(); 422 } else if (e.getKeyCode() == e.VK_DOWN && 423 e.getModifiers() == 0) { 424 SwingUtil.scrollUpLine(MessageView.this); 425 e.consume(); 426 } 427 } 428 }; 429 430 432 434 439 public MessageView (String contents) 440 { 441 textArea = new JTextArea (contents) { 442 public boolean isManagingFocus() 443 { 444 return false; 445 } 446 }; 447 textArea.setLineWrap(true); 448 textArea.setEditable(false); 449 textArea.addFocusListener(this); 450 451 setViewportView(textArea); 452 453 defaultBorder = BorderFactory.createCompoundBorder 454 (BorderFactory.createEmptyBorder(1, 1, 1, 1), 455 BorderFactory.createLoweredBevelBorder()); 456 focusBorder = BorderFactory.createCompoundBorder 457 (Borders.STIPPLE, 458 BorderFactory.createLoweredBevelBorder()); 459 this.setBorder(defaultBorder); 460 461 textArea.addKeyListener(scrollListener); 462 } 463 464 466 468 473 public JTextArea getTextArea() 474 { 475 return textArea; 476 } 477 478 481 484 public void focusGained(FocusEvent e) 485 { 486 this.setBorder(focusBorder); 487 } 488 489 490 493 496 public void focusLost(FocusEvent e) 497 { 498 this.setBorder(defaultBorder); 499 } 500 501 503 505 508 public void cleanup() 509 { 510 if (logging && mvLogger.level >= Log.MTD) { 511 mvLogger.log(Log.MTD, "cleanup()", ""); 512 } 513 setViewportView(null); 514 this.setBorder(null); 515 defaultBorder = null; 516 focusBorder = null; 517 518 textArea.removeKeyListener(scrollListener); 519 scrollListener = null; 520 textArea.removeFocusListener(this); 521 textArea = null; 522 523 resources = null; 524 } 525 526 529 532 public void finalize() 533 { 534 if (logging && mvLogger.level >= Log.MTD) { 535 mvLogger.log(Log.MTD, "finalize()", ""); 536 } 537 } 538 539 } 541 542 } 544 | Popular Tags |