001 /*
002 * Copyright 2002-2003 Sun Microsystems, Inc. All Rights Reserved.
003 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
004 *
005 * This code is free software; you can redistribute it and/or modify it
006 * under the terms of the GNU General Public License version 2 only, as
007 * published by the Free Software Foundation. Sun designates this
008 * particular file as subject to the "Classpath" exception as provided
009 * by Sun in the LICENSE file that accompanied this code.
010 *
011 * This code is distributed in the hope that it will be useful, but WITHOUT
012 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
013 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
014 * version 2 for more details (a copy is included in the LICENSE file that
015 * accompanied this code).
016 *
017 * You should have received a copy of the GNU General Public License version
018 * 2 along with this work; if not, write to the Free Software Foundation,
019 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
020 *
021 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
022 * CA 95054 USA or visit www.sun.com if you need additional information or
023 * have any questions.
024 */
025
026 package javax.swing.plaf.synth;
027
028 import java.awt.*;
029 import java.beans.*;
030 import javax.swing.*;
031 import javax.swing.event.*;
032 import javax.swing.plaf.*;
033 import javax.swing.plaf.basic.*;
034 import sun.swing.DefaultLookup;
035 import sun.swing.plaf.synth.SynthUI;
036
037 /**
038 * Synth's OptionPaneUI.
039 *
040 * @version 1.21, 05/05/07
041 * @author James Gosling
042 * @author Scott Violet
043 * @author Amy Fowler
044 */
045 class SynthOptionPaneUI extends BasicOptionPaneUI implements
046 PropertyChangeListener, SynthUI {
047 private SynthStyle style;
048
049 /**
050 * Creates a new BasicOptionPaneUI instance.
051 */
052 public static ComponentUI createUI(JComponent x) {
053 return new SynthOptionPaneUI();
054 }
055
056 protected void installDefaults() {
057 updateStyle(optionPane);
058 }
059
060 protected void installListeners() {
061 super .installListeners();
062 optionPane.addPropertyChangeListener(this );
063 }
064
065 private void updateStyle(JComponent c) {
066 SynthContext context = getContext(c, ENABLED);
067 SynthStyle oldStyle = style;
068
069 style = SynthLookAndFeel.updateStyle(context, this );
070 if (style != oldStyle) {
071 minimumSize = (Dimension) style.get(context,
072 "OptionPane.minimumSize");
073 if (minimumSize == null) {
074 minimumSize = new Dimension(262, 90);
075 }
076 if (oldStyle != null) {
077 uninstallKeyboardActions();
078 installKeyboardActions();
079 }
080 }
081 context.dispose();
082 }
083
084 protected void uninstallDefaults() {
085 SynthContext context = getContext(optionPane, ENABLED);
086
087 style.uninstallDefaults(context);
088 context.dispose();
089 style = null;
090 }
091
092 protected void uninstallListeners() {
093 super .uninstallListeners();
094 optionPane.removePropertyChangeListener(this );
095 }
096
097 protected void installComponents() {
098 optionPane.add(createMessageArea());
099
100 Container separator = createSeparator();
101 if (separator != null) {
102 optionPane.add(separator);
103 SynthContext context = getContext(optionPane, ENABLED);
104 optionPane
105 .add(Box.createVerticalStrut(context.getStyle()
106 .getInt(context,
107 "OptionPane.separatorPadding", 6)));
108 context.dispose();
109 }
110 optionPane.add(createButtonArea());
111 optionPane.applyComponentOrientation(optionPane
112 .getComponentOrientation());
113 }
114
115 public SynthContext getContext(JComponent c) {
116 return getContext(c, getComponentState(c));
117 }
118
119 private SynthContext getContext(JComponent c, int state) {
120 return SynthContext.getContext(SynthContext.class, c,
121 SynthLookAndFeel.getRegion(c), style, state);
122 }
123
124 private Region getRegion(JComponent c) {
125 return SynthLookAndFeel.getRegion(c);
126 }
127
128 private int getComponentState(JComponent c) {
129 return SynthLookAndFeel.getComponentState(c);
130 }
131
132 public void update(Graphics g, JComponent c) {
133 SynthContext context = getContext(c);
134
135 SynthLookAndFeel.update(context, g);
136 context.getPainter().paintOptionPaneBackground(context, g, 0,
137 0, c.getWidth(), c.getHeight());
138 paint(context, g);
139 context.dispose();
140 }
141
142 public void paint(Graphics g, JComponent c) {
143 SynthContext context = getContext(c);
144
145 paint(context, g);
146 context.dispose();
147 }
148
149 protected void paint(SynthContext context, Graphics g) {
150 }
151
152 public void paintBorder(SynthContext context, Graphics g, int x,
153 int y, int w, int h) {
154 context.getPainter().paintOptionPaneBorder(context, g, x, y, w,
155 h);
156 }
157
158 public void propertyChange(PropertyChangeEvent e) {
159 if (SynthLookAndFeel.shouldUpdateStyle(e)) {
160 updateStyle((JOptionPane) e.getSource());
161 }
162 }
163
164 protected boolean getSizeButtonsToSameWidth() {
165 return DefaultLookup.getBoolean(optionPane, this ,
166 "OptionPane.sameSizeButtons", true);
167 }
168
169 /**
170 * Messaged from installComponents to create a Container containing the
171 * body of the message. The icon is the created by calling
172 * <code>addIcon</code>.
173 */
174 protected Container createMessageArea() {
175 JPanel top = new JPanel();
176 top.setName("OptionPane.messageArea");
177 top.setLayout(new BorderLayout());
178
179 /* Fill the body. */
180 Container body = new JPanel(new GridBagLayout());
181 Container realBody = new JPanel(new BorderLayout());
182
183 body.setName("OptionPane.body");
184 realBody.setName("OptionPane.realBody");
185
186 if (getIcon() != null) {
187 JPanel sep = new JPanel();
188 sep.setName("OptionPane.separator");
189 sep.setPreferredSize(new Dimension(15, 1));
190 realBody.add(sep, BorderLayout.BEFORE_LINE_BEGINS);
191 }
192 realBody.add(body, BorderLayout.CENTER);
193
194 GridBagConstraints cons = new GridBagConstraints();
195 cons.gridx = cons.gridy = 0;
196 cons.gridwidth = GridBagConstraints.REMAINDER;
197 cons.gridheight = 1;
198
199 SynthContext context = getContext(optionPane, ENABLED);
200 cons.anchor = context.getStyle().getInt(context,
201 "OptionPane.messageAnchor", GridBagConstraints.CENTER);
202 context.dispose();
203
204 cons.insets = new Insets(0, 0, 3, 0);
205
206 addMessageComponents(body, cons, getMessage(),
207 getMaxCharactersPerLineCount(), false);
208 top.add(realBody, BorderLayout.CENTER);
209
210 addIcon(top);
211 return top;
212 }
213
214 protected Container createSeparator() {
215 JSeparator separator = new JSeparator(SwingConstants.HORIZONTAL);
216
217 separator.setName("OptionPane.separator");
218 return separator;
219 }
220 }
|