summaryrefslogtreecommitdiff
path: root/java/debugger/impl/src/com/intellij/debugger/ui/impl/TipManager.java
blob: df91efed9579c31486aae7f8b932e41c0e92e61b (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
/*
 * 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.debugger.ui.impl;

import com.intellij.ide.FrameStateListener;
import com.intellij.ide.FrameStateManager;
import com.intellij.openapi.Disposable;
import com.intellij.openapi.actionSystem.AnAction;
import com.intellij.openapi.actionSystem.AnActionEvent;
import com.intellij.openapi.actionSystem.CustomShortcutSet;
import com.intellij.openapi.keymap.KeymapUtil;
import com.intellij.openapi.util.Disposer;
import com.intellij.openapi.util.Weighted;
import com.intellij.openapi.util.registry.Registry;
import com.intellij.openapi.wm.IdeGlassPane;
import com.intellij.openapi.wm.IdeGlassPaneUtil;
import com.intellij.util.Alarm;
import com.intellij.util.ui.UIUtil;
import com.intellij.util.ui.update.Activatable;
import com.intellij.util.ui.update.UiNotifyConnector;
import com.intellij.xdebugger.settings.XDebuggerSettingsManager;

import javax.swing.*;
import javax.swing.event.PopupMenuEvent;
import javax.swing.event.PopupMenuListener;
import java.awt.*;
import java.awt.event.*;

/**
 * @author lex
 */
public class TipManager implements Disposable, PopupMenuListener {

  private volatile boolean myIsDisposed = false;
  private boolean myPopupShown;
  private MyAwtPreprocessor myHideCanceller;

  private MouseEvent myLastMouseEvent;

  public interface TipFactory {
    JComponent createToolTip (MouseEvent e);
    MouseEvent createTooltipEvent(MouseEvent candidateEvent);
    boolean isFocusOwner();
  }


  private boolean isOverTip(MouseEvent e) {
    if (myCurrentTooltip != null) {
      if(!myCurrentTooltip.isShowing()) {
        hideTooltip(true);
        return false;
      }
      final Component eventOriginator = e.getComponent();
      if (eventOriginator == null) {
        return false;
      }
      final Point point = e.getPoint();
      SwingUtilities.convertPointToScreen(point, eventOriginator);

      final Rectangle bounds = myCurrentTooltip.getBounds();
      final Point tooltipLocationOnScreen = myCurrentTooltip.getLocationOnScreen();
      bounds.setLocation(tooltipLocationOnScreen.x, tooltipLocationOnScreen.y);

      return bounds.contains(point);
    }
    return false;
  }

  boolean myInsideComponent;

  private class MyMouseListener extends MouseAdapter implements Weighted{
    @Override
    public void mouseExited(final MouseEvent e) {
      myInsideComponent = false;
    }

    @Override
    public void mousePressed(final MouseEvent e) {
      if (myInsideComponent) {
        hideTooltip(true);
      }
    }

    @Override
    public double getWeight() {
      return 0;
    }

    @Override
    public void mouseEntered(final MouseEvent e) {
      myInsideComponent = true;
    }
  }

  private class MyFrameStateListener extends FrameStateListener.Adapter {
    @Override
    public void onFrameDeactivated() {
      hideTooltip(true);
    }
  }

  public JPopupMenu registerPopup(JPopupMenu menu) {
    menu.addPopupMenuListener(this);
    return menu;
  }

  @Override
  public void popupMenuWillBecomeVisible(final PopupMenuEvent e) {
    myPopupShown = true;
  }

  @Override
  public void popupMenuWillBecomeInvisible(final PopupMenuEvent e) {
    onPopupClosed(e);
  }

  @Override
  public void popupMenuCanceled(final PopupMenuEvent e) {
    onPopupClosed(e);
  }

  private void onPopupClosed(final PopupMenuEvent e) {
    myPopupShown = false;
    if (e.getSource() instanceof JPopupMenu) {
      ((JPopupMenu)e.getSource()).removePopupMenuListener(this);
    }
  }

  private class MyMouseMotionListener extends MouseMotionAdapter implements Weighted{
    @Override
    public void mouseMoved(final MouseEvent e) {
      myLastMouseEvent = e;

      if (!myComponent.isShowing()) return;

      myInsideComponent = true;

      if (myCurrentTooltip == null) {
        if (isInsideComponent(e)) {
          tryTooltip(e, true);
        }
      } else {
        if (!isOverTip(e)) {
          tryTooltip(e, true);
        }
      }
    }

    @Override
    public double getWeight() {
      return 0;
    }
  }

  private boolean isInsideComponent(final MouseEvent e) {
    final Rectangle compBounds = myComponent.getVisibleRect();
    final Point compPoint = SwingUtilities.convertPoint(e.getComponent(), e.getPoint(), myComponent);

    return compBounds.contains(compPoint);
  }


  private void tryTooltip(final InputEvent e, final boolean auto) {
    myShowAlarm.cancelAllRequests();
    myHideAlarm.cancelAllRequests();
    myShowAlarm.addRequest(new Runnable() {
      @Override
      public void run() {
        if (!myIsDisposed && !myPopupShown) {
          showTooltip(e, auto);
        }
      }
    }, auto ? XDebuggerSettingsManager.getInstance().getDataViewSettings().getValueLookupDelay() : 10);
  }

  private void showTooltip(InputEvent e, boolean auto) {
    if (auto && !Registry.is("debugger.valueTooltipAutoShow")) return;

    MouseEvent sourceEvent = null;
    JComponent newTip = null;

    if (e instanceof MouseEvent) {
      sourceEvent = (MouseEvent)e;
    } else if (e instanceof KeyEvent) {
      sourceEvent = myTipFactory.createTooltipEvent(myLastMouseEvent);
    }


    MouseEvent convertedEvent = null;
    if (sourceEvent != null) {
      convertedEvent = SwingUtilities.convertMouseEvent(sourceEvent.getComponent(), sourceEvent, myComponent);
      newTip = myTipFactory.createToolTip(convertedEvent);
    }

    if (newTip == null || (auto && !myTipFactory.isFocusOwner())) {
      hideTooltip(false);
      return;
    }

    if(newTip == myCurrentTooltip) {
      if (!auto) {
        hideTooltip(true);
        return;
      }
      return;
    }

    hideTooltip(true);

    if(myComponent.isShowing()) {
      PopupFactory popupFactory = PopupFactory.getSharedInstance();
      final Point location = convertedEvent.getPoint();
      final Component sourceComponent = convertedEvent.getComponent();
      if (sourceComponent != null) {
        SwingUtilities.convertPointToScreen(location, sourceComponent);
      }

      myTipPopup = popupFactory.getPopup(myComponent, newTip, location.x, location.y);
      myInsideComponent = false;
      myTipPopup.show();
      myCurrentTooltip = newTip;
    }
  }

  public void hideTooltip() {
    hideTooltip(true);
  }
  
  public void hideTooltip(boolean now) {
    if (myTipPopup == null) return;

    if (now) {
      myHideAlarm.cancelAllRequests();
      myTipPopup.hide();
      myTipPopup = null;
      myCurrentTooltip = null;
    } else {
      myHideAlarm.addRequest(new Runnable() {
        @Override
        public void run() {
          if (myInsideComponent) {
            hideTooltip(true);
          }
        }
      }, 100);
    }
  }

  private JComponent myCurrentTooltip;
  private Popup myTipPopup;
  private final TipFactory myTipFactory;
  private final JComponent myComponent;
  private MouseListener myMouseListener = new MyMouseListener();
  private MouseMotionListener myMouseMotionListener = new MyMouseMotionListener();
  private FrameStateListener myFrameStateListener = new MyFrameStateListener();

  private final Alarm myShowAlarm = new Alarm();
  private final Alarm myHideAlarm = new Alarm();


  private IdeGlassPane myGP;

  public TipManager(final JComponent component, TipFactory factory) {
    myTipFactory = factory;
    myComponent = component;

    new UiNotifyConnector.Once(component, new Activatable() {
      @Override
      public void showNotify() {
        installListeners();
      }

      @Override
      public void hideNotify() {
      }
    });

    final HideTooltipAction hide = new HideTooltipAction();
    hide.registerCustomShortcutSet(new CustomShortcutSet(KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0)), myComponent);
    Disposer.register(this, new Disposable() {
      @Override
      public void dispose() {
        hide.unregisterCustomShortcutSet(myComponent);
      }
    });
  }


  private class HideTooltipAction extends AnAction {
    @Override
    public void actionPerformed(AnActionEvent e) {
      hideTooltip(true);
    }

    @Override
    public void update(AnActionEvent e) {
      e.getPresentation().setEnabled(myTipPopup != null);
    }
  }

  private void installListeners() {
    if (myIsDisposed) return;

    myGP = IdeGlassPaneUtil.find(myComponent);
    assert myGP != null;

    myGP.addMousePreprocessor(myMouseListener, this);
    myGP.addMouseMotionPreprocessor(myMouseMotionListener, this);

    myHideCanceller = new MyAwtPreprocessor();
    Toolkit.getDefaultToolkit().addAWTEventListener(myHideCanceller, AWTEvent.MOUSE_MOTION_EVENT_MASK | AWTEvent.KEY_EVENT_MASK | AWTEvent.MOUSE_EVENT_MASK);
    FrameStateManager.getInstance().addListener(myFrameStateListener);
  }

  @Override
  public void dispose() {
    Disposer.dispose(this);

    hideTooltip(true);

    Toolkit.getDefaultToolkit().removeAWTEventListener(myHideCanceller);

    myIsDisposed = true;
    myShowAlarm.cancelAllRequests();
    myMouseListener = null;
    myMouseMotionListener = null;
    FrameStateManager.getInstance().removeListener(myFrameStateListener);
    myFrameStateListener = null;
  }

  private class MyAwtPreprocessor implements AWTEventListener {

    @Override
    public void eventDispatched(AWTEvent event) {
      if (event.getID() == MouseEvent.MOUSE_MOVED) {
        preventFromHideIfInsideTooltip(event);
      } else if (event.getID() == MouseEvent.MOUSE_PRESSED || event.getID() == MouseEvent.MOUSE_RELEASED) {
        hideTooltipIfCloseClick((MouseEvent)event);
      } else if (event instanceof KeyEvent) {
        tryToShowTooltipIfRequested((KeyEvent)event);
      }
    }

    private void hideTooltipIfCloseClick(MouseEvent me) {
      if (myCurrentTooltip == null) return;

      if (isInsideTooltip(me) && UIUtil.isCloseClick(me)) {
        hideTooltip(true);
      }
    }

    private void tryToShowTooltipIfRequested(KeyEvent event) {
      if (KeymapUtil.isTooltipRequest(event)) {
        tryTooltip(event, false);
      } else {
        if (event.getID() == KeyEvent.KEY_PRESSED) {
          myLastMouseEvent = null;
        }
      }
    }

    private void preventFromHideIfInsideTooltip(AWTEvent event) {
      if (myCurrentTooltip == null) return;

      if (event.getID() == MouseEvent.MOUSE_MOVED) {
        final MouseEvent me = (MouseEvent)event;
        if (isInsideTooltip(me)) {
          myHideAlarm.cancelAllRequests();
        }
      }
    }

    private boolean isInsideTooltip(MouseEvent me) {
      return myCurrentTooltip == me.getComponent() || SwingUtilities.isDescendingFrom(me.getComponent(), myCurrentTooltip);
    }
  }
}