summaryrefslogtreecommitdiff
path: root/propertysheet/src/org/eclipse/wb/internal/core/model/property/table/PropertyTableTooltipHelper.java
blob: 16b9d8f6b28c769bf675c77bcee639f65f1c4d3d (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
/*******************************************************************************
 * Copyright (c) 2011 Google, Inc.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *    Google, Inc. - initial API and implementation
 *******************************************************************************/
package org.eclipse.wb.internal.core.model.property.table;

import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.wb.internal.core.EnvironmentUtils;
import org.eclipse.wb.internal.core.model.property.Property;
import org.eclipse.wb.internal.core.utils.ui.GridLayoutFactory;

/**
 * Helper class for displaying tooltips.
 *
 * @author scheglov_ke
 * @coverage core.model.property.table
 */
class PropertyTableTooltipHelper implements IPropertyTooltipSite {
  private final PropertyTable m_table;
  private Shell m_tooltip;

  ////////////////////////////////////////////////////////////////////////////
  //
  // Constructor
  //
  ////////////////////////////////////////////////////////////////////////////
  public PropertyTableTooltipHelper(PropertyTable table) {
    m_table = table;
    m_table.addListener(SWT.MouseHover, new Listener() {
      @Override
    public void handleEvent(Event event) {
        if (event.stateMask == 0) {
          showTooltip();
        }
      }
    });
    m_table.addListener(SWT.MouseExit, new Listener() {
      @Override
    public void handleEvent(Event event) {
        // check, may be cursor is now on tooltip, so ignore this MouseExit
        {
          Control control = Display.getCurrent().getCursorControl();
          while (control != null) {
            if (control == m_tooltip) {
              return;
            }
            control = control.getParent();
          }
        }
        // no, we should hide tooltip
        hideTooltip();
      }
    });
  }

  ////////////////////////////////////////////////////////////////////////////
  //
  // Access
  //
  ////////////////////////////////////////////////////////////////////////////
  private Property m_property;
  private boolean m_onTitle;
  private boolean m_onValue;
  private int m_beginX;
  private int m_endX;
  private int m_y;
  private int m_rowHeight;

  /**
   * {@link PropertyTable} call this method to inform that cursor location was changed.
   */
  public void update(Property property,
      boolean onTitle,
      boolean onValue,
      int beginX,
      int endX,
      int y,
      int rowHeight) {
    m_property = property;
    m_onTitle = onTitle;
    m_onValue = onValue;
    m_beginX = beginX;
    m_endX = endX;
    m_y = y;
    m_rowHeight = rowHeight;
  }

  ////////////////////////////////////////////////////////////////////////////
  //
  // IPropertyTooltipSite
  //
  ////////////////////////////////////////////////////////////////////////////
  @Override
public PropertyTable getTable() {
    return m_table;
  }

  @Override
public void hideTooltip() {
    if (m_tooltip != null && !m_tooltip.isDisposed()) {
      m_tooltip.dispose();
    }
    m_tooltip = null;
  }

  ////////////////////////////////////////////////////////////////////////////
  //
  // Showing tooltip
  //
  ////////////////////////////////////////////////////////////////////////////
  private void showTooltip() {
    hideTooltip();
    // check for property
    if (m_property == null) {
      return;
    }
    //
    if (m_onTitle) {
      showTooltip(m_property.getAdapter(PropertyTooltipProvider.class), m_beginX, m_endX);
    }
    if (m_onValue) {
      showTooltip(m_property.getEditor().getAdapter(PropertyTooltipProvider.class),
              m_beginX, m_endX);
    }
  }

  private void showTooltip(PropertyTooltipProvider provider, int startX, int endX) {
      if (provider == null) {
        return;
      }
    // create Shell
    {
      m_tooltip = new Shell(m_table.getShell(), SWT.NO_FOCUS | SWT.ON_TOP | SWT.TOOL | SWT.SINGLE);
      configureColors(m_tooltip);
      GridLayoutFactory.create(m_tooltip).noMargins();
    }
    // prepare control
    Control control = provider.createTooltipControl(m_property, m_tooltip, endX - startX, this);
    if (control == null) {
      hideTooltip();
      return;
    }
    // show Shell
    {
      // prepare tooltip location
      Point tooltipLocation;
      if (provider.getTooltipPosition() == PropertyTooltipProvider.ON) {
        tooltipLocation = m_table.toDisplay(new Point(startX, m_y));
      } else {
        tooltipLocation = m_table.toDisplay(new Point(startX, m_y + m_rowHeight));
      }
      // set location/size and open
      m_tooltip.setLocation(tooltipLocation.x, tooltipLocation.y);
      // for non-windows systems the tooltip may have invalid tooltip bounds
      // because some widget's API functions may fail if tooltip content is not visible
      // ex., on MacOSX tree widget's items has zero bounds since they are not yet visible.
      // the workaround is to preset tooltip size to big values before any computeSize called.
      if (!EnvironmentUtils.IS_WINDOWS) {
        m_tooltip.setSize(1000, 1000);
      }
      m_tooltip.setSize(m_tooltip.computeSize(SWT.DEFAULT, SWT.DEFAULT));
      provider.show(m_tooltip);
    }
  }

  ////////////////////////////////////////////////////////////////////////////
  //
  // Utils
  //
  ////////////////////////////////////////////////////////////////////////////
  /**
   * Sets given {@link Control} correct background/foreground for tooltips.
   */
  private void configureColors(Control control) {
    Display display = Display.getCurrent();
    control.setForeground(display.getSystemColor(SWT.COLOR_INFO_FOREGROUND));
    control.setBackground(display.getSystemColor(SWT.COLOR_INFO_BACKGROUND));
  }
}