[Schmitzm-commits] r1111 - in trunk: . src/schmitzm/swing src/skrueger/versionnumber src_junit/schmitzm/swing

scm-commit@wald.intevation.org scm-commit at wald.intevation.org
Thu Oct 14 20:28:29 CEST 2010


Author: alfonx
Date: 2010-10-14 20:28:28 +0200 (Thu, 14 Oct 2010)
New Revision: 1111

Added:
   trunk/src_junit/schmitzm/swing/ExceptionDialogTest.java
Modified:
   trunk/pom.xml
   trunk/src/schmitzm/swing/ExceptionDialog.java
   trunk/src/skrueger/versionnumber/ReleaseUtil.java
Log:
Changed the ExceptionDialog JDialog to always only appear on the EDT.

Modified: trunk/pom.xml
===================================================================
--- trunk/pom.xml	2010-10-14 18:07:03 UTC (rev 1110)
+++ trunk/pom.xml	2010-10-14 18:28:28 UTC (rev 1111)
@@ -369,6 +369,7 @@
 								<taskdef resource="net/sf/antcontrib/antlib.xml">
 									<classpath refid="maven.dependency.classpath" />
 								</taskdef>
+								
 								<typedef name="native2ascii"
 									classname="org.apache.tools.ant.taskdefs.optional.Native2Ascii">
 									<classpath refid="maven.dependency.classpath" />

Modified: trunk/src/schmitzm/swing/ExceptionDialog.java
===================================================================
--- trunk/src/schmitzm/swing/ExceptionDialog.java	2010-10-14 18:07:03 UTC (rev 1110)
+++ trunk/src/schmitzm/swing/ExceptionDialog.java	2010-10-14 18:28:28 UTC (rev 1111)
@@ -35,6 +35,7 @@
 import java.awt.GraphicsEnvironment;
 import java.awt.event.ActionEvent;
 import java.awt.event.ActionListener;
+import java.lang.reflect.InvocationTargetException;
 import java.util.HashSet;
 import java.util.Set;
 
@@ -78,8 +79,8 @@
 	/** Verbindung zwischen StackTrace-PrintStream und TextArea */
 	private TextAreaPrintStream detailsPrintStream = null;
 	/**
-	 * Hält ein Set optionaler EventListener, die bei jeder Instanziierung
-	 * eines ExceptionDialogs aufgerufen werden. 
+	 * Hält ein Set optionaler EventListener, die bei jeder Instanziierung eines
+	 * ExceptionDialogs aufgerufen werden.
 	 **/
 	private static Set<ActionListener> staticListeners = new HashSet<ActionListener>();
 	/**
@@ -152,6 +153,10 @@
 			String errMessage, boolean showDetails) {
 		super((Frame) null, true);
 
+		/**
+		 * Wenn eingestellt, werden Exceptions nicht angezeigt, sondern
+		 * weitergeworfen. Nützlich für JUnit testing z.B.
+		 */
 		if (throwRuntimeExceptionsBack)
 			throw new RuntimeException("throwRuntimeExceptionsBack==true", err);
 
@@ -159,12 +164,12 @@
 			title = err.getClass().getSimpleName();
 		if (err != null && (errMessage == null || errMessage.trim().equals("")))
 			errMessage = err.getMessage();
-		
+
 		// Listener informieren
 		for (ActionListener el : staticListeners) {
 			el.actionPerformed(new ActionEvent(err, 0, title));
 		}
-		
+
 		if (!SwingUtilities.isEventDispatchThread()) {
 			System.err
 					.println("ExceptionDialog has been called on the wrong thread. Swing is not thread-save!! It has been called for the following Throwable: ");
@@ -175,12 +180,12 @@
 		this.err = err;
 		this.messageLabel = new JLabel(errMessage);
 		this.okButton = new JButton(SwingUtil.RESOURCE.getString("Ok"));
-		this.copyToConsoleButton = new JButton(SwingUtil.RESOURCE
-				.getString("ExceptionDialog.CopyToConsole"));
-		this.copyToClipboardButton = new JButton(SwingUtil.RESOURCE
-				.getString("ExceptionDialog.CopyToClipboard"));
-		this.detailsButton = new JToggleButton(SwingUtil.RESOURCE
-				.getString("Details"), showDetails);
+		this.copyToConsoleButton = new JButton(
+				SwingUtil.RESOURCE.getString("ExceptionDialog.CopyToConsole"));
+		this.copyToClipboardButton = new JButton(
+				SwingUtil.RESOURCE.getString("ExceptionDialog.CopyToClipboard"));
+		this.detailsButton = new JToggleButton(
+				SwingUtil.RESOURCE.getString("Details"), showDetails);
 		this.detailsTextArea = new JTextArea(10, 60);
 		this.detailsTextArea.setEditable(false);
 		this.detailsPrintStream = new TextAreaPrintStream(detailsTextArea);
@@ -226,8 +231,8 @@
 		});
 
 		pack();
-		SwingUtil.setRelativeFramePosition(this, SwingUtil
-				.getParentWindow(parent), 0.5, 0.5);
+		SwingUtil.setRelativeFramePosition(this,
+				SwingUtil.getParentWindow(parent), 0.5, 0.5);
 	}
 
 	/**
@@ -284,8 +289,7 @@
 		if (GraphicsEnvironment.isHeadless())
 			err.printStackTrace();
 		else
-			new ExceptionDialog(parent, err, title, errMessage)
-					.setVisible(true);
+			constructDialog(parent, err, title, errMessage, false);
 	}
 
 	/**
@@ -300,7 +304,8 @@
 		if (GraphicsEnvironment.isHeadless())
 			err.printStackTrace();
 		else
-			new ExceptionDialog(parent, err).setVisible(true);
+			constructDialog(parent, err, null, null, false);
+
 	}
 
 	/**
@@ -313,9 +318,8 @@
 		if (GraphicsEnvironment.isHeadless())
 			err.printStackTrace();
 		else {
-			// TODO Hier sollte überprüft werden, ob man sich auf dem Swing Thread befindet!
-			// if (SwingUtilities.isEventDispatchThread())
-			new ExceptionDialog(null, err).setVisible(true);
+			constructDialog(null, err, null, null, false);
+
 		}
 	}
 
@@ -334,13 +338,45 @@
 	 * @param showDetails
 	 *            wenn {@code true} werden die Details initial angezeigt
 	 */
-	public static void show(Component parent, Throwable err, String title,
-			String errMessage, boolean showDetails) {
+	public static void show(final Component parent, final Throwable err,
+			final String title, final String errMessage,
+			final boolean showDetails) {
 		if (GraphicsEnvironment.isHeadless())
 			err.printStackTrace();
-		else
+		else {
+			constructDialog(parent, err, title, errMessage, showDetails);
+		}
+	}
+
+	/**
+	 * This method will construct the {@link ExceptionDialog} on the Event Dispatching Thread, even if it has been started from another thread! 
+	 */
+	private static void constructDialog(final Component parent,
+			final Throwable err, final String title, final String errMessage,
+			final boolean showDetails) {
+		if (SwingUtilities.isEventDispatchThread()) {
 			new ExceptionDialog(parent, err, title, errMessage, showDetails)
 					.setVisible(true);
+		} else {
+			try {
+				SwingUtilities.invokeAndWait(new Runnable() {
+
+					@Override
+					public void run() {
+						new ExceptionDialog(parent, err, title, errMessage,
+								showDetails).setVisible(true);
+					}
+				});
+			} catch (InterruptedException e) {
+				System.err
+						.println("Die Fehlermeldung wurde konnte nicht auf dem EDT angezeigt werden!");
+				e.printStackTrace();
+			} catch (InvocationTargetException e) {
+				System.err
+						.println("Die Fehlermeldung wurde konnte nicht auf dem EDT angezeigt werden!");
+				e.printStackTrace();
+			}
+		}
 	}
 
 	/**
@@ -371,7 +407,7 @@
 
 	/**
 	 * Adds an ExceptionListenerm, which will be informed on any exceptions
-	 * shown. 
+	 * shown.
 	 */
 	public static void addListener(ActionListener listener) {
 		staticListeners.add(listener);

Modified: trunk/src/skrueger/versionnumber/ReleaseUtil.java
===================================================================
--- trunk/src/skrueger/versionnumber/ReleaseUtil.java	2010-10-14 18:07:03 UTC (rev 1110)
+++ trunk/src/skrueger/versionnumber/ReleaseUtil.java	2010-10-14 18:28:28 UTC (rev 1111)
@@ -21,7 +21,7 @@
  * 
  * <br/>
  *         1. pom.xml anpassen: <code>
-			<plugin>
+					<plugin>
 				<groupId>org.codehaus.mojo</groupId>
 				<artifactId>buildnumber-maven-plugin</artifactId>
 				<version>1.0-beta-4</version>
@@ -35,12 +35,12 @@
 					</execution>
 				</executions>
 				<configuration>
-				
-					<doCheck>false</doCheck>
-					<doUpdate>false</doUpdate>
-					<providerImplementations>
-						<svn>javasvn</svn>
-					</providerImplementations>
+					<!-- <doCheck>false</doCheck> <doUpdate>false</doUpdate> <providerImplementations> 
+						<svn>javasvn</svn> </providerImplementations> -->
+					<format>{0,date,yyyyMMddHHmm}</format>
+					<items>
+						<item>timestamp</item>
+					</items>
 				</configuration>
 			</plugin>
 		</code><br/>

Added: trunk/src_junit/schmitzm/swing/ExceptionDialogTest.java
===================================================================
--- trunk/src_junit/schmitzm/swing/ExceptionDialogTest.java	2010-10-14 18:07:03 UTC (rev 1110)
+++ trunk/src_junit/schmitzm/swing/ExceptionDialogTest.java	2010-10-14 18:28:28 UTC (rev 1111)
@@ -0,0 +1,34 @@
+package schmitzm.swing;
+
+import java.lang.reflect.InvocationTargetException;
+
+import javax.swing.SwingUtilities;
+
+import org.junit.After;
+import org.junit.Before;
+import org.junit.Test;
+
+public class ExceptionDialogTest {
+
+	@Before
+	public void setUp() throws Exception {
+	}
+
+	@After
+	public void tearDown() throws Exception {
+	}
+
+	@Test
+	public void testShowThrowable() throws InterruptedException, InvocationTargetException {
+		ExceptionDialog.show(new RuntimeException("Test on Application Thread"));
+		
+		SwingUtilities.invokeAndWait(new Runnable() {
+			
+			@Override
+			public void run() {
+				ExceptionDialog.show(new RuntimeException("Test on EDT Thread"));	
+			}
+		});
+	}
+
+}


Property changes on: trunk/src_junit/schmitzm/swing/ExceptionDialogTest.java
___________________________________________________________________
Name: svn:mime-type
   + text/plain
Name: svn:keywords
   + Id URL
Name: svn:eol-style
   + native



More information about the Schmitzm-commits mailing list