summaryrefslogtreecommitdiff
path: root/platform/platform-impl/src/com/intellij/openapi/progress/util/ProgressWindow.java
blob: 0460a713d9055b973c8a99ac63a75fb385facd3f (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
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
/*
 * Copyright 2000-2013 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.openapi.progress.util;

import com.intellij.ide.IdeEventQueue;
import com.intellij.openapi.Disposable;
import com.intellij.openapi.application.ApplicationManager;
import com.intellij.openapi.diagnostic.Logger;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.ui.DialogWrapper;
import com.intellij.openapi.ui.DialogWrapperPeer;
import com.intellij.openapi.ui.impl.DialogWrapperPeerImpl;
import com.intellij.openapi.ui.impl.FocusTrackbackProvider;
import com.intellij.openapi.ui.impl.GlassPaneDialogWrapperPeer;
import com.intellij.openapi.util.*;
import com.intellij.openapi.wm.IdeFocusManager;
import com.intellij.openapi.wm.WindowManager;
import com.intellij.openapi.wm.ex.WindowManagerEx;
import com.intellij.ui.FocusTrackback;
import com.intellij.ui.PopupBorder;
import com.intellij.ui.TitlePanel;
import com.intellij.ui.awt.RelativePoint;
import com.intellij.ui.components.JBLabel;
import com.intellij.util.Alarm;
import com.intellij.util.ui.UIUtil;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import javax.swing.*;
import javax.swing.border.Border;
import java.awt.*;
import java.awt.event.*;
import java.io.File;

@SuppressWarnings({"NonStaticInitializer"})
public class ProgressWindow extends BlockingProgressIndicator implements Disposable {
  private static final Logger LOG = Logger.getInstance("#com.intellij.openapi.progress.util.ProgressWindow");

  /**
   * This constant defines default delay for showing progress dialog (in millis).
   *
   * @see #setDelayInMillis(int)
   */
  public static final int DEFAULT_PROGRESS_DIALOG_POSTPONE_TIME_MILLIS = 300;

  private static final int UPDATE_INTERVAL = 50; //msec. 20 frames per second.

  private MyDialog myDialog;
  private final Alarm myUpdateAlarm = new Alarm(this);

  private final Project myProject;
  private final boolean myShouldShowCancel;
  private       String  myCancelText;

  private String myTitle = null;

  private boolean myStoppedAlready = false;
  protected final FocusTrackback myFocusTrackback;
  private boolean myStarted      = false;
  protected boolean myBackgrounded = false;
  private boolean myWasShown;
  private String myProcessId = "<unknown>";
  @Nullable private volatile Runnable myBackgroundHandler;
  private int myDelayInMillis = DEFAULT_PROGRESS_DIALOG_POSTPONE_TIME_MILLIS;

  public ProgressWindow(boolean shouldShowCancel, Project project) {
    this(shouldShowCancel, false, project);
  }

  public ProgressWindow(boolean shouldShowCancel, boolean shouldShowBackground, @Nullable Project project) {
    this(shouldShowCancel, shouldShowBackground, project, null);
  }

  public ProgressWindow(boolean shouldShowCancel, boolean shouldShowBackground, @Nullable Project project, String cancelText) {
    this(shouldShowCancel, shouldShowBackground, project, null, cancelText);
  }

  public ProgressWindow(boolean shouldShowCancel,
                        boolean shouldShowBackground,
                        @Nullable Project project,
                        JComponent parentComponent,
                        String cancelText) {
    myProject = project;
    myShouldShowCancel = shouldShowCancel;
    myCancelText = cancelText;
    setModalityProgress(shouldShowBackground ? null : this);
    myFocusTrackback = new FocusTrackback(this, WindowManager.getInstance().suggestParentWindow(project), false);

    Component parent = parentComponent;
    if (parent == null && project == null) {
      parent = JOptionPane.getRootFrame();
    }

    if (parent != null) {
      myDialog = new MyDialog(shouldShowBackground, parent, myCancelText);
    }
    else {
      myDialog = new MyDialog(shouldShowBackground, myProject, myCancelText);
    }

    Disposer.register(this, myDialog);

    myFocusTrackback.registerFocusComponent(myDialog.getPanel());
  }

  @Override
  public synchronized void start() {
    LOG.assertTrue(!isRunning());
    LOG.assertTrue(!myStoppedAlready);

    super.start();
    if (!ApplicationManager.getApplication().isUnitTestMode()) {
      prepareShowDialog();
    }

    myStarted = true;
  }

  /**
   * There is a possible case that many short (in terms of time) progress tasks are executed in a small amount of time.
   * Problem: UI blinks and looks ugly if we show progress dialog for every such task (every dialog disappears shortly).
   * Solution is to postpone showing progress dialog in assumption that the task may be already finished when it's
   * time to show the dialog.
   * <p/>
   * Default value is {@link #DEFAULT_PROGRESS_DIALOG_POSTPONE_TIME_MILLIS}
   *
   * @param delayInMillis   new delay time in milliseconds
   */
  public void setDelayInMillis(int delayInMillis) {
    myDelayInMillis = delayInMillis;
  }

  private synchronized boolean isStarted() {
    return myStarted;
  }

  protected void prepareShowDialog() {
    // We know at least about one use-case that requires special treatment here: many short (in terms of time) progress tasks are
    // executed in a small amount of time. Problem: UI blinks and looks ugly if we show progress dialog that disappears shortly
    // for each of them. Solution is to postpone the tasks of showing progress dialog. Hence, it will not be shown at all
    // if the task is already finished when the time comes.
    Timer timer = UIUtil.createNamedTimer("Progress window timer",myDelayInMillis, new ActionListener() {
      @Override
      public void actionPerformed(ActionEvent e) {
        ApplicationManager.getApplication().invokeLater(new Runnable() {
          @Override
          public void run() {
            if (isRunning()) {
              if (myDialog != null) {
                final DialogWrapper popup = myDialog.myPopup;
                if (popup != null) {
                  myFocusTrackback.registerFocusComponent(new FocusTrackback.ComponentQuery() {
                    @Override
                    public Component getComponent() {
                      return popup.getPreferredFocusedComponent();
                    }
                  });
                  if (popup.isShowing()) {
                    myWasShown = true;
                  }
                }
              }
              showDialog();
            }
            else {
              Disposer.dispose(ProgressWindow.this);
            }
          }
        }, getModalityState());
      }
    });
    timer.setRepeats(false);
    timer.start();
  }

  @Override
  public void startBlocking() {
    ApplicationManager.getApplication().assertIsDispatchThread();
    synchronized (this) {
      LOG.assertTrue(!isRunning());
      LOG.assertTrue(!myStoppedAlready);
    }

    enterModality();

    IdeEventQueue.getInstance().pumpEventsForHierarchy(myDialog.myPanel, new Condition<AWTEvent>() {
      @Override
      public boolean value(final AWTEvent object) {
        if (myShouldShowCancel &&
            object instanceof KeyEvent &&
            object.getID() == KeyEvent.KEY_PRESSED &&
            ((KeyEvent)object).getKeyCode() == KeyEvent.VK_ESCAPE &&
            ((KeyEvent)object).getModifiers() == 0) {
          SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
              cancel();
            }
          });
        }
        return isStarted() && !isRunning();
      }
    });

    exitModality();
  }

  @NotNull
  public String getProcessId() {
    return myProcessId;
  }

  public void setProcessId(@NotNull String processId) {
    myProcessId = processId;
  }

  protected void showDialog() {
    if (!isRunning() || isCanceled()) {
      return;
    }

    myWasShown = true;
    myDialog.show();
    if (myDialog != null) {
      myDialog.myRepaintRunnable.run();
    }
  }

  @Override
  public void setIndeterminate(boolean indeterminate) {
    super.setIndeterminate(indeterminate);
    update();
  }

  @Override
  public synchronized void stop() {
    LOG.assertTrue(!myStoppedAlready);

    super.stop();

    if (isDialogShowing()) {
      if (myFocusTrackback != null) {
        myFocusTrackback.setWillBeSheduledForRestore();
      }
    }

    UIUtil.invokeLaterIfNeeded(new Runnable() {
      @Override
      public void run() {
        boolean wasShowing = isDialogShowing();
        if (myDialog != null) {
          myDialog.hide();
        }

        if (myFocusTrackback != null) {
          if (wasShowing) {
            myFocusTrackback.restoreFocus();
          }
          else {
            myFocusTrackback.consume();
          }
        }

        synchronized (ProgressWindow.this) {
          myStoppedAlready = true;
        }

        Disposer.dispose(ProgressWindow.this);
      }
    });

    SwingUtilities.invokeLater(EmptyRunnable.INSTANCE); // Just to give blocking dispatching a chance to go out.
  }

  private boolean isDialogShowing() {
    return myDialog != null && myDialog.getPanel() != null && myDialog.getPanel().isShowing();
  }

  @Override
  public void cancel() {
    super.cancel();
    if (myDialog != null) {
      myDialog.cancel();
    }
  }

  public void background() {
    final Runnable backgroundHandler = myBackgroundHandler;
    if (backgroundHandler != null) {
      backgroundHandler.run();
      return;
    }

    if (myDialog != null) {
      myBackgrounded = true;
      myDialog.background();

      if (myDialog.wasShown()) {
        myFocusTrackback.restoreFocus();
      }
      else {
        myFocusTrackback.consume();
      }

      myDialog = null;
    }
  }

  public boolean isBackgrounded() {
    return myBackgrounded;
  }

  @Override
  public void setText(String text) {
    if (!Comparing.equal(text, getText())) {
      super.setText(text);
      update();
    }
  }

  @Override
  public void setFraction(double fraction) {
    if (fraction != getFraction()) {
      super.setFraction(fraction);
      update();
    }
  }

  @Override
  public void setText2(String text) {
    if (!Comparing.equal(text, getText2())) {
      super.setText2(text);
      update();
    }
  }

  private void update() {
    if (myDialog != null) {
      myDialog.update();
    }
  }

  public void setTitle(String title) {
    if (!Comparing.equal(title, myTitle)) {
      myTitle = title;
      update();
    }
  }

  public String getTitle() {
    return myTitle;
  }

  protected class MyDialog implements Disposable {
    private long myLastTimeDrawn = -1;
    private volatile boolean myShouldShowBackground;

    private final Runnable myRepaintRunnable = new Runnable() {
      @Override
      public void run() {
        String text = getText();
        double fraction = getFraction();
        String text2 = getText2();

        myTextLabel.setText(text != null && !text.isEmpty() ? text : " ");

        if (myProgressBar.isShowing()) {
          final int perc = (int)(fraction * 100);
          myProgressBar.setIndeterminate(perc == 0 || isIndeterminate());
          myProgressBar.setValue(perc);
        }

        myText2Label.setText(getTitle2Text(text2, myText2Label.getWidth()));

        myTitlePanel.setText(myTitle != null && !myTitle.isEmpty() ? myTitle : " ");

        myLastTimeDrawn = System.currentTimeMillis();
        myRepaintedFlag = true;
      }
    };

    private String getTitle2Text(String fullText, int labelWidth) {
      if (fullText == null || fullText.isEmpty()) return " ";
      while (myText2Label.getFontMetrics(myText2Label.getFont()).stringWidth(fullText) > labelWidth) {
        int sep = fullText.indexOf(File.separatorChar, 4);
        if (sep < 0) return fullText;
        fullText = "..." + fullText.substring(sep);
      }

      return fullText;
    }

    private final Runnable myUpdateRequest = new Runnable() {
      @Override
      public void run() {
        update();
      }
    };

    private JPanel myPanel;

    private JLabel myTextLabel;
    private JBLabel myText2Label;

    private JButton myCancelButton;
    private JButton myBackgroundButton;

    private JProgressBar myProgressBar;
    private boolean myRepaintedFlag = true;
    private TitlePanel myTitlePanel;
    private JPanel myInnerPanel;
    private DialogWrapper myPopup;
    private final Window myParentWindow;
    private Point myLastClicked;

    public MyDialog(boolean shouldShowBackground, Project project, String cancelText) {
      Window parentWindow = WindowManager.getInstance().suggestParentWindow(project);
      if (parentWindow == null) {
        parentWindow = WindowManagerEx.getInstanceEx().getMostRecentFocusedWindow();
      }
      myParentWindow = parentWindow;

      initDialog(shouldShowBackground, cancelText);
    }

    public MyDialog(boolean shouldShowBackground, Component parent, String cancelText) {
      myParentWindow = parent instanceof Window
                       ? (Window)parent
                       : (Window)SwingUtilities.getAncestorOfClass(Window.class, parent);
      initDialog(shouldShowBackground, cancelText);
    }

    private void initDialog(boolean shouldShowBackground, String cancelText) {
      if (SystemInfo.isMac) {
        UIUtil.applyStyle(UIUtil.ComponentStyle.SMALL, myText2Label);
      }
      myInnerPanel.setPreferredSize(new Dimension(SystemInfo.isMac ? 350 : 450, -1));

      myCancelButton.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent e) {
          doCancelAction();
        }
      });

      myCancelButton.registerKeyboardAction(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent e) {
          if (myCancelButton.isEnabled()) {
            doCancelAction();
          }
        }
      }, KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0), JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT);

      myShouldShowBackground = shouldShowBackground;
      if (cancelText != null) {
        setCancelButtonText(cancelText);
      }
      myProgressBar.setMaximum(100);
      createCenterPanel();

      myTitlePanel.setActive(true);
      myTitlePanel.addMouseListener(new MouseAdapter() {
        @Override
        public void mousePressed(MouseEvent e) {
          final Point titleOffset = RelativePoint.getNorthWestOf(myTitlePanel).getScreenPoint();
          myLastClicked = new RelativePoint(e).getScreenPoint();
          myLastClicked.x -= titleOffset.x;
          myLastClicked.y -= titleOffset.y;
        }
      });

      myTitlePanel.addMouseMotionListener(new MouseMotionAdapter() {
        @Override
        public void mouseDragged(MouseEvent e) {
          if (myLastClicked == null) {
            return;
          }
          final Point draggedTo = new RelativePoint(e).getScreenPoint();
          draggedTo.x -= myLastClicked.x;
          draggedTo.y -= myLastClicked.y;

          if (myPopup != null) {
            myPopup.setLocation(draggedTo);
          }
        }
      });
    }

    @Override
    public void dispose() {
      UIUtil.disposeProgress(myProgressBar);
      UIUtil.dispose(myTitlePanel);
      final ActionListener[] listeners = myCancelButton.getActionListeners();
      for (ActionListener listener : listeners) {
        myCancelButton.removeActionListener(listener);
      }

    }

    public JPanel getPanel() {
      return myPanel;
    }

    public void setShouldShowBackground(final boolean shouldShowBackground) {
      myShouldShowBackground = shouldShowBackground;
      SwingUtilities.invokeLater(new Runnable() {
        @Override
        public void run() {
          myBackgroundButton.setVisible(shouldShowBackground);
          myPanel.revalidate();
        }
      });
    }

    public void changeCancelButtonText(String text) {
      myCancelButton.setText(text);
    }

    public void doCancelAction() {
      if (myShouldShowCancel) {
        ProgressWindow.this.cancel();
      }
    }

    public void cancel() {
      if (myShouldShowCancel) {
        SwingUtilities.invokeLater(new Runnable() {
          @Override
          public void run() {
            myCancelButton.setEnabled(false);
          }
        });
      }
    }

    private void createCenterPanel() {
      // Cancel button (if any)

      if (myCancelText != null) {
        myCancelButton.setText(myCancelText);
      }
      myCancelButton.setVisible(myShouldShowCancel);

      myBackgroundButton.setVisible(myShouldShowBackground);
      myBackgroundButton.addActionListener(
        new ActionListener() {
          @Override
          public void actionPerformed(ActionEvent e) {
            if (myShouldShowBackground) {
              ProgressWindow.this.background();
            }
          }
        }
      );
    }

    private synchronized void update() {
      if (myRepaintedFlag) {
        if (System.currentTimeMillis() > myLastTimeDrawn + UPDATE_INTERVAL) {
          myRepaintedFlag = false;
          SwingUtilities.invokeLater(myRepaintRunnable);
        }
        else {
          if (myUpdateAlarm.getActiveRequestCount() == 0 && !myUpdateAlarm.isDisposed()) {
            myUpdateAlarm.addRequest(myUpdateRequest, 500, getModalityState());
          }
        }
      }
    }

    public synchronized void background() {
      if (myShouldShowBackground) {
        myBackgroundButton.setEnabled(false);
      }

      hide();
    }

    public void hide() {
      SwingUtilities.invokeLater(new Runnable() {
        @Override
        public void run() {
          if (myPopup != null) {
            myPopup.close(DialogWrapper.CANCEL_EXIT_CODE);
            myPopup = null;
          }
        }
      });
    }

    public void show() {
      if (ApplicationManager.getApplication().isHeadlessEnvironment()) return;
      if (myParentWindow == null) return;
      if (myPopup != null) {
        myPopup.close(DialogWrapper.CANCEL_EXIT_CODE);
      }

      myPopup = myParentWindow.isShowing()
                ? new MyDialogWrapper(myParentWindow, myShouldShowCancel)
                : new MyDialogWrapper(myProject, myShouldShowCancel);
      myPopup.setUndecorated(true);
      if (SystemInfo.isAppleJvm) {
        // With Apple JDK we look for MacMessage parent by the window title.
        // Let's set just the title as the window title for simplicity.
        myPopup.setTitle(myTitle);
      }
      if (myPopup.getPeer() instanceof DialogWrapperPeerImpl) {
        ((DialogWrapperPeerImpl)myPopup.getPeer()).setAutoRequestFocus(false);
      }
      myPopup.pack();

      SwingUtilities.invokeLater(new Runnable() {
        @Override
        public void run() {
          if (myPopup != null) {
            if (myPopup.getPeer() instanceof FocusTrackbackProvider) {
              final FocusTrackback focusTrackback = ((FocusTrackbackProvider)myPopup.getPeer()).getFocusTrackback();
              if (focusTrackback != null) {
                focusTrackback.consume();
              }
            }

            getFocusManager().requestFocus(myCancelButton, true);
          }
        }
      });

      myPopup.show();
    }

    public boolean wasShown() {
      return myWasShown;
    }

    private class MyDialogWrapper extends DialogWrapper {
      private final boolean myIsCancellable;

      public MyDialogWrapper(Project project, final boolean cancellable) {
        super(project, false);
        init();
        myIsCancellable = cancellable;
      }

      public MyDialogWrapper(Component parent, final boolean cancellable) {
        super(parent, false);
        init();
        myIsCancellable = cancellable;
      }

      @Override
      public void doCancelAction() {
        if (myIsCancellable) {
          super.doCancelAction();
        }
      }

      @Override
      protected DialogWrapperPeer createPeer(@NotNull final Component parent, final boolean canBeParent) {
        if (System.getProperty("vintage.progress") == null) {
          try {
            return new GlassPaneDialogWrapperPeer(this, parent, canBeParent);
          }
          catch (GlassPaneDialogWrapperPeer.GlasspanePeerUnavailableException e) {
            return super.createPeer(parent, canBeParent);
          }
        }
        else {
          return super.createPeer(parent, canBeParent);
        }
      }

      @Override
      protected DialogWrapperPeer createPeer(final boolean canBeParent, final boolean applicationModalIfPossible) {
        return createPeer(null, canBeParent, applicationModalIfPossible);
      }

      @Override
      protected DialogWrapperPeer createPeer(final Window owner, final boolean canBeParent, final boolean applicationModalIfPossible) {
        if (System.getProperty("vintage.progress") == null) {
          try {
            return new GlassPaneDialogWrapperPeer(this, canBeParent);
          }
          catch (GlassPaneDialogWrapperPeer.GlasspanePeerUnavailableException e) {
            return super.createPeer(WindowManager.getInstance().suggestParentWindow(myProject), canBeParent, applicationModalIfPossible);
          }
        }
        else {
          return super.createPeer(WindowManager.getInstance().suggestParentWindow(myProject), canBeParent, applicationModalIfPossible);
        }
      }

      @Override
      protected DialogWrapperPeer createPeer(final Project project, final boolean canBeParent) {
        if (System.getProperty("vintage.progress") == null) {
          try {
            return new GlassPaneDialogWrapperPeer(this, project, canBeParent);
          }
          catch (GlassPaneDialogWrapperPeer.GlasspanePeerUnavailableException e) {
            return super.createPeer(project, canBeParent);
          }
        }
        else {
          return super.createPeer(project, canBeParent);
        }
      }

      @Override
      protected void init() {
        super.init();
        setUndecorated(true);
        myPanel.setBorder(PopupBorder.Factory.create(true, true));
      }

      @Override
      protected boolean isProgressDialog() {
        return true;
      }

      @Override
      protected JComponent createCenterPanel() {
        return myPanel;
      }

      @Override
      @Nullable
      protected JComponent createSouthPanel() {
        return null;
      }

      @Override
      @Nullable
      protected Border createContentPaneBorder() {
        return null;
      }
    }
  }

  public void setBackgroundHandler(@Nullable Runnable backgroundHandler) {
    myBackgroundHandler = backgroundHandler;
    myDialog.setShouldShowBackground(backgroundHandler != null);
  }

  public void setCancelButtonText(String text) {
    if (myDialog != null) {
      myDialog.changeCancelButtonText(text);
    }
    else {
      myCancelText = text;
    }
  }

  private IdeFocusManager getFocusManager() {
    return IdeFocusManager.getInstance(myProject);
  }

  @Override
  public void dispose() {
  }

  @Override
  public boolean isPopupWasShown() {
    return myDialog != null && myDialog.myPopup != null && myDialog.myPopup.isShowing();
  }
}