summaryrefslogtreecommitdiff
path: root/javax/swing/plaf/basic/BasicComboBoxUI.java
blob: ea6f98504351c3258d204ddabb5e4e4f8ca9accb (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
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
/* BasicComboBoxUI.java --
   Copyright (C) 2004, 2005, 2006,  Free Software Foundation, Inc.

This file is part of GNU Classpath.

GNU Classpath is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2, or (at your option)
any later version.

GNU Classpath is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
General Public License for more details.

You should have received a copy of the GNU General Public License
along with GNU Classpath; see the file COPYING.  If not, write to the
Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
02110-1301 USA.

Linking this library statically or dynamically with other modules is
making a combined work based on this library.  Thus, the terms and
conditions of the GNU General Public License cover the whole
combination.

As a special exception, the copyright holders of this library give you
permission to link this library with independent modules to produce an
executable, regardless of the license terms of these independent
modules, and to copy and distribute the resulting executable under
terms of your choice, provided that you also meet, for each linked
independent module, the terms and conditions of the license of that
module.  An independent module is a module which is not derived from
or based on this library.  If you modify this library, you may extend
this exception to your version of the library, but you are not
obligated to do so.  If you do not wish to do so, delete this
exception statement from your version. */


package javax.swing.plaf.basic;

import gnu.classpath.NotImplementedException;

import java.awt.Color;
import java.awt.Component;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Insets;
import java.awt.LayoutManager;
import java.awt.Rectangle;
import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;

import javax.accessibility.Accessible;
import javax.swing.CellRendererPane;
import javax.swing.ComboBoxEditor;
import javax.swing.ComboBoxModel;
import javax.swing.DefaultListCellRenderer;
import javax.swing.InputMap;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JComponent;
import javax.swing.JList;
import javax.swing.ListCellRenderer;
import javax.swing.LookAndFeel;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;
import javax.swing.event.ListDataEvent;
import javax.swing.event.ListDataListener;
import javax.swing.plaf.ComboBoxUI;
import javax.swing.plaf.ComponentUI;
import javax.swing.plaf.UIResource;

/**
 * A UI delegate for the {@link JComboBox} component.
 *
 * @author Olga Rodimina
 * @author Robert Schuster
 */
public class BasicComboBoxUI extends ComboBoxUI
{
  /**
   * The arrow button that is displayed in the right side of JComboBox. This
   * button is used to hide and show combo box's list of items.
   */
  protected JButton arrowButton;

  /**
   * The combo box represented by this UI delegate.
   */
  protected JComboBox comboBox;

  /**
   * The component that is responsible for displaying/editing the selected 
   * item of the combo box. 
   * 
   * @see BasicComboBoxEditor#getEditorComponent()
   */
  protected Component editor;

  /**
   * A listener listening to focus events occurring in the {@link JComboBox}.
   */
  protected FocusListener focusListener;

  /**
   * A flag indicating whether JComboBox currently has the focus.
   */
  protected boolean hasFocus;

  /**
   * A listener listening to item events fired by the {@link JComboBox}.
   */
  protected ItemListener itemListener;

  /**
   * A listener listening to key events that occur while {@link JComboBox} has
   * the focus.
   */
  protected KeyListener keyListener;

  /**
   * List used when rendering selected item of the combo box. The selection
   * and foreground colors for combo box renderer are configured from this
   * list.
   */
  protected JList listBox;

  /**
   * ListDataListener listening to JComboBox model
   */
  protected ListDataListener listDataListener;

  /**
   * Popup list containing the combo box's menu items.
   */
  protected ComboPopup popup;
  
  protected KeyListener popupKeyListener;
  
  protected MouseListener popupMouseListener;
  
  protected MouseMotionListener popupMouseMotionListener;

  /**
   * Listener listening to changes in the bound properties of JComboBox
   */
  protected PropertyChangeListener propertyChangeListener;

  /* Size of the largest item in the comboBox
   * This is package-private to avoid an accessor method.
   */
  Dimension displaySize = new Dimension();

  /**
   * Used to render the combo box values.
   */
  protected CellRendererPane currentValuePane;

  /**
   * The current minimum size if isMinimumSizeDirty is false.
   * Setup by getMinimumSize() and invalidated by the various listeners.
   */
  protected Dimension cachedMinimumSize;

  /**
   * Indicates whether or not the cachedMinimumSize field is valid or not.
   */
  protected boolean isMinimumSizeDirty = true;

  /**
   * Creates a new <code>BasicComboBoxUI</code> object.
   */
  public BasicComboBoxUI()
  {
    currentValuePane = new CellRendererPane();
    cachedMinimumSize = new Dimension();
  }

  /**
   * A factory method to create a UI delegate for the given 
   * {@link JComponent}, which should be a {@link JComboBox}.
   *
   * @param c The {@link JComponent} a UI is being created for.
   *
   * @return A UI delegate for the {@link JComponent}.
   */
  public static ComponentUI createUI(JComponent c)
  {
    return new BasicComboBoxUI();
  }

  /**
   * Installs the UI for the given {@link JComponent}.
   *
   * @param c  the JComponent to install a UI for.
   * 
   * @see #uninstallUI(JComponent)
   */
  public void installUI(JComponent c)
  {
    super.installUI(c);

    if (c instanceof JComboBox)
      {
        isMinimumSizeDirty = true;
        comboBox = (JComboBox) c;
        installDefaults();

        // Set editor and renderer for the combo box. Editor is used
        // only if combo box becomes editable, otherwise renderer is used
        // to paint the selected item; combobox is not editable by default.
        ListCellRenderer renderer = comboBox.getRenderer();
        if (renderer == null || renderer instanceof UIResource)
          comboBox.setRenderer(createRenderer());

        ComboBoxEditor currentEditor = comboBox.getEditor();
        if (currentEditor == null || currentEditor instanceof UIResource)
          {
            currentEditor = createEditor();
            comboBox.setEditor(currentEditor);
          } 
        editor = currentEditor.getEditorComponent();

        installComponents();
        installListeners();
        if (arrowButton != null)
          configureArrowButton();
        if (editor != null)
          configureEditor();
        comboBox.setLayout(createLayoutManager());
        comboBox.setFocusable(true);
        installKeyboardActions();
      }
  }

  /**
   * Uninstalls the UI for the given {@link JComponent}.
   *
   * @param c The JComponent that is having this UI removed.
   * 
   * @see #installUI(JComponent)
   */
  public void uninstallUI(JComponent c)
  {
    setPopupVisible(comboBox, false);
    popup.uninstallingUI();
    uninstallKeyboardActions();
    comboBox.setLayout(null);
    uninstallComponents();
    uninstallListeners();
    uninstallDefaults();
    comboBox = null;
  }

  /**
   * Installs the defaults that are defined in the {@link BasicLookAndFeel} 
   * for this {@link JComboBox}.
   * 
   * @see #uninstallDefaults()
   */
  protected void installDefaults()
  {
    LookAndFeel.installColorsAndFont(comboBox, "ComboBox.background",
                                     "ComboBox.foreground", "ComboBox.font");
    LookAndFeel.installBorder(comboBox, "ComboBox.border");
  }

  /**
   * Creates and installs the listeners for this UI.
   * 
   * @see #uninstallListeners()
   */
  protected void installListeners()
  {
    // install combo box's listeners
    propertyChangeListener = createPropertyChangeListener();
    comboBox.addPropertyChangeListener(propertyChangeListener);

    focusListener = createFocusListener();
    editor.addFocusListener(focusListener);

    itemListener = createItemListener();
    comboBox.addItemListener(itemListener);

    keyListener = createKeyListener();
    comboBox.addKeyListener(keyListener);

    // install listeners that listen to combo box model
    listDataListener = createListDataListener();
    comboBox.getModel().addListDataListener(listDataListener);

    // Install mouse and key listeners from the popup.
    popupMouseListener = popup.getMouseListener();
    comboBox.addMouseListener(popupMouseListener);

    popupMouseMotionListener = popup.getMouseMotionListener();
    comboBox.addMouseMotionListener(popupMouseMotionListener);

    popupKeyListener = popup.getKeyListener();
    comboBox.addKeyListener(popupKeyListener);
  }

  /**
   * Uninstalls the defaults and sets any objects created during
   * install to <code>null</code>.
   * 
   * @see #installDefaults()
   */
  protected void uninstallDefaults()
  {
    if (comboBox.getFont() instanceof UIResource)
      comboBox.setFont(null);

    if (comboBox.getForeground() instanceof UIResource)
      comboBox.setForeground(null);
    
    if (comboBox.getBackground() instanceof UIResource)
      comboBox.setBackground(null);

    LookAndFeel.uninstallBorder(comboBox);
  }

  /**
   * Detaches all the listeners we attached in {@link #installListeners}.
   * 
   * @see #installListeners()
   */
  protected void uninstallListeners()
  {
    comboBox.removePropertyChangeListener(propertyChangeListener);
    propertyChangeListener = null;

    comboBox.removeFocusListener(focusListener);
    listBox.removeFocusListener(focusListener);
    focusListener = null;

    comboBox.removeItemListener(itemListener);
    itemListener = null;

    comboBox.removeKeyListener(keyListener);
    keyListener = null;

    comboBox.getModel().removeListDataListener(listDataListener);
    listDataListener = null;

    if (popupMouseListener != null)
      comboBox.removeMouseListener(popupMouseListener);
    popupMouseListener = null;

    if (popupMouseMotionListener != null)
      comboBox.removeMouseMotionListener(popupMouseMotionListener);
    popupMouseMotionListener = null;

    if (popupKeyListener != null)
      comboBox.removeKeyListener(popupKeyListener);
    popupKeyListener = null;
  }

  /**
   * Creates the popup that will contain list of combo box's items.
   *
   * @return popup containing list of combo box's items
   */
  protected ComboPopup createPopup()
  {
    return new BasicComboPopup(comboBox);
  }

  /**
   * Creates a {@link KeyListener} to listen to key events.
   *
   * @return KeyListener that listens to key events.
   */
  protected KeyListener createKeyListener()
  {
    return new KeyHandler();
  }

  /**
   * Creates the {@link FocusListener} that will listen to changes in this
   * JComboBox's focus.
   *
   * @return the FocusListener.
   */
  protected FocusListener createFocusListener()
  {
    return new FocusHandler();
  }

  /**
   * Creates a {@link ListDataListener} to listen to the combo box's data model.
   *
   * @return The new listener.
   */
  protected ListDataListener createListDataListener()
  {
    return new ListDataHandler();
  }

  /**
   * Creates an {@link ItemListener} that will listen to the changes in
   * the JComboBox's selection.
   *
   * @return The ItemListener
   */
  protected ItemListener createItemListener()
  {
    return new ItemHandler();
  }

  /**
   * Creates a {@link PropertyChangeListener} to listen to the changes in
   * the JComboBox's bound properties.
   *
   * @return The PropertyChangeListener
   */
  protected PropertyChangeListener createPropertyChangeListener()
  {
    return new PropertyChangeHandler();
  }

  /**
   * Creates and returns a layout manager for the combo box.  Subclasses can 
   * override this method to provide a different layout.
   *
   * @return a layout manager for the combo box.
   */
  protected LayoutManager createLayoutManager()
  {
    return new ComboBoxLayoutManager();
  }

  /**
   * Creates a component that will be responsible for rendering the
   * selected component in the combo box.
   *
   * @return A renderer for the combo box.
   */
  protected ListCellRenderer createRenderer()
  {
    return new BasicComboBoxRenderer.UIResource();
  }

  /**
   * Creates the component that will be responsible for displaying/editing
   * the selected item in the combo box. This editor is used only when combo 
   * box is editable.
   *
   * @return A new component that will be responsible for displaying/editing
   *         the selected item in the combo box.
   */
  protected ComboBoxEditor createEditor()
  {
    return new BasicComboBoxEditor.UIResource();
  }

  /**
   * Installs the components for this JComboBox. ArrowButton, main
   * part of combo box (upper part) and popup list of items are created and
   * configured here.
   */
  protected void installComponents()
  {
    // create drop down list of items
    popup = createPopup();
    listBox = popup.getList();

    // create and install arrow button
    arrowButton = createArrowButton();
    comboBox.add(arrowButton);

    if (comboBox.isEditable())
      addEditor();

    comboBox.add(currentValuePane);
  }

  /**
   * Uninstalls components from this {@link JComboBox}.
   * 
   * @see #installComponents()
   */
  protected void uninstallComponents()
  {
    // uninstall arrow button
    unconfigureArrowButton();
    comboBox.remove(arrowButton);
    arrowButton = null;

    popup = null;

    if (comboBox.getRenderer() instanceof UIResource)
      comboBox.setRenderer(null);

    // if the editor is not an instanceof UIResource, it was not set by the
    // UI delegate, so don't clear it...
    ComboBoxEditor currentEditor = comboBox.getEditor();
    if (currentEditor instanceof UIResource)
      {
        comboBox.setEditor(null);
        editor = null;
      }
  }

  /**
   * Adds the current editor to the combo box.
   */
  public void addEditor()
  {
    removeEditor();
    editor = comboBox.getEditor().getEditorComponent();
    comboBox.add(editor);
  }

  /**
   * Removes the current editor from the combo box.
   */
  public void removeEditor()
  {
    if (editor != null)
      {
        unconfigureEditor();
        comboBox.remove(editor);
      }
  }

  /**
   * Configures the editor for this combo box.
   */
  protected void configureEditor()
  {
    editor.setFont(comboBox.getFont());
    if (popupKeyListener != null)
        editor.addKeyListener(popupKeyListener);
    comboBox.configureEditor(comboBox.getEditor(),
                             comboBox.getSelectedItem());
  }

  /**
   * Unconfigures the editor for this combo box. 
   */
  protected void unconfigureEditor()
  {
    if (popupKeyListener != null)
      editor.removeKeyListener(popupKeyListener);
  }

  /**
   * Configures the arrow button.
   * 
   * @see #configureArrowButton()
   */
  public void configureArrowButton()
  {
    if (arrowButton != null)
      {
        arrowButton.setEnabled(comboBox.isEnabled());
        arrowButton.setFocusable(false);
        if (popupMouseListener != null)
          arrowButton.addMouseListener(popupMouseListener);
        if (popupMouseMotionListener != null)
          arrowButton.addMouseMotionListener(popupMouseMotionListener);
      }
  }

  /**
   * Unconfigures the arrow button.
   * 
   * @see #configureArrowButton()
   *
   * @specnote The specification says this method is implementation specific
   *           and should not be used or overridden.
   */
  public void unconfigureArrowButton()
  {
    if (arrowButton != null)
      {
        if (popupMouseListener != null)
          arrowButton.removeMouseListener(popupMouseListener);
        if (popupMouseMotionListener != null)
          arrowButton.removeMouseMotionListener(popupMouseMotionListener);
      }
  }

  /**
   * Creates an arrow button for this {@link JComboBox}.  The arrow button is
   * displayed at the right end of the combo box and is used to display/hide
   * the drop down list of items.
   *
   * @return A new button.
   */
  protected JButton createArrowButton()
  {
    return new BasicArrowButton(BasicArrowButton.SOUTH);
  }

  /**
   * Returns <code>true</code> if the popup is visible, and <code>false</code>
   * otherwise.
   *
   * @param c The JComboBox to check
   *
   * @return <code>true</code> if popup part of the JComboBox is visible and 
   *         <code>false</code> otherwise.
   */
  public boolean isPopupVisible(JComboBox c)
  {
    return popup.isVisible();
  }

  /**
   * Displays/hides the {@link JComboBox}'s list of items on the screen.
   *
   * @param c The combo box, for which list of items should be
   *        displayed/hidden
   * @param v true if show popup part of the jcomboBox and false to hide.
   */
  public void setPopupVisible(JComboBox c, boolean v)
  {
    if (v)
      popup.show();
    else
      popup.hide();
  }

  /**
   * JComboBox is focus traversable if it is editable and not otherwise.
   *
   * @param c combo box for which to check whether it is focus traversable
   *
   * @return true if focus tranversable and false otherwise
   */
  public boolean isFocusTraversable(JComboBox c)
  {
    if (!comboBox.isEditable())
      return true;

    return false;
  }

  /**
   * Paints given menu item using specified graphics context
   *
   * @param g The graphics context used to paint this combo box
   * @param c comboBox which needs to be painted.
   */
  public void paint(Graphics g, JComponent c)
  {
    hasFocus = comboBox.hasFocus();
    if (! comboBox.isEditable())
      {
        Rectangle rect = rectangleForCurrentValue();
        paintCurrentValueBackground(g, rect, hasFocus);
        paintCurrentValue(g, rect, hasFocus);
      }
  }

  /**
   * Returns preferred size for the combo box.
   *
   * @param c comboBox for which to get preferred size
   *
   * @return The preferred size for the given combo box
   */
  public Dimension getPreferredSize(JComponent c)
  {
    return getMinimumSize(c);
  }

  /**
   * Returns the minimum size for this {@link JComboBox} for this
   * look and feel. Also makes sure cachedMinimimSize is setup correctly.
   *
   * @param c The {@link JComponent} to find the minimum size for.
   *
   * @return The dimensions of the minimum size.
   */
  public Dimension getMinimumSize(JComponent c)
  {
    if (isMinimumSizeDirty)
      {
        Insets i = getInsets();
        Dimension d = getDisplaySize();
        d.width += i.left + i.right + d.height;
        cachedMinimumSize = new Dimension(d.width, d.height + i.top + i.bottom);
        isMinimumSizeDirty = false;
      }
    return new Dimension(cachedMinimumSize);
  }

  /**
   * Returns the maximum size for this {@link JComboBox} for this
   * look and feel.
   *
   * @param c The {@link JComponent} to find the maximum size for
   *
   * @return The maximum size (<code>Dimension(32767, 32767)</code>).
   */
  public Dimension getMaximumSize(JComponent c)
  {
    return new Dimension(32767, 32767);
  }

  public int getAccessibleChildrenCount(JComponent c)
    throws NotImplementedException
  {
    // FIXME: Need to implement
    return 0;
  }

  public Accessible getAccessibleChild(JComponent c, int i)
    throws NotImplementedException
  {
    // FIXME: Need to implement
    return null;
  }

  /**
   * Returns true if the specified key is a navigation key and false otherwise
   *
   * @param keyCode a key for which to check whether it is navigation key or
   *        not.
   *
   * @return true if the specified key is a navigation key and false otherwis
   */
  protected boolean isNavigationKey(int keyCode)
    throws NotImplementedException
  {
    // FIXME: Need to implement
    return false;
  }

  /**
   * Selects next possible item relative to the current selection
   * to be next selected item in the combo box.
   */
  protected void selectNextPossibleValue()
  {
    int index = comboBox.getSelectedIndex();
    if (index != comboBox.getItemCount() - 1)
      comboBox.setSelectedIndex(index + 1);
  }

  /**
   * Selects previous item relative to current selection to be
   * next selected item.
   */
  protected void selectPreviousPossibleValue()
  {
    int index = comboBox.getSelectedIndex();
    if (index != 0)
      comboBox.setSelectedIndex(index - 1);
  }

  /**
   * Displays combo box popup if the popup is not currently shown
   * on the screen and hides it if it is currently shown
   */
  protected void toggleOpenClose()
  {
    setPopupVisible(comboBox, ! isPopupVisible(comboBox));
  }

  /**
   * Returns the bounds in which comboBox's selected item will be
   * displayed.
   *
   * @return rectangle bounds in which comboBox's selected Item will be
   *         displayed
   */
  protected Rectangle rectangleForCurrentValue()
  {
    int w = comboBox.getWidth();
    int h = comboBox.getHeight();
    Insets i = comboBox.getInsets();
    int arrowSize = h - (i.top + i.bottom);
    if (arrowButton != null)
      arrowSize = arrowButton.getWidth();
    return new Rectangle(i.left, i.top, w - (i.left + i.right + arrowSize),
                         h - (i.top + i.left));
  }

  /**
   * Returns the insets of the current border.
   *
   * @return Insets representing space between combo box and its border
   */
  protected Insets getInsets()
  {
    return comboBox.getInsets();
  }

  /**
   * Paints currently selected value in the main part of the combo
   * box (part without popup).
   *
   * @param g graphics context
   * @param bounds Rectangle representing the size of the area in which
   *        selected item should be drawn
   * @param hasFocus true if combo box has focus and false otherwise
   */
  public void paintCurrentValue(Graphics g, Rectangle bounds, boolean hasFocus)
  {
    Object currentValue = comboBox.getSelectedItem();
    boolean isPressed = arrowButton.getModel().isPressed();

    /* Gets the component to be drawn for the current value.
     * If there is currently no selected item we will take an empty
     * String as replacement.
     */
    ListCellRenderer renderer = comboBox.getRenderer();
    if (comboBox.getSelectedIndex() != -1)
      {
        Component comp;
        if (hasFocus && ! isPopupVisible(comboBox))
          {
            comp = renderer.getListCellRendererComponent(listBox,
                comboBox.getSelectedItem(), -1, true, false);
          }
        else
          {
            comp = renderer.getListCellRendererComponent(listBox,
                comboBox.getSelectedItem(), -1, false, false);
            Color bg = UIManager.getColor("ComboBox.disabledForeground");
            comp.setBackground(bg);
          }
        comp.setFont(comboBox.getFont());
        if (hasFocus && ! isPopupVisible(comboBox))
          {
            comp.setForeground(listBox.getSelectionForeground());
            comp.setBackground(listBox.getSelectionBackground());
          }
        else if (comboBox.isEnabled())
          {
            comp.setForeground(comboBox.getForeground());
            comp.setBackground(comboBox.getBackground());
          }
        else
          {
            Color fg = UIManager.getColor("ComboBox.disabledForeground");
            comp.setForeground(fg);
            Color bg = UIManager.getColor("ComboBox.disabledBackground");
            comp.setBackground(bg);
          }
        currentValuePane.paintComponent(g, comp, comboBox, bounds.x, bounds.y,
                                        bounds.width, bounds.height);
      }
  }

  /**
   * Paints the background of part of the combo box, where currently
   * selected value is displayed. If the combo box has focus this method
   * should also paint focus rectangle around the combo box.
   *
   * @param g graphics context
   * @param bounds Rectangle representing the size of the largest item  in the
   *        comboBox
   * @param hasFocus true if combo box has fox and false otherwise
   */
  public void paintCurrentValueBackground(Graphics g, Rectangle bounds,
                                          boolean hasFocus)
  {
    Color saved = g.getColor();
    if (comboBox.isEnabled())
      g.setColor(UIManager.getColor("UIManager.background"));
    else
      g.setColor(UIManager.getColor("UIManager.disabledBackground"));
    g.fillRect(bounds.x, bounds.y, bounds.width, bounds.height);
    g.setColor(saved);
  }

  private static final ListCellRenderer DEFAULT_RENDERER
    = new DefaultListCellRenderer();

  /**
   * Returns the default size for the display area of a combo box that does 
   * not contain any elements.  This method returns the width and height of
   * a single space in the current font, plus a margin of 1 pixel. 
   *
   * @return The default display size.
   * 
   * @see #getDisplaySize()
   */
  protected Dimension getDefaultSize()
  {
    Component comp = DEFAULT_RENDERER.getListCellRendererComponent(listBox,
        " ", -1, false, false);
    currentValuePane.add(comp);
    comp.setFont(comboBox.getFont());
    Dimension d = comp.getPreferredSize();
    currentValuePane.remove(comp);
    return d;
  }

  /**
   * Returns the size of the display area for the combo box. This size will be 
   * the size of the combo box, not including the arrowButton.
   *
   * @return The size of the display area for the combo box.
   */
  protected Dimension getDisplaySize()
  {
    Dimension dim = new Dimension();
    ListCellRenderer renderer = comboBox.getRenderer();
    if (renderer == null)
      {
        renderer = DEFAULT_RENDERER;
      }
    
    Object prototype = comboBox.getPrototypeDisplayValue();
    if (prototype != null)
      {
        Component comp = renderer.getListCellRendererComponent
          (listBox, prototype, -1, false, false);
        currentValuePane.add(comp);
        comp.setFont(comboBox.getFont());
        Dimension renderSize = comp.getPreferredSize();
        currentValuePane.remove(comp);
        dim.height = renderSize.height;
        dim.width = renderSize.width;
      }
    else
      {
        ComboBoxModel model = comboBox.getModel();
        int size = model.getSize();
        if (size > 0)
          {
            for (int i = 0; i < size; ++i)
              {
                Component comp = renderer.getListCellRendererComponent
                (listBox, model.getElementAt(i), -1, false, false);
                currentValuePane.add(comp);
                comp.setFont(comboBox.getFont());
                Dimension renderSize = comp.getPreferredSize();
                currentValuePane.remove(comp);
                dim.width = Math.max(dim.width, renderSize.width);
                dim.height = Math.max(dim.height, renderSize.height);
              }
          }
        else
          {
            dim = getDefaultSize();
            if (comboBox.isEditable())
              dim.width = 100;
          }
      }
    if (comboBox.isEditable())
      {
        Dimension editSize = editor.getPreferredSize();
        dim.width = Math.max(dim.width, editSize.width);
        dim.height = Math.max(dim.height, editSize.height);
      }
    displaySize.setSize(dim.width, dim.height);
    return dim;
  }

  /**
   * Installs the keyboard actions for the {@link JComboBox} as specified
   * by the look and feel.
   */
  protected void installKeyboardActions()
  {
    SwingUtilities.replaceUIInputMap(comboBox,
        JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT,
        (InputMap) UIManager.get("ComboBox.ancestorInputMap"));
    // Install any action maps here.
  }
  
  /**
   * Uninstalls the keyboard actions for the {@link JComboBox} there were
   * installed by in {@link #installListeners}.
   */
  protected void uninstallKeyboardActions()
  {
    SwingUtilities.replaceUIInputMap(comboBox,
        JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT, null);
    // Uninstall any action maps here.
  }

  /**
   * A {@link LayoutManager} used to position the sub-components of the
   * {@link JComboBox}.
   * 
   * @see BasicComboBoxUI#createLayoutManager()
   */
  public class ComboBoxLayoutManager implements LayoutManager
  {
    /**
     * Creates a new ComboBoxLayoutManager object.
     */
    public ComboBoxLayoutManager()
    {
      // Nothing to do here.
    }

    /**
     * Adds a component to the layout.  This method does nothing, since the
     * layout manager doesn't need to track the components.
     * 
     * @param name  the name to associate the component with (ignored).
     * @param comp  the component (ignored).
     */
    public void addLayoutComponent(String name, Component comp)
    {
      // Do nothing
    }

    /**
     * Removes a component from the layout.  This method does nothing, since
     * the layout manager doesn't need to track the components.
     * 
     * @param comp  the component.
     */
    public void removeLayoutComponent(Component comp)
    {
      // Do nothing
    }

    /**
     * Returns preferred layout size of the JComboBox.
     *
     * @param parent  the Container for which the preferred size should be 
     *                calculated.
     *
     * @return The preferred size for the given container
     */
    public Dimension preferredLayoutSize(Container parent)
    {
      return parent.getPreferredSize();
    }

    /**
     * Returns the minimum layout size.
     * 
     * @param parent  the container.
     * 
     * @return The minimum size.
     */
    public Dimension minimumLayoutSize(Container parent)
    {
      return parent.getMinimumSize();
    }

    /**
     * Arranges the components in the container.  It puts arrow
     * button right end part of the comboBox. If the comboBox is editable
     * then editor is placed to the left of arrow  button, starting from the
     * beginning.
     *
     * @param parent Container that should be layed out.
     */
    public void layoutContainer(Container parent)
    {
      // Position editor component to the left of arrow button if combo box is 
      // editable
      Insets i = getInsets();
      int arrowSize = comboBox.getHeight() - (i.top + i.bottom);
      int editorWidth = comboBox.getBounds().width - arrowSize;

      if (arrowButton != null)
        arrowButton.setBounds(comboBox.getWidth() - (i.right + arrowSize),
                              i.top, arrowSize, arrowSize);
      if (editor != null)
        editor.setBounds(rectangleForCurrentValue());
    }
  }

  /**
   * Handles focus changes occuring in the combo box. This class is
   * responsible for repainting combo box whenever focus is gained or lost
   * and also for hiding popup list of items whenever combo box loses its
   * focus.
   */
  public class FocusHandler extends Object implements FocusListener
  {
    /**
     * Creates a new FocusHandler object.
     */
    public FocusHandler()
    {
      // Nothing to do here.
    }

    /**
     * Invoked when combo box gains focus. It repaints main
     * part of combo box accordingly.
     *
     * @param e the FocusEvent
     */
    public void focusGained(FocusEvent e)
    {
      hasFocus = true;
      comboBox.repaint();
    }

    /**
     * Invoked when the combo box loses focus.  It repaints the main part
     * of the combo box accordingly and hides the popup list of items.
     *
     * @param e the FocusEvent
     */
    public void focusLost(FocusEvent e)
    {
      hasFocus = false;
      if (! e.isTemporary() && comboBox.isLightWeightPopupEnabled())
        setPopupVisible(comboBox, false);
      comboBox.repaint();
    }
  }

  /**
   * Handles {@link ItemEvent}s fired by the {@link JComboBox} when its 
   * selected item changes.
   */
  public class ItemHandler extends Object implements ItemListener
  {
    /**
     * Creates a new ItemHandler object.
     */
    public ItemHandler()
    {
      // Nothing to do here.
    }

    /**
     * Invoked when selected item becomes deselected or when
     * new item becomes selected.
     *
     * @param e the ItemEvent representing item's state change.
     */
    public void itemStateChanged(ItemEvent e)
    {
      ComboBoxModel model = comboBox.getModel();
      Object v = model.getSelectedItem();
      if (editor != null)
        comboBox.configureEditor(comboBox.getEditor(), v);
      comboBox.repaint();
    }
  }

  /**
   * KeyHandler handles key events occuring while JComboBox has focus.
   */
  public class KeyHandler extends KeyAdapter
  {
    public KeyHandler()
    {
      // Nothing to do here.
    }

    /**
     * Invoked whenever key is pressed while JComboBox is in focus.
     */
    public void keyPressed(KeyEvent e)
      throws NotImplementedException
    {
      // FIXME: This method calls JComboBox.selectWithKeyChar if the key that 
      // was pressed is not a navigation key. 
    }
  }

  /**
   * Handles the changes occurring in the JComboBox's data model.
   */
  public class ListDataHandler extends Object implements ListDataListener
  {
    /**
     * Creates a new ListDataHandler object.
     */
    public ListDataHandler()
    {
      // Nothing to do here.
    }

    /**
     * Invoked if the content's of JComboBox's data model are changed.
     *
     * @param e ListDataEvent describing the change.
     */
    public void contentsChanged(ListDataEvent e)
    {
      if (e.getIndex0() != -1 || e.getIndex1() != -1)
        {
          isMinimumSizeDirty = true;
          comboBox.revalidate();
        }
      if (editor != null)
        comboBox.configureEditor(comboBox.getEditor(),
            comboBox.getSelectedItem());
      comboBox.repaint();
    }

    /**
     * Invoked when items are added to the JComboBox's data model.
     *
     * @param e ListDataEvent describing the change.
     */
    public void intervalAdded(ListDataEvent e)
    {
      int start = e.getIndex0();
      int end = e.getIndex1();
      if (start == 0 && comboBox.getItemCount() - (end - start + 1) == 0)
        contentsChanged(e);
      else if (start != -1  || end != -1)
        {
          ListCellRenderer renderer = comboBox.getRenderer();
          ComboBoxModel model = comboBox.getModel();
          int w = displaySize.width;
          int h = displaySize.height;
          // TODO: Optimize using prototype here.
          for (int i = start; i <= end; ++i)
            {
              Component comp = renderer.getListCellRendererComponent(listBox,
                  model.getElementAt(i), -1, false, false);
              currentValuePane.add(comp);
              comp.setFont(comboBox.getFont());
              Dimension dim = comp.getPreferredSize();
              w = Math.max(w, dim.width);
              h = Math.max(h, dim.height);
              currentValuePane.remove(comp);
            }
          if (displaySize.width < w || displaySize.height < h)
            {
              if (displaySize.width < w)
                displaySize.width = w;
              if (displaySize.height < h)
                displaySize.height = h;
              comboBox.revalidate();
              if (editor != null)
                {
                  comboBox.configureEditor(comboBox.getEditor(),
                                           comboBox.getSelectedItem());
                }
            }
        }
      
    }

    /**
     * Invoked when items are removed from the JComboBox's
     * data model.
     *
     * @param e ListDataEvent describing the change.
     */
    public void intervalRemoved(ListDataEvent e)
    {
      contentsChanged(e);
    }
  }

  /**
   * Handles {@link PropertyChangeEvent}s fired by the {@link JComboBox}.
   */
  public class PropertyChangeHandler extends Object
    implements PropertyChangeListener
  {
    /**
     * Creates a new instance.
     */
    public PropertyChangeHandler()
    {
      // Nothing to do here.
    }

    /**
     * Invoked whenever bound property of JComboBox changes.
     * 
     * @param e  the event.
     */
    public void propertyChange(PropertyChangeEvent e)
    {
      // Lets assume every change invalidates the minimumsize.
      isMinimumSizeDirty = true;

      if (e.getPropertyName().equals("enabled"))
        {
          arrowButton.setEnabled(comboBox.isEnabled());

          if (comboBox.isEditable())
            comboBox.getEditor().getEditorComponent().setEnabled(
                comboBox.isEnabled());
        }
      else if (e.getPropertyName().equals("editable"))
        {
          if (comboBox.isEditable())
            {
              configureEditor();
              addEditor();
            }
          else
            {
              unconfigureEditor();
              removeEditor();
            }

          comboBox.revalidate();
          comboBox.repaint();
        }
      else if (e.getPropertyName().equals("dataModel"))
        {
          // remove ListDataListener from old model and add it to new model
          ComboBoxModel oldModel = (ComboBoxModel) e.getOldValue();
          if (oldModel != null)
            oldModel.removeListDataListener(listDataListener);

          if ((ComboBoxModel) e.getNewValue() != null)
            comboBox.getModel().addListDataListener(listDataListener);
        }
      else if (e.getPropertyName().equals("font"))
        {
          Font font = (Font) e.getNewValue();
          editor.setFont(font);
          listBox.setFont(font);
          arrowButton.setFont(font);
          comboBox.revalidate();
          comboBox.repaint();
        }

      // FIXME: Need to handle changes in other bound properties.       
    }
  }

}