fixed memleak in BreakpointsDialogFactory
[idea/community.git] / platform / xdebugger-impl / src / com / intellij / xdebugger / impl / breakpoints / ui / BreakpointsDialogFactory.java
1 /*
2  * Copyright 2000-2015 JetBrains s.r.o.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 package com.intellij.xdebugger.impl.breakpoints.ui;
17
18 import com.intellij.openapi.Disposable;
19 import com.intellij.openapi.components.ServiceManager;
20 import com.intellij.openapi.project.Project;
21 import com.intellij.openapi.ui.popup.Balloon;
22 import com.intellij.openapi.util.Disposer;
23 import com.intellij.xdebugger.impl.breakpoints.XBreakpointUtil;
24 import org.jetbrains.annotations.Nullable;
25
26 public class BreakpointsDialogFactory {
27
28   private Project myProject;
29   private Balloon myBalloonToHide;
30   private Object myBreakpoint;
31   private BreakpointsDialog myDialogShowing;
32
33
34   public BreakpointsDialogFactory(Project project) {
35     myProject = project;
36   }
37
38   public void setBalloonToHide(final Balloon balloonToHide, Object breakpoint) {
39     myBalloonToHide = balloonToHide;
40     myBreakpoint = breakpoint;
41     Disposer.register(myBalloonToHide, new Disposable() {
42       @Override
43       public void dispose() {
44         if (myBalloonToHide == balloonToHide) {
45           myBalloonToHide = null;
46           myBreakpoint = null;
47         }
48       }
49     });
50   }
51
52   public static BreakpointsDialogFactory getInstance(Project project) {
53     return ServiceManager.getService(project, BreakpointsDialogFactory.class);
54   }
55
56   public boolean isBreakpointPopupShowing() {
57     return (myBalloonToHide != null && !myBalloonToHide.isDisposed()) || myDialogShowing != null;
58   }
59
60   public void showDialog(@Nullable Object initialBreakpoint) {
61     if (myDialogShowing != null) {
62       return;
63     }
64
65     final BreakpointsDialog dialog = new BreakpointsDialog(myProject, initialBreakpoint != null ? initialBreakpoint : myBreakpoint, XBreakpointUtil.collectPanelProviders()) {
66       @Override
67       protected void dispose() {
68         myBreakpoint = null;
69         for (BreakpointPanelProvider provider : XBreakpointUtil.collectPanelProviders()) {
70           provider.onDialogClosed(myProject);
71         }
72         myDialogShowing = null;
73
74         super.dispose();
75       }
76     };
77
78     if (myBalloonToHide != null) {
79       if (!myBalloonToHide.isDisposed()) {
80         myBalloonToHide.hide();
81       }
82       myBalloonToHide = null;
83     }
84     myDialogShowing = dialog;
85
86     dialog.show();
87   }
88 }