1/*******************************************************************************
2 * Copyright (c) 2011 Google, Inc.
3 * All rights reserved. This program and the accompanying materials
4 * are made available under the terms of the Eclipse Public License v1.0
5 * which accompanies this distribution, and is available at
6 * http://www.eclipse.org/legal/epl-v10.html
7 *
8 * Contributors:
9 *    Google, Inc. - initial API and implementation
10 *******************************************************************************/
11package org.eclipse.wb.internal.core.model.property.table;
12
13import org.eclipse.wb.internal.core.model.property.Property;
14
15import org.eclipse.swt.SWT;
16import org.eclipse.swt.graphics.Point;
17import org.eclipse.swt.widgets.Composite;
18import org.eclipse.swt.widgets.Control;
19import org.eclipse.swt.widgets.Event;
20import org.eclipse.swt.widgets.Listener;
21import org.eclipse.swt.widgets.Shell;
22
23/**
24 * Provider for tooltip controls.
25 *
26 * @author scheglov_ke
27 * @coverage core.model.property.table
28 */
29public abstract class PropertyTooltipProvider {
30  /**
31   * Show tooltip directly on property row.
32   */
33  public static final int ON = 0;
34  /**
35   * Show tooltip below property row.
36   */
37  public static final int BELOW = 1;
38
39  ////////////////////////////////////////////////////////////////////////////
40  //
41  // PropertyTooltipProvider
42  //
43  ////////////////////////////////////////////////////////////////////////////
44  /**
45   * Create tooltip control.
46   */
47  public abstract Control createTooltipControl(Property property,
48      Composite parent,
49      int availableWidth,
50      IPropertyTooltipSite site);
51
52  /**
53   * Shows tooltip {@link Shell}.
54   */
55  public void show(Shell shell) {
56    shell.setVisible(true);
57  }
58
59  /**
60   * Returns position for tooltip control. Usually we should show directly on same row, because we
61   * use tooltip to show just longer (full) text of property. But for "class" property we show
62   * hierarchy, so it is better show it below and allow user see also property row.
63   */
64  public int getTooltipPosition() {
65    return ON;
66  }
67
68  ////////////////////////////////////////////////////////////////////////////
69  //
70  // Tooltip listener
71  //
72  ////////////////////////////////////////////////////////////////////////////
73  /**
74   * {@link Listener} that hides tooltip on mouse exit or click.
75   */
76  protected static final class HideListener implements Listener {
77    private final IPropertyTooltipSite m_site;
78
79    ////////////////////////////////////////////////////////////////////////////
80    //
81    // Constructor
82    //
83    ////////////////////////////////////////////////////////////////////////////
84    public HideListener(IPropertyTooltipSite site) {
85      m_site = site;
86    }
87
88    ////////////////////////////////////////////////////////////////////////////
89    //
90    // Listener
91    //
92    ////////////////////////////////////////////////////////////////////////////
93    public void handleEvent(Event event) {
94      Control tooltipControl = (Control) event.widget;
95      switch (event.type) {
96        case SWT.MouseDown : {
97          PropertyTable table = m_site.getTable();
98          // convert location from tooltip to table
99          Point p = new Point(event.x, event.y);
100          p = tooltipControl.toDisplay(p);
101          p = table.toControl(p);
102          // send MouseDown to table
103          Event newEvent = new Event();
104          newEvent.x = p.x;
105          newEvent.y = p.y;
106          table.notifyListeners(SWT.MouseDown, newEvent);
107          // hide tooltip
108          m_site.hideTooltip();
109          break;
110        }
111        case SWT.MouseExit :
112          m_site.hideTooltip();
113          break;
114      }
115    }
116  }
117}
118