KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * @(#)SynthToolBarUI.java 1.10 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.event.*;
12 import java.awt.*;
13 import java.awt.event.*;
14
15 import java.beans.*;
16
17 import javax.swing.border.*;
18 import javax.swing.plaf.*;
19 import javax.swing.plaf.basic.BasicToolBarUI JavaDoc;
20 import sun.swing.plaf.synth.*;
21
22
23 /**
24  * A Synth L&F implementation of ToolBarUI. This implementation
25  * is a "combined" view/controller.
26  * <p>
27  *
28  * @version 1.10, 12/19/03
29  * @author Georges Saab
30  * @author Jeff Shapiro
31  */

32 class SynthToolBarUI extends BasicToolBarUI JavaDoc implements PropertyChangeListener,
33            SynthUI {
34     protected Icon handleIcon = null;
35     protected Rectangle contentRect = new Rectangle();
36
37     private SynthStyle JavaDoc style;
38     private SynthStyle JavaDoc contentStyle;
39     private SynthStyle JavaDoc dragWindowStyle;
40
41     public static ComponentUI createUI(JComponent c) {
42     return new SynthToolBarUI JavaDoc();
43     }
44
45     protected void installDefaults() {
46         toolBar.setLayout(createLayout());
47         updateStyle(toolBar);
48     }
49
50     protected void installListeners() {
51         super.installListeners();
52         toolBar.addPropertyChangeListener(this);
53     }
54
55     protected void uninstallListeners() {
56         super.uninstallListeners();
57         toolBar.removePropertyChangeListener(this);
58     }
59
60     private void updateStyle(JToolBar c) {
61         SynthContext JavaDoc context = getContext(c, ENABLED);
62         SynthStyle JavaDoc oldStyle = style;
63
64         style = SynthLookAndFeel.updateStyle(context, this);
65         if (oldStyle != style) {
66             handleIcon =
67                 style.getIcon(context, "ToolBar.handleIcon");
68             if (oldStyle != null) {
69                 uninstallKeyboardActions();
70                 installKeyboardActions();
71             }
72         }
73         context.dispose();
74
75         context = getContext(c, Region.TOOL_BAR_CONTENT, ENABLED);
76         contentStyle = SynthLookAndFeel.updateStyle(context, this);
77         context.dispose();
78
79         context = getContext(c, Region.TOOL_BAR_DRAG_WINDOW, ENABLED);
80         dragWindowStyle = SynthLookAndFeel.updateStyle(context, this);
81         context.dispose();
82     }
83
84     protected void uninstallDefaults() {
85         SynthContext JavaDoc context = getContext(toolBar, ENABLED);
86
87         style.uninstallDefaults(context);
88         context.dispose();
89         style = null;
90
91         handleIcon = null;
92
93         context = getContext(toolBar, Region.TOOL_BAR_CONTENT, ENABLED);
94         contentStyle.uninstallDefaults(context);
95         context.dispose();
96         contentStyle = null;
97
98         context = getContext(toolBar, Region.TOOL_BAR_DRAG_WINDOW, ENABLED);
99         dragWindowStyle.uninstallDefaults(context);
100         context.dispose();
101         dragWindowStyle = null;
102
103         toolBar.setLayout(null);
104     }
105
106     protected void installComponents() {
107     }
108
109     protected void uninstallComponents() {
110     }
111
112     protected LayoutManager createLayout() {
113         return new SynthToolBarLayoutManager();
114     }
115
116     public SynthContext JavaDoc getContext(JComponent c) {
117         return getContext(c, getComponentState(c));
118     }
119
120     private SynthContext JavaDoc getContext(JComponent c, int state) {
121         return SynthContext.getContext(SynthContext JavaDoc.class, c,
122                     SynthLookAndFeel.getRegion(c), style, state);
123     }
124
125     private SynthContext JavaDoc getContext(JComponent c, Region JavaDoc region) {
126         return getContext(c, region, getComponentState(c, region));
127     }
128
129     private SynthContext JavaDoc getContext(JComponent c, Region JavaDoc region, int state) {
130         return SynthContext.getContext(SynthContext JavaDoc.class, c, region,
131                                        dragWindowStyle, state);
132     }
133
134     private Region JavaDoc getRegion(JComponent c) {
135         return SynthLookAndFeel.getRegion(c);
136     }
137
138     private int getComponentState(JComponent c) {
139         return SynthLookAndFeel.getComponentState(c);
140     }
141
142     private int getComponentState(JComponent c, Region JavaDoc region) {
143         return SynthLookAndFeel.getComponentState(c);
144     }
145
146     public void update(Graphics g, JComponent c) {
147         SynthContext JavaDoc context = getContext(c);
148
149         SynthLookAndFeel.update(context, g);
150         context.getPainter().paintToolBarBackground(context,
151                           g, 0, 0, c.getWidth(), c.getHeight());
152         paint(context, g);
153         context.dispose();
154     }
155
156     public void paint(Graphics g, JComponent c) {
157         SynthContext JavaDoc context = getContext(c);
158
159         paint(context, g);
160         context.dispose();
161     }
162
163     public void paintBorder(SynthContext JavaDoc context, Graphics g, int x,
164                             int y, int w, int h) {
165         context.getPainter().paintToolBarBorder(context, g, x, y, w, h);
166     }
167
168     // Overloaded to do nothing so we can share listeners.
169
protected void setBorderToNonRollover(Component c) {}
170
171     // Overloaded to do nothing so we can share listeners.
172
protected void setBorderToRollover(Component c) {}
173
174     // Overloaded to do nothing so we can share listeners.
175
protected void setBorderToNormal(Component c) {}
176
177     protected void paint(SynthContext JavaDoc context, Graphics g) {
178         if (handleIcon != null && toolBar.isFloatable()) {
179             int startX = toolBar.getComponentOrientation().isLeftToRight() ?
180                 0 : toolBar.getWidth() -
181                     SynthIcon.getIconWidth(handleIcon, context);
182             SynthIcon.paintIcon(handleIcon, context, g, startX, 0,
183                     SynthIcon.getIconWidth(handleIcon, context),
184                     SynthIcon.getIconHeight(handleIcon, context));
185         }
186
187         SynthContext JavaDoc subcontext = getContext(toolBar, Region.TOOL_BAR_CONTENT);
188         paintContent(subcontext, g, contentRect);
189         subcontext.dispose();
190     }
191
192     public void paintContent(SynthContext JavaDoc context, Graphics g,
193             Rectangle bounds) {
194         SynthLookAndFeel.updateSubregion(context, g, bounds);
195         context.getPainter().paintToolBarContentBackground(context, g,
196                              bounds.x, bounds.y, bounds.width, bounds.height);
197         context.getPainter().paintToolBarContentBorder(context, g,
198                              bounds.x, bounds.y, bounds.width, bounds.height);
199     }
200
201     protected void paintDragWindow(Graphics g) {
202         int w = dragWindow.getWidth();
203         int h = dragWindow.getHeight();
204         SynthContext JavaDoc context = getContext(toolBar,Region.TOOL_BAR_DRAG_WINDOW);
205         SynthLookAndFeel.updateSubregion(context, g, new Rectangle(
206                          0, 0, w, h));
207         context.getPainter().paintToolBarDragWindowBackground(context,
208                                                            g, 0, 0, w, h);
209         context.getPainter().paintToolBarDragWindowBorder(context, g, 0,0,w,h);
210         context.dispose();
211     }
212
213     //
214
// PropertyChangeListener
215
//
216

217     public void propertyChange(PropertyChangeEvent e) {
218         if (SynthLookAndFeel.shouldUpdateStyle(e)) {
219             updateStyle((JToolBar)e.getSource());
220         }
221     }
222
223
224     class SynthToolBarLayoutManager implements LayoutManager {
225         public void addLayoutComponent(String JavaDoc name, Component comp) {}
226
227         public void removeLayoutComponent(Component comp) {}
228
229         public Dimension minimumLayoutSize(Container parent) {
230             JToolBar tb = (JToolBar)parent;
231             Dimension dim = new Dimension();
232             SynthContext JavaDoc context = getContext(tb);
233
234             if (tb.getOrientation() == JToolBar.HORIZONTAL) {
235                 dim.width = SynthIcon.getIconWidth(handleIcon, context);
236                 Dimension compDim;
237                 for (int i = 0; i < tb.getComponentCount(); i++) {
238                     compDim = tb.getComponent(i).getMinimumSize();
239                     dim.width += compDim.width;
240                     dim.height = Math.max(dim.height, compDim.height);
241                 }
242             } else {
243                 dim.height =
244                     SynthIcon.getIconHeight(handleIcon, context);
245                 Dimension compDim;
246                 for (int i = 0; i < tb.getComponentCount(); i++) {
247                     compDim = tb.getComponent(i).getMinimumSize();
248                     dim.width = Math.max(dim.width, compDim.width);
249                     dim.height += compDim.height;
250                 }
251             }
252             context.dispose();
253             return dim;
254         }
255
256         public Dimension preferredLayoutSize(Container parent) {
257             JToolBar tb = (JToolBar)parent;
258             Dimension dim = new Dimension();
259             SynthContext JavaDoc context = getContext(tb);
260
261             if (tb.getOrientation() == JToolBar.HORIZONTAL) {
262                 dim.width = SynthIcon.getIconWidth(handleIcon, context);
263                 Dimension compDim;
264                 for (int i = 0; i < tb.getComponentCount(); i++) {
265                     compDim = tb.getComponent(i).getPreferredSize();
266                     dim.width += compDim.width;
267                     dim.height = Math.max(dim.height, compDim.height);
268                 }
269             } else {
270                 dim.height =
271                     SynthIcon.getIconHeight(handleIcon, context);
272                 Dimension compDim;
273                 for (int i = 0; i < tb.getComponentCount(); i++) {
274                     compDim = tb.getComponent(i).getPreferredSize();
275                     dim.width = Math.max(dim.width, compDim.width);
276                     dim.height += compDim.height;
277                 }
278             }
279             context.dispose();
280             return dim;
281         }
282
283         public void layoutContainer(Container parent) {
284             JToolBar tb = (JToolBar)parent;
285             boolean ltr = tb.getComponentOrientation().isLeftToRight();
286             SynthContext JavaDoc context = getContext(tb);
287             int handleWidth = SynthIcon.getIconWidth(handleIcon, context);
288
289             Component c;
290             Dimension d;
291             if (tb.getOrientation() == JToolBar.HORIZONTAL) {
292                 int x = ltr ? handleWidth : tb.getWidth() - handleWidth;
293                 for (int i = 0; i < tb.getComponentCount(); i++) {
294                     c = tb.getComponent(i);
295                     d = c.getPreferredSize();
296                     c.setBounds(ltr ? x : x - d.width, 0, d.width, d.height);
297                     x = ltr ? x + d.width : x - d.width;
298                 }
299                 contentRect.x = ltr ?
300                         SynthIcon.getIconWidth(handleIcon, context) : 0;
301                 contentRect.y = 0;
302                 contentRect.width = tb.getWidth() - contentRect.x;
303                 contentRect.height = tb.getHeight();
304             } else {
305                 int y = SynthIcon.getIconHeight(handleIcon, context);
306                 for (int i = 0; i < tb.getComponentCount(); i++) {
307                     c = tb.getComponent(i);
308                     d = c.getPreferredSize();
309                     c.setBounds(0, y, d.width, d.height);
310                     y += d.height;
311                 }
312                 contentRect.x = 0;
313                 contentRect.y =
314                     SynthIcon.getIconHeight(handleIcon, context);
315                 contentRect.width = tb.getWidth();
316                 contentRect.height = tb.getHeight() - contentRect.y;
317             }
318             context.dispose();
319         }
320     }
321 }
322
Popular Tags