summaryrefslogtreecommitdiff
path: root/propertysheet/src/org/eclipse/wb/internal/core/model/property/editor/AbstractComboBoxPropertyEditor.java
blob: f1223816bbfe79a7329a5760ad732b2945273f18 (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
/*******************************************************************************
 * 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.core.controls.CComboBox;
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.execution.ExecutionUtils;
import org.eclipse.wb.internal.core.utils.execution.RunnableEx;

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.FocusAdapter;
import org.eclipse.swt.events.FocusEvent;
import org.eclipse.swt.events.KeyAdapter;
import org.eclipse.swt.events.KeyEvent;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.graphics.Rectangle;

/**
 * The {@link PropertyEditor} for selecting single value using {@link CComboBox}. This editor has
 * in-line search-feature and is more suitable (vs {@link AbstractComboPropertyEditor}) for
 * properties with large lists of value items.
 * 
 * @author sablin_aa
 * @author scheglov_ke
 * @coverage core.model.property.editor
 */
public abstract class AbstractComboBoxPropertyEditor extends TextDisplayPropertyEditor {
  ////////////////////////////////////////////////////////////////////////////
  //
  // Editing
  //
  ////////////////////////////////////////////////////////////////////////////
  private CComboBox m_combo;
  private String m_dropDelayedText;

  @Override
  public final boolean activate(final PropertyTable propertyTable,
      final Property property,
      Point location) throws Exception {
    m_combo = new CComboBox(propertyTable, SWT.NONE);
    // initialize
    addItems(property, m_combo);
    selectItem(property, m_combo);
    // install listeners
    m_combo.addKeyListener(new KeyAdapter() {
      @Override
      public void keyPressed(KeyEvent e) {
        handleKeyPressed(propertyTable, property, e);
      }
    });
    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) {
        propertyTable.deactivateEditor(true);
      }
    });
    m_combo.setFocus();
    // schedule showing drop-down, because we don't have bounds yet
    ExecutionUtils.runAsync(new RunnableEx() {
      public void run() throws Exception {
        m_combo.comboDropDown(true);
        if (m_dropDelayedText != null) {
          m_combo.setEditText(m_dropDelayedText);
          m_combo.setEditSelection(m_dropDelayedText.length(), m_dropDelayedText.length());
          m_dropDelayedText = null;
        }
      }
    });
    // keep editor active
    return true;
  }

  private void handleKeyPressed(PropertyTable propertyTable, Property property, KeyEvent e) {
    if (e.keyCode == SWT.ESC) {
      propertyTable.deactivateEditor(false);
    } else if (e.keyCode == SWT.ARROW_UP || e.keyCode == SWT.ARROW_DOWN) {
      e.doit = false;
      propertyTable.deactivateEditor(true);
      propertyTable.navigate(e);
    }
  }

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

  private void toProperty(PropertyTable propertyTable, Property property) {
    try {
      toPropertyEx(property, m_combo);
    } catch (Throwable e) {
      propertyTable.handleException(e);
    }
  }

  @Override
  public void setBounds(Rectangle bounds) {
    m_combo.setBounds(bounds);
  }

  @Override
  public void keyDown(PropertyTable propertyTable, Property property, KeyEvent event)
      throws Exception {
    boolean withAlt = (event.stateMask & SWT.ALT) != 0;
    boolean withCtrl = (event.stateMask & SWT.CTRL) != 0;
    if (event.character > 0x20 && !(withAlt || withCtrl)) {
      propertyTable.activateEditor(property, null);
      m_dropDelayedText = "" + event.character;
    }
  }

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

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

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