Explorar o código

SDK:
- Make real nice notify about failed scene / application ;)
- Use notify system in logger (warnings, errors, exceptions)

git-svn-id: https://jmonkeyengine.googlecode.com/svn/trunk@10253 75d07b2b-3a1a-0410-a2c5-0572b91ccdca

nor..67 %!s(int64=12) %!d(string=hai) anos
pai
achega
27868a3976

+ 8 - 0
jme3-core/src/com/jme3/gde/core/scene/ApplicationLogHandler.java

@@ -31,6 +31,7 @@
  */
 package com.jme3.gde.core.scene;
 
+import com.jme3.gde.core.util.notify.NotifyUtil;
 import com.jme3.util.JmeFormatter;
 import java.util.logging.Handler;
 import java.util.logging.Level;
@@ -54,9 +55,16 @@ public class ApplicationLogHandler extends Handler {
     @Override
     public void publish(LogRecord record) {
         if (record.getLevel().equals(Level.SEVERE)) {
+            Throwable thrown = record.getThrown();
+            if (thrown != null) {
+                NotifyUtil.error("Exception!", formatter.formatMessage(record), false);
+            }else{
+                NotifyUtil.error("Severe error!", formatter.formatMessage(record), true);
+            }
             io.getErr().println(formatter.formatMessage(record));
         } else if (record.getLevel().equals(Level.WARNING)) {
             io.getErr().println(formatter.formatMessage(record));
+            NotifyUtil.warn("Warning!", formatter.formatMessage(record), true);
         } else if (record.getLevel().equals(Level.INFO)) {
             io.getOut().println(formatter.formatMessage(record));
         } else {

+ 25 - 5
jme3-core/src/com/jme3/gde/core/scene/SceneApplication.java

@@ -38,6 +38,8 @@ import com.jme3.gde.core.scene.processors.WireProcessor;
 import com.jme3.gde.core.sceneexplorer.nodes.NodeUtility;
 import com.jme3.gde.core.sceneviewer.SceneViewerTopComponent;
 import com.jme3.gde.core.undoredo.SceneUndoRedoManager;
+import com.jme3.gde.core.util.notify.MessageType;
+import com.jme3.gde.core.util.notify.NotifyUtil;
 import com.jme3.input.FlyByCamera;
 import com.jme3.input.MouseInput;
 import com.jme3.input.controls.MouseAxisTrigger;
@@ -57,7 +59,11 @@ import com.jme3.system.awt.AwtPanel;
 import com.jme3.system.awt.AwtPanelsContext;
 import com.jme3.system.awt.PaintMode;
 import java.awt.Component;
+import java.awt.event.ActionEvent;
+import java.awt.event.ActionListener;
 import java.io.IOException;
+import java.net.MalformedURLException;
+import java.net.URL;
 import java.util.Iterator;
 import java.util.concurrent.Callable;
 import java.util.concurrent.ConcurrentLinkedQueue;
@@ -69,6 +75,7 @@ import org.openide.DialogDisplayer;
 import org.openide.NotifyDescriptor;
 import org.openide.NotifyDescriptor.Confirmation;
 import org.openide.NotifyDescriptor.Message;
+import org.openide.awt.HtmlBrowser;
 import org.openide.awt.StatusDisplayer;
 import org.openide.loaders.DataObject;
 import org.openide.util.Exceptions;
@@ -157,11 +164,11 @@ public class SceneApplication extends Application implements LookupProvider {
                 start();
             }
         } catch (Exception e) {
-            SceneViewerTopComponent.showOpenGLError(e.toString());
             Exceptions.printStackTrace(e);
+            showStartupErrorMessage(e);
         } catch (Error e) {
-            SceneViewerTopComponent.showOpenGLError(e.toString());
             Exceptions.printStackTrace(e);
+            showStartupErrorMessage(e);
         } finally {
             getProgressHandle().finish();
         }
@@ -262,10 +269,10 @@ public class SceneApplication extends Application implements LookupProvider {
             started = true;
         } catch (Exception e) {
             Exceptions.printStackTrace(e);
-            SceneViewerTopComponent.showOpenGLError(e.toString());
+            showStartupErrorMessage(e);
         } catch (Error e) {
             Exceptions.printStackTrace(e);
-            SceneViewerTopComponent.showOpenGLError(e.toString());
+            showStartupErrorMessage(e);
         } finally {
             getProgressHandle().finish();
         }
@@ -614,7 +621,7 @@ public class SceneApplication extends Application implements LookupProvider {
             return;
         }
         if (!started) {
-            SceneViewerTopComponent.showOpenGLError(msg);
+            showStartupErrorMessage(t);
             Exceptions.printStackTrace(t);
         } else {
             if (lastError != null && !lastError.equals(msg)) {
@@ -625,6 +632,19 @@ public class SceneApplication extends Application implements LookupProvider {
         }
     }
 
+    public static void showStartupErrorMessage(Throwable exception) {
+        ActionListener lst = new ActionListener() {
+            public void actionPerformed(ActionEvent e) {
+                try {
+                    HtmlBrowser.URLDisplayer.getDefault().showURL(new URL("http://jmonkeyengine.org/wiki/doku.php/sdk:troubleshooting"));
+                } catch (MalformedURLException ex) {
+                    Exceptions.printStackTrace(ex);
+                }
+            }
+        };
+        NotifyUtil.show("Error starting OpenGL context!", exception.getMessage() + " - Click here to go to troubleshooting web page.", MessageType.EXCEPTION, lst, 0);
+    }
+
     @Override
     public RenderManager getRenderManager() {
         return renderManager;

+ 6 - 20
jme3-core/src/com/jme3/gde/core/sceneviewer/SceneViewerTopComponent.java

@@ -28,9 +28,13 @@ import com.jme3.gde.core.filters.FilterExplorerTopComponent;
 import com.jme3.gde.core.icons.IconList;
 import com.jme3.gde.core.scene.SceneApplication;
 import com.jme3.gde.core.scene.SceneRequest;
+import com.jme3.gde.core.util.notify.MessageType;
+import com.jme3.gde.core.util.notify.NotifyUtil;
 import com.jme3.input.awt.AwtKeyInput;
 import com.jme3.input.event.KeyInputEvent;
 import java.awt.Component;
+import java.awt.event.ActionEvent;
+import java.awt.event.ActionListener;
 import java.awt.event.KeyEvent;
 import java.awt.event.KeyListener;
 import java.awt.event.MouseWheelEvent;
@@ -90,10 +94,10 @@ public final class SceneViewerTopComponent extends TopComponent {
 
         } catch (Exception e) {
             Exceptions.printStackTrace(e);
-            showOpenGLError(e.toString());
+            SceneApplication.showStartupErrorMessage(e);
         } catch (Error err) {
             Exceptions.printStackTrace(err);
-            showOpenGLError(err.toString());
+            SceneApplication.showStartupErrorMessage(err);
         }
         //TODO: camera tools (see SwitchFrontViewAction)
 //        Collection<? extends Action> result = Lookups.forPath("CameraTools").lookupAll(Action.class);
@@ -300,24 +304,6 @@ public final class SceneViewerTopComponent extends TopComponent {
         return getDefault();
     }
 
-    public static void showOpenGLError(String e) {
-        Message msg = new NotifyDescriptor.Message(
-                "Error opening OpenGL window!\n"
-                + "Error: " + e + "\n"
-                + "See http://jmonkeyengine.org/wiki/doku.php/sdk:troubleshooting \n"
-                + "for more info."
-                + NotifyDescriptor.ERROR_MESSAGE);
-        DialogDisplayer.getDefault().notifyLater(msg);
-        if (!browserOpened) {
-            browserOpened = true;
-            try {
-                HtmlBrowser.URLDisplayer.getDefault().showURL(new URL("http://jmonkeyengine.org/wiki/doku.php/sdk:troubleshooting"));
-            } catch (MalformedURLException ex) {
-                Exceptions.printStackTrace(ex);
-            }
-        }
-    }
-
     @Override
     public int getPersistenceType() {
         return TopComponent.PERSISTENCE_ALWAYS;

+ 32 - 14
jme3-core/src/com/jme3/gde/core/util/notify/NotifyUtil.java

@@ -33,6 +33,7 @@ package com.jme3.gde.core.util.notify;
 
 import java.awt.event.ActionEvent;
 import java.awt.event.ActionListener;
+import javax.swing.Timer;
 import org.openide.awt.Notification;
 import org.openide.awt.NotificationDisplayer;
 
@@ -48,10 +49,20 @@ public class NotifyUtil {
     /**
      * Show message with the specified type and action listener
      */
-    public static void show(String title, String message, MessageType type, ActionListener actionListener, boolean clear) {
-        Notification n = (Notification) NotificationDisplayer.getDefault().notify(title, type.getIcon(), message, actionListener);
-        if (clear == true) {
-            n.clear();
+    public static void show(String title, String message, MessageType type, ActionListener actionListener, int timeout) {
+        final Notification n = (Notification) NotificationDisplayer.getDefault().notify(title, type.getIcon(), message, actionListener);
+        if (timeout > 0) {
+            java.awt.EventQueue.invokeLater(new Runnable() {
+                public void run() {
+                    Timer timer = new Timer(10000, new ActionListener() {
+                        public void actionPerformed(ActionEvent e) {
+                            n.clear();
+                        }
+                    });
+                    timer.setRepeats(false);
+                    timer.start();
+                }
+            });
         }
     }
 
@@ -59,7 +70,7 @@ public class NotifyUtil {
      * Show message with the specified type and a default action which displays
      * the message using {@link MessageUtil} with the same message type
      */
-    public static void show(String title, final String message, final MessageType type, boolean clear) {
+    public static void show(String title, final String message, final MessageType type, int timeout) {
         ActionListener actionListener = new ActionListener() {
             @Override
             public void actionPerformed(ActionEvent e) {
@@ -67,43 +78,48 @@ public class NotifyUtil {
             }
         };
 
-        show(title, message, type, actionListener, clear);
+        show(title, message, type, actionListener, timeout);
     }
 
     /**
      * Show an information notification
+     *
      * @param message
      */
     public static void info(String title, String message) {
         error(title, message, true);
     }
-    
+
     /**
      * Show an information notification
+     *
      * @param message
      */
     public static void info(String title, String message, boolean clear) {
-        show(title, message, MessageType.INFO, clear);
+        show(title, message, MessageType.INFO, 3000);
     }
 
     /**
      * Show an exception
-     * @param exception 
+     *
+     * @param exception
      */
     public static void error(Throwable exception) {
         error("Exception in SDK!", exception.getMessage(), exception, true);
     }
-    
+
     /**
      * Show an error notification
+     *
      * @param message
      */
     public static void error(String title, String message, boolean clear) {
-        show(title, message, MessageType.ERROR, clear);
+        show(title, message, MessageType.ERROR, 10000);
     }
 
     /**
      * Show an error notification for an exception
+     *
      * @param message
      * @param exception
      */
@@ -116,22 +132,24 @@ public class NotifyUtil {
             }
         };
 
-        show(title, message, MessageType.EXCEPTION, actionListener, clear);
+        show(title, message, MessageType.EXCEPTION, actionListener, 10000);
     }
 
     /**
      * Show an warning notification
+     *
      * @param message
      */
     public static void warn(String title, String message, boolean clear) {
-        show(title, message, MessageType.WARNING, clear);
+        show(title, message, MessageType.WARNING, 5000);
     }
 
     /**
      * Show an plain notification
+     *
      * @param message
      */
     public static void plain(String title, String message, boolean clear) {
-        show(title, message, MessageType.PLAIN, clear);
+        show(title, message, MessageType.PLAIN, 5000);
     }
 }