⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 configurationpanel.java

📁 jxta官方例程
💻 JAVA
字号:
/**  Copyright (c) 2001 Sun Microsystems, Inc.  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*  Sun Microsystems, Inc. for Project JXTA."*  Alternately, this acknowledgment may appear in the software itself,*  if and wherever such third-party acknowledgments normally appear.**  4. The names "Sun", "Sun Microsystems, Inc.", "JXTA" and "Project JXTA"*  must not be used to endorse or promote products derived from this*  software without prior written permission. For written*  permission, please contact Project JXTA at http://www.jxta.org.**  5. Products derived from this software may not be called "JXTA",*  nor may "JXTA" appear in their name, without prior written*  permission of Sun.**  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 APACHE SOFTWARE FOUNDATION 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 Project JXTA.  For more*  information on Project JXTA, please see*  <http://www.jxta.org/>.**  This license is based on the BSD license adopted by the Apache Foundation.**  $Id: ConfigurationPanel.java,v 1.11 2006/06/10 06:31:48 nano Exp $*/package net.jxta.myjxta.ui;import net.jxta.myjxta.util.Constants;import net.jxta.myjxta.util.Resources;import java.awt.GridBagConstraints;import java.awt.GridBagLayout;import java.awt.GridLayout;import java.awt.Insets;import java.awt.event.ActionEvent;import java.awt.event.KeyAdapter;import java.awt.event.KeyEvent;import javax.swing.AbstractAction;import javax.swing.BorderFactory;import javax.swing.JButton;import javax.swing.JLabel;import javax.swing.JPanel;import javax.swing.JPasswordField;import javax.swing.JTextField;import java.net.MalformedURLException;import java.net.URL;import java.util.List;import java.util.ResourceBundle;/** * * @author james todd [gonzo at jxta dot org] * @version $Id: ConfigurationPanel.java,v 1.11 2006/06/10 06:31:48 nano Exp $ */public class ConfigurationPanel    extends JPanel {    private static final String LEFT_PARENTHESIS = "(";    private static final String RIGHT_PARENTHESIS = ")";    private static final String URL_DELIMITER = "://";    private static final ResourceBundle STRINGS = Resources.getStrings();        /** The field that contains the name of the peer */    private JTextField peerName = null;    /** The field for the password used for pse login */    private JPasswordField pwd = null;    /** The field used to confirm the password  for pse login */    private JPasswordField vpwd = null;    private JTextField httpProxy = null;    private boolean isExit = false;        private JButton ok = null;    /**     * Create a new ConfigurationPanel     *     * @param configuration file containing configuration information     */    public ConfigurationPanel(List configuration) {        super();        ui();    }    /**     * Return the peerName the peer signed on with.     *      * @return the peerName the peer signed on with     * @exception  IllegalStateException if the peer did not complete login     */    public String getPeerName() {        return this.peerName.getText().trim();    }    /**      * Return the password the peer used  to secure the pse       * directory       *       * @return the pse password      * @exception  IllegalStateException if the peer did not complete login      */    public String getPassword() {        return String.valueOf(this.pwd.getPassword()).trim();    }    public URL getHttpProxy() {        return toURL(this.httpProxy.getText());    }        public boolean isExit() {        return this.isExit;    }    /**     * Construct the UI layout to      * soliciated the login information     */    private void ui() {        GridBagLayout gb = new GridBagLayout();        GridBagConstraints gbc = new GridBagConstraints();        // set up the layout and border conditions        setLayout(gb);        setBorder(BorderFactory.createEmptyBorder(3, 3, 3, 3));        /*        //  add the combo box containing the configuration options        DefaultComboBoxModel cm = new DefaultComboBoxModel();        for (Iterator ci = this.configuration.iterator(); ci.hasNext(); ) {            cm.addElement((URL)ci.next());        }        this.configOptions = new JComboBox(cm);        gbc.gridx = 0;        gbc.gridy = 0;        gbc.gridwidth = GridBagConstraints.REMAINDER;        gbc.insets = new Insets(3, 3, 3, 3);        gbc.anchor = GridBagConstraints.NORTHWEST;        gbc.weightx = 0.0;        gbc.weighty = 0.0;        gbc.fill = GridBagConstraints.BOTH;        gb.setConstraints(this.configOptions, gbc);        // xxx: support selectible configurations        //        add(this.configOptions);        */                //add the  input widgets for the peer name        JLabel l = new JLabel(STRINGS.getString("label.config.user.name"));        gbc.gridx = 0;        gbc.gridy = 0;        gbc.gridwidth = 1;        gbc.gridheight = 1;        gbc.weightx = 1.0;        gbc.weighty = 1.0;        gbc.anchor = GridBagConstraints.FIRST_LINE_START;        gbc.fill = GridBagConstraints.BOTH;        gbc.insets = new Insets(3, 3, 3, 3);        gbc.ipadx = 0;        gbc.ipady = 0;        gb.setConstraints(l, gbc);        add(l);        this.peerName = new JTextField(15);                this.peerName.addKeyListener(new KeyAdapter() {            // xxx: doesn't work, no harm            public void keyPressed(KeyEvent ke) {                if (ke.getKeyCode() == KeyEvent.VK_TAB) {                    int l = peerName.getText().length();                                        peerName.select(l, l);                }            }                        public void keyReleased(KeyEvent ke) {                ok.setEnabled(isValidInput());            }        });                String un = System.getProperty("user.name", "");                this.peerName.setText(un);        this.peerName.setCaretPosition(un.length());        this.peerName.selectAll();                gbc.gridx++;        gbc.gridwidth = GridBagConstraints.REMAINDER;        gb.setConstraints(this.peerName, gbc);        add(this.peerName);        // add the input widget for the pse password        l = new JLabel(STRINGS.getString("label.config.user.password"));        gbc.gridx = 0;        gbc.gridy++;        gbc.gridwidth = 1;        gbc.anchor = GridBagConstraints.LINE_START;        gb.setConstraints(l, gbc);        add(l);        this.pwd = new JPasswordField(15);                this.pwd.addKeyListener(new KeyAdapter() {            public void keyReleased(KeyEvent ke) {                ok.setEnabled(isValidInput());            }        });        gbc.gridx++;        gbc.gridwidth = GridBagConstraints.REMAINDER;        gbc.fill = GridBagConstraints.BOTH;        gb.setConstraints(this.pwd, gbc);        add(this.pwd);        // add the input widgets that confirm the password        l = new JLabel(STRINGS.getString("label.config.user.password.validate"));        gbc.gridx = 0;        gbc.gridy++;        gbc.gridwidth = 1;        gbc.anchor = GridBagConstraints.LINE_START;        gb.setConstraints(l, gbc);        add(l);        this.vpwd = new JPasswordField(15);                this.vpwd.addKeyListener(new KeyAdapter() {            public void keyReleased(KeyEvent ke) {                ok.setEnabled(isValidInput());            }        });        gbc.gridx++;        gbc.gridwidth = GridBagConstraints.REMAINDER;        gbc.fill = GridBagConstraints.BOTH;        gb.setConstraints(this.vpwd, gbc);        add(this.vpwd);        l = new JLabel(STRINGS.getString("label.config.proxy.http") +            LEFT_PARENTHESIS + STRINGS.getString("label.optional") +            RIGHT_PARENTHESIS);                        gbc.gridx = 0;        gbc.gridy++;        gbc.gridwidth = 1;        gbc.anchor = GridBagConstraints.LINE_START;        gb.setConstraints(l, gbc);        add(l);        this.httpProxy = new JTextField(15);                this.httpProxy.setText(Constants.getInstance().get(Constants.PROXY_HTTP));        this.httpProxy.addKeyListener(new KeyAdapter() {            public void keyReleased(KeyEvent ke) {                ok.setEnabled(isValidInput());            }        });        gbc.gridx++;        gbc.gridwidth = GridBagConstraints.REMAINDER;        gbc.fill = GridBagConstraints.BOTH;        gb.setConstraints(this.httpProxy, gbc);        add(this.httpProxy);                JPanel bp = new JPanel();                bp.setLayout(new GridLayout(1, 0));                this.ok = new JButton(new AbstractAction() {            public void actionPerformed(ActionEvent ae) {                if (isValidInput()) {                    exit();                }            }        });        this.ok.setText(STRINGS.getString("action.ok"));        this.ok.addKeyListener(new AbstractButtonKeyListener(this.ok) {            public void keyReleased(KeyEvent ke) {                getButton().getAction().actionPerformed(null);            }        });        this.ok.setEnabled(isValidInput());        bp.add(this.ok);        JButton b = new JButton(new AbstractAction() {            public void actionPerformed(ActionEvent ae) {                setExit(true);                exit();            }        });        b.setText(STRINGS.getString("action.exit"));        b.addKeyListener(new AbstractButtonKeyListener(b) {            public void keyReleased(KeyEvent ke) {                getButton().getAction().actionPerformed(null);            }        });        bp.add(b);                gbc.gridx = 0;        gbc.gridy++;        gbc.gridwidth = 2;        gbc.anchor = GridBagConstraints.LAST_LINE_END;        gbc.fill = GridBagConstraints.NONE;        gb.setConstraints(bp, gbc);        add(bp);//        getRootPane().setDefaultButton(this.ok);    }    private void setExit(boolean isExit) {        this.isExit = isExit;    }    private void clear() {        this.peerName.setText(null);        this.pwd.setText(null);        this.vpwd.setText(null);        validate();    }    private void exit() {        getRootPane().getParent().setVisible(false);    }    /**     * Validate user input and display a descriptive message     * if the input is incorrect     */    private boolean isValidInput() {        String n = this.peerName.getText().trim();        String p = String.valueOf(this.pwd.getPassword()).trim();        String vp = String.valueOf(this.vpwd.getPassword()).trim();        String pr = this.httpProxy.getText().trim();        URL pu = toURL(pr);        boolean isValid = n.length() > 0 &&            p.length() > 0 &&            vp.length() > 0 &&            p.equals(vp) &&            (pr.length() == 0 ||                (pr.length() > 0 &&                    pu != null));                if (pu != null) {            this.httpProxy.setText(pu.toString());        }        if (! isValid) {            validate();        }        return isValid;    }        private URL toURL(String s) {        URL u = null;                if (s != null &&            s.trim().length() > 0) {            try {                u = new URL(s);            } catch (MalformedURLException mue) {            }                    if (u == null) {                try {                    u = new URL(Constants.PROTOCOL_HTTP + URL_DELIMITER + s);                } catch (MalformedURLException mue) {                }            }        }                if (u != null &&            u.getPort() == -1) {            u = null;        }                        return u;    }}

⌨️ 快捷键说明

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