001: /*
002: * Copyright 2001-2006 C:1 Financial Services GmbH
003: *
004: * This software is free software; you can redistribute it and/or
005: * modify it under the terms of the GNU Lesser General Public
006: * License Version 2.1, as published by the Free Software Foundation.
007: *
008: * This software is distributed in the hope that it will be useful,
009: * but WITHOUT ANY WARRANTY; without even the implied warranty of
010: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
011: * Lesser General Public License for more details.
012: *
013: * You should have received a copy of the GNU Lesser General Public
014: * License along with this library; if not, write to the Free Software
015: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA
016: */
017:
018: package de.finix.contelligent.client.modules.log;
019:
020: import java.awt.Dimension;
021: import java.awt.event.ActionEvent;
022:
023: import javax.swing.AbstractAction;
024: import javax.swing.Action;
025: import javax.swing.JScrollPane;
026: import javax.swing.JTable;
027: import javax.swing.table.DefaultTableModel;
028: import javax.swing.text.Style;
029: import javax.swing.text.StyleContext;
030:
031: import de.finix.contelligent.client.base.Session;
032: import de.finix.contelligent.client.event.LogEvent;
033: import de.finix.contelligent.client.event.LogEventListener;
034: import de.finix.contelligent.client.gui.ContelligentAction;
035: import de.finix.contelligent.client.i18n.Resources;
036: import de.finix.contelligent.client.modules.AbstractModule;
037:
038: public class LogModule extends AbstractModule implements
039: LogEventListener {
040:
041: private JTable mainTable = null;
042:
043: private DefaultTableModel mainTableModel = null;
044:
045: private ClearLogAction clearLogAction = null;
046:
047: private ToggleLogAction toggleLogAction = null;
048:
049: private boolean logActive;
050:
051: /**
052: * The action for clearing the log display.
053: */
054: private static class ClearLogAction extends AbstractAction
055: implements ContelligentAction {
056:
057: LogModule logModule = null;
058:
059: /**
060: * Create an action for the monitor module.
061: *
062: * @param monitorModule
063: * the monitor module for which this is an action.
064: */
065: public ClearLogAction(LogModule logModule) {
066: super ("clear_log_action", Resources.deleteIcon);
067: this .logModule = logModule;
068: putValue(ROLLOVER_ICON, Resources.deleteIconRollOver);
069: putValue(SHORT_DESCRIPTION, "clear_log_action_description");
070: putValue(TYPE, PUSH_ACTION);
071: putValue(ACTION_TYPE, EDIT_ACTION);
072: putValue(MENU_TARGET, MENU);
073: putValue(BUTTON_TARGET, TOOLBAR);
074: }
075:
076: public void actionPerformed(ActionEvent ae) {
077: logModule.clearMonitor();
078: }
079: }
080:
081: /**
082: * The action for toggling the monitor display.
083: */
084: private static class ToggleLogAction extends AbstractAction
085: implements ContelligentAction {
086:
087: LogModule logModule = null;
088:
089: /**
090: * Create an action for the monitor module.
091: *
092: * @param monitorModule
093: * the monitor module for which this is an action.
094: */
095: public ToggleLogAction(LogModule LogModule) {
096: super ("toggle_log_action", Resources.loggingIcon);
097: this .logModule = logModule;
098: putValue(SHORT_DESCRIPTION, "toggle_log_action_description");
099: putValue(TYPE, PUSH_ACTION);
100: putValue(ACTION_TYPE, EDIT_ACTION);
101: putValue(MENU_TARGET, MENU);
102: putValue(BUTTON_TARGET, TOOLBAR);
103: }
104:
105: public void actionPerformed(ActionEvent ae) {
106: logModule.toggleMonitor();
107: }
108: }
109:
110: /**
111: * Clear the monitor output screen.
112: */
113: public synchronized void clearMonitor() {
114: int rows = mainTableModel.getRowCount();
115: for (int i = 0; i < rows; i++) {
116: mainTableModel.removeRow(0);
117: }
118: mainTable.repaint();
119: }
120:
121: public synchronized void toggleMonitor() {
122: if (logActive) {
123: Session.getInstance().removeLogEventListener(this );
124: logActive = false;
125: } else {
126: Session.getInstance().addLogEventListener(this , true);
127: logActive = true;
128: }
129: }
130:
131: public Action[] getActions() {
132: return new Action[] { clearLogAction, toggleLogAction };
133: }
134:
135: public synchronized void init() {
136: clearLogAction = new ClearLogAction(this );
137: toggleLogAction = new ToggleLogAction(this );
138: // eventFormatAction = new EventFormatAction(this);
139: // mainTextPane = new JTextPane();
140: StyleContext defaultStyleContext = StyleContext
141: .getDefaultStyleContext();
142: Style defaultStyle = defaultStyleContext
143: .getStyle(StyleContext.DEFAULT_STYLE);
144: // mainTextPane.addStyle(STYLE_NAME, defaultStyle);
145: // add(new JScrollPane(mainTextPane));
146: mainTableModel = new LogTableModel();
147: mainTableModel.setColumnCount(5);
148: mainTableModel.setColumnIdentifiers(new String[] { "Time",
149: "Level", "Message", "X", "Location" });
150: mainTableModel.isCellEditable(0, 0);
151: mainTable = new LogTable(mainTableModel);
152: mainTable.getColumnModel().getColumn(0).setMinWidth(0);
153: mainTable.getColumnModel().getColumn(0).setPreferredWidth(185);
154: mainTable.getColumnModel().getColumn(1).setMinWidth(0);
155: mainTable.getColumnModel().getColumn(1).setMaxWidth(45);
156: mainTable.getColumnModel().getColumn(1).setPreferredWidth(45);
157: mainTable.getColumnModel().getColumn(2).setMinWidth(0);
158: mainTable.getColumnModel().getColumn(2).setPreferredWidth(370);
159: mainTable.getColumnModel().getColumn(3).setMinWidth(0);
160: mainTable.getColumnModel().getColumn(3).setPreferredWidth(16);
161: mainTable.getColumnModel().getColumn(3).setMaxWidth(16);
162: mainTable.getColumnModel().getColumn(4).setMinWidth(0);
163: mainTable.getColumnModel().getColumn(4).setPreferredWidth(210);
164: mainTable.setColumnSelectionAllowed(false);
165: mainTable.setRowSelectionAllowed(true);
166: int width = 826; // Should be the sum of preferred column widths
167: int height = width / 2; // Whatever looks nice
168: mainTable.setPreferredScrollableViewportSize(new Dimension(
169: width, height));
170: JScrollPane scroller = new JScrollPane(mainTable);
171: scroller.getViewport().setOpaque(false);
172: scroller.setOpaque(false);
173: add(scroller);
174: Session.getInstance().addLogEventListener(this , true);
175: logActive = true;
176: }
177:
178: public synchronized void onLogReceived(LogEvent le) {
179: Object[] columns = new Object[] { le, le, le, le, le };
180: mainTableModel.addRow(columns);
181: }
182: }
|