[Schmitzm-commits] r1780 - in trunk/schmitzm-core/src/main/java/de/schmitzm/swing: . log4j
scm-commit@wald.intevation.org
scm-commit at wald.intevation.org
Wed Nov 16 16:34:46 CET 2011
Author: mojays
Date: 2011-11-16 16:34:45 +0100 (Wed, 16 Nov 2011)
New Revision: 1780
Added:
trunk/schmitzm-core/src/main/java/de/schmitzm/swing/MessagesStatusDialog.java
trunk/schmitzm-core/src/main/java/de/schmitzm/swing/log4j/TextComponentAppender.java
Modified:
trunk/schmitzm-core/src/main/java/de/schmitzm/swing/StatusDialog.java
Log:
StatusDialog: new additional constructor parameters
new TextComponentAppender
new MessagesStatusDialog
Added: trunk/schmitzm-core/src/main/java/de/schmitzm/swing/MessagesStatusDialog.java
===================================================================
--- trunk/schmitzm-core/src/main/java/de/schmitzm/swing/MessagesStatusDialog.java 2011-11-16 09:48:57 UTC (rev 1779)
+++ trunk/schmitzm-core/src/main/java/de/schmitzm/swing/MessagesStatusDialog.java 2011-11-16 15:34:45 UTC (rev 1780)
@@ -0,0 +1,133 @@
+/*******************************************************************************
+ * Copyright (c) 2009 Martin O. J. Schmitz.
+ *
+ * Contributors:
+ * Martin O. J. Schmitz
+ ******************************************************************************/
+
+package de.schmitzm.swing;
+
+import java.awt.Component;
+import java.awt.Dimension;
+import java.util.Vector;
+
+import javax.swing.Icon;
+import javax.swing.JLabel;
+import javax.swing.JScrollPane;
+import javax.swing.JTextPane;
+import javax.swing.text.JTextComponent;
+
+/**
+ * A {@link StatusDialog} contains a {@link JTextComponent} to show
+ * status messages during the process.
+ * @author <a href="mailto:martin.schmitz at koeln.de">Martin Schmitz</a>
+ *
+ */
+public class MessagesStatusDialog extends StatusDialog {
+ protected JTextComponent textComp;
+
+ /**
+ * Creates a dialog which is centered relatively to the parent
+ * window. A {@link JTextPane} is used to show additional
+ * messages.
+ * @param parent parent window (can be <code>null</code>!)
+ * @param message message to show above the status bar
+ */
+ public MessagesStatusDialog(Component parent, String message) {
+ this(parent,SwingUtil.RESOURCE.getString("WaitMess"),message);
+ }
+
+ /**
+ * Creates a dialog which is centered relatively to the parent
+ * window. A {@link JTextPane} is used to show additional
+ * messages.
+ * @param parent parent window (can be <code>null</code>!)
+ * @param title window title
+ * @param message message to show above the status bar
+ */
+ public MessagesStatusDialog(Component parent, String title, String message) {
+ this(parent,title,message,new JTextPane(),0.5,0.5);
+ }
+
+ /**
+ * Creates a dialog which is centered relatively to the parent
+ * window.
+ * @param parent parent window (can be <code>null</code>!)
+ * @param message message to show above the status bar
+ * @param textComp component to show status messages
+ */
+ public MessagesStatusDialog(Component parent, String message, JTextComponent textComp) {
+ this(parent,SwingUtil.RESOURCE.getString("WaitMess"),message,textComp);
+ }
+
+ /**
+ * Creates a dialog which is centered relatively to the parent
+ * window.
+ * @param parent parent window (can be <code>null</code>!)
+ * @param title window title
+ * @param message message to show above the status bar
+ * @param textComp component to show status messages
+ */
+ public MessagesStatusDialog(Component parent, String title, String message, JTextComponent textComp) {
+ this(parent,title,message,textComp,0.5,0.5);
+ }
+
+ /**
+ * Creates a dialog.
+ * @param parent parent window (can be <code>null</code>!)
+ * @param title window title
+ * @param message message to show above the status bar
+ * @param textComp component to show status messages
+ * @param relX relative horizontal position (to parent frame)
+ * @param relY relative vertical position (to parent frame)
+ */
+ public MessagesStatusDialog(Component parent, String title, String message, JTextComponent textComp, double relX, double relY) {
+ this(parent,title,message,textComp,null,relX,relY);
+ }
+
+ /**
+ * Creates a dialog.
+ * @param parent parent window (can be <code>null</code>!)
+ * @param title window title
+ * @param message message to show above the status bar
+ * @param textComp component to show status messages
+ * @param icon icon for the status dialog
+ * @param relX relative horizontal position (to parent frame)
+ * @param relY relative vertical position (to parent frame)
+ */
+ public MessagesStatusDialog(Component parent, String title, String message, JTextComponent textComp, Icon icon, double relX, double relY) {
+ super(parent, title, message, icon, relX, relY, textComp);
+ this.textComp = textComp;
+ }
+
+ /**
+ * Adds the {@link JTextComponent} to the component list.
+ */
+ @Override
+ protected void insertInformationComponents(Vector<Component> list) {
+ super.insertInformationComponents(list);
+ JTextComponent textComp = (JTextComponent)addConstrParams[0];
+ textComp.setEditable(false);
+// textComp.setFont( new JLabel().getFont() );
+ textComp.setBackground( new JLabel().getBackground() );
+ list.add( new JScrollPane(textComp) );
+ setPreferredSize(new Dimension(400,300));
+ }
+
+ /**
+ * Resets the dialog options.
+ */
+ @Override
+ public void reset() {
+ super.reset();
+ textComp.setText("");
+ }
+
+ /**
+ * Returns the {@link JTextComponent} to show additional
+ * messages.
+ */
+ public JTextComponent getTextComponent() {
+ return textComp;
+ }
+}
Modified: trunk/schmitzm-core/src/main/java/de/schmitzm/swing/StatusDialog.java
===================================================================
--- trunk/schmitzm-core/src/main/java/de/schmitzm/swing/StatusDialog.java 2011-11-16 09:48:57 UTC (rev 1779)
+++ trunk/schmitzm-core/src/main/java/de/schmitzm/swing/StatusDialog.java 2011-11-16 15:34:45 UTC (rev 1780)
@@ -68,6 +68,10 @@
protected JButton button = null;
/** Flag signalisiert, ob der Dialog ueber den Button abgebrochen wurde. */
protected boolean canceled = false;
+
+ /** Zusaetzliche Konstruktor-Parameter (fuer Unterklassen!) */
+ protected Object[] addConstrParams = null;
+
/**
* Typ des Dialogs (Stanard: {@link #CANCEL_OPTION}).
* @see #CANCEL_OPTION
@@ -98,6 +102,7 @@
public StatusDialog(Component parent, String title, String message, double relX, double relY) {
this(parent,title,message,null,relX,relY);
}
+
/**
* Erzeugt einen neuen Status-Dialog.
* @param parent uebergeordnetes Fenster (kann <code>null</code> sein!)
@@ -108,8 +113,23 @@
* @param relY relative vertikale Position zum Parent-Fenster
*/
public StatusDialog(Component parent, String title, String message, Icon icon, double relX, double relY) {
+ this(parent,title,message,icon,relX,relY,new Object[0]);
+ }
+
+ /**
+ * Erzeugt einen neuen Status-Dialog.
+ * @param parent uebergeordnetes Fenster (kann <code>null</code> sein!)
+ * @param title Titel fuer das Fenster
+ * @param message Meldung, die zu dem Status-Balken angezeigt wird
+ * @param icon Icon fuer das Status-Fenster
+ * @param relX relative horizontale Position zum Parent-Fenster
+ * @param relY relative vertikale Position zum Parent-Fenster
+ * @param addConstrParams zuaetzliche Konstruktor-Parameter (fuer Unter-Klassen)
+ */
+ protected StatusDialog(Component parent, String title, String message, Icon icon, double relX, double relY, Object... addConstrParams) {
// War: super((Frame)parent,true); aber getParentWindow ist wohl besser
super( SwingUtil.getParentWindow(parent), ModalityType.APPLICATION_MODAL);
+ this.addConstrParams = addConstrParams;
// wenn kein uebergeordnetes Fenster angegeben ist, wird es immer
// im Vordergrund angezeigt
@@ -211,5 +231,13 @@
public boolean isCanceled() {
return canceled;
}
+
+ /**
+ * Setzt den Dialog zurueck.
+ */
+ public void reset() {
+ setDialogOption( CANCEL_OPTION );
+ getProgressBar().setValue(0);
+ }
}
Added: trunk/schmitzm-core/src/main/java/de/schmitzm/swing/log4j/TextComponentAppender.java
===================================================================
--- trunk/schmitzm-core/src/main/java/de/schmitzm/swing/log4j/TextComponentAppender.java 2011-11-16 09:48:57 UTC (rev 1779)
+++ trunk/schmitzm-core/src/main/java/de/schmitzm/swing/log4j/TextComponentAppender.java 2011-11-16 15:34:45 UTC (rev 1780)
@@ -0,0 +1,140 @@
+/*******************************************************************************
+ * Copyright (c) 2009 Martin O. J. Schmitz.
+ *
+ * Contributors:
+ * Martin O. J. Schmitz
+ ******************************************************************************/
+
+package de.schmitzm.swing.log4j;
+
+import java.lang.reflect.InvocationTargetException;
+
+import javax.swing.SwingUtilities;
+import javax.swing.text.JTextComponent;
+
+import org.apache.log4j.AppenderSkeleton;
+import org.apache.log4j.Layout;
+import org.apache.log4j.PatternLayout;
+import org.apache.log4j.spi.LoggingEvent;
+
+/**
+ * Panel which shows all log message in a {@link JTextComponent}.
+ * @author <a href="mailto:martin.schmitz at koeln.de">Martin Schmitz</a>
+ */
+public class TextComponentAppender extends AppenderSkeleton {
+ private static final int UPDATE_MILLIS = 100;
+
+ /** Holds the {@link JTextComponent} the messages are piped
+ * to. */
+ protected JTextComponent textComp = null;
+
+ /** Buffers log messages, so that the text component must only be
+ * updated every 100ms and not on every log. */
+ private StringBuffer textBuffer = new StringBuffer();
+ /** Holds the last time the text component was updated. */
+ private long lastUpdate = 0;
+
+ /**
+ * Creates a new appender which shows only the message.
+ * @param textComp text component to pipe the messages to
+ */
+ public TextComponentAppender(JTextComponent textComp) {
+ this(textComp, "%m%n");
+ }
+
+
+ /**
+ * Creates a new appender.
+ * @param textComp text component to pipe the messages to
+ * @param layoutPattern pattern to format the log messages
+ */
+ public TextComponentAppender(JTextComponent textComp, String layoutPattern) {
+ this(textComp, new PatternLayout(layoutPattern));
+ }
+
+ /**
+ * Creates a new appender.
+ * @param textComp text component to pipe the messages to
+ * @param layout layout to format the log messages
+ */
+ public TextComponentAppender(JTextComponent textComp, Layout layout) {
+ super();
+ setLayout(layout);
+ this.textComp = textComp;
+ }
+
+
+ /**
+ * Pipes the event message to {@link #textComp}.
+ */
+ @Override
+ protected void append(final LoggingEvent event) {
+ String mess = getLayout().format(event);
+ textBuffer.append(mess);
+ // Update GUI not on every LOG event, but only
+ if ( System.currentTimeMillis() - lastUpdate > UPDATE_MILLIS ) {
+ flush();
+ }
+ }
+
+ /**
+ * Sends all buffered log messages immediately to the text component (via EDT).
+ */
+ public void flush() {
+ Runnable work = new Runnable() {
+ @Override
+ public void run() {
+ textComp.setText( textComp.getText() + textBuffer.toString() );
+ textBuffer = new StringBuffer();
+ lastUpdate = System.currentTimeMillis();
+ }
+ };
+ if ( SwingUtilities.isEventDispatchThread() )
+ work.run();
+ else
+// MS: GUI blocks some times for a some 100ms if invokeLater is used
+// with invokeAndWait it works fine??!!
+// SwingUtilities.invokeLater(work);
+ try {
+ SwingUtilities.invokeAndWait(work);
+ } catch (InterruptedException err) {
+ // TODO Auto-generated catch block
+ err.printStackTrace();
+ } catch (InvocationTargetException err) {
+ // TODO Auto-generated catch block
+ err.printStackTrace();
+ }
+ }
+
+ /**
+ * Does nothing.
+ */
+ @Override
+ public void close() {
+ }
+
+ /**
+ * Returns {@code true}.
+ */
+ @Override
+ public boolean requiresLayout() {
+ return true;
+ }
+
+ /**
+ * Clears the text component.
+ */
+ public void clearTextComponent() {
+ textComp.setText("");
+ textBuffer = new StringBuffer();
+ }
+
+ /**
+ * Returns the {@link JTextComponent} the log messages are
+ * piped to.
+ */
+ public JTextComponent getTextComponent() {
+ return textComp;
+ }
+
+}
More information about the Schmitzm-commits
mailing list