KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > java > swing > plaf > motif > MotifComboBoxUI


1 /*
2  * @(#)MotifComboBoxUI.java 1.39 03/12/19
3  *
4  * Copyright 2004 Sun Microsystems, Inc. All rights reserved.
5  * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
6  */

7 package com.sun.java.swing.plaf.motif;
8
9 import java.awt.*;
10 import javax.swing.*;
11 import javax.swing.plaf.*;
12 import javax.swing.border.*;
13 import javax.swing.plaf.basic.*;
14 import java.io.Serializable JavaDoc;
15 import java.awt.event.*;
16
17 /**
18  * ComboBox motif look and feel
19  * <p> * <strong>Warning:</strong>
20  * Serialized objects of this class will not be compatible with
21  * future Swing releases. The current serialization support is appropriate
22  * for short term storage or RMI between applications running the same
23  * version of Swing. A future release of Swing will provide support for
24  * long term persistence.
25  *
26  * @version 1.39, 12/19/03
27  * @author Arnaud Weber
28  */

29 public class MotifComboBoxUI extends BasicComboBoxUI implements Serializable JavaDoc {
30     Icon arrowIcon;
31     static final int HORIZ_MARGIN = 3;
32
33     public static ComponentUI createUI(JComponent c) {
34         return new MotifComboBoxUI();
35     }
36
37     public void installUI(JComponent c) {
38         super.installUI(c);
39         arrowIcon = new MotifComboBoxArrowIcon(UIManager.getColor("controlHighlight"),
40                                                UIManager.getColor("controlShadow"),
41                                                UIManager.getColor("control"));
42
43         Runnable JavaDoc initCode = new Runnable JavaDoc() {
44             public void run(){
45                 if ( motifGetEditor() != null ) {
46                     motifGetEditor().setBackground( UIManager.getColor( "text" ) );
47                 }
48             }
49         };
50
51         SwingUtilities.invokeLater( initCode );
52     }
53
54     public Dimension getMinimumSize( JComponent c ) {
55         if ( !isMinimumSizeDirty ) {
56             return new Dimension( cachedMinimumSize );
57         }
58         Dimension size;
59         Insets insets = getInsets();
60         size = getDisplaySize();
61         size.height += insets.top + insets.bottom;
62         int buttonSize = iconAreaWidth();
63         size.width += insets.left + insets.right + buttonSize;
64
65         cachedMinimumSize.setSize( size.width, size.height );
66         isMinimumSizeDirty = false;
67
68         return size;
69     }
70
71     protected ComboPopup createPopup() {
72         return new MotifComboPopup( comboBox );
73     }
74
75     /**
76      * Overriden to empty the MouseMotionListener.
77      */

78     protected class MotifComboPopup extends BasicComboPopup {
79
80         public MotifComboPopup( JComboBox comboBox ) {
81             super( comboBox );
82         }
83
84         /**
85          * Motif combo popup should not track the mouse in the list.
86          */

87         public MouseMotionListener createListMouseMotionListener() {
88            return new MouseMotionAdapter() {};
89         }
90     
91         public KeyListener createKeyListener() {
92             return super.createKeyListener();
93         }
94
95         protected class InvocationKeyHandler extends BasicComboPopup.InvocationKeyHandler {
96         protected InvocationKeyHandler() {
97         MotifComboPopup.this.super();
98         }
99     }
100     }
101
102     protected void installComponents() {
103         if ( comboBox.isEditable() ) {
104             addEditor();
105         }
106
107         comboBox.add( currentValuePane );
108     }
109
110     protected void uninstallComponents() {
111         removeEditor();
112         comboBox.removeAll();
113     }
114
115     public void paint(Graphics g, JComponent c) {
116         boolean hasFocus = comboBox.hasFocus();
117         Rectangle r;
118
119     if (comboBox.isEnabled()) {
120         g.setColor(comboBox.getBackground());
121     } else {
122         g.setColor(UIManager.getColor("ComboBox.disabledBackground"));
123     }
124         g.fillRect(0,0,c.getWidth(),c.getHeight());
125
126         if ( !comboBox.isEditable() ) {
127             r = rectangleForCurrentValue();
128             paintCurrentValue(g,r,hasFocus);
129         }
130         r = rectangleForArrowIcon();
131         arrowIcon.paintIcon(c,g,r.x,r.y);
132         if ( !comboBox.isEditable() ) {
133             Border border = comboBox.getBorder();
134             Insets in;
135             if ( border != null ) {
136                 in = border.getBorderInsets(comboBox);
137             }
138             else {
139                 in = new Insets( 0, 0, 0, 0 );
140             }
141             // Draw the separation
142
if(MotifGraphicsUtils.isLeftToRight(comboBox)) {
143             r.x -= (HORIZ_MARGIN + 2);
144         }
145         else {
146             r.x += r.width + HORIZ_MARGIN + 1;
147         }
148             r.y = in.top;
149             r.width = 1;
150             r.height = comboBox.getBounds().height - in.bottom - in.top;
151             g.setColor(UIManager.getColor("controlShadow"));
152             g.fillRect(r.x,r.y,r.width,r.height);
153             r.x++;
154             g.setColor(UIManager.getColor("controlHighlight"));
155             g.fillRect(r.x,r.y,r.width,r.height);
156         }
157     }
158
159     public void paintCurrentValue(Graphics g,Rectangle bounds,boolean hasFocus) {
160         ListCellRenderer renderer = comboBox.getRenderer();
161         Component c;
162         Dimension d;
163         c = renderer.getListCellRendererComponent(listBox, comboBox.getSelectedItem(), -1, false, false);
164         c.setFont(comboBox.getFont());
165     if ( comboBox.isEnabled() ) {
166             c.setForeground(comboBox.getForeground());
167             c.setBackground(comboBox.getBackground());
168     }
169         else {
170             c.setForeground(UIManager.getColor("ComboBox.disabledForeground"));
171             c.setBackground(UIManager.getColor("ComboBox.disabledBackground"));
172     }
173         d = c.getPreferredSize();
174         currentValuePane.paintComponent(g,c,comboBox,bounds.x,bounds.y,
175                                         bounds.width,d.height);
176     }
177
178     protected Rectangle rectangleForArrowIcon() {
179         Rectangle b = comboBox.getBounds();
180         Border border = comboBox.getBorder();
181         Insets in;
182         if ( border != null ) {
183             in = border.getBorderInsets(comboBox);
184         }
185         else {
186             in = new Insets( 0, 0, 0, 0 );
187         }
188         b.x = in.left;
189         b.y = in.top;
190         b.width -= (in.left + in.right);
191         b.height -= (in.top + in.bottom);
192
193     if(MotifGraphicsUtils.isLeftToRight(comboBox)) {
194         b.x = b.x + b.width - HORIZ_MARGIN - arrowIcon.getIconWidth();
195     }
196     else {
197         b.x += HORIZ_MARGIN;
198     }
199         b.y = b.y + (b.height - arrowIcon.getIconHeight()) / 2;
200         b.width = arrowIcon.getIconWidth();
201         b.height = arrowIcon.getIconHeight();
202         return b;
203     }
204
205     protected Rectangle rectangleForCurrentValue() {
206         int width = comboBox.getWidth();
207         int height = comboBox.getHeight();
208         Insets insets = getInsets();
209     if(MotifGraphicsUtils.isLeftToRight(comboBox)) {
210         return new Rectangle(insets.left, insets.top,
211                  (width - (insets.left + insets.right)) -
212                                         iconAreaWidth(),
213                  height - (insets.top + insets.bottom));
214     }
215     else {
216         return new Rectangle(insets.left + iconAreaWidth(), insets.top,
217                  (width - (insets.left + insets.right)) -
218                                         iconAreaWidth(),
219                  height - (insets.top + insets.bottom));
220     }
221     }
222
223     public int iconAreaWidth() {
224         if ( comboBox.isEditable() )
225             return arrowIcon.getIconWidth() + (2 * HORIZ_MARGIN);
226         else
227             return arrowIcon.getIconWidth() + (3 * HORIZ_MARGIN) + 2;
228     }
229
230     public void configureEditor() {
231         super.configureEditor();
232         editor.setBackground( UIManager.getColor( "text" ) );
233     }
234
235     protected LayoutManager createLayoutManager() {
236         return new ComboBoxLayoutManager();
237     }
238
239     private Component motifGetEditor() {
240         return editor;
241     }
242
243     /**
244      * This inner class is marked &quot;public&quot; due to a compiler bug.
245      * This class should be treated as a &quot;protected&quot; inner class.
246      * Instantiate it only within subclasses of <FooUI>.
247      */

248     public class ComboBoxLayoutManager extends BasicComboBoxUI.ComboBoxLayoutManager {
249     public ComboBoxLayoutManager() {
250         MotifComboBoxUI.this.super();
251     }
252         public void layoutContainer(Container parent) {
253             if ( motifGetEditor() != null ) {
254                 Rectangle cvb = rectangleForCurrentValue();
255                 cvb.x += 1;
256                 cvb.y += 1;
257                 cvb.width -= 1;
258                 cvb.height -= 2;
259                 motifGetEditor().setBounds(cvb);
260             }
261         }
262     }
263
264     static class MotifComboBoxArrowIcon implements Icon, Serializable JavaDoc {
265         private Color lightShadow;
266         private Color darkShadow;
267         private Color fill;
268
269         public MotifComboBoxArrowIcon(Color lightShadow, Color darkShadow, Color fill) {
270             this.lightShadow = lightShadow;
271             this.darkShadow = darkShadow;
272             this.fill = fill;
273         }
274
275
276         public void paintIcon(Component c, Graphics g, int xo, int yo) {
277             int w = getIconWidth();
278             int h = getIconHeight();
279
280             g.setColor(lightShadow);
281             g.drawLine(xo, yo, xo+w-1, yo);
282             g.drawLine(xo, yo+1, xo+w-3, yo+1);
283             g.setColor(darkShadow);
284             g.drawLine(xo+w-2, yo+1, xo+w-1, yo+1);
285
286             for ( int x = xo+1, y = yo+2, dx = w-6; y+1 < yo+h; y += 2 ) {
287                 g.setColor(lightShadow);
288                 g.drawLine(x, y, x+1, y);
289                 g.drawLine(x, y+1, x+1, y+1);
290                 if ( dx > 0 ) {
291                     g.setColor(fill);
292                     g.drawLine(x+2, y, x+1+dx, y);
293                     g.drawLine(x+2, y+1, x+1+dx, y+1);
294                 }
295                 g.setColor(darkShadow);
296                 g.drawLine(x+dx+2, y, x+dx+3, y);
297                 g.drawLine(x+dx+2, y+1, x+dx+3, y+1);
298                 x += 1;
299                 dx -= 2;
300             }
301
302             g.setColor(darkShadow);
303             g.drawLine(xo+(w/2), yo+h-1, xo+(w/2), yo+h-1);
304
305         }
306
307         public int getIconWidth() {
308             return 11;
309         }
310
311         public int getIconHeight() {
312             return 11;
313         }
314     }
315 }
316
317
Popular Tags