/* * MessageArea.java * * Copyright (c) 2001-2002 Digi International * This program and the information contained in it is confidential and * proprietary to Digi International and may not be used, copied, or re- * produced without the prior written permission of Digi International. * */ package com.digi.config.ui; import com.digi.config.util.*; import java.awt.*; import java.awt.event.MouseAdapter; import java.awt.event.MouseEvent; import java.util.Calendar; import java.util.StringTokenizer; import javax.swing.*; import javax.swing.event.ListDataEvent; import javax.swing.event.ListDataListener; /** * This is the portion of the GUI that shows informative messages to the user. * Messages sent from the systemLog related to this user will show up here. */ public class MessageArea extends CustomPanel implements ListDataListener { private static ImageIcon fatalIcon =null; private static ImageIcon errorIcon =null; private static ImageIcon warningIcon =null; private static ImageIcon informationIcon=null; private static ImageIcon debugIcon =null; private static ImageIcon detailFatalIcon =null; private static ImageIcon detailErrorIcon =null; private static ImageIcon detailWarningIcon =null; private static ImageIcon detailInformationIcon=null; private static ImageIcon detailDebugIcon =null; private static BasicMsgFrame detailFrame=null; SystemLogCtlr systemLogCtlr; JList logList; JScrollPane scrollPane; ControllerListModel logListModel; MessageLogFilter logFilter; long clearTime; boolean priorMessageFilter; int minimumSeverityFilter; JPopupMenu popupMenu; //-------------------------------------------------------------------------- // Commonly Used Job Related Controller Actions //-------------------------------------------------------------------------- protected ConfigAction clearLogAction; protected ConfigAction refreshLogAction; protected ConfigAction setMinimumSeverityFatalFilterAction; protected ConfigAction setMinimumSeverityErrorFilterAction; protected ConfigAction setMinimumSeverityWarningFilterAction; protected ConfigAction setMinimumSeverityInformationFilterAction; protected ConfigAction setMinimumSeverityDebugFilterAction; public MessageArea() throws Exception { super("Message"); setLayout(new BorderLayout()); // Initialize the icons to be used fatalIcon=ConfigResource.getImageRbIcon("FatalImage"); errorIcon=ConfigResource.getImageRbIcon("ErrorImage"); warningIcon=ConfigResource.getImageRbIcon("WarningImage"); informationIcon=ConfigResource.getImageRbIcon("InformationImage"); debugIcon=ConfigResource.getImageRbIcon("DebugImage"); detailFatalIcon=ConfigResource.getImageRbIcon("DetailedFatalImage"); detailErrorIcon=ConfigResource.getImageRbIcon("DetailedErrorImage"); detailWarningIcon=ConfigResource.getImageRbIcon("DetailedWarningImage"); detailInformationIcon=ConfigResource.getImageRbIcon("DetailedInformationImage"); detailDebugIcon=ConfigResource.getImageRbIcon("DetailedDebugImage"); // Access the SystemLogCtlr systemLogCtlr=SystemLog.getSystemLogCtlr(); // Create a list Model that observes the ctlr logListModel=new ControllerListModel(systemLogCtlr); // Add a filter to the listModel that filters out log items // based on start date and possibly severity clearTime=Calendar.getInstance().getTime().getTime(); logFilter=new MessageLogFilter(); logListModel.addFilter(logFilter); setPriorMessageFilter(ConfigSettings.getBoolean("WindowSettings", "MessageAreaPriorMessageFilter")); this.setMinimumSeverityFilter(ConfigSettings.getInteger( "WindowSettings", "MessageAreaMinimumSeverityFilter")); // Create a JList that uses the custom list model logList=new JList(logListModel); logList.setOpaque(false); logList.setSelectionBackground(Color.BLACK); logList.setSelectionForeground(Color.WHITE); logList.setBackground(new Color(0,0,0)); logListModel.addListDataListener(this); logList.setCellRenderer(new LogListRenderer()); logList.addMouseListener( new MouseAdapter() { /** * This method is called when the mouse is clicked on * an item in the list. */ public void mouseClicked(MouseEvent e) { if (e.getClickCount()==2) { //Get selected item // See if it has detailed messages // Show it in a frame SystemLogEntry logItem=null; try { logItem=(SystemLogEntry)logList.getSelectedValue(); } catch (Exception exc) { return; } if(logItem==null) { return; } String newline=System.getProperty("line.separator"); StringBuffer info=new StringBuffer(); info.append("MsgId: " +logItem.getServiceId()+newline); info.append("Time: " +logItem.getCreationTime().getTime(). toString()+newline); info.append("System: " +logItem.getSystemName()+newline); info.append("Severity: " +logItem.getSeverity()+ newline+newline); info.append("Text: " +logItem.getMsg()+newline+newline); if (logItem.hasDetailedMsg()) { info.append("Detailed Description: " + logItem.getDetailedMsg()+newline+newline); } SystemLogEntry.ExceptionInfo excInfo= logItem.getExceptionInfo(); if (excInfo!=null) { info.append("Exception Information:" +newline+ excInfo.callStack); } if (detailFrame==null) { String frameTitle=ConfigResource.getUiRbString( "MsgDetailsFrameTitle"); detailFrame=new BasicMsgFrame(frameTitle); } detailFrame.showText(info.toString()); // could do logList.getBackground() } } }); // Now plop the JList in a scroll pane which is added to the MessageArea // panel scrollPane=new JScrollPane(logList); scrollPane.setOpaque(false); scrollPane.getViewport().setOpaque(false); add(scrollPane, BorderLayout.CENTER); // properly size ourselves int width=ConfigSettings.getInteger("WindowSettings", "MessageAreaWidth"); int height=ConfigSettings.getInteger("WindowSettings", "MessageAreaHeight"); this.setSize(width, height); // Add the popup menu this.defineActions(); this.addPopupMenu(); } /** * Save the layout information for this message area */ public void saveLayout() { ConfigSettings.setInteger("WindowSettings", "MessageAreaWidth", this.getWidth()); ConfigSettings.setInteger("WindowSettings", "MessageAreaHeight", this.getHeight()); } /** * Controls whether messages prior to startup are filtered out of the log or * not * * @param filterPrevious - if true, then messages that occured before * startup of the client will not be displayed in the message area */ public void setPriorMessageFilter(boolean filterPrevious) { this.priorMessageFilter=filterPrevious; if (filterPrevious) { this.logFilter.setMinimumCreateTime(this.clearTime); } else { this.logFilter.setMinimumCreateTime(0); } this.logListModel.updateFilter(logFilter); ConfigSettings.setBoolean("WindowSettings", "MessageAreaPriorMessageFilter", priorMessageFilter); } /** * Controls filtering of messages in the message area based upon their * severity. * * @param minimumSeverityLevel - The lowest message severity level to * display in the log. Messages with lower severity will not be * displayed in the message area * * @see SystemLogEntry */ public void setMinimumSeverityFilter(int minimumSeverityLevel) { this.minimumSeverityFilter=minimumSeverityLevel; this.logFilter.setMinimumSeverity(minimumSeverityLevel); this.logListModel.updateFilter(logFilter); ConfigSettings.setInteger("WindowSettings", "MessageAreaMinimumSeverityFilter", minimumSeverityFilter); } /** * Defines the main actions that can be invoked from the menus or * toolbar buttons. These actions will be used to construct the menus & * toolbar buttons. */ private void defineActions() throws Exception { clearLogAction=new ConfigAction("ClearLog", this, "doClearLogAction"); refreshLogAction=new ConfigAction("RefreshLog", this, "doRefreshLogAction"); setMinimumSeverityFatalFilterAction= new ConfigAction("Fatal", this, "doSetMinimumSeverityFatalFilterAction"); setMinimumSeverityErrorFilterAction= new ConfigAction("Error", this, "doSetMinimumSeverityErrorFilterAction"); setMinimumSeverityWarningFilterAction= new ConfigAction("Warning", this, "doSetMinimumSeverityWarningFilterAction"); setMinimumSeverityInformationFilterAction= new ConfigAction("Information", this, "doSetMinimumSeverityInformationFilterAction"); setMinimumSeverityDebugFilterAction= new ConfigAction("Debug", this, "doSetMinimumSeverityDebugFilterAction"); } /** * Define the Log menu. * @return a menu for the Log label. */ public JMenu getLogMenu() { JMenu menu; JMenu subMenu; JMenuItem menuItem; String label; label = ConfigResource.getUiRbString("LogMenu"); menu = new JMenu(label); menu.setMnemonic (ConfigResource.getUiRbString("LogMnemonic").charAt(0)); menuItem = menu.add(clearLogAction); clearLogAction.addDecorations(menuItem); menuItem = menu.add(refreshLogAction); refreshLogAction.addDecorations(menuItem); subMenu=new JMenu(ConfigResource.getUiRbString("MinimumSeverityMenu")); subMenu.setToolTipText(ConfigResource.getUiRbString( "MinimumSeverityTip")); subMenu.setMnemonic(ConfigResource.getUiRbString( "MinimumSeverityMnemonic").charAt(0)); subMenu.add(setMinimumSeverityFatalFilterAction); subMenu.add(setMinimumSeverityErrorFilterAction); subMenu.add(setMinimumSeverityWarningFilterAction); subMenu.add(setMinimumSeverityInformationFilterAction); subMenu.add(setMinimumSeverityDebugFilterAction); menuItem = menu.add(subMenu); return menu; } /** * Add a popup menu to the message area list */ public void addPopupMenu() { JMenu menu; JMenuItem menuItem=null; popupMenu=new JPopupMenu(); //JMenuItem item = new JCheckBoxMenuItem( // (String)refreshLogAction.getValue(Action.NAME), // this.priorMessageFilter); //item.addActionListener(refreshLogAction); //popupMenu.add(item); menuItem = popupMenu.add(clearLogAction); clearLogAction.addDecorations(menuItem); menuItem = popupMenu.add(refreshLogAction); refreshLogAction.addDecorations(menuItem); menu=new JMenu(ConfigResource.getUiRbString("MinimumSeverityMenu")); menu.setToolTipText(ConfigResource.getUiRbString( "MinimumSeverityTip")); menu.setMnemonic(ConfigResource.getUiRbString( "MinimumSeverityMnemonic").charAt(0)); menu.add(setMinimumSeverityFatalFilterAction); menu.add(setMinimumSeverityErrorFilterAction); menu.add(setMinimumSeverityWarningFilterAction); menu.add(setMinimumSeverityInformationFilterAction); menu.add(setMinimumSeverityDebugFilterAction); menuItem = popupMenu.add(menu); logList.addMouseListener(new MouseAdapter() { public void mouseClicked(MouseEvent e) { if (SwingUtilities.isRightMouseButton(e)) { popupMenu.show(logList, e.getX(), e.getY()); } } }); } /** * This method performs the logic of the doRefreshLogAction operation. */ public void doClearLogAction() throws Exception { // Reset the clear time to only show messages that occur from now on this.clearTime=Calendar.getInstance().getTime().getTime(); this.setPriorMessageFilter(true); } public void doRefreshLogAction() throws Exception { this.setPriorMessageFilter(false); } public void doSetMinimumSeverityFatalFilterAction() throws Exception { this.setMinimumSeverityFilter(SystemLogEntry.MSG_LEVEL_FATAL); } public void doSetMinimumSeverityErrorFilterAction() throws Exception { this.setMinimumSeverityFilter(SystemLogEntry.MSG_LEVEL_ERROR); } public void doSetMinimumSeverityWarningFilterAction() throws Exception { this.setMinimumSeverityFilter(SystemLogEntry.MSG_LEVEL_WARNING); } public void doSetMinimumSeverityInformationFilterAction() throws Exception { this.setMinimumSeverityFilter(SystemLogEntry.MSG_LEVEL_INFORMATION); } public void doSetMinimumSeverityDebugFilterAction() throws Exception { this.setMinimumSeverityFilter(SystemLogEntry.MSG_LEVEL_DEBUG); } /** * This class is responsible for properly rendering SystemLogEntries in the * message area. */ private class LogListRenderer extends DefaultListCellRenderer implements ListCellRenderer { public static final String NEWLINE="\n"; /** * Constructor for Custom Renderer *

*/ public LogListRenderer() { super(); setOpaque(false); } /** * This method returns appropriate imageIcon *

* @param logItem Item to be displayed * @return ImageIcon */ public ImageIcon getImageIcon(Object logItem) { ImageIcon image=informationIcon; // Information by default SystemLogEntry value=(SystemLogEntry)logItem; switch (value.getSeverity()) { case SystemLogEntry.MSG_LEVEL_FATAL: if (value.hasDetailedMsg()) { image=detailFatalIcon; } else { image=fatalIcon; } break; case SystemLogEntry.MSG_LEVEL_ERROR: if (value.hasDetailedMsg()) { image=detailErrorIcon; } else { image=errorIcon; } break; case SystemLogEntry.MSG_LEVEL_WARNING: if (value.hasDetailedMsg()) { image=detailWarningIcon; } else { image=warningIcon; } break; case SystemLogEntry.MSG_LEVEL_INFORMATION: if (value.hasDetailedMsg()) { image=detailInformationIcon; } break; case SystemLogEntry.MSG_LEVEL_DEBUG: if (value.hasDetailedMsg()) { image=detailDebugIcon; } else { image=debugIcon; } break; default: break; } return image; } /** * This method implements the ListRenderer interface method *

* @param list The list * @param listLog The object to be displayed. * @param index Index of the item in the list * @param isSelected If the cell is selected * @param cellHasFocus If the cell has focus */ public Component getListCellRendererComponent(JList list, Object listLog, int index, boolean isSelected, boolean cellHasFocus) { SystemLogEntry value=(SystemLogEntry)listLog; ImageIcon image=getImageIcon(listLog); setIcon((ImageIcon)image); if (isSelected) { setOpaque(true); setBackground(list.getSelectionBackground()); setForeground(list.getSelectionForeground()); if (value.hasDetailedMsg()) { String tip=value.getDetailedMsg(); //If tip is more than 2 lines show the first line // only for tool tip //Does not look nice if it is more than two lines. StringTokenizer tok= new StringTokenizer(tip, NEWLINE, false); if (tok.countTokens()>2) { tip=tok.nextToken(); } list.setToolTipText(tip); } else { list.setToolTipText(null); } } else { setOpaque(false); setBackground(list.getBackground()); setForeground(list.getForeground()); } setFont(list.getFont()); setText((value==null)?"" :value.getMsg()); return this; } } /** * This class is responsible for putting messages in a frame for detailed * messages. */ public class BasicMsgFrame extends JFrame { // Text Area to display detailed message JTextArea textArea; /** * This is constructor for detailed frame *

* @param title Title to be displayed */ public BasicMsgFrame(String title) { super(title); setBackground(ConfigSettings.getColor("WindowSettings","BackgroundColor")); setDefaultCloseOperation(HIDE_ON_CLOSE); textArea=new JTextArea(20, 100); textArea.setEditable(false); JScrollPane scrollPane=new JScrollPane(textArea); scrollPane.setOpaque(false); scrollPane.getViewport().setOpaque(false); getContentPane().add(scrollPane, BorderLayout.CENTER); } /** * This method shows the text inside of the frame *

* @param txt Text to display */ public void showText(String txt) { textArea.setText(txt); textArea.setCaretPosition(0); // move to top of window pack(); setVisible(true); } /** * This method shows the text inside of the frame *

* @param txt Text to display * @param c Color to set the back ground */ public void showText(String txt, Color c) { textArea.setText(txt); textArea.setBackground(c); textArea.setCaretPosition(0); // move to top of window pack(); setVisible(true); } } /** * This implements contentsChanged method of ListDataListener Interface *

* @param e ListDataEvent */ public void contentsChanged(ListDataEvent e) { } /** * This implements intervalAdded method of ListDataListener Interface *

* @param e ListDataEvent */ public void intervalAdded(javax.swing.event.ListDataEvent e) { final int index=logListModel.getSize(); if (index>0) { SwingUtilities.invokeLater(new Runnable() { public void run() { logList.ensureIndexIsVisible(index-1); } }); } } /** * This implements intervalRemoved method in ListDataListener Interface * @param e ListDataEvent. */ public void intervalRemoved(ListDataEvent e) { } }