📄 componentfactory.java
字号:
/*** $Id: ComponentFactory.java,v 1.2 2001/05/07 12:34:45 kunugi Exp $**** Copyright (c) 2000-2001 Jeff Gay** on behalf of ICEMail.org <http://www.icemail.org>** Copyright (c) 1998-2000 by Timothy Gerard Endres**** This program is free software.**** You may redistribute it and/or modify it under the terms of the GNU** General Public License as published by the Free Software Foundation.** Version 2 of the license should be included with this distribution in** the file LICENSE, as well as License.html. If the license is not** included with this distribution, you may find a copy at the FSF web** site at 'www.gnu.org' or 'www.fsf.org', or you may write to the** Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139 USA.**** THIS SOFTWARE IS PROVIDED AS-IS WITHOUT WARRANTY OF ANY KIND,** NOT EVEN THE IMPLIED WARRANTY OF MERCHANTABILITY. THE AUTHOR** OF THIS SOFTWARE, ASSUMES _NO_ RESPONSIBILITY FOR ANY** CONSEQUENCE RESULTING FROM THE USE, MODIFICATION, OR** REDISTRIBUTION OF THIS SOFTWARE.*/package org.icemail.util;import java.awt.Color;import java.awt.Font;import java.awt.Image;import java.awt.event.ActionListener;import java.awt.event.FocusListener;import java.awt.event.InputEvent;import java.awt.event.KeyEvent;import java.io.IOException;import java.util.MissingResourceException;import java.util.StringTokenizer;import javax.swing.Icon;import javax.swing.ImageIcon;import javax.swing.JButton;import javax.swing.JCheckBox;import javax.swing.JCheckBoxMenuItem;import javax.swing.JComponent;import javax.swing.JDialog;import javax.swing.JLabel;import javax.swing.JMenu;import javax.swing.JMenuItem;import javax.swing.JOptionPane;import javax.swing.JPopupMenu;import javax.swing.JPasswordField;import javax.swing.JTabbedPane;import javax.swing.JTextField;import javax.swing.KeyStroke;/** * ComponentFactory produces Swing components from bundled resources. * The components are produced by searching the bundle for specific resource ids, * and applying the appropriate attributes as the component is constructed. * <p> * This class supports a static interface and is not intended to be instantiated. * * @see org.icemail.util.FormatResourceBundle */public class ComponentFactory { /** * Default constructor. * This is private to eliminate any chance of instantiation. */ private ComponentFactory() {} /** * */ public static JTabbedPane getTabbedPane( FormatResourceBundle bundle, String id ) { JTabbedPane xpane = new JTabbedPane( JTabbedPane.TOP ); // @todo added support for tab placement // add any tabs found int xitems = 0; String xresource; while ( 1 == 1 ) { // generate a prefix for the resource name String xprefix = id + ".tab" + xitems; // locate icon ImageIcon xicon = null; xresource = null; try { xresource = bundle.getString( xprefix + ".icon" ); } catch ( MissingResourceException xex ) { } if ( xresource != null ) { try { Image ximage = AWTUtilities.getImageResource( xresource ); xicon = new ImageIcon( ximage ); } catch ( IOException ex ) { } } // locate title String xtitle = null; try { xtitle = bundle.getString( xprefix + ".title" ); } catch ( MissingResourceException xex ) { } // locate tip String xtip = null; try { xtip = bundle.getString( xprefix + ".tip" ); } catch ( MissingResourceException xex ) { } // add a tab to the tabbed pane if ( xtitle == null && xicon == null ) { // no title or icon, so exit break; } xpane.addTab( xtitle, xicon, new JLabel(), xtip ); // next item please xitems++; } return xpane; } /** * Show an appropriate JOptionPane of the given type, applying the arguments * to the text body of the dialog. * <p> * The dialog is produced with the attributes found in the resource bundle. If no * attributes are found for the dialog, then the dialog is shown void of any * useful attributes. * <p> * Attributes are found using the following resource ids: * <pre> * [id].dialog.message - message text of the dialog; arguments are applied also * [id].dialog.title - title text of the dialog * [id].dialog.icon - image resource name of the dialog * [id].dialog.options - comma separated list of options(buttons) of the dialog * </pre> * * @param bundle resource bundle used in obtaining resource strings * @param id prefix of all resource strings * @param optionType JOptionPane option type or 0 * @param messageType JOptionPane message type * @param args list of arguments applied to text body of dialog, or null * @return an int indicating the option(button) selected by the user * @see JOptionPane */ public static int showDialog( FormatResourceBundle bundle, String id, int optionType, int messageType, Object[] args ) { String xprefix = id + ".dialog"; String xresource; // locate message and format message String xmessage = xprefix; xresource = null; try { xresource = bundle.getFormatString( xprefix + ".message", args ); } catch ( MissingResourceException xex ) { } if ( xresource != null ) { xmessage = xresource; } // locate title String xtitle = xprefix; xresource = null; try { xresource = bundle.getString( xprefix + ".title" ); } catch ( MissingResourceException xex ) { } if ( xresource != null ) { xtitle = xresource; } // locate icon Icon xicon = null; xresource = null; try { xresource = bundle.getString( xprefix + ".icon" ); } catch ( MissingResourceException xex ) { } if ( xresource != null ) { try { Image ximage = AWTUtilities.getImageResource( xresource ); xicon = new ImageIcon( ximage ); } catch ( IOException ex ) { } } // locate choices String[] xoptions = null; String xdefault = null; try { xresource = bundle.getString( xprefix + ".options" ); } catch ( MissingResourceException xex ) { } if ( xresource != null ) { StringTokenizer xtoker = new StringTokenizer( xresource, "," ); int xitems = xtoker.countTokens(); xoptions = new String[xitems]; while ( xitems != 0 ) { xoptions[--xitems] = xtoker.nextToken(); } xdefault = xoptions[0]; } else { // check for problems if ( optionType != JOptionPane.YES_NO_OPTION && optionType != JOptionPane.YES_NO_CANCEL_OPTION ) { optionType = JOptionPane.YES_NO_OPTION; } } return JOptionPane.showOptionDialog( null, xmessage, xtitle, optionType, messageType, xicon, xoptions, xdefault ); } /** * Produce a JButton using the given resource bundle, and attaching the given listeners. * <p> * The button is produced with the attributes found in the resource bundle. If no * attributes are found for the button, then the button is returned void of any * useful attributes. * <p> * Attributes are found using the following resource ids: * <pre> * [id].button.icon - image resource name of the button * [id].button.text - text of the button * [id].button.command - command string of the button when activated * [id].button.tip - tip text of the button * [id].button.foreground - foreground color of the button * [id].button.background - background color of the button * [id].button.font - font of the button * </pre> * * @param bundle resource bundle used in obtaining resource strings * @param id prefix of all resource ids * @param alistener action listener to attach, or null * @param flistener focus listener to attach, or null * @return a button with all found attributes, possibly none * @see JButton */ public static JButton getButton( FormatResourceBundle bundle, String id, ActionListener alistener, FocusListener flistener ) { String xprefix = id + ".button"; JButton xbutton = new JButton(); String xresource; // locate default icon xresource = null; try { xresource = bundle.getString( xprefix + ".icon" ); } catch ( MissingResourceException xex ) { } if ( xresource != null ) { try { Image ximage = AWTUtilities.getImageResource( xresource ); xbutton.setIcon( new ImageIcon( ximage ) ); } catch ( IOException ex ) { } } // locate text xresource = null; try { xresource = bundle.getString( xprefix + ".text" ); } catch ( MissingResourceException xex ) { } if ( xresource != null ) { xbutton.setText( xresource ); } // setBorderPainted( boolean ) // setContentAreaFilled(boolean) // locate disabled icon xresource = null; try { xresource = bundle.getString( xprefix + ".disabledicon" ); } catch ( MissingResourceException xex ) { } if ( xresource != null ) { try { Image ximage = AWTUtilities.getImageResource( xresource ); xbutton.setDisabledIcon( new ImageIcon( ximage ) ); } catch ( IOException ex ) { } } // setDisableSelectedIcon( Icon ) // setPressedIcon( Icon ) // setRolloverIcon( Icon ) // setRolloverSelectedIcon( Icon ) // locate selected icon xresource = null; try { xresource = bundle.getString( xprefix + ".selectedicon" ); } catch ( MissingResourceException xex ) { } if ( xresource != null ) { try { Image ximage = AWTUtilities.getImageResource( xresource ); xbutton.setSelectedIcon( new ImageIcon( ximage ) ); } catch ( IOException ex ) { } } // locate component controls setComponent( bundle, xprefix, xbutton ); // setup the controls xresource = null; try { xresource = bundle.getString( xprefix + ".command" ); } catch ( MissingResourceException xex ) { } if ( xresource != null ) { xbutton.setActionCommand( xresource ); } if ( alistener != null ) { xbutton.addActionListener( alistener ); } xbutton.addFocusListener( flistener ); return xbutton; } /** * Produce a RotateButton using the given resource bundle, and attaching the given listeners. * <p> * The button is produced with the attributes found in the resource bundle. If no * attributes are found for the button, then the button is returned void of any * useful attributes. * <p> * Attributes are found using the following resource ids: * <pre> * [id].rotatebutton[#].icon - image resource name of the button * [id].rotatebutton[#].command - command string of the button when activated * [id].rotatebutton[#].text - text of the button * [id].rotatebutton[#].tip - tip text of the button * [id].rotatebutton[#].foreground - foreground color of the button * [id].rotatebutton[#].background - background color of the button * [id].rotatebutton[#].font - font of the button * </pre> * * @param bundle resource bundle used in obtaining resource strings * @param id prefix of all resource ids * @param alistener action listener to attach, or null * @param flistener focus listener to attach, or null * @return a button with all found attributes, possibly none * @see RotateButton */ public static RotateButton getRotateButton( FormatResourceBundle bundle, String id, ActionListener alistener, FocusListener flistener ) {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -