001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017:
018: /**
019: * @author Anton Avtamonov
020: * @version $Revision$
021: */package javax.swing.plaf.basic;
022:
023: import java.awt.Color;
024: import java.awt.Component;
025: import java.awt.ComponentOrientation;
026: import java.awt.Container;
027: import java.awt.Dimension;
028: import java.awt.Font;
029: import java.awt.Graphics;
030: import java.awt.Insets;
031: import java.awt.LayoutManager;
032: import java.awt.Rectangle;
033: import java.awt.event.ActionEvent;
034: import java.awt.event.ActionListener;
035: import java.awt.event.FocusEvent;
036: import java.awt.event.FocusListener;
037: import java.awt.event.ItemEvent;
038: import java.awt.event.ItemListener;
039: import java.awt.event.KeyAdapter;
040: import java.awt.event.KeyEvent;
041: import java.awt.event.KeyListener;
042: import java.awt.event.MouseListener;
043: import java.awt.event.MouseMotionListener;
044: import java.beans.PropertyChangeEvent;
045: import java.beans.PropertyChangeListener;
046:
047: import javax.accessibility.Accessible;
048: import javax.swing.CellRendererPane;
049: import javax.swing.ComboBoxEditor;
050: import javax.swing.ComboBoxModel;
051: import javax.swing.JButton;
052: import javax.swing.JComboBox;
053: import javax.swing.JComponent;
054: import javax.swing.JList;
055: import javax.swing.ListCellRenderer;
056: import javax.swing.LookAndFeel;
057: import javax.swing.Timer;
058: import javax.swing.UIManager;
059: import javax.swing.event.ListDataEvent;
060: import javax.swing.event.ListDataListener;
061: import javax.swing.plaf.ComboBoxUI;
062: import javax.swing.plaf.ComponentUI;
063: import javax.swing.plaf.UIResource;
064:
065: import org.apache.harmony.x.swing.ExtendedListElement;
066: import org.apache.harmony.x.swing.StringConstants;
067: import org.apache.harmony.x.swing.Utilities;
068:
069: import org.apache.harmony.x.swing.internal.nls.Messages;
070:
071: import org.apache.harmony.x.swing.internal.nls.Messages;
072:
073: public class BasicComboBoxUI extends ComboBoxUI {
074:
075: public class ComboBoxLayoutManager implements LayoutManager {
076: public void addLayoutComponent(final String name,
077: final Component component) {
078: }
079:
080: public void removeLayoutComponent(final Component component) {
081:
082: }
083:
084: public Dimension preferredLayoutSize(final Container parent) {
085: if (parent == null) {
086: throw new NullPointerException(Messages.getString(
087: "swing.03", "Parent")); //$NON-NLS-1$ //$NON-NLS-2$
088: }
089: return new Dimension(0, 0);
090: }
091:
092: public Dimension minimumLayoutSize(final Container parent) {
093: if (parent == null) {
094: throw new NullPointerException(Messages.getString(
095: "swing.03", "Parent")); //$NON-NLS-1$ //$NON-NLS-2$
096: }
097: return new Dimension(0, 0);
098: }
099:
100: public void layoutContainer(final Container parent) {
101: Rectangle bounds = comboBox.getBounds();
102: Insets insets = getInsets();
103: int arrowButtonSize = bounds.height - insets.top
104: - insets.bottom;
105: if (comboBox.getComponentOrientation().isLeftToRight()) {
106: arrowButton.setBounds(bounds.width - arrowButtonSize
107: - insets.right, insets.top, arrowButtonSize,
108: arrowButtonSize);
109: cachedTextPartBounds.setBounds(insets.left, insets.top,
110: bounds.width - insets.left - insets.right
111: - arrowButtonSize, arrowButtonSize);
112: if (comboBox.isEditable()) {
113: editor.setBounds(cachedTextPartBounds);
114: }
115: } else {
116: arrowButton.setBounds(insets.left, insets.top,
117: arrowButtonSize, arrowButtonSize);
118: cachedTextPartBounds.setBounds(insets.left
119: + arrowButtonSize, insets.top, bounds.width
120: - insets.left - insets.right - arrowButtonSize,
121: arrowButtonSize);
122: if (comboBox.isEditable()) {
123: editor.setBounds(cachedTextPartBounds);
124: }
125: }
126: }
127: }
128:
129: public class FocusHandler implements FocusListener {
130: public void focusGained(final FocusEvent e) {
131: hasFocus = true;
132: comboBox.repaint();
133: }
134:
135: public void focusLost(final FocusEvent e) {
136: hasFocus = false;
137: comboBox.repaint();
138: }
139: }
140:
141: public class ItemHandler implements ItemListener {
142: public void itemStateChanged(final ItemEvent e) {
143:
144: }
145: }
146:
147: public class KeyHandler extends KeyAdapter {
148: public void keyPressed(final KeyEvent e) {
149: if (!isNavigationKey(e.getKeyCode())) {
150: comboBox.selectWithKeyChar(e.getKeyChar());
151: }
152: }
153: }
154:
155: public class ListDataHandler implements ListDataListener {
156: public void contentsChanged(final ListDataEvent e) {
157: if (comboBox.isEditable()) {
158: comboBox.configureEditor(comboBox.getEditor(), comboBox
159: .getSelectedItem());
160: }
161: listChanged();
162: }
163:
164: public void intervalAdded(final ListDataEvent e) {
165: listChanged();
166: }
167:
168: public void intervalRemoved(final ListDataEvent e) {
169: listChanged();
170: }
171:
172: private void listChanged() {
173: isMinimumSizeDirty = true;
174: cachedDisplaySize = null;
175: comboBox.revalidate();
176: comboBox.repaint();
177: }
178: }
179:
180: public class PropertyChangeHandler implements
181: PropertyChangeListener {
182: public void propertyChange(final PropertyChangeEvent event) {
183: if (StringConstants.ENABLED_PROPERTY_CHANGED.equals(event
184: .getPropertyName())) {
185: if (arrowButton != null) {
186: arrowButton.setEnabled(((Boolean) event
187: .getNewValue()).booleanValue());
188: }
189: if (comboBox.isEditable() && (editor != null)) {
190: editor.setEnabled(((Boolean) event.getNewValue())
191: .booleanValue());
192: }
193: } else if (StringConstants.TOOLTIP_PROPERTY_CHANGED
194: .equals(event.getPropertyName())) {
195: if (arrowButton != null) {
196: arrowButton.setToolTipText((String) event
197: .getNewValue());
198: }
199: if (comboBox.isEditable()
200: && (editor instanceof JComponent)) {
201: ((JComponent) editor).setToolTipText((String) event
202: .getNewValue());
203: }
204: } else if (StringConstants.FONT_PROPERTY_CHANGED
205: .equals(event.getPropertyName())) {
206: if (arrowButton != null) {
207: arrowButton.setFont((Font) event.getNewValue());
208: }
209: if (comboBox.isEditable() && (editor != null)) {
210: editor.setFont((Font) event.getNewValue());
211: }
212: } else if (StringConstants.EDITABLE_PROPERTY_CHANGED
213: .equals(event.getPropertyName())) {
214: if (((Boolean) event.getNewValue()).booleanValue()) {
215: addEditor();
216: } else {
217: removeEditor();
218: }
219: } else if (StringConstants.MODEL_PROPERTY_CHANGED
220: .equals(event.getPropertyName())) {
221: ComboBoxModel newModel = (ComboBoxModel) event
222: .getNewValue();
223: listBox.setModel(newModel);
224: comboBox.configureEditor(comboBox.getEditor(), newModel
225: .getSelectedItem());
226:
227: ComboBoxModel oldModel = (ComboBoxModel) event
228: .getOldValue();
229: oldModel.removeListDataListener(listDataListener);
230: newModel.addListDataListener(listDataListener);
231: } else if (StringConstants.COMPONENT_ORIENTATION
232: .equals(event.getPropertyName())) {
233: listBox
234: .setComponentOrientation((ComponentOrientation) event
235: .getNewValue());
236: } else if (StringConstants.EXTENDED_SUPPORT_ENABLED_PROPERTY
237: .equals(event.getPropertyName())) {
238: listBox
239: .putClientProperty(
240: StringConstants.EXTENDED_SUPPORT_ENABLED_PROPERTY,
241: event.getNewValue());
242: if (((Boolean) event.getNewValue()).booleanValue()) {
243: for (int i = 0; i < comboBox.getModel().getSize(); i++) {
244: Object element = comboBox.getModel()
245: .getElementAt(i);
246: if (!(element instanceof ExtendedListElement)
247: || ((ExtendedListElement) element)
248: .isChoosable()) {
249:
250: comboBox.setSelectedIndex(i);
251: break;
252: }
253: }
254: }
255: } else if (StringConstants.IS_TABLE_EDITOR.equals(event
256: .getPropertyName())) {
257: isTableEditor = ((Boolean) event.getNewValue())
258: .booleanValue();
259: } else if (StringConstants.EDITOR_PROPERTY_CHANGED
260: .equals(event.getPropertyName())) {
261: removeEditor();
262: addEditor();
263: }
264:
265: isMinimumSizeDirty = true;
266: cachedDisplaySize = null;
267: comboBox.revalidate();
268: comboBox.repaint();
269: }
270: }
271:
272: private class EditorFocusHandler extends FocusHandler {
273: public void focusLost(final FocusEvent e) {
274: super .focusLost(e);
275: ActionEvent actionEvent = new ActionEvent(e.getSource(),
276: ActionEvent.ACTION_PERFORMED, null);
277: editorActionListener.actionPerformed(actionEvent);
278: comboBox.actionPerformed(actionEvent);
279: }
280: }
281:
282: private class DefaultKeySelectionManager implements
283: JComboBox.KeySelectionManager {
284: private final StringBuffer keySequence = new StringBuffer();
285: private final Timer timer = new Timer(1000,
286: new ActionListener() {
287: public void actionPerformed(final ActionEvent e) {
288: clearKeySequence();
289: timer.stop();
290: }
291: });
292:
293: public int selectionForKey(final char keyChar,
294: final ComboBoxModel model) {
295: keySequence.append(keyChar);
296: timer.start();
297: int result = findNextOccurence(model);
298: if (result != -1) {
299: return result;
300: } else {
301: clearKeySequence();
302: keySequence.append(keyChar);
303: return findNextOccurence(model);
304: }
305: }
306:
307: private void clearKeySequence() {
308: keySequence.delete(0, keySequence.length());
309: }
310:
311: private int findNextOccurence(final ComboBoxModel model) {
312: String beginPart = keySequence.toString().toUpperCase();
313: int selectedIndex = getIndex(model.getSelectedItem(), model);
314: for (int i = selectedIndex + 1; i < model.getSize(); i++) {
315: Object item = model.getElementAt(i);
316: if (item.toString().toUpperCase().startsWith(beginPart)
317: && isChoosable(item)) {
318:
319: return i;
320: }
321: }
322:
323: for (int i = 0; i <= selectedIndex; i++) {
324: Object item = model.getElementAt(i);
325: if (item.toString().toUpperCase().startsWith(beginPart)
326: && isChoosable(item)) {
327:
328: return i;
329: }
330: }
331:
332: return -1;
333: }
334:
335: private int getIndex(final Object element,
336: final ComboBoxModel model) {
337: if (element == null) {
338: return -1;
339: }
340:
341: for (int i = 0; i < model.getSize(); i++) {
342: if (element.equals(model.getElementAt(i))) {
343: return i;
344: }
345: }
346:
347: return -1;
348: }
349:
350: private boolean isChoosable(final Object element) {
351: return !((BasicListUI) listBox.getUI()).extendedSupportEnabled
352: || !(element instanceof ExtendedListElement)
353: || ((ExtendedListElement) element).isChoosable();
354: }
355:
356: }
357:
358: protected JButton arrowButton;
359: protected JList listBox;
360: protected JComboBox comboBox;
361: protected ComboPopup popup;
362: protected CellRendererPane currentValuePane;
363: protected Component editor;
364: protected boolean hasFocus;
365: protected Dimension cachedMinimumSize = new Dimension();
366: protected boolean isMinimumSizeDirty = true;
367: protected FocusListener focusListener;
368: protected ItemListener itemListener;
369: protected KeyListener keyListener;
370: protected ListDataListener listDataListener;
371: protected KeyListener popupKeyListener;
372: protected MouseListener popupMouseListener;
373: protected MouseMotionListener popupMouseMotionListener;
374: protected PropertyChangeListener propertyChangeListener;
375:
376: boolean isTableEditor;
377:
378: private Dimension cachedDisplaySize;
379: private Rectangle cachedTextPartBounds = new Rectangle();
380: private Insets cachedInsets = new Insets(0, 0, 0, 0);
381:
382: private FocusListener editorFocusListener;
383: private ActionListener editorActionListener;
384: private Object selectedValue;
385:
386: private static final String PROTOTYPE_VALUE_FOR_EDITABLE_COMBOBOX = "wwwwwwwwww";
387:
388: public static ComponentUI createUI(final JComponent comboBox) {
389: return new BasicComboBoxUI();
390: }
391:
392: public BasicComboBoxUI() {
393: currentValuePane = new CellRendererPane();
394: currentValuePane.setVisible(false);
395: }
396:
397: public void installUI(final JComponent c) {
398: comboBox = (JComboBox) c;
399: comboBox.setLayout(createLayoutManager());
400:
401: if (comboBox.getRenderer() == null
402: || comboBox.getRenderer() instanceof UIResource) {
403: comboBox.setRenderer(createRenderer());
404: }
405:
406: if (comboBox.getEditor() == null
407: || comboBox.getEditor() instanceof UIResource) {
408: comboBox.setEditor(createEditor());
409: }
410:
411: popup = createPopup();
412: listBox = popup.getList();
413:
414: installListeners();
415: installComponents();
416: installKeyboardActions();
417: installDefaults();
418: }
419:
420: public void uninstallUI(final JComponent c) {
421: uninstallDefaults();
422: uninstallKeyboardActions();
423: uninstallComponents();
424: uninstallListeners();
425:
426: popup.uninstallingUI();
427:
428: if (comboBox.getRenderer() instanceof UIResource) {
429: comboBox.setRenderer(null);
430: }
431: if (comboBox.getEditor() instanceof UIResource) {
432: comboBox.setEditor(null);
433: }
434:
435: comboBox.remove(currentValuePane);
436:
437: comboBox.setLayout(null);
438:
439: comboBox = null;
440: listBox = null;
441: popup = null;
442: arrowButton = null;
443: }
444:
445: public void addEditor() {
446: ComboBoxEditor cbe = comboBox.getEditor();
447: if (cbe == null)
448: return;
449:
450: editor = cbe.getEditorComponent();
451: if (editor == null)
452: return;
453:
454: configureEditor();
455: comboBox.add(editor);
456: }
457:
458: public void removeEditor() {
459: if (editor != null) {
460: comboBox.remove(editor);
461: unconfigureEditor();
462: editor = null;
463: }
464: }
465:
466: public boolean isPopupVisible(final JComboBox c) {
467: return popup.isVisible();
468: }
469:
470: public void setPopupVisible(final JComboBox c,
471: final boolean isVisible) {
472: if (isVisible == isPopupVisible(c)) {
473: return;
474: }
475: if (isVisible) {
476: popup.show();
477: selectedValue = popup.getList().getSelectedValue();
478: } else {
479: popup.hide();
480: }
481: }
482:
483: public boolean isFocusTraversable(final JComboBox c) {
484: if (comboBox.isEditable()) {
485: return false;
486: } else {
487: return true;
488: }
489: }
490:
491: public void paint(final Graphics g, final JComponent c) {
492: paintCurrentValueBackground(g, cachedTextPartBounds, hasFocus);
493: paintCurrentValue(g, cachedTextPartBounds, hasFocus);
494: }
495:
496: public void paintCurrentValue(final Graphics g,
497: final Rectangle bounds, final boolean hasFocus) {
498: Component renderer = comboBox.getRenderer()
499: .getListCellRendererComponent(listBox,
500: comboBox.getSelectedItem(), -1, hasFocus,
501: hasFocus);
502: if (!comboBox.isEnabled()) {
503: renderer.setForeground(UIManager
504: .getColor("ComboBox.disabledForeground"));
505: renderer.setBackground(UIManager
506: .getColor("ComboBox.disabledBackground"));
507: } else if (hasFocus) {
508: renderer.setForeground(UIManager
509: .getColor("ComboBox.selectionForeground"));
510: renderer.setBackground(UIManager
511: .getColor("ComboBox.selectionBackground"));
512: }
513:
514: currentValuePane.paintComponent(g, renderer, comboBox,
515: bounds.x, bounds.y, bounds.width, bounds.height);
516: }
517:
518: public void paintCurrentValueBackground(final Graphics g,
519: final Rectangle bounds, final boolean hasFocus) {
520: Color oldColor = g.getColor();
521: if (comboBox == null) {
522: throw new NullPointerException(Messages.getString(
523: "swing.03", "ComboBox")); //$NON-NLS-1$ //$NON-NLS-2$
524: }
525: if (hasFocus) {
526: g.setColor(UIManager
527: .getColor("ComboBox.selectionBackground"));
528: } else {
529: if (comboBox.isEnabled()) {
530: g.setColor(comboBox.getBackground());
531: } else {
532: g.setColor(UIManager
533: .getColor("ComboBox.disabledBackground"));
534: }
535: }
536: g.fillRect(bounds.x, bounds.y, bounds.width, bounds.height);
537:
538: g.setColor(oldColor);
539: }
540:
541: public Dimension getPreferredSize(final JComponent c) {
542: return getMinimumSize(c);
543: }
544:
545: public Dimension getMinimumSize(final JComponent c) {
546: Dimension result;
547: if (isMinimumSizeDirty) {
548: result = getDisplaySize();
549: result.width += result.height;
550: Utilities.addInsets(result, getInsets());
551:
552: cachedMinimumSize.setSize(result);
553: isMinimumSizeDirty = false;
554: } else {
555: result = new Dimension(cachedMinimumSize);
556: }
557: return result;
558: }
559:
560: public Dimension getMaximumSize(final JComponent c) {
561: return new Dimension(Integer.MAX_VALUE, Integer.MAX_VALUE);
562: }
563:
564: public void configureArrowButton() {
565: if (arrowButton == null) {
566: return;
567: }
568: arrowButton.addMouseListener(popup.getMouseListener());
569: arrowButton.addMouseMotionListener(popup
570: .getMouseMotionListener());
571: }
572:
573: public void unconfigureArrowButton() {
574: if (arrowButton == null) {
575: return;
576: }
577: arrowButton.removeMouseListener(popup.getMouseListener());
578: arrowButton.removeMouseMotionListener(popup
579: .getMouseMotionListener());
580: }
581:
582: public int getAccessibleChildrenCount(final JComponent c) {
583: if (comboBox == null) {
584: throw new NullPointerException(Messages.getString(
585: "swing.03", "ComboBox")); //$NON-NLS-1$ //$NON-NLS-2$
586: }
587: return 0;
588: }
589:
590: public Accessible getAccessibleChild(final JComponent c, final int i) {
591: return null;
592: }
593:
594: protected void installDefaults() {
595: LookAndFeel.installColorsAndFont(comboBox,
596: "ComboBox.background", "ComboBox.foreground",
597: "ComboBox.font");
598: }
599:
600: protected void uninstallDefaults() {
601: Utilities.uninstallColorsAndFont(comboBox);
602: }
603:
604: protected void installListeners() {
605: focusListener = createFocusListener();
606: comboBox.addFocusListener(focusListener);
607:
608: keyListener = createKeyListener();
609: comboBox.addKeyListener(keyListener);
610:
611: listDataListener = createListDataListener();
612: comboBox.getModel().addListDataListener(listDataListener);
613:
614: propertyChangeListener = createPropertyChangeListener();
615: comboBox.addPropertyChangeListener(propertyChangeListener);
616:
617: popupMouseListener = popup.getMouseListener();
618: comboBox.addMouseListener(popupMouseListener);
619:
620: popupMouseMotionListener = popup.getMouseMotionListener();
621: comboBox.addMouseMotionListener(popupMouseMotionListener);
622:
623: popupKeyListener = popup.getKeyListener();
624: if (popupKeyListener != null) {
625: comboBox.addKeyListener(popupKeyListener);
626: }
627: }
628:
629: protected void uninstallListeners() {
630: comboBox.removeFocusListener(focusListener);
631: focusListener = null;
632:
633: comboBox.removeKeyListener(keyListener);
634: keyListener = null;
635:
636: popup.getList().getModel().removeListDataListener(
637: listDataListener);
638: listDataListener = null;
639:
640: comboBox.removePropertyChangeListener(propertyChangeListener);
641: propertyChangeListener = null;
642:
643: comboBox.removeMouseListener(popupMouseListener);
644: popupMouseListener = null;
645:
646: comboBox.removeMouseMotionListener(popupMouseMotionListener);
647: popupMouseMotionListener = null;
648:
649: if (popupKeyListener != null) {
650: comboBox.removeKeyListener(popupKeyListener);
651: popupKeyListener = null;
652: }
653: }
654:
655: protected void installKeyboardActions() {
656: comboBox
657: .setKeySelectionManager(new DefaultKeySelectionManager());
658: BasicComboBoxKeyboardActions.installKeyboardActions(comboBox);
659: }
660:
661: protected void uninstallKeyboardActions() {
662: comboBox.setKeySelectionManager(null);
663: BasicComboBoxKeyboardActions.uninstallKeyboardActions(comboBox);
664: }
665:
666: protected void installComponents() {
667: arrowButton = createArrowButton();
668: configureArrowButton();
669: comboBox.add(arrowButton);
670:
671: comboBox.add(currentValuePane);
672:
673: if (comboBox.isEditable()) {
674: addEditor();
675: }
676: }
677:
678: protected void uninstallComponents() {
679: unconfigureArrowButton();
680: if (comboBox.isEditable()) {
681: removeEditor();
682: }
683: comboBox.removeAll();
684: arrowButton = null;
685: }
686:
687: protected Dimension getDefaultSize() {
688: return comboBox.getRenderer().getListCellRendererComponent(
689: listBox, null, 0, false, false).getPreferredSize();
690: }
691:
692: protected Dimension getDisplaySize() {
693: if (cachedDisplaySize != null) {
694: return cachedDisplaySize;
695: }
696:
697: Dimension result = null;
698: if (comboBox.getPrototypeDisplayValue() != null) {
699: result = comboBox.getRenderer()
700: .getListCellRendererComponent(listBox,
701: comboBox.getPrototypeDisplayValue(), -1,
702: false, false).getPreferredSize();
703: } else {
704: if (comboBox.getItemCount() > 0) {
705: for (int i = 0; i < comboBox.getItemCount(); i++) {
706: Dimension itemPart = comboBox.getRenderer()
707: .getListCellRendererComponent(listBox,
708: comboBox.getItemAt(i), i, false,
709: false).getPreferredSize();
710: if (result == null) {
711: result = itemPart;
712: } else {
713: if (result.height < itemPart.height) {
714: result.height = itemPart.height;
715: }
716: if (result.width < itemPart.width) {
717: result.width = itemPart.width;
718: }
719: }
720: }
721: } else {
722: result = getDefaultSize();
723: }
724: if (comboBox.isEditable()) {
725: Dimension minEditableSize = result = comboBox
726: .getRenderer().getListCellRendererComponent(
727: listBox,
728: PROTOTYPE_VALUE_FOR_EDITABLE_COMBOBOX,
729: 0, false, false).getPreferredSize();
730: if (result.width < minEditableSize.width) {
731: result = minEditableSize;
732: }
733: }
734: }
735:
736: cachedDisplaySize = result;
737:
738: return result;
739: }
740:
741: protected boolean isNavigationKey(final int keyCode) {
742: return keyCode == KeyEvent.VK_UP || keyCode == KeyEvent.VK_DOWN;
743: }
744:
745: protected void selectNextPossibleValue() {
746: int selectedIndex = comboBox.getSelectedIndex();
747: if (!isTableEditor && selectedIndex != -1
748: && selectedIndex + 1 < comboBox.getItemCount()) {
749: comboBox.setSelectedIndex(selectedIndex + 1);
750: }
751: }
752:
753: protected void selectPreviousPossibleValue() {
754: int selectedIndex = comboBox.getSelectedIndex();
755: if (!isTableEditor && selectedIndex != -1 && selectedIndex > 0) {
756: comboBox.setSelectedIndex(selectedIndex - 1);
757: }
758: }
759:
760: protected void toggleOpenClose() {
761: setPopupVisible(comboBox, !isPopupVisible(comboBox));
762: }
763:
764: protected Rectangle rectangleForCurrentValue() {
765: return new Rectangle(0, 0, 0, 0);
766: }
767:
768: protected Insets getInsets() {
769: return comboBox.getInsets(cachedInsets);
770: }
771:
772: protected void configureEditor() {
773: editorFocusListener = new EditorFocusHandler();
774: editor.addFocusListener(editorFocusListener);
775: editor.setFont(comboBox.getFont());
776: comboBox.getEditor().setItem(comboBox.getSelectedItem());
777:
778: editorActionListener = new ActionListener() {
779: public void actionPerformed(ActionEvent e) {
780: if (selectedValue != null
781: && !selectedValue.equals(popup.getList()
782: .getSelectedValue())) {
783: comboBox.getEditor().setItem(
784: popup.getList().getSelectedValue());
785: }
786: }
787: };
788: comboBox.getEditor().addActionListener(editorActionListener);
789: }
790:
791: protected void unconfigureEditor() {
792: editor.removeFocusListener(editorFocusListener);
793: comboBox.getEditor().removeActionListener(editorActionListener);
794: editorFocusListener = null;
795: }
796:
797: protected JButton createArrowButton() {
798: return new BasicArrowButton(BasicArrowButton.SOUTH, UIManager
799: .getColor("ComboBox.buttonBackground"), UIManager
800: .getColor("ComboBox.buttonShadow"), UIManager
801: .getColor("ComboBox.buttonDarkShadow"), UIManager
802: .getColor("ComboBox.buttonHighlight"));
803: }
804:
805: protected ComboPopup createPopup() {
806: return new BasicComboPopup(comboBox);
807: }
808:
809: protected KeyListener createKeyListener() {
810: return new KeyHandler();
811: }
812:
813: protected FocusListener createFocusListener() {
814: return new FocusHandler();
815: }
816:
817: protected ListDataListener createListDataListener() {
818: return new ListDataHandler();
819: }
820:
821: protected ItemListener createItemListener() {
822: return null;
823: }
824:
825: protected PropertyChangeListener createPropertyChangeListener() {
826: return new PropertyChangeHandler();
827: }
828:
829: protected LayoutManager createLayoutManager() {
830: return new ComboBoxLayoutManager();
831: }
832:
833: protected ListCellRenderer createRenderer() {
834: return new BasicComboBoxRenderer.UIResource();
835: }
836:
837: protected ComboBoxEditor createEditor() {
838: return new BasicComboBoxEditor.UIResource();
839: }
840: }
|