package zephyr.plugin.critterview.views;

import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Frame;

import org.eclipse.swt.SWT;
import org.eclipse.swt.awt.SWT_AWT;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.ui.part.ViewPart;

import rlpark.plugin.utils.time.Clock;
import zephyr.plugin.common.views.TimedView;
import critterbot.CritterbotProblem;

public class ObservationView extends ViewPart implements TimedView {

  public static final String ID = "zephyr.plugin.critterview.view.observation";
  protected double[] currentObservation;
  private CritterbotProblem environment;
  private CritterViz critterViz;
  private Frame awtFrame;
  private Composite swtAwtComponent;
  private final rlpark.plugin.utils.events.Listener<Clock> clockKilled = new rlpark.plugin.utils.events.Listener<Clock>() {
    @Override
    public void listen(Clock clock) {
      addTimed(null);
    }
  };

  @Override
  synchronized public void addTimed(Object drawn) {
    if (environment != null)
      environment.clock().onKill.disconnect(clockKilled);
    if (critterViz != null) {
      critterViz.dispose();
      critterViz = null;
    }
    environment = (CritterbotProblem) drawn;
    if (environment == null)
      return;
    environment.clock().onKill.connect(clockKilled);
    critterViz = new CritterViz(awtFrame, environment);
    Point point = swtAwtComponent.getSize();
    critterViz.setSize(point.x, point.y);
    critterViz.setPreferredSize(new Dimension(point.x, point.y));
    awtFrame.pack();
  }

  @Override
  public boolean synchronize() {
    currentObservation = environment.currentStep().o_tp1;
    return true;
  }

  @Override
  public void createPartControl(final Composite parent) {
    swtAwtComponent = new Composite(parent, SWT.EMBEDDED | SWT.DOUBLE_BUFFERED);
    awtFrame = SWT_AWT.new_Frame(swtAwtComponent);
    Listener listener = new Listener() {
      @Override
      public void handleEvent(Event e) {
        switch (e.type) {
        case SWT.Dispose:
                  parent.setVisible(false);
                  EventQueue.invokeLater(new Runnable() {
                    @Override
                    @SuppressWarnings("synthetic-access")
                    public void run() {
                      awtFrame.dispose();
                    }
                  });
                  break;
                }
              }
    };
    addListenerObject(listener);
  }

  @Override
  public void setFocus() {
  }

  @Override
  synchronized public void dispose() {
    if (environment != null)
      environment.clock().kill();
    super.dispose();
  }

  @Override
  synchronized public void repaint() {
    if (currentObservation != null && environment != null) {
      critterViz.updateDisplay(environment.clock().period(), currentObservation);
      critterViz.repaint();
    } else
      awtFrame.repaint();
  }

  @Override
  public boolean canTimedAdded() {
    return environment == null;
  }

  @Override
  public boolean isDisposed() {
    return swtAwtComponent.isDisposed();
  }
}

@@ -259,16 +259,56 @@
 posite;%0A
+import org.eclipse.swt.widgets.Display;%0A
 import o
@@ -333,24 +333,24 @@
 gets.Event;%0A
-
 import org.e
@@ -727,32 +727,24 @@
 ervation;%0A  
-private 
 CritterbotPr
@@ -764,24 +764,16 @@
 ment;%0A  
-private 
 CritterV
@@ -789,24 +789,16 @@
 rViz;%0A  
-private 
 Frame aw
@@ -807,24 +807,16 @@
 rame;%0A  
-private 
 Composit
@@ -1421,24 +1421,186 @@
 ockKilled);%0A
+    createVizualizer();%0A  %7D%0A%0A  private void createVizualizer() %7B%0A    Display.getDefault().syncExec(new Runnable() %7B%0A      @Override%0A      public void run() %7B%0A    
     critterV
@@ -1647,16 +1647,20 @@
 t);%0A    
+  
+  
 Point po
@@ -1688,24 +1688,28 @@
 .getSize();%0A
+    
     critterV
@@ -1738,16 +1738,20 @@
 int.y);%0A
+    
     crit
@@ -1808,16 +1808,20 @@
 nt.y));%0A
+    
     awtF
@@ -1829,24 +1829,40 @@
 ame.pack();%0A
+      %7D%0A    %7D);%0A
   %7D%0A%0A  @Over
@@ -2429,24 +2429,24 @@
 unnable() %7B%0A
+
             
@@ -2467,66 +2467,8 @@
 ide%0A
-                    @SuppressWarnings(%22synthetic-access%22)%0A
     

Fix a bug when the Critterbot environment was not created in the UI thread