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

import org.eclipse.wb.core.controls.CCombo3;
import org.eclipse.wb.internal.core.model.property.Property;
import org.eclipse.wb.internal.core.model.property.table.PropertyTable;

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.FocusAdapter;
import org.eclipse.swt.events.FocusEvent;
import org.eclipse.swt.events.MouseAdapter;
import org.eclipse.swt.events.MouseEvent;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.graphics.Rectangle;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Listener;

/**
 * The {@link PropertyEditor} for selecting single value using {@link CCombo3}.
 * 
 * @author scheglov_ke
 * @coverage core.model.property.editor
 */
public abstract class AbstractComboPropertyEditor extends TextDisplayPropertyEditor {
  ////////////////////////////////////////////////////////////////////////////
  //
  // Editing
  //
  ////////////////////////////////////////////////////////////////////////////
  private CCombo3 m_combo;
  private boolean m_doDropDown;

  @Override
  public boolean activate(final PropertyTable propertyTable, final Property property, Point location)
      throws Exception {
    // create combo
    {
      m_combo = new CCombo3(propertyTable, SWT.NONE);
      m_doDropDown = true;
      // add items
      addItems(property, m_combo);
      // select item
      selectItem(property, m_combo);
    }
    // add listeners
    m_combo.addFocusListener(new FocusAdapter() {
      @Override
      public void focusLost(FocusEvent e) {
        propertyTable.deactivateEditor(true);
      }
    });
    m_combo.addSelectionListener(new SelectionAdapter() {
      @Override
      public void widgetSelected(SelectionEvent e) {
        int index = m_combo.getSelectionIndex();
        toProperty(propertyTable, property, index);
      }
    });
    m_combo.addListener(SWT.KeyDown, new Listener() {
      public void handleEvent(Event event) {
        switch (event.keyCode) {
          case SWT.ESC :
            propertyTable.deactivateEditor(false);
            break;
          case SWT.DEL :
            try {
              property.setValue(Property.UNKNOWN_VALUE);
              event.doit = false;
              selectItem(property, m_combo);
            } catch (Throwable e) {
              propertyTable.handleException(e);
              propertyTable.deactivateEditor(false);
            }
            m_combo.doDropDown(false);
            break;
        }
      }
    });
    m_combo.addMouseListener(new MouseAdapter() {
      @Override
      public void mouseDoubleClick(MouseEvent e) {
        int index = (m_combo.getSelectionIndex() + 1) % m_combo.getItemCount();
        toProperty(propertyTable, property, index);
      }
    });
    // keep editor active
    return true;
  }

  @Override
  public final void setBounds(Rectangle bounds) {
    m_combo.setBounds(bounds);
    // editor created without bounds, so activate it after first setBounds()
    if (m_doDropDown) {
      m_doDropDown = false;
      m_combo.setFocus();
      m_combo.doDropDown(true);
      m_combo.startDrag();
    }
  }

  @Override
  public final void deactivate(PropertyTable propertyTable, Property property, boolean save) {
    if (m_combo != null) {
      m_combo.dispose();
      m_combo = null;
    }
  }

  ////////////////////////////////////////////////////////////////////////////
  //
  // Abstract methods
  //
  ////////////////////////////////////////////////////////////////////////////
  /**
   * Adds items to given {@link CCombo3}.
   */
  protected abstract void addItems(Property property, CCombo3 combo) throws Exception;

  /**
   * Selects current item in given {@link CCombo3}.
   */
  protected abstract void selectItem(Property property, CCombo3 combo) throws Exception;

  /**
   * Transfers data from widget to {@link Property}.
   */
  protected abstract void toPropertyEx(Property property, CCombo3 combo, int index)
      throws Exception;

  /**
   * Transfers data from widget to {@link Property}.
   */
  private void toProperty(PropertyTable propertyTable, Property property, int index) {
    try {
      toPropertyEx(property, m_combo, index);
    } catch (Throwable e) {
      propertyTable.handleException(e);
    }
    propertyTable.deactivateEditor(false);
  }
}