001 package org.LiveGraph.gui.msg; 002 003 import java.awt.BorderLayout; 004 import java.awt.Dimension; 005 import java.awt.Font; 006 import java.text.DateFormat; 007 import java.text.SimpleDateFormat; 008 import java.util.Date; 009 010 import javax.swing.JScrollBar; 011 import javax.swing.JScrollPane; 012 import javax.swing.JTextArea; 013 014 import org.LiveGraph.LiveGraph; 015 import org.LiveGraph.events.Event; 016 import org.LiveGraph.events.EventType; 017 import org.LiveGraph.gui.GUIEvent; 018 import org.LiveGraph.gui.LiveGraphSettingsPanel; 019 import org.LiveGraph.settings.SettingsEvent; 020 021 /** 022 * The message panel of the application. This is the only component contained in 023 * the content pane of the application's message window. API users may request 024 * {@link org.LiveGraph.gui.GUIManager} to create additional instances of a 025 * {@code MessagePanel} if they wish to integrate the LiveGraph GUI into their application. 026 * 027 * <p> 028 * <strong>LiveGraph</strong> 029 * (<a href="http://www.live-graph.org" target="_blank">http://www.live-graph.org</a>). 030 * </p> 031 * <p>Copyright (c) 2007-2008 by G. Paperin.</p> 032 * <p>File: MessagePanel.java</p> 033 * <p style="font-size:smaller;">Redistribution and use in source and binary forms, with or 034 * without modification, are permitted provided that the following terms and conditions are met: 035 * </p> 036 * <p style="font-size:smaller;">1. Redistributions of source code must retain the above 037 * acknowledgement of the LiveGraph project and its web-site, the above copyright notice, 038 * this list of conditions and the following disclaimer.<br /> 039 * 2. Redistributions in binary form must reproduce the above acknowledgement of the 040 * LiveGraph project and its web-site, the above copyright notice, this list of conditions 041 * and the following disclaimer in the documentation and/or other materials provided with 042 * the distribution.<br /> 043 * 3. All advertising materials mentioning features or use of this software or any derived 044 * software must display the following acknowledgement:<br /> 045 * <em>This product includes software developed by the LiveGraph project and its 046 * contributors.<br />(http://www.live-graph.org)</em><br /> 047 * 4. All advertising materials distributed in form of HTML pages or any other technology 048 * permitting active hyper-links that mention features or use of this software or any 049 * derived software must display the acknowledgment specified in condition 3 of this 050 * agreement, and in addition, include a visible and working hyper-link to the LiveGraph 051 * homepage (http://www.live-graph.org). 052 * </p> 053 * <p style="font-size:smaller;">THIS SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY 054 * OF ANY KIND, EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 055 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 056 * THE AUTHORS, CONTRIBUTORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 057 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR 058 * IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 059 * </p> 060 * 061 * @author Greg Paperin (<a href="http://www.paperin.org" target="_blank">http://www.paperin.org</a>) 062 * @version {@value org.LiveGraph.LiveGraph#version} 063 * 064 */ 065 public class MessagePanel extends LiveGraphSettingsPanel { 066 067 public static final boolean DEBUG_EVENTS = false; 068 069 private static final DateFormat logTimestampFormat = new SimpleDateFormat("HH:mm:ss"); 070 071 private JTextArea messageArea = null; 072 073 /** 074 * This is the default constructor. 075 */ 076 public MessagePanel() { 077 super(); 078 initialize(); 079 } 080 081 /** 082 * This method initializes the main window. 083 */ 084 private void initialize() { 085 086 // General settings: 087 088 Dimension panelDim = new Dimension(600, 200); 089 this.setPreferredSize(panelDim); 090 this.setSize(panelDim); 091 this.setLayout(new BorderLayout()); 092 093 // Message area in the centre: 094 JScrollPane centreScrollPane = new JScrollPane(); 095 this.add(centreScrollPane, BorderLayout.CENTER); 096 messageArea = new JTextArea(); 097 messageArea.setEditable(false); 098 messageArea.setFont(new Font("Courier New", Font.PLAIN, 12)); 099 centreScrollPane.setViewportView(messageArea); 100 101 } // private void initialize() 102 103 /** 104 * Displays a log message. 105 * @param s A message. 106 */ 107 private void logLn(String s) { 108 messageArea.append(s); 109 JScrollBar sb = ((JScrollPane) messageArea.getParent().getParent()).getVerticalScrollBar(); 110 if (null != sb) 111 sb.setValue(sb.getMaximum()); 112 messageArea.append("\n"); 113 } 114 115 /** 116 * Displays an event debug log message. 117 * @param s A message 118 */ 119 private void logEventDebugLn(String s) { 120 StringBuffer b = new StringBuffer(); 121 b.append("[EVBD]("); 122 b.append(logTimestampFormat.format(new Date())); 123 b.append("): "); 124 b.append(s); 125 logLn(b.toString()); 126 } 127 128 /** 129 * Displays an info message. 130 * @param s A message 131 */ 132 private void logInfoLn(String s) { 133 StringBuffer b = new StringBuffer(); 134 b.append("[INFO]("); 135 b.append(logTimestampFormat.format(new Date())); 136 b.append("): "); 137 b.append(s); 138 logLn(b.toString()); 139 } 140 141 /** 142 * Displays an error message. 143 * @param s A message 144 */ 145 private void logErrorLn(String s) { 146 StringBuffer b = new StringBuffer(); 147 b.append("[ERR ]("); 148 b.append(logTimestampFormat.format(new Date())); 149 b.append("): "); 150 b.append(s); 151 logLn(b.toString()); 152 LiveGraph.application().guiManager().setDisplayMessageWindows(true); 153 } 154 155 /** 156 * Displays an success message. 157 * @param s A message 158 */ 159 private void logSuccessLn(String s) { 160 StringBuffer b = new StringBuffer(); 161 b.append("[SUCC]("); 162 b.append(logTimestampFormat.format(new Date())); 163 b.append("): "); 164 b.append(s); 165 logLn(b.toString()); 166 } 167 168 /** 169 * Processes events. 170 * 171 * @param event Event to process. 172 */ 173 @Override 174 public void eventRaised(Event<? extends EventType> event) { 175 176 if (DEBUG_EVENTS) { 177 logEventDebugLn(null == event ? "null" : event.toString()); 178 } 179 180 super.eventRaised(event); 181 182 if (event.getDomain() == GUIEvent.class) { 183 processGUIEvent(event.cast(GUIEvent.class)); 184 return; 185 } 186 } 187 188 /** 189 * Updates local view on GUI events. 190 * 191 * @param event The GUI event. 192 */ 193 private void processGUIEvent(Event<GUIEvent> event) { 194 195 switch(event.getType()) { 196 197 case GUI_LogMessageInfo: 198 logInfoLn((String) event.getInfoObject()); 199 break; 200 201 case GUI_LogMessageError: 202 logErrorLn((String) event.getInfoObject()); 203 event.addAnnotation(this, Boolean.TRUE); 204 break; 205 206 case GUI_LogMessageSuccess: 207 logSuccessLn((String) event.getInfoObject()); 208 break; 209 210 default: 211 break; 212 } 213 } 214 215 /** 216 * Logs when settings have been loaded or saved. 217 */ 218 @Override 219 protected void processSettingsEvent(Event<SettingsEvent> event) { 220 221 String fn = null; 222 switch(event.getType()) { 223 224 case DFS_Load: 225 fn = (null == event.getInfoObject() ? "null" : '\"' + event.getInfoObject().toString() + '\"'); 226 logSuccessLn("Data file settings loaded from " + fn + "."); 227 break; 228 229 case DFS_Save: 230 fn = (null == event.getInfoObject() ? "null" : '\"' + event.getInfoObject().toString() + '\"'); 231 logSuccessLn("Data file settings saved to " + fn + "."); 232 break; 233 234 case GS_Load: 235 fn = (null == event.getInfoObject() ? "null" : '\"' + event.getInfoObject().toString() + '\"'); 236 logSuccessLn("Graph settings loaded from " + fn + "."); 237 break; 238 239 case GS_Save: 240 fn = (null == event.getInfoObject() ? "null" : '\"' + event.getInfoObject().toString() + '\"'); 241 logSuccessLn("Graph settings saved to " + fn + "."); 242 break; 243 244 case DSS_Load: 245 fn = (null == event.getInfoObject() ? "null" : '\"' + event.getInfoObject().toString() + '\"'); 246 logSuccessLn("Data series settings loaded from " + fn + "."); 247 break; 248 249 case DSS_Save: 250 fn = (null == event.getInfoObject() ? "null" : '\"' + event.getInfoObject().toString() + '\"'); 251 logSuccessLn("Data series settings saved to " + fn + "."); 252 break; 253 254 default: 255 break; 256 } 257 } 258 259 } // public class MessagePanel