KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > javax > swing > plaf > synth > SynthListUI


1 /*
2  * @(#)SynthListUI.java 1.11 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
8 package javax.swing.plaf.synth;
9
10 import javax.swing.*;
11 import javax.swing.border.*;
12 import javax.swing.event.*;
13 import javax.swing.plaf.*;
14 import javax.swing.plaf.basic.*;
15 import javax.swing.text.Position JavaDoc;
16
17 import java.awt.*;
18 import java.awt.event.*;
19 import java.awt.datatransfer.Transferable JavaDoc;
20 import java.awt.dnd.*;
21
22 import java.util.ArrayList JavaDoc;
23 import java.util.TooManyListenersException JavaDoc;
24
25 import java.beans.PropertyChangeListener JavaDoc;
26 import java.beans.PropertyChangeEvent JavaDoc;
27 import sun.swing.plaf.synth.SynthUI;
28
29 /**
30  * Synth's ListUI.
31  *
32  * @version 1.11, 12/19/03
33  * @author Scott Violet
34  */

35 class SynthListUI extends BasicListUI implements PropertyChangeListener JavaDoc,
36                                SynthUI {
37     private SynthStyle JavaDoc style;
38     private boolean useListColors;
39     private boolean useUIBorder;
40
41     public static ComponentUI createUI(JComponent list) {
42         return new SynthListUI JavaDoc();
43     }
44
45     public void update(Graphics g, JComponent c) {
46         SynthContext JavaDoc context = getContext(c);
47
48         SynthLookAndFeel.update(context, g);
49         context.getPainter().paintListBackground(context,
50                           g, 0, 0, c.getWidth(), c.getHeight());
51         context.dispose();
52         paint(g, c);
53     }
54
55     public void paintBorder(SynthContext JavaDoc context, Graphics g, int x,
56                             int y, int w, int h) {
57         context.getPainter().paintListBorder(context, g, x, y, w, h);
58     }
59
60     protected void installListeners() {
61         super.installListeners();
62         list.addPropertyChangeListener(this);
63     }
64
65     public void propertyChange(PropertyChangeEvent JavaDoc e) {
66         if (SynthLookAndFeel.shouldUpdateStyle(e)) {
67             updateStyle((JList)e.getSource());
68         }
69     }
70
71     protected void uninstallListeners() {
72         super.uninstallListeners();
73         list.removePropertyChangeListener(this);
74     }
75
76     protected void installDefaults() {
77         if (list.getCellRenderer() == null ||
78                  (list.getCellRenderer() instanceof UIResource)) {
79             list.setCellRenderer(new SynthListCellRenderer());
80         }
81         updateStyle(list);
82     }
83
84     private void updateStyle(JComponent c) {
85         SynthContext JavaDoc context = getContext(list, ENABLED);
86         SynthStyle JavaDoc oldStyle = style;
87
88         style = SynthLookAndFeel.updateStyle(context, this);
89
90         if (style != oldStyle) {
91             context.setComponentState(SELECTED);
92             Color sbg = list.getSelectionBackground();
93             if (sbg == null || sbg instanceof UIResource) {
94                 list.setSelectionBackground(style.getColor(
95                                  context, ColorType.TEXT_BACKGROUND));
96             }
97
98             Color sfg = list.getSelectionForeground();
99             if (sfg == null || sfg instanceof UIResource) {
100                 list.setSelectionForeground(style.getColor(
101                                  context, ColorType.TEXT_FOREGROUND));
102             }
103
104             useListColors = style.getBoolean(context,
105                                   "List.rendererUseListColors", true);
106             useUIBorder = style.getBoolean(context,
107                                   "List.rendererUseUIBorder", true);
108
109             int height = style.getInt(context, "List.cellHeight", -1);
110             if (height != -1) {
111                 list.setFixedCellHeight(height);
112             }
113             if (oldStyle != null) {
114                 uninstallKeyboardActions();
115                 installKeyboardActions();
116             }
117         }
118         context.dispose();
119     }
120
121     protected void uninstallDefaults() {
122         super.uninstallDefaults();
123
124         SynthContext JavaDoc context = getContext(list, ENABLED);
125
126         style.uninstallDefaults(context);
127         context.dispose();
128         style = null;
129     }
130
131     public SynthContext JavaDoc getContext(JComponent c) {
132         return getContext(c, getComponentState(c));
133     }
134
135     private SynthContext JavaDoc getContext(JComponent c, int state) {
136         return SynthContext.getContext(SynthContext JavaDoc.class, c,
137                     SynthLookAndFeel.getRegion(c), style, state);
138     }
139
140     private Region JavaDoc getRegion(JComponent c) {
141         return SynthLookAndFeel.getRegion(c);
142     }
143
144     private int getComponentState(JComponent c) {
145         return SynthLookAndFeel.getComponentState(c);
146     }
147
148
149     private class SynthListCellRenderer extends DefaultListCellRenderer.UIResource {
150         public String JavaDoc getName() {
151             return "List.cellRenderer";
152         }
153
154         public void setBorder(Border b) {
155             if (useUIBorder || b instanceof SynthBorder JavaDoc) {
156                 super.setBorder(b);
157             }
158         }
159
160         public Component getListCellRendererComponent(JList list, Object JavaDoc value,
161                   int index, boolean isSelected, boolean cellHasFocus) {
162             if (!useListColors && (isSelected || cellHasFocus)) {
163                 SynthLookAndFeel.setSelectedUI((SynthLabelUI JavaDoc)SynthLookAndFeel.
164                              getUIOfType(getUI(), SynthLabelUI JavaDoc.class),
165                                    isSelected, cellHasFocus, list.isEnabled());
166             }
167             else {
168                 SynthLookAndFeel.resetSelectedUI();
169             }
170             
171             super.getListCellRendererComponent(list, value, index,
172                                                isSelected, cellHasFocus);
173             return this;
174         }
175
176         public void paint(Graphics g) {
177             super.paint(g);
178             SynthLookAndFeel.resetSelectedUI();
179         }
180     }
181 }
182
Popular Tags