summaryrefslogtreecommitdiff
path: root/propertysheet/src/org/eclipse/wb/internal/core/model/property/editor/presentation/ButtonPropertyEditorPresentationImpl.java
blob: 209bf39f68c0f31c0dcb28f1da5f338d65436d00 (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
/*******************************************************************************
 * 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.editor.presentation;

import com.google.common.collect.Maps;

import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Rectangle;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.wb.internal.core.model.property.Property;
import org.eclipse.wb.internal.core.model.property.table.PropertyTable;
import org.eclipse.wb.internal.core.utils.Pair;

import java.util.Map;

/**
 * Internal implementation of {@link PropertyEditorPresentation} for displaying {@link Button}.
 *
 * @author scheglov_ke
 * @author mitin_aa
 * @coverage core.model.property.editor
 */
class ButtonPropertyEditorPresentationImpl extends PropertyEditorPresentation {
  protected final PropertyToControlMap m_propertyToControl = new PropertyToControlMap();
  private final ButtonPropertyEditorPresentation m_presentation;

  ////////////////////////////////////////////////////////////////////////////
  //
  // Constructor
  //
  ////////////////////////////////////////////////////////////////////////////
  public ButtonPropertyEditorPresentationImpl(ButtonPropertyEditorPresentation presentation) {
    m_presentation = presentation;
  }

  ////////////////////////////////////////////////////////////////////////////
  //
  // PropertyEditorPresentation
  //
  ////////////////////////////////////////////////////////////////////////////
  @Override
  public final void hide(PropertyTable propertyTable, Property property) {
    Control control = m_propertyToControl.remove(propertyTable, property);
    if (control != null) {
      control.dispose();
    }
  }

  @Override
  public final int show(PropertyTable propertyTable,
      Property property,
      int x,
      int y,
      int width,
      int height) {
    // prepare control
    Control control = m_propertyToControl.get(propertyTable, property);
    if (control == null) {
      control = createControl(propertyTable, property);
    }
    // set bounds
    final int controlWidth = height;
    final int controlX = x + width - controlWidth;
    setBounds(control, controlX, y, controlWidth, height);
    return controlWidth;
  }

  /**
   * Finds and select the appropriate {@link Control} belonging to given property.
   */
  public void setSelection(PropertyTable propertyTable, Property property, boolean selected) {
    Button button = (Button) m_propertyToControl.get(propertyTable, property);
    if (button != null) {
      button.setSelection(selected);
    }
  }

  ////////////////////////////////////////////////////////////////////////////
  //
  // Control
  //
  ////////////////////////////////////////////////////////////////////////////
  /**
   * Creates the control for given property and initializes newly created control.
   */
  private Control createControl(final PropertyTable propertyTable, final Property property) {
    Control control = createControlImpl(propertyTable, property);
    m_propertyToControl.put(propertyTable, property, control);
    // when Control disposed, remove Control/Property from map to avoid memory leak
    control.addListener(SWT.Dispose, new Listener() {
      @Override
    public void handleEvent(Event e) {
        m_propertyToControl.remove(propertyTable, property);
      }
    });
    // activate property on mouse down
    control.addListener(SWT.MouseDown, new Listener() {
      @Override
    public void handleEvent(Event event) {
        propertyTable.deactivateEditor(true);
        propertyTable.setActiveProperty(property);
      }
    });
    // return focus on propertyTable after click
    control.addListener(SWT.MouseUp, new Listener() {
      @Override
    public void handleEvent(Event event) {
        propertyTable.forceFocus();
      }
    });
    // handle selection
    control.addListener(SWT.Selection, new Listener() {
      @Override
    public void handleEvent(Event event) {
        try {
          getPresentation().onClick(propertyTable, property);
        } catch (Throwable e) {
          propertyTable.deactivateEditor(false);
          propertyTable.handleException(e);
        }
      }
    });
    return control;
  }

  /**
   * Creates the {@link Control} instance. By default, {@link Button} instance created.
   */
  protected Control createControlImpl(final PropertyTable propertyTable, final Property property) {
    Button button = new Button(propertyTable, getPresentation().getStyle());
    button.setImage(getPresentation().getImage());
    button.setToolTipText(getPresentation().getTooltip());
    return button;
  }

  ////////////////////////////////////////////////////////////////////////////
  //
  // Access
  //
  ////////////////////////////////////////////////////////////////////////////
  /**
   * @return the 'parent' presentation. Internal usage only.
   */
  protected final ButtonPropertyEditorPresentation getPresentation() {
    return m_presentation;
  }

  ////////////////////////////////////////////////////////////////////////////
  //
  // Utils
  //
  ////////////////////////////////////////////////////////////////////////////
  /**
   * Sets new bounds for {@link Control}, optimizing when possible.
   */
  private static void setBounds(Control control, int newX, int newY, int newWidth, int newHeight) {
    // check, may be Control is invisible, so no reason to change bounds
    {
      // is in negative zone
      if (newY + newHeight < 0) {
        control.setVisible(false);
        return;
      }
      // is out of client area height
      Rectangle parentArea = control.getParent().getClientArea();
      if (newY > parentArea.height) {
        control.setVisible(false);
        return;
      }
    }
    // well, now we sure that Control is visible
    if (!control.getVisible()) {
      control.setVisible(true);
    }
    // prepare old size, remember new
    Integer oldWidthObject = (Integer) control.getData("oldWidth");
    Integer oldHeightObject = (Integer) control.getData("oldHeight");
    control.setData("oldWidth", newWidth);
    control.setData("oldHeight", newHeight);
    // check, may be same size
    if (oldWidthObject != null) {
      int oldWidth = oldWidthObject.intValue();
      int oldHeight = oldHeightObject.intValue();
      if (oldWidth == newWidth && oldHeight == newHeight) {
        control.setLocation(newX, newY);
        return;
      }
    }
    // no any optimization possible, just set bounds
    control.setBounds(newX, newY, newWidth, newHeight);
  }

  ////////////////////////////////////////////////////////////////////////////
  //
  // Controls map
  //
  ////////////////////////////////////////////////////////////////////////////
  protected static final class PropertyToControlMap {
    private final Map<Pair<PropertyTable, Property>, Control> m_map = Maps.newHashMap();

    void put(PropertyTable propertyTable, Property property, Control control) {
      m_map.put(Pair.create(propertyTable, property), control);
    }

    Control remove(PropertyTable propertyTable, Property property) {
      return m_map.remove(Pair.create(propertyTable, property));
    }

    Control get(PropertyTable propertyTable, Property property) {
      return m_map.get(Pair.create(propertyTable, property));
    }
  }
}