📄 groupjoinpanel.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 groups "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 group, 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: GroupJoinPanel.java,v 1.8 2006/07/17 19:21:58 nano Exp $*/package net.jxta.myjxta.ui;import net.jxta.credential.AuthenticationCredential;import net.jxta.document.StructuredDocument;import net.jxta.exception.PeerGroupException;import net.jxta.exception.ProtocolNotSupportedException;import net.jxta.impl.membership.passwd.PasswdMembershipService;import net.jxta.membership.Authenticator;import net.jxta.membership.MembershipService;import net.jxta.myjxta.util.PeerGroupUtil;import net.jxta.myjxta.util.Resources;import net.jxta.peergroup.PeerGroup;import javax.swing.*;import java.awt.*;import java.awt.event.ActionEvent;import java.awt.event.KeyAdapter;import java.awt.event.KeyEvent;import java.util.ResourceBundle;/** * * @version $Id: GroupJoinPanel.java,v 1.8 2006/07/17 19:21:58 nano Exp $ * * @author james todd [gonzo at jxta dot org] */public class GroupJoinPanel extends JPanel { private static final ResourceBundle STRINGS = Resources.getStrings(); /** The field that collects the password */ private JPasswordField pwd = null; private JButton ok = null; private PeerGroup peerGroup = null; private boolean isAuthorized = false; private boolean isCancel = false; /** * Create panel that collects the password to join the group */ public GroupJoinPanel(PeerGroup peerGroup) { super(); this.peerGroup = peerGroup; ui(); } /** * Get the password the user typed in * * @return the provided password */ public String getPassword() { String pwd = new String(this.pwd.getPassword()).trim(); return pwd.length() > 0 ? pwd : null; } public boolean isAuthorized() { return this.isAuthorized; } public boolean isCancel() { return this.isCancel; } /** * Build the ui of the panel */ private void ui() { GridBagLayout gb = new GridBagLayout(); GridBagConstraints gbc = new GridBagConstraints(); setLayout(gb); setBorder(BorderFactory.createEmptyBorder(3, 3, 3, 3)); JLabel l = new JLabel(STRINGS.getString("label.group.password")); 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.pwd = new JPasswordField(15); this.pwd.addKeyListener(new KeyAdapter() { public void keyReleased(KeyEvent ke) { ok.setEnabled(isValidInput()); } }); gbc.gridx++; gbc.gridwidth = GridBagConstraints.REMAINDER; gb.setConstraints(this.pwd, gbc); add(this.pwd); 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) {// clear();// }// });//// b.setText(STRINGS.getString("action.clear"));// b.addKeyListener(new AbstractButtonKeyListener(b) {// public void keyReleased(KeyEvent ke) {// getButton().getAction().actionPerformed(null);// }// });// // bp.add(b); JButton b = new JButton(new AbstractAction() { public void actionPerformed(ActionEvent ae) { setCancel(true); exit(); } }); b.setText(STRINGS.getString("action.cancel")); 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 setCancel(boolean isCancel) { this.isCancel = isCancel; } private void exit() { getRootPane().getParent().setVisible(false); } private boolean isValidInput() { String p = String.valueOf(this.pwd.getPassword()).trim(); boolean isValid = p.length() > 0; this.isAuthorized = isValid && authorize(p); if (! isValid) { validate(); } return this.isAuthorized; } private boolean authorize(String pwd) { boolean isAuthorized = false; try { StructuredDocument c = null; MembershipService ms = this.peerGroup.getMembershipService(); Authenticator a = ms.apply(new AuthenticationCredential(this.peerGroup, null, c)); PasswdMembershipService.PasswdAuthenticator pa = (PasswdMembershipService.PasswdAuthenticator)a; pa.setAuth1Identity(PeerGroupUtil.MEMBERSHIP_ID); pa.setAuth2_Password(pwd); if (pa.isReadyForJoin()) { ms.join(pa); isAuthorized = true; } } catch (PeerGroupException pge) {} catch (ProtocolNotSupportedException pnse) {} return isAuthorized; }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -