summaryrefslogtreecommitdiff
path: root/platform/platform-api/src/com/intellij/openapi/ui/FixedComboBoxEditor.java
blob: 6ef25dddb5bcebfc7e5ea2af0d0bb7dc9e75088e (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
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
/*
 * Copyright 2000-2014 JetBrains s.r.o.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.intellij.openapi.ui;

import com.intellij.openapi.util.SystemInfo;
import com.intellij.ui.Gray;
import com.intellij.util.ReflectionUtil;
import com.intellij.util.ui.MacUIUtil;
import com.intellij.util.ui.UIUtil;
import org.jetbrains.annotations.Nullable;

import javax.swing.*;
import javax.swing.border.Border;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;
import javax.swing.plaf.ComboBoxUI;
import javax.swing.plaf.basic.BasicComboBoxUI;
import javax.swing.plaf.basic.ComboPopup;
import java.awt.*;
import java.awt.event.ActionListener;
import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.lang.reflect.Method;

/**
 * Use this editor if you wish your combobox editor to look good on Macs.
 *
 * User: spLeaner
 */
public class FixedComboBoxEditor implements ComboBoxEditor {
  public static final Border EDITOR_BORDER = new MacComboBoxEditorBorder(false);
  public static final Border DISABLED_EDITOR_BORDER = new MacComboBoxEditorBorder(true);

  private JTextField myField;
  private Object oldValue;

  public FixedComboBoxEditor() {
    if (SystemInfo.isMac && UIUtil.isUnderAquaLookAndFeel()) {
      myField = new MacComboBoxTextField();
    } else {
      myField = new JTextField();
      myField.setBorder(null);
    }
  }

  protected JTextField getField() {
    return myField;
  }

  @Override
  public Component getEditorComponent() {
    return myField;
  }

  public void setItem(Object anObject) {
    if (anObject != null) {
      myField.setText(anObject.toString());
      oldValue = anObject;
    }
    else {
      myField.setText("");
    }
  }

  public Object getItem() {
    Object newValue = myField.getText();
    if (oldValue != null && !(oldValue instanceof String)) {
      // The original value is not a string. Should return the value in it's
      // original type.
      if (newValue.equals(oldValue.toString())) {
        return oldValue;
      }
      else {
        // Must take the value from the editor and get the value and cast it to the new type.
        Class cls = oldValue.getClass();
        try {
          Method method = cls.getMethod("valueOf", new Class[]{String.class});
          newValue = method.invoke(oldValue, new Object[]{myField.getText()});
        }
        catch (Exception ex) {
          // Fail silently and return the newValue (a String object)
        }
      }
    }
    return newValue;
  }

  public void selectAll() {
    myField.selectAll();
    myField.requestFocus();
  }

  @Override
  public void addActionListener(ActionListener l) {
  }

  @Override
  public void removeActionListener(ActionListener l) {
  }

  @Nullable
  private static ComboPopup getComboboxPopup(final JComboBox comboBox) {
    final ComboBoxUI ui = comboBox.getUI();
    ComboPopup popup = null;
    if (ui instanceof BasicComboBoxUI) {
      popup = ReflectionUtil.getField(BasicComboBoxUI.class, ui, ComboPopup.class, "popup");
    }

    return popup;
  }

  private class MacComboBoxTextField extends JTextField implements DocumentListener, FocusListener {
    private MacComboBoxTextField() {
      setBorder(isEnabled() ? EDITOR_BORDER : DISABLED_EDITOR_BORDER);
      //setFont(UIUtil.getListFont());

      final InputMap inputMap = getInputMap();

      inputMap.put(KeyStroke.getKeyStroke("DOWN"), "aquaSelectNext");
      inputMap.put(KeyStroke.getKeyStroke("KP_DOWN"), "aquaSelectNext");
      inputMap.put(KeyStroke.getKeyStroke("UP"), "aquaSelectPrevious");
      inputMap.put(KeyStroke.getKeyStroke("KP_UP"), "aquaSelectPrevious");

      inputMap.put(KeyStroke.getKeyStroke("HOME"), "aquaSelectHome");
      inputMap.put(KeyStroke.getKeyStroke("END"), "aquaSelectEnd");
      inputMap.put(KeyStroke.getKeyStroke("PAGE_UP"), "aquaSelectPageUp");
      inputMap.put(KeyStroke.getKeyStroke("PAGE_DOWN"), "aquaSelectPageDown");

      inputMap.put(KeyStroke.getKeyStroke("ENTER"), "aquaEnterPressed");
      inputMap.put(KeyStroke.getKeyStroke("SPACE"), "aquaSpacePressed");

      //getActionMap().put("macEnterPressed", macEnterPressedAction);
      //getDocument().addDocumentListener(this);

      addPropertyChangeListener(new PropertyChangeListener() {
        @Override
        public void propertyChange(PropertyChangeEvent evt) {
          if ("enabled".equals(evt.getPropertyName())) {
            setBorder(Boolean.TRUE.equals(evt.getNewValue()) ? EDITOR_BORDER : DISABLED_EDITOR_BORDER);
            repaint();
          }
        }
      });

      addFocusListener(this);
    }

    @Override
    public boolean hasFocus() {
      final Container parent = getParent();
      if (parent instanceof ComboBox && ((ComboBox)parent).myPaintingNow) {
        return false; // to disable focus painting around combobox button
      }
      return super.hasFocus();
    }

    @Override
    public void focusGained(FocusEvent e) {
      repaintCombobox();
    }

    private void repaintCombobox() {
      final Container parent = getParent();
      if (parent == null) return;
      if (parent instanceof JComponent && Boolean.TRUE == ((JComponent)parent).getClientProperty("JComboBox.isTableCellEditor")) return;
      final Container grandParent = parent.getParent();
      if (grandParent != null) {
        grandParent.repaint();
      }
    }

    @Override
    public void focusLost(FocusEvent e) {
      repaintCombobox();
    }

    @Override
    public Dimension getMinimumSize() {
      final Dimension minimumSize = super.getMinimumSize();
      return new Dimension(minimumSize.width, minimumSize.height + 2);
    }

    @Override
    public Dimension getPreferredSize() {
      return getMinimumSize();
    }

    @Override
    public void setBounds(final int x, final int y, final int width, final int height) {
      UIUtil.setComboBoxEditorBounds(x, y, width, height, this);
    }

    @Override
    public void insertUpdate(DocumentEvent e) {
      textChanged();
    }

    @Override
    public void removeUpdate(DocumentEvent e) {
      textChanged();
    }

    @Override
    public void changedUpdate(DocumentEvent e) {
      textChanged();
    }

    private void textChanged() {
      final Container ancestor = SwingUtilities.getAncestorOfClass(JComboBox.class, this);
      if (ancestor == null || !ancestor.isVisible()) return;

      final JComboBox comboBox = (JComboBox)ancestor;
      if (!comboBox.isPopupVisible()) return;

      final ComboPopup popup = getComboboxPopup(comboBox);
      if (popup == null) return;

      String s = myField.getText();

      final ListModel listmodel = comboBox.getModel();
      int i = listmodel.getSize();
      if (s.length() > 0) {
        for (int j = 0; j < i; j++) {
          Object obj = listmodel.getElementAt(j);
          if (obj == null) continue;

          String s1 = obj.toString();
          if (s1 != null && (s1.startsWith(s) || s1.equals(s))) {
            popup.getList().setSelectedIndex(j);
            return;
          }
        }
      }

      popup.getList().clearSelection();
    }
  }

  public static class MacComboBoxEditorBorder implements Border {

    private boolean myDisabled;

    public MacComboBoxEditorBorder(final boolean disabled) {
      myDisabled = disabled;
    }

    @Override
    public void paintBorder(final Component c, final Graphics g, final int x, final int y, final int width, final int height) {
      Color topColor;
      Color secondTopColor;
      Color leftRightColor;
      Color bottomColor;

      if (myDisabled) {
        topColor = Gray._200;
        secondTopColor = Gray._250;
        leftRightColor = Gray._205;
        bottomColor = Gray._220;
      }
      else {
        topColor = Gray._150;
        secondTopColor = Gray._230;
        leftRightColor = Gray._175;
        bottomColor = Gray._200;
      }

      int _y = y + MacUIUtil.MAC_COMBO_BORDER_V_OFFSET;
      
      g.setColor(topColor);
      g.drawLine(x + 3, _y + 3, x + width - 1, _y + 3);

      g.setColor(secondTopColor);
      g.drawLine(x + 3, _y + 4, x + width - 1, _y + 4);

      g.setColor(leftRightColor);
      g.drawLine(x + 3, _y + 4, x + 3, _y + height - 4);
      g.drawLine(x + width - 1, _y + 4, x + width - 1, _y + height - 4);

      g.setColor(bottomColor);
      g.drawLine(x + 4, _y + height - 4, x + width - 2, _y + height - 4);

      g.setColor(UIUtil.getPanelBackground());

      g.fillRect(x,  y, width, 3 + (SystemInfo.isMacOSLion ? 1 : 0));
      g.fillRect(x, _y, 3, height);
      g.fillRect(x, _y + height - 3, width, 3);
    }

    @Override
    public Insets getBorderInsets(final Component c) {
      return new Insets(6, 6, 4, 3);
    }

    @Override
    public boolean isBorderOpaque() {
      return true;
    }
  }
}