summaryrefslogtreecommitdiff
path: root/plugins/junit/src/com/intellij/execution/junit2/ui/ConsolePanel.java
blob: 3fe67a6d5e1b07406cefd4284fb4e0beb3d481f8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
/*
 * Copyright 2000-2009 JetBrains s.r.o.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.intellij.execution.junit2.ui;

import com.intellij.execution.ExecutionBundle;
import com.intellij.execution.junit2.TestProxy;
import com.intellij.execution.junit2.ui.actions.JUnitToolbarPanel;
import com.intellij.execution.junit2.ui.model.JUnitAdapter;
import com.intellij.execution.junit2.ui.model.JUnitRunningModel;
import com.intellij.execution.junit2.ui.properties.JUnitConsoleProperties;
import com.intellij.execution.process.ProcessAdapter;
import com.intellij.execution.process.ProcessEvent;
import com.intellij.execution.process.ProcessHandler;
import com.intellij.execution.runners.ExecutionEnvironment;
import com.intellij.execution.testframework.PoolOfTestIcons;
import com.intellij.execution.testframework.Printer;
import com.intellij.execution.testframework.TestTreeView;
import com.intellij.execution.testframework.ToolbarPanel;
import com.intellij.execution.testframework.ui.TestResultsPanel;
import com.intellij.execution.testframework.ui.TestStatusLine;
import com.intellij.execution.testframework.ui.TestsOutputConsolePrinter;
import com.intellij.openapi.actionSystem.AnAction;
import com.intellij.openapi.application.ApplicationManager;
import com.intellij.openapi.application.ModalityState;
import com.intellij.ui.SimpleColoredComponent;
import com.intellij.ui.SimpleTextAttributes;
import com.intellij.ui.treeStructure.Tree;
import com.intellij.util.Alarm;
import org.jetbrains.annotations.NonNls;

import javax.swing.*;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.DefaultTreeModel;
import javax.swing.tree.TreeCellRenderer;
import java.awt.*;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;

public class ConsolePanel extends TestResultsPanel {
  @NonNls private static final String PROPORTION_PROPERTY = "test_tree_console_proprtion";
  private static final float DEFAULT_PROPORTION = 0.2f;

  private JUnitStatusLine myStatusLine;
  private StatisticsPanel myStatisticsPanel;
  private TestTreeView myTreeView;
  private TestsOutputConsolePrinter myPrinter;
  private StartingProgress myStartingProgress;

  public ConsolePanel(final JComponent console,
                      final TestsOutputConsolePrinter printer,
                      final JUnitConsoleProperties properties,
                      final ExecutionEnvironment environment,
                      AnAction[] consoleActions) {
    super(console, consoleActions, properties, environment, PROPORTION_PROPERTY, DEFAULT_PROPORTION);
    myPrinter = printer;
  }

  public void initUI() {
    super.initUI();
    myStartingProgress = new StartingProgress(myTreeView);
  }

  protected JComponent createStatisticsPanel() {
    myStatisticsPanel = new StatisticsPanel();
    return myStatisticsPanel;
  }

  protected ToolbarPanel createToolbarPanel() {
    return new JUnitToolbarPanel(myProperties, myEnvironment, this);
  }

  protected TestStatusLine createStatusLine() {
    myStatusLine = new JUnitStatusLine();
    return myStatusLine;
  }

  protected JComponent createTestTreeView() {
    myTreeView = new JUnitTestTreeView();
    return myTreeView;
  }

  public void onProcessStarted(final ProcessHandler process) {
    myStatusLine.onProcessStarted(process);
    if (myStartingProgress == null) return;
    myStartingProgress.start(process);
  }

  public void setModel(final JUnitRunningModel model) {
    stopStartingProgress();
    final TestTreeView treeView = model.getTreeView();
    treeView.setLargeModel(true);
    setLeftComponent(treeView);
    myToolbarPanel.setModel(model);
    myStatusLine.setModel(model);

    model.addListener(new JUnitAdapter() {
      @Override
      public void onTestSelected(final TestProxy test) {
        if (myPrinter != null) myPrinter.updateOnTestSelected(test);
      }
    });
    myStatisticsPanel.attachTo(model);
  }

  private void stopStartingProgress() {
    if (myStartingProgress != null) myStartingProgress.doStop();
    myStartingProgress = null;
  }

  public TestTreeView getTreeView() {
    return myTreeView;
  }

  public Printer getPrinter() {
    return myPrinter;
  }

  public void dispose() {
    stopStartingProgress();
    myPrinter = null;
  }

  private static class StartingProgress implements Runnable {
    private final Alarm myAlarm = new Alarm();
    private final Tree myTree;
    private final DefaultTreeModel myModel;
    private final DefaultMutableTreeNode myRootNode = new DefaultMutableTreeNode();
    private boolean myStarted = false;
    private boolean myStopped = false;
    private final SimpleColoredComponent myStartingLabel;
    private ProcessHandler myProcess;
    private long myStartedAt = System.currentTimeMillis();
    private final ProcessAdapter myProcessListener = new ProcessAdapter() {
      public void processTerminated(ProcessEvent event) {
        ApplicationManager.getApplication().invokeLater(new Runnable() {
          public void run() {
            doStop();
          }
        });
      }
    };

    public StartingProgress(final Tree tree) {
      myTree = tree;
      myModel = new DefaultTreeModel(myRootNode);
      myTree.setModel(myModel);
      myStartingLabel = new SimpleColoredComponent();
      myTree.setPaintBusy(true);
      //myStartingLabel.setBackground(UIManager.getColor("Tree.background"));
      myTree.setCellRenderer(new TreeCellRenderer() {
        public Component getTreeCellRendererComponent(final JTree tree, final Object value,
                                                      final boolean selected, final boolean expanded,
                                                      final boolean leaf, final int row, final boolean hasFocus) {
          myStartingLabel.clear();
          myStartingLabel.setIcon(PoolOfTestIcons.LOADING_ICON);
          myStartingLabel.append(getProgressText(), SimpleTextAttributes.REGULAR_ATTRIBUTES);
          if (!myStarted) postRepaint();
          return myStartingLabel;
        }
      });
      myTree.addPropertyChangeListener(JTree.TREE_MODEL_PROPERTY, new PropertyChangeListener() {
        public void propertyChange(final PropertyChangeEvent evt) {
          myTree.removePropertyChangeListener(JTree.TREE_MODEL_PROPERTY, this);
          doStop();
        }
      });
    }

    private void doStop() {
      myStopped = true;
      myTree.setPaintBusy(false);
      myModel.nodeChanged(myRootNode);
      myAlarm.cancelAllRequests();
      if (myProcess != null) myProcess.removeProcessListener(myProcessListener);
      myProcess = null;
    }

    public void run() {
      myModel.nodeChanged(myRootNode);
      postRepaint();
    }

    private void postRepaint() {
      if (myStopped) return;
      myStarted = true;
      myAlarm.cancelAllRequests();
      myAlarm.addRequest(this, 300, ModalityState.NON_MODAL);
    }

    public void start(final ProcessHandler process) {
      if (process.isProcessTerminated()) return;
      myProcess = process;
      myStartedAt = System.currentTimeMillis();
      process.addProcessListener(myProcessListener);
    }

    private String getProgressText() {
      if (myStopped) return ExecutionBundle.message("test.not.started.progress.text");
      final long millis = System.currentTimeMillis() - myStartedAt;
      final String phaseName = myProcess == null ? ExecutionBundle.message("starting.jvm.progress.text") : ExecutionBundle.message("instantiating.tests.progress.text");
      return phaseName + Formatters.printMinSec(millis);
    }
  }
}