KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > jgoodies > looks > windows > WindowsComboBoxUI


1 /*
2  * Copyright (c) 2001-2005 JGoodies Karsten Lentzsch. All Rights Reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions are met:
6  *
7  * o Redistributions of source code must retain the above copyright notice,
8  * this list of conditions and the following disclaimer.
9  *
10  * o Redistributions in binary form must reproduce the above copyright notice,
11  * this list of conditions and the following disclaimer in the documentation
12  * and/or other materials provided with the distribution.
13  *
14  * o Neither the name of JGoodies Karsten Lentzsch nor the names of
15  * its contributors may be used to endorse or promote products derived
16  * from this software without specific prior written permission.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
20  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
21  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
22  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
23  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
24  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
25  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
26  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
27  * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
28  * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29  */

30
31 package com.jgoodies.looks.windows;
32
33 import java.awt.*;
34
35 import javax.swing.*;
36 import javax.swing.plaf.ComponentUI JavaDoc;
37 import javax.swing.plaf.basic.BasicComboBoxUI JavaDoc;
38
39 import com.jgoodies.looks.LookUtils;
40
41 /**
42  * The JGoodies Windows Look&Feel implementation of
43  * {@link javax.swing.plaf.ComboBoxUI}.<p>
44  *
45  * Corrects the editor insets for editable combo boxes as well as
46  * the render insets for non-editable combos.
47  * Also, it has the same height as text fields - unless you change the renderer.
48  *
49  * @author Karsten Lentzsch
50  * @version $Revision: 1.5 $
51  */

52
53 public final class WindowsComboBoxUI extends com.sun.java.swing.plaf.windows.WindowsComboBoxUI {
54     
55     /*
56      * Used to determine the minimum height of a text field,
57      * which in turn is used to answer the combobox's minimum height.
58      */

59     private static final JTextField phantom = new JTextField("Phantom");
60     
61
62     public static ComponentUI JavaDoc createUI(JComponent b) {
63         return new WindowsComboBoxUI();
64     }
65     
66     /**
67      * The minumum size is the size of the display area plus insets plus the button.
68      */

69     public Dimension getMinimumSize(JComponent c) {
70         Dimension size = super.getMinimumSize(c);
71         Dimension textFieldSize = phantom.getMinimumSize();
72         return new Dimension(size.width, Math.max(textFieldSize.height, size.height));
73     }
74
75
76     /**
77      * Creates the editor that is to be used in editable combo boxes.
78      * This method only gets called if a custom editor has not already
79      * been installed in the JComboBox.
80      */

81     protected ComboBoxEditor createEditor() {
82         return new com.jgoodies.looks.windows.WindowsComboBoxEditor.UIResource();
83     }
84
85
86     /**
87      * Creates a layout manager for managing the components which
88      * make up the combo box.<p>
89      *
90      * Overriden to use a layout that has a fixed width arrow button.
91      *
92      * @return an instance of a layout manager
93      */

94     protected LayoutManager createLayoutManager() {
95         return new WindowsComboBoxLayoutManager();
96     }
97
98
99     /**
100      * Creates the arrow button that is to be used in the combo box.<p>
101      *
102      * Overridden to paint black triangles.
103      */

104     protected JButton createArrowButton() {
105         return LookUtils.IS_LAF_WINDOWS_XP_ENABLED
106                     ? super.createArrowButton()
107                     : new WindowsArrowButton(SwingConstants.SOUTH);
108     }
109
110     
111     /**
112      * Returns the area that is reserved for drawing the currently selected item.
113      */

114     protected Rectangle rectangleForCurrentValue() {
115         if (comboBox.isEditable() || !comboBox.isEnabled())
116             return super.rectangleForCurrentValue();
117         
118         int width = comboBox.getWidth();
119         int height = comboBox.getHeight();
120         Insets insets = getInsets();
121         Insets rendererMargin = UIManager.getInsets("ComboBox.rendererMargin");
122         int buttonSize = height - (insets.top + insets.bottom);
123         //System.out.println("height=" + height + "; insets=" + insets + "; rendererMargin=" + rendererMargin);
124
if (arrowButton != null) {
125             buttonSize = arrowButton.getWidth();
126         }
127         if (comboBox.getComponentOrientation().isLeftToRight()) {
128             return new Rectangle(
129                     insets.left + rendererMargin.left,
130                     insets.top + rendererMargin.top,
131                     width - (insets.left + rendererMargin.left + insets.right
132                                           + rendererMargin.right + buttonSize),
133                     height - (insets.top + rendererMargin.top + insets.bottom
134                                           + rendererMargin.bottom));
135         } else {
136             return new Rectangle(
137                     insets.left + rendererMargin.left + buttonSize,
138                     insets.top + rendererMargin.top,
139                     width - (insets.left + rendererMargin.left + insets.right
140                                           + rendererMargin.right + buttonSize),
141                     height - (insets.top + rendererMargin.top + insets.bottom
142                                           + rendererMargin.bottom));
143         }
144     }
145
146
147
148     /**
149      * This layout manager handles the 'standard' layout of combo boxes.
150      * It puts the arrow button to the right and the editor to the left.
151      * If there is no editor it still keeps the arrow button to the right.
152      *
153      * Overriden to use a fixed arrow button width.
154      */

155     private class WindowsComboBoxLayoutManager extends BasicComboBoxUI.ComboBoxLayoutManager JavaDoc {
156         
157         public void layoutContainer(Container parent) {
158             JComboBox cb = (JComboBox) parent;
159             int width = cb.getWidth();
160             int height = cb.getHeight();
161
162             Insets insets = getInsets();
163             int buttonWidth = UIManager.getInt("ScrollBar.width");
164             int buttonHeight = height - (insets.top + insets.bottom);
165             //System.out.println("ButtonHeight=" + buttonHeight);
166

167             if (arrowButton != null) {
168                 if (cb.getComponentOrientation().isLeftToRight()) {
169                     arrowButton.setBounds(width - (insets.right + buttonWidth),
170                         insets.top, buttonWidth, buttonHeight);
171                 } else {
172                     arrowButton.setBounds(insets.left, insets.top, buttonWidth, buttonHeight);
173                 }
174             }
175             if (editor != null) {
176                 editor.setBounds(rectangleForCurrentValue());
177             }
178         }
179     
180    }
181     
182     
183 }
Popular Tags