ui.java

来自「java pos,你可以直接编译运行,」· Java 代码 · 共 547 行 · 第 1/2 页

JAVA
547
字号
/* * Copyright (c) 2000 jPOS.org.  All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * * 1. Redistributions of source code must retain the above copyright *    notice, this list of conditions and the following disclaimer. * * 2. Redistributions in binary form must reproduce the above copyright *    notice, this list of conditions and the following disclaimer in *    the documentation and/or other materials provided with the *    distribution. * * 3. The end-user documentation included with the redistribution, *    if any, must include the following acknowledgment: *    "This product includes software developed by the jPOS project *    (http://www.jpos.org/)". Alternately, this acknowledgment may *    appear in the software itself, if and wherever such third-party *    acknowledgments normally appear. * * 4. The names "jPOS" and "jPOS.org" must not be used to endorse *    or promote products derived from this software without prior *    written permission. For written permission, please contact *    license@jpos.org. * * 5. Products derived from this software may not be called "jPOS", *    nor may "jPOS" appear in their name, without prior written *    permission of the jPOS project. * * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. * IN NO EVENT SHALL THE JPOS PROJECT OR ITS CONTRIBUTORS BE LIABLE FOR * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE * POSSIBILITY OF SUCH DAMAGE. * ==================================================================== * * This software consists of voluntary contributions made by many * individuals on behalf of the jPOS Project.  For more * information please see <http://www.jpos.org/>. */package org.jpos.ui;import java.awt.*;import java.awt.event.*;import javax.swing.*;import java.util.Map;import java.util.HashMap;import java.util.List;import java.util.Iterator;import java.util.ResourceBundle;import java.util.MissingResourceException;import java.util.Arrays;import org.jdom.Element;import org.jdom.JDOMException;import org.jpos.util.Log;import org.jpos.util.Logger;import org.jpos.util.LogEvent;import java.net.URL;import java.net.MalformedURLException;import javax.swing.border.EmptyBorder;/** * @author Alejandro Revilla * * <p>jPOS UI main class</p> * * @see UIFactory * * See src/examples/ui/* for usage details */public class UI implements UIFactory, UIObjectFactory {    JFrame mainFrame;    Map registrar, mapping;    Element config;    UIObjectFactory objFactory;    Log log;    boolean destroyed = false;    static final ResourceBundle classMapping;    static {        classMapping = ResourceBundle.getBundle(UI.class.getName());    }    /**     * Create a new UI object     */    public UI () {        super ();        registrar = new HashMap ();        mapping = new HashMap ();        setObjectFactory (this);    }    /**     * Creates a new UI object     * @param config configuration element     */    public UI (Element config) {        this ();        setConfig (config);    }    /**     * Assigns an object factory use to create new object instances.     * If no object factory is asigned, UI uses the default classloader     *     * @param objFactory reference to an Object Factory     */    public void setObjectFactory (UIObjectFactory objFactory) {        this.objFactory = objFactory;    }    /**     * @param config the Configuration element     */    public void setConfig (Element config) {        this.config = config;    }    /**     * @param Log an optional Log instance     * @see org.jpos.util.Log     */    public void setLog (Log log) {        this.log = log;    }    public Log getLog () {        return log;    }    /**     * UI uses a map to hold references to its components     * ("id" attribute)     *     * @return UI component registrar     */    public Map getRegistrar () {        return registrar;    }    /**     * @param id Component id ("id" configuration attribute)     * @return the Object or null     */    public Object get (String id) {        return registrar.get (id);    }   /**    * UI is itself a UIFactory.     * This strategy is used to recursively instantiate components    * inside a container    *     * @param ui reference to this UI instance    * @param config free form configuration Element    * @return JComponent    */    public JComponent create (UI ui, Element e) {        return create (e);    }    /**     * UIObjectFactory implementation.     * uses default classloader     * @param clazz the Clazzzz     * @return the Object     * @throws throw exception if unable to instantiate      * @see setLog     */    public Object newInstance (String clazz) throws Exception {        ClassLoader cl = Thread.currentThread().getContextClassLoader ();        Class type = cl.loadClass (clazz);        return type.newInstance ();    }    /**     * configure this UI object     */    public void configure () throws JDOMException {        configure (config);    }     /**     * reconfigure can be used in order to re-configure components     * inside a container (i.e. changing a panel in response to     * an event).     * @see org.jpos.ui.action.Redirect     *     * @param elementName the element name used as new configuration     * @param panelName panel ID (see "id" attribute)     */    public void reconfigure (String elementName, String panelName) {        Container c =             panelName == null ? mainFrame.getContentPane() : ((JComponent) get (panelName));        if (c != null) {            c.removeAll ();            c.add (                createComponent (config.getChild (elementName))            );            if (c instanceof JComponent) {                ((JComponent)c).revalidate ();            }            c.repaint ();        }    }    /**     * dispose this UI object     */    public void dispose () {     /* This is the right code for the dispose, but it freezes in        JVM running under WinXP (in linux went fine.. I didn't         test it under other OS's)        (last version tested: JRE 1.5.0-beta2)          if (mainFrame != null) {            // dumpComponent (mainFrame);            mainFrame.dispose ();     */        destroyed = true;        Iterator it = (Arrays.asList(Frame.getFrames())).iterator();        while (it.hasNext()) {            JFrame jf = (JFrame) it.next();            removeComponent(jf);        }    }    /**     * @return true if this UI object has been disposed and is no longer valid     */    public boolean isDestroyed () {        return destroyed;    }    protected void configure (Element ui) throws JDOMException {        setLookAndFeel (ui);        createMappings (ui);        createObjects (ui, "object");        createObjects (ui, "action");        if (!"ui".equals (ui.getName())) {            ui = ui.getChild ("ui");        }        if (ui != null) {            JFrame frame = initFrame (ui);            Element mb = ui.getChild ("menubar");            if (mb != null)                 frame.setJMenuBar (buildMenuBar (mb));            frame.setContentPane (                createComponent (ui.getChild ("components"))            );            if ("true".equals (ui.getAttributeValue ("full-screen"))) {                GraphicsDevice device = GraphicsEnvironment                                            .getLocalGraphicsEnvironment()                                            .getDefaultScreenDevice();                frame.setUndecorated (                    "true".equals (ui.getAttributeValue ("undecorated"))                );                device.setFullScreenWindow(frame);            } else {                frame.show ();            }        }    }    private void removeComponent (Component c) {        if (c instanceof Container) {            Container cont = (Container) c;            Component[] cc = cont.getComponents();                        for (int i=0; i<cc.length; i++) {                removeComponent (cc[i]);            }            cont.removeAll();        }

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?