KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * @(#)SynthOptionPaneUI.java 1.14 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 java.awt.*;
11 import java.beans.*;
12 import javax.swing.*;
13 import javax.swing.event.*;
14 import javax.swing.plaf.*;
15 import javax.swing.plaf.basic.*;
16 import sun.swing.DefaultLookup;
17 import sun.swing.plaf.synth.SynthUI;
18
19 /**
20  * Synth's OptionPaneUI.
21  *
22  * @version 1.14, 12/19/03
23  * @author James Gosling
24  * @author Scott Violet
25  * @author Amy Fowler
26  */

27 class SynthOptionPaneUI extends BasicOptionPaneUI implements
28                                 PropertyChangeListener, SynthUI {
29     private SynthStyle JavaDoc style;
30
31     /**
32       * Creates a new BasicOptionPaneUI instance.
33       */

34     public static ComponentUI createUI(JComponent x) {
35     return new SynthOptionPaneUI JavaDoc();
36     }
37
38     protected void installDefaults() {
39         updateStyle(optionPane);
40     }
41
42     protected void installListeners() {
43         super.installListeners();
44         optionPane.addPropertyChangeListener(this);
45     }
46
47     private void updateStyle(JComponent c) {
48         SynthContext JavaDoc context = getContext(c, ENABLED);
49         SynthStyle JavaDoc oldStyle = style;
50
51         style = SynthLookAndFeel.updateStyle(context, this);
52         if (style != oldStyle) {
53             minimumSize = (Dimension)style.get(context,
54                                                "OptionPane.minimumSize");
55             if (minimumSize == null) {
56                 minimumSize = new Dimension(262, 90);
57             }
58             if (oldStyle != null) {
59                 uninstallKeyboardActions();
60                 installKeyboardActions();
61             }
62         }
63         context.dispose();
64     }
65
66     protected void uninstallDefaults() {
67         SynthContext JavaDoc context = getContext(optionPane, ENABLED);
68
69         style.uninstallDefaults(context);
70         context.dispose();
71         style = null;
72     }
73
74     protected void uninstallListeners() {
75         super.uninstallListeners();
76         optionPane.removePropertyChangeListener(this);
77     }
78
79     protected void installComponents() {
80     optionPane.add(createMessageArea());
81         
82         Container separator = createSeparator();
83         if (separator != null) {
84             optionPane.add(separator);
85             SynthContext JavaDoc context = getContext(optionPane, ENABLED);
86             optionPane.add(Box.createVerticalStrut(context.getStyle().
87                        getInt(context, "OptionPane.separatorPadding", 6)));
88             context.dispose();
89         }
90     optionPane.add(createButtonArea());
91     optionPane.applyComponentOrientation(optionPane.getComponentOrientation());
92     }
93
94     public SynthContext JavaDoc getContext(JComponent c) {
95         return getContext(c, getComponentState(c));
96     }
97
98     private SynthContext JavaDoc getContext(JComponent c, int state) {
99         return SynthContext.getContext(SynthContext JavaDoc.class, c,
100                     SynthLookAndFeel.getRegion(c), style, state);
101     }
102
103     private Region JavaDoc getRegion(JComponent c) {
104         return SynthLookAndFeel.getRegion(c);
105     }
106
107     private int getComponentState(JComponent c) {
108         return SynthLookAndFeel.getComponentState(c);
109     }
110
111     public void update(Graphics g, JComponent c) {
112         SynthContext JavaDoc context = getContext(c);
113
114         SynthLookAndFeel.update(context, g);
115         context.getPainter().paintOptionPaneBackground(context,
116                           g, 0, 0, c.getWidth(), c.getHeight());
117         paint(context, g);
118         context.dispose();
119     }
120
121     public void paint(Graphics g, JComponent c) {
122         SynthContext JavaDoc context = getContext(c);
123
124         paint(context, g);
125         context.dispose();
126     }
127
128     protected void paint(SynthContext JavaDoc context, Graphics g) {
129     }
130
131     public void paintBorder(SynthContext JavaDoc context, Graphics g, int x,
132                             int y, int w, int h) {
133         context.getPainter().paintOptionPaneBorder(context, g, x, y, w, h);
134     }
135
136     public void propertyChange(PropertyChangeEvent e) {
137         if (SynthLookAndFeel.shouldUpdateStyle(e)) {
138             updateStyle((JOptionPane)e.getSource());
139         }
140     }
141
142     protected boolean getSizeButtonsToSameWidth() {
143     return DefaultLookup.getBoolean(optionPane, this,
144                                         "OptionPane.sameSizeButtons", true);
145     }
146
147     /**
148      * Messaged from installComponents to create a Container containing the
149      * body of the message. The icon is the created by calling
150      * <code>addIcon</code>.
151      */

152     protected Container createMessageArea() {
153         JPanel top = new JPanel();
154         top.setName("OptionPane.messageArea");
155     top.setLayout(new BorderLayout());
156
157     /* Fill the body. */
158     Container body = new JPanel(new GridBagLayout());
159     Container realBody = new JPanel(new BorderLayout());
160
161         body.setName("OptionPane.body");
162         realBody.setName("OptionPane.realBody");
163
164     if (getIcon() != null) {
165             JPanel sep = new JPanel();
166             sep.setName("OptionPane.separator");
167             sep.setPreferredSize(new Dimension(15, 1));
168         realBody.add(sep, BorderLayout.BEFORE_LINE_BEGINS);
169     }
170     realBody.add(body, BorderLayout.CENTER);
171
172     GridBagConstraints cons = new GridBagConstraints();
173     cons.gridx = cons.gridy = 0;
174     cons.gridwidth = GridBagConstraints.REMAINDER;
175     cons.gridheight = 1;
176
177         SynthContext JavaDoc context = getContext(optionPane, ENABLED);
178     cons.anchor = context.getStyle().getInt(context,
179                       "OptionPane.messageAnchor", GridBagConstraints.CENTER);
180         context.dispose();
181
182     cons.insets = new Insets(0,0,3,0);
183
184     addMessageComponents(body, cons, getMessage(),
185               getMaxCharactersPerLineCount(), false);
186     top.add(realBody, BorderLayout.CENTER);
187
188     addIcon(top);
189     return top;
190     }
191
192     protected Container createSeparator() {
193         JSeparator separator = new JSeparator(SwingConstants.HORIZONTAL);
194
195         separator.setName("OptionPane.separator");
196         return separator;
197     }
198 }
199
Popular Tags