package jetbrains.mps.debugger.java.runtime.ui.evaluation; /*Generated by MPS */ import javax.swing.JPanel; import org.apache.log4j.Logger; import org.apache.log4j.LogManager; import com.intellij.openapi.actionSystem.DataKey; import jetbrains.mps.debugger.java.runtime.evaluation.container.IEvaluationContainer; import jetbrains.mps.debugger.java.runtime.state.DebugSession; import org.jetbrains.annotations.NotNull; import jetbrains.mps.debug.api.SessionChangeAdapter; import java.awt.BorderLayout; import com.sun.jdi.ThreadReference; import jetbrains.mps.baseLanguage.closures.runtime._FunctionTypes; import jetbrains.mps.debugger.java.api.evaluation.Evaluator; import jetbrains.mps.debugger.java.api.evaluation.proxies.IValueProxy; import jetbrains.mps.debugger.java.api.state.proxy.JavaValue; import jetbrains.mps.debugger.java.api.state.customViewers.CustomViewersManager; import jetbrains.mps.debugger.java.api.evaluation.EvaluationException; import org.jetbrains.annotations.Nullable; import com.intellij.openapi.application.ApplicationManager; import com.intellij.openapi.application.ModalityState; import jetbrains.mps.debug.api.AbstractDebugSession; import jetbrains.mps.debugger.java.runtime.state.JavaUiStateImpl; import jetbrains.mps.debugger.java.api.state.proxy.JavaThread; import jetbrains.mps.debugger.java.api.state.proxy.JavaLocation; import jetbrains.mps.debugger.java.api.state.proxy.JavaStackFrame; public abstract class EvaluationUi extends JPanel { private static final Logger LOG = LogManager.getLogger(EvaluationUi.class); public static final DataKey<IEvaluationContainer> EVALUATION_CONTAINER = DataKey.create("Evaluation Container"); public static final DataKey<DebugSession> DEBUG_SESSION = DataKey.create("Debug Session"); @NotNull protected final DebugSession myDebugSession; protected final EvaluationTree myTree; private EvaluationUi.IErrorTextListener myErrorListener; private final boolean myAutoUpdate; private final SessionChangeAdapter mySessionChangeAdapter = new EvaluationUi.MySessionChangeAdapter(); public EvaluationUi(@NotNull DebugSession session, boolean autoUpdate) { super(new BorderLayout()); myDebugSession = session; myAutoUpdate = autoUpdate; myDebugSession.addChangeListener(mySessionChangeAdapter); myTree = new EvaluationTree(myDebugSession); } protected abstract void update(); public abstract void evaluate(); public void dispose() { myDebugSession.removeChangeListener(mySessionChangeAdapter); myTree.dispose(); } protected void evaluate(final IEvaluationContainer model) { if (!(myDebugSession.getEvaluationProvider().canEvaluate())) { setErrorText("Program should be paused on breakpoint to evaluate"); return; } new Thread("Debugger Evaluation thread") { @Override public void run() { try { final Class clazz = model.generateClass(); setEvaluating(model); final ThreadReference thread = check_4q63yg_a0c0a0a0a0a1a21(myDebugSession.getUiState().getThread()); myDebugSession.getEventsProcessor().scheduleEvaluation(new _FunctionTypes._void_P0_E0() { public void invoke() { try { Evaluator evaluator = model.createEvaluatorInstance(clazz); IValueProxy evaluatedValue = evaluator.evaluate(); if (evaluatedValue != null) { JavaValue value = CustomViewersManager.getInstance().fromJdi(evaluatedValue.getJDIValue(), thread); value.initSubvalues(); setSuccess(value, model); } else { setFailure(null, "Evaluation returned null.", model); } } catch (EvaluationException e) { setFailure(e, null, model); } catch (Throwable t) { setFailure(t, null, model); LOG.error(null, t); } } }, thread); } catch (EvaluationException e) { setFailure(e, null, model); } catch (Throwable t) { setFailure(t, null, model); LOG.error(null, t); } } }.start(); } private void setSuccess(@NotNull final JavaValue evaluatedValue, final IEvaluationContainer model) { invokeLaterIfNeeded(new Runnable() { @Override public void run() { myTree.setResultValue(evaluatedValue, model); myTree.rebuildEvaluationTreeNowIfNotDisposed(); } }); } private void setEvaluating(final IEvaluationContainer model) { invokeLaterIfNeeded(new Runnable() { @Override public void run() { myTree.setEvaluating(model); myTree.rebuildEvaluationTreeNowIfNotDisposed(); } }); } private void setFailure(@Nullable final Throwable error, @Nullable final String message, final IEvaluationContainer model) { invokeLaterIfNeeded(new Runnable() { @Override public void run() { if (error != null) { myTree.setError(error, model); } else { myTree.setError(message, model); } myTree.rebuildEvaluationTreeNowIfNotDisposed(); } }); } protected void setErrorText(String text) { if (myErrorListener != null) { myErrorListener.updateErrorText(text); } } private void invokeLaterIfNeeded(Runnable runnable) { if (ApplicationManager.getApplication().isDispatchThread()) { runnable.run(); } else { ApplicationManager.getApplication().invokeLater(runnable, ModalityState.NON_MODAL); } } public void setErrorTextListener(EvaluationUi.IErrorTextListener listener) { myErrorListener = listener; } public interface IErrorTextListener { void updateErrorText(String text); } private class MySessionChangeAdapter extends SessionChangeAdapter { public MySessionChangeAdapter() { } @Override public void paused(AbstractDebugSession session) { if (myDebugSession == session) { JavaUiStateImpl uiState = myDebugSession.getUiState(); String unitName = check_4q63yg_a0b0a0b02(check_4q63yg_a0a1a0a1u(uiState.getStackFrame())); if ((unitName != null && unitName.length() > 0)) { myTree.updateLocation(uiState.getThread().getThread()); } ApplicationManager.getApplication().invokeLater(new Runnable() { @Override public void run() { setErrorText(""); update(); if (myAutoUpdate) { evaluate(); } } }); } } @Override public void stateChanged(AbstractDebugSession session) { if (myDebugSession == session) { ApplicationManager.getApplication().invokeLater(new Runnable() { @Override public void run() { update(); } }); } } @Override public void resumed(AbstractDebugSession session) { if (myDebugSession == session) { ApplicationManager.getApplication().invokeLater(new Runnable() { @Override public void run() { myTree.rebuildEvaluationTreeNowIfNotDisposed(); } }); } } } private static ThreadReference check_4q63yg_a0c0a0a0a0a1a21(JavaThread checkedDotOperand) { if (null != checkedDotOperand) { return checkedDotOperand.getThread(); } return null; } private static String check_4q63yg_a0b0a0b02(JavaLocation checkedDotOperand) { if (null != checkedDotOperand) { return checkedDotOperand.getUnitName(); } return null; } private static JavaLocation check_4q63yg_a0a1a0a1u(JavaStackFrame checkedDotOperand) { if (null != checkedDotOperand) { return checkedDotOperand.getLocation(); } return null; } }