001: /**
002: * L2FProd.com Common Components 7.3 License.
003: *
004: * Copyright 2005-2007 L2FProd.com
005: *
006: * Licensed under the Apache License, Version 2.0 (the "License");
007: * you may not use this file except in compliance with the License.
008: * You may obtain a copy of the License at
009: *
010: * http://www.apache.org/licenses/LICENSE-2.0
011: *
012: * Unless required by applicable law or agreed to in writing, software
013: * distributed under the License is distributed on an "AS IS" BASIS,
014: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015: * See the License for the specific language governing permissions and
016: * limitations under the License.
017: */package com.l2fprod.common.swing;
018:
019: import com.l2fprod.common.swing.plaf.FontChooserUI;
020: import com.l2fprod.common.swing.plaf.JFontChooserAddon;
021: import com.l2fprod.common.swing.plaf.LookAndFeelAddons;
022:
023: import java.awt.BorderLayout;
024: import java.awt.Component;
025: import java.awt.Dialog;
026: import java.awt.Font;
027: import java.awt.Frame;
028: import java.awt.Window;
029:
030: import javax.swing.JComponent;
031: import javax.swing.JOptionPane;
032: import javax.swing.SwingUtilities;
033:
034: /**
035: * <code>JFontChooser</code> provides a pane of controls designed to allow a
036: * user to manipulate and select a font.
037: *
038: * @javabean.class
039: * name="JFontChooser"
040: * shortDescription="A component that supports selecting a Font."
041: * stopClass="javax.swing.JComponent"
042: *
043: * @javabean.attribute
044: * name="isContainer"
045: * value="Boolean.FALSE"
046: * rtexpr="true"
047: *
048: * @javabean.icons
049: * mono16="JFontChooser16-mono.gif"
050: * color16="JFontChooser16.gif"
051: * mono32="JFontChooser32-mono.gif"
052: * color32="JFontChooser32.gif"
053: */
054: public class JFontChooser extends JComponent {
055:
056: // ensure at least the default ui is registered
057: static {
058: LookAndFeelAddons.contribute(new JFontChooserAddon());
059: }
060:
061: public static final String SELECTED_FONT_CHANGED_KEY = "selectedFont";
062:
063: protected Font selectedFont;
064:
065: private FontChooserModel model;
066:
067: /**
068: * Creates a font chooser with an initial default font and a default
069: * model.
070: */
071: public JFontChooser() {
072: this (new DefaultFontChooserModel());
073: }
074:
075: /**
076: * Creates a font chooser with an initial default font and a custom
077: * model.
078: *
079: * @param model
080: */
081: public JFontChooser(FontChooserModel model) {
082: super ();
083: this .model = model;
084: selectedFont = getFont();
085: updateUI();
086: }
087:
088: /**
089: * Notification from the <code>UIManager</code> that the L&F has
090: * changed. Replaces the current UI object with the latest version
091: * from the <code>UIManager</code>.
092: *
093: * @see javax.swing.JComponent#updateUI
094: */
095: public void updateUI() {
096: setUI((FontChooserUI) LookAndFeelAddons.getUI(this ,
097: FontChooserUI.class));
098: }
099:
100: /**
101: * Sets the L&F object that renders this component.
102: *
103: * @param ui the <code>FontChooserUI</code> L&F object
104: * @see javax.swing.UIDefaults#getUI
105: *
106: * @beaninfo
107: * bound: true
108: * hidden: true
109: * description: The UI object that implements the font chooser's LookAndFeel.
110: */
111: public void setUI(FontChooserUI ui) {
112: super .setUI(ui);
113: }
114:
115: /**
116: * Returns the name of the L&F class that renders this component.
117: *
118: * @return the string "FontChooserUI"
119: * @see javax.swing.JComponent#getUIClassID
120: * @see javax.swing.UIDefaults#getUI
121: */
122: public String getUIClassID() {
123: return "FontChooserUI";
124: }
125:
126: /**
127: * Sets the selected font of this JFontChooser. This will fire a <code>PropertyChangeEvent</code>
128: * for the property named {@link #SELECTED_FONT_CHANGED_KEY}.
129: *
130: * @param f the font to select
131: * @see javax.swing.JComponent#addPropertyChangeListener(java.beans.PropertyChangeListener)
132: *
133: * @javabean.property
134: * bound="true"
135: * preferred="true"
136: * shortDescription="The current font the chooser is to display"
137: */
138: public void setSelectedFont(Font f) {
139: Font oldFont = selectedFont;
140: selectedFont = f;
141: firePropertyChange(SELECTED_FONT_CHANGED_KEY, oldFont,
142: selectedFont);
143: }
144:
145: /**
146: * Gets the current font value from the font chooser.
147: *
148: * @return the current font value of the font chooser
149: */
150: public Font getSelectedFont() {
151: return selectedFont;
152: }
153:
154: /**
155: * Gets the font chooser model of this font chooser.
156: *
157: * @return the font chooser model of this font chooser.
158: */
159: public FontChooserModel getModel() {
160: return model;
161: }
162:
163: /**
164: * Shows a modal font-chooser dialog and blocks until the dialog is
165: * hidden. If the user presses the "OK" button, then this method
166: * hides/disposes the dialog and returns the selected color. If the
167: * user presses the "Cancel" button or closes the dialog without
168: * pressing "OK", then this method hides/disposes the dialog and
169: * returns <code>null</code>.
170: *
171: * @param parent the parent <code>Component</code> for the
172: * dialog
173: * @param title the String containing the dialog's title
174: * @return the selected font or <code>null</code> if the user
175: * opted out
176: * @exception java.awt.HeadlessException if GraphicsEnvironment.isHeadless()
177: * returns true.
178: * @see java.awt.GraphicsEnvironment#isHeadless
179: */
180: public Font showFontDialog(Component parent, String title) {
181: BaseDialog dialog = createDialog(parent, title);
182: if (dialog.ask()) {
183: return getSelectedFont();
184: } else {
185: return null;
186: }
187: }
188:
189: protected BaseDialog createDialog(Component parent, String title) {
190: BaseDialog dialog;
191: Window window = (parent == null ? JOptionPane.getRootFrame()
192: : SwingUtilities.windowForComponent(parent));
193: if (window instanceof Frame) {
194: dialog = new BaseDialog((Frame) window, title, true);
195: } else {
196: dialog = new BaseDialog((Dialog) window, title, true);
197: }
198: dialog.setDialogMode(BaseDialog.OK_CANCEL_DIALOG);
199: dialog.getBanner().setVisible(false);
200:
201: dialog.getContentPane().setLayout(new BorderLayout());
202: dialog.getContentPane().add("Center", this );
203: dialog.pack();
204: dialog.setLocationRelativeTo(parent);
205:
206: return dialog;
207: }
208:
209: /**
210: * Similar to {@link #showFontDialog(Component, String)} except it can be
211: * called from a static context. Prefer
212: * {@link #showFontDialog(Component, String)} if you want to control the
213: * dialog created by the method call or if you want to specify a custom
214: * {@link FontChooserModel}.
215: *
216: * @param parent
217: * the parent <code>Component</code> for the dialog
218: * @param title
219: * the String containing the dialog's title
220: * @param initialFont
221: * the initial Font set when the font-chooser is shown
222: * @return the selected font or <code>null</code> if the user opted out
223: * @exception java.awt.HeadlessException
224: * if GraphicsEnvironment.isHeadless() returns true.
225: * @see java.awt.GraphicsEnvironment#isHeadless
226: * @see #showFontDialog(Component, String)
227: */
228: public static Font showDialog(Component parent, String title,
229: Font initialFont) {
230: JFontChooser chooser = new JFontChooser();
231: chooser.setSelectedFont(initialFont);
232: return chooser.showFontDialog(parent, title);
233: }
234:
235: }
|