001 /* This file is part of the project "Hilbert II" - http://www.qedeq.org
002 *
003 * Copyright 2000-2013, Michael Meyling <mime@qedeq.org>.
004 *
005 * "Hilbert II" is free software; you can redistribute
006 * it and/or modify it under the terms of the GNU General Public
007 * License as published by the Free Software Foundation; either
008 * version 2 of the License, or (at your option) any later version.
009 *
010 * This program is distributed in the hope that it will be useful,
011 * but WITHOUT ANY WARRANTY; without even the implied warranty of
012 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
013 * GNU General Public License for more details.
014 */
015
016 package org.qedeq.gui.se.pane;
017
018 import java.awt.BorderLayout;
019 import java.awt.Color;
020 import java.awt.Font;
021 import java.awt.event.ComponentAdapter;
022 import java.awt.event.ComponentEvent;
023
024 import javax.swing.JPanel;
025 import javax.swing.JScrollPane;
026 import javax.swing.JViewport;
027 import javax.swing.SwingUtilities;
028 import javax.swing.text.BadLocationException;
029 import javax.swing.text.DefaultStyledDocument;
030 import javax.swing.text.SimpleAttributeSet;
031 import javax.swing.text.StyleConstants;
032 import javax.swing.text.StyleContext;
033
034 import org.qedeq.base.io.UrlUtility;
035 import org.qedeq.base.trace.Trace;
036 import org.qedeq.gui.se.element.CPTextPane;
037 import org.qedeq.kernel.bo.log.LogListener;
038
039 /**
040 * Global log. All events are displayed here.
041 *
042 * @author Michael Meyling
043 */
044
045 public class LogPane extends JPanel implements LogListener {
046
047 /** This class. */
048 private static final Class CLASS = LogPane.class;
049
050 /** The log panel. */
051 private CPTextPane textPane = new CPTextPane(false);
052
053 /** Text attributes for errors. */
054 private final SimpleAttributeSet errorAttrs = new SimpleAttributeSet();
055
056 /** Text attributes for success messages. */
057 private final SimpleAttributeSet successAttrs = new SimpleAttributeSet();
058
059 /** Text attributes for information messages. */
060 private final SimpleAttributeSet messageAttrs = new SimpleAttributeSet();
061
062 /** Text attributes for requests. */
063 private final SimpleAttributeSet requestAttrs = new SimpleAttributeSet();
064
065 /** Last module we had a status or message output. */
066 private String lastModuleUrl = "";
067
068
069 /**
070 * Creates new panel.
071 */
072 public LogPane() {
073 super(false);
074 setupView();
075 StyleContext sc = new StyleContext();
076 DefaultStyledDocument doc = new DefaultStyledDocument(sc);
077 textPane.setDocument(doc);
078 }
079
080
081
082 /**
083 * Assembles the GUI components of the panel.
084 */
085 private final void setupView() {
086 textPane.setDragEnabled(true);
087 textPane.setAutoscrolls(true);
088 textPane.setCaretPosition(0);
089 textPane.setEditable(false);
090 textPane.getCaret().setVisible(false);
091 textPane.setFont(new Font("Lucida Sans Unicode", Font.PLAIN,
092 textPane.getFont().getSize()));
093 // final DefaultCaret caret = (DefaultCaret) text.getCaret();
094 // caret.setUpdatePolicy(DefaultCaret.ALWAYS_UPDATE); // LATER mime JRE 1.5
095 textPane.setFocusable(true);
096 final JScrollPane scroller = new JScrollPane();
097 final JViewport vp = scroller.getViewport();
098 vp.add(textPane);
099 setLayout(new BorderLayout(1, 1));
100 add(scroller);
101
102 addComponentListener(new ComponentAdapter() {
103 public void componentHidden(final ComponentEvent e) {
104 Trace.trace(CLASS, this, "componentHidden", e);
105 }
106 public void componentShown(final ComponentEvent e) {
107 Trace.trace(CLASS, this, "componentHidden", e);
108 }
109 });
110
111 StyleConstants.setForeground(
112 errorAttrs, Color.red);
113 StyleConstants.setForeground(
114 successAttrs, new Color(0, 192, 0));
115 StyleConstants.setForeground(
116 messageAttrs, Color.black);
117 StyleConstants.setForeground(
118 requestAttrs, Color.blue);
119 }
120
121 public final void logMessageState(final String text, final String url) {
122
123 final Runnable runLater = new Runnable() {
124 public void run() {
125 try {
126 if (!lastModuleUrl.equals(url)) {
127 textPane.getDocument().insertString(textPane.getDocument().getLength(),
128 UrlUtility.easyUrl(url) + "\n", messageAttrs);
129 lastModuleUrl = (url != null ? url : "");
130 }
131 textPane.getDocument().insertString(textPane.getDocument().getLength(),
132 "\t" + text + "\n", messageAttrs);
133 } catch (BadLocationException e) {
134 Trace.trace(CLASS, this, "logMessageState", e);
135 }
136 rework();
137 }
138 };
139 SwingUtilities.invokeLater(runLater);
140 }
141
142 public final void logFailureState(final String text, final String url, final String description) {
143
144 final Runnable runLater = new Runnable() {
145 public void run() {
146 try {
147 if (!lastModuleUrl.equals(url)) {
148 textPane.getDocument().insertString(textPane.getDocument().getLength(),
149 UrlUtility.easyUrl(url) + "\n", messageAttrs);
150 lastModuleUrl = (url != null ? url : "");
151 }
152 textPane.getDocument().insertString(textPane.getDocument().getLength(),
153 "\t" + text + "\n", errorAttrs);
154 } catch (BadLocationException e) {
155 Trace.trace(CLASS, this, "logFailureState", e);
156 }
157 rework();
158 }
159 };
160 SwingUtilities.invokeLater(runLater);
161 }
162
163 public final void logSuccessfulState(final String text, final String url) {
164
165 final Runnable runLater = new Runnable() {
166 public void run() {
167 try {
168 if (!lastModuleUrl.equals(url)) {
169 textPane.getDocument().insertString(textPane.getDocument().getLength(),
170 UrlUtility.easyUrl(url) + "\n", messageAttrs);
171 lastModuleUrl = (url != null ? url : "");
172 }
173 textPane.getDocument().insertString(textPane.getDocument().getLength(),
174 "\t" + text + "\n", messageAttrs);
175 } catch (BadLocationException e) {
176 Trace.trace(CLASS, this, "logSuccessfulState", e);
177 }
178 rework();
179 }
180 };
181 SwingUtilities.invokeLater(runLater);
182 }
183
184 public void logRequest(final String text, final String url) {
185
186 final Runnable runLater = new Runnable() {
187 public void run() {
188 try {
189 if (!lastModuleUrl.equals(url)) {
190 textPane.getDocument().insertString(textPane.getDocument().getLength(),
191 UrlUtility.easyUrl(url) + "\n", messageAttrs);
192 lastModuleUrl = (url != null ? url : "");
193 }
194 textPane.getDocument().insertString(textPane.getDocument().getLength(),
195 "\t" + text + "\n", requestAttrs);
196 } catch (BadLocationException e) {
197 Trace.trace(CLASS, this, "logRequest", e);
198 }
199 rework();
200 }
201 };
202 SwingUtilities.invokeLater(runLater);
203 }
204
205 public void logSuccessfulReply(final String text, final String url) {
206
207 final Runnable runLater = new Runnable() {
208 public void run() {
209 try {
210 if (!lastModuleUrl.equals(url)) {
211 textPane.getDocument().insertString(textPane.getDocument().getLength(),
212 UrlUtility.easyUrl(url) + "\n", messageAttrs);
213 lastModuleUrl = (url != null ? url : "");
214 }
215 textPane.getDocument().insertString(textPane.getDocument().getLength(),
216 "\t" + text + "\n", successAttrs);
217 } catch (BadLocationException e) {
218 Trace.trace(CLASS, this, "logSuccessfulReply", e);
219 }
220 rework();
221 }
222 };
223 SwingUtilities.invokeLater(runLater);
224 }
225
226 public void logFailureReply(final String text, final String url, final String description) {
227
228 final Runnable runLater = new Runnable() {
229 public void run() {
230 try {
231 if (!lastModuleUrl.equals(url)) {
232 textPane.getDocument().insertString(textPane.getDocument().getLength(),
233 UrlUtility.easyUrl(url) + "\n", messageAttrs);
234 lastModuleUrl = (url != null ? url : "");
235 }
236 textPane.getDocument().insertString(textPane.getDocument().getLength(),
237 "\t" + text + "\n\t" + description + "\n", errorAttrs);
238 } catch (BadLocationException e) {
239 Trace.trace(CLASS, this, "logFailureReply", e);
240 }
241 rework();
242 }
243 };
244 SwingUtilities.invokeLater(runLater);
245 }
246
247 public void logMessage(final String text) {
248
249 final Runnable runLater = new Runnable() {
250 public void run() {
251 try {
252 lastModuleUrl = "";
253 textPane.getDocument().insertString(textPane.getDocument().getLength(),
254 text + "\n", messageAttrs);
255 } catch (BadLocationException e) {
256 Trace.trace(CLASS, this, "logFailureReply", e);
257 }
258 rework();
259 }
260 };
261 SwingUtilities.invokeLater(runLater);
262 }
263
264 private void rework() {
265 if (QedeqGuiConfig.getInstance().isAutomaticLogScroll()) {
266 textPane.setCaretPosition(textPane.getDocument().getLength());
267 }
268 }
269
270 }
|