From: Mathieu Baudier Date: Mon, 22 Oct 2012 09:40:12 +0000 (+0000) Subject: Move SwingUserInfo to more generic package X-Git-Tag: argeo-slc-2.1.7~600 X-Git-Url: http://git.argeo.org/?a=commitdiff_plain;h=fb98a0c9bad30d10970a059c65e3dec4eb03cc4b;p=gpl%2Fargeo-slc.git Move SwingUserInfo to more generic package git-svn-id: https://svn.argeo.org/slc/trunk@5627 4cfe0d0a-d680-48aa-b62c-e0a02a3f76cc --- diff --git a/runtime/org.argeo.slc.support.simple/src/main/java/org/argeo/slc/jsch/SwingUserInfo.java b/runtime/org.argeo.slc.support.simple/src/main/java/org/argeo/slc/jsch/SwingUserInfo.java new file mode 100644 index 000000000..a10dcefcb --- /dev/null +++ b/runtime/org.argeo.slc.support.simple/src/main/java/org/argeo/slc/jsch/SwingUserInfo.java @@ -0,0 +1,155 @@ +/* + * Copyright (C) 2007-2012 Mathieu Baudier + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.argeo.slc.jsch; + +import java.awt.Container; +import java.awt.GridLayout; +import java.awt.Panel; +import java.awt.event.ActionEvent; +import java.awt.event.ActionListener; +import java.util.Arrays; + +import javax.swing.JButton; +import javax.swing.JDialog; +import javax.swing.JFrame; +import javax.swing.JLabel; +import javax.swing.JPanel; +import javax.swing.JPasswordField; + + +public class SwingUserInfo extends SimpleUserInfo { + + private Boolean alwaysPrompt = false; + + public boolean promptPassphrase(String message) { + if (passphrase != null) + return true; + + if (!alwaysPrompt && passphraseSafe != null) + return true; + + PasswordDialog dialog = new PasswordDialog(message) { + private static final long serialVersionUID = 3266299327166418364L; + + @Override + protected void useCredentials(char[] password) { + passphraseSafe = new char[password.length]; + System.arraycopy(password, 0, passphraseSafe, 0, + password.length); + // passphraseSafe = Arrays.copyOf(password, password.length); + } + }; + dialog.setVisible(true); + return dialog.getWasProvided(); + } + + public boolean promptPassword(String message) { + if (password != null) + return true; + + if (!alwaysPrompt && passwordSafe != null) + return true; + + PasswordDialog dialog = new PasswordDialog(message) { + private static final long serialVersionUID = 3266299327166418364L; + + @Override + protected void useCredentials(char[] password) { + // passwordSafe = Arrays.copyOf(password, password.length); + passwordSafe = new char[password.length]; + System.arraycopy(password, 0, passwordSafe, 0, password.length); + } + }; + dialog.setVisible(true); + return dialog.getWasProvided(); + } + + public void setAlwaysPrompt(Boolean alwaysPrompt) { + this.alwaysPrompt = alwaysPrompt; + } + + protected static class PasswordDialog extends JDialog implements + ActionListener { + private static final long serialVersionUID = 3399155607980846207L; + + private static final String OK = "ok"; + + private JPasswordField password = new JPasswordField("", 10); + + private JButton okButton; + private JButton cancelButton; + + private Boolean wasProvided = false; + + public PasswordDialog(String title) { + setTitle(title); + setModal(true); + setLocationRelativeTo(null); + setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); + + JPanel p1 = new JPanel(new GridLayout(1, 2, 3, 3)); + p1.add(new JLabel("Password")); + password.setActionCommand(OK); + password.addActionListener(this); + p1.add(password); + add("Center", p1); + + Panel p2 = new Panel(); + okButton = addButton(p2, "OK"); + okButton.setActionCommand(OK); + cancelButton = addButton(p2, "Cancel"); + add("South", p2); + setSize(240, 120); + + pack(); + } + + /** To be overridden */ + protected void useCredentials(char[] password) { + // does nothing + } + + private JButton addButton(Container c, String name) { + JButton button = new JButton(name); + button.addActionListener(this); + c.add(button); + return button; + } + + public final void actionPerformed(ActionEvent evt) { + Object source = evt.getSource(); + if (source == okButton || evt.getActionCommand().equals(OK)) { + char[] p = password.getPassword(); + useCredentials(p); + wasProvided = true; + Arrays.fill(p, '0'); + cleanUp(); + } else if (source == cancelButton) + cleanUp(); + } + + private void cleanUp() { + password.setText(""); + dispose(); + } + + public Boolean getWasProvided() { + return wasProvided; + } + + } + +} diff --git a/runtime/org.argeo.slc.support.simple/src/main/java/org/argeo/slc/jsch/swing/SwingUserInfo.java b/runtime/org.argeo.slc.support.simple/src/main/java/org/argeo/slc/jsch/swing/SwingUserInfo.java deleted file mode 100644 index 7e02f5bdb..000000000 --- a/runtime/org.argeo.slc.support.simple/src/main/java/org/argeo/slc/jsch/swing/SwingUserInfo.java +++ /dev/null @@ -1,156 +0,0 @@ -/* - * Copyright (C) 2007-2012 Mathieu Baudier - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.argeo.slc.jsch.swing; - -import java.awt.Container; -import java.awt.GridLayout; -import java.awt.Panel; -import java.awt.event.ActionEvent; -import java.awt.event.ActionListener; -import java.util.Arrays; - -import javax.swing.JButton; -import javax.swing.JDialog; -import javax.swing.JFrame; -import javax.swing.JLabel; -import javax.swing.JPanel; -import javax.swing.JPasswordField; - -import org.argeo.slc.jsch.SimpleUserInfo; - -public class SwingUserInfo extends SimpleUserInfo { - - private Boolean alwaysPrompt = false; - - public boolean promptPassphrase(String message) { - if (passphrase != null) - return true; - - if (!alwaysPrompt && passphraseSafe != null) - return true; - - PasswordDialog dialog = new PasswordDialog(message) { - private static final long serialVersionUID = 3266299327166418364L; - - @Override - protected void useCredentials(char[] password) { - passphraseSafe = new char[password.length]; - System.arraycopy(password, 0, passphraseSafe, 0, - password.length); - // passphraseSafe = Arrays.copyOf(password, password.length); - } - }; - dialog.setVisible(true); - return dialog.getWasProvided(); - } - - public boolean promptPassword(String message) { - if (password != null) - return true; - - if (!alwaysPrompt && passwordSafe != null) - return true; - - PasswordDialog dialog = new PasswordDialog(message) { - private static final long serialVersionUID = 3266299327166418364L; - - @Override - protected void useCredentials(char[] password) { - // passwordSafe = Arrays.copyOf(password, password.length); - passwordSafe = new char[password.length]; - System.arraycopy(password, 0, passwordSafe, 0, password.length); - } - }; - dialog.setVisible(true); - return dialog.getWasProvided(); - } - - public void setAlwaysPrompt(Boolean alwaysPrompt) { - this.alwaysPrompt = alwaysPrompt; - } - - protected static class PasswordDialog extends JDialog implements - ActionListener { - private static final long serialVersionUID = 3399155607980846207L; - - private static final String OK = "ok"; - - private JPasswordField password = new JPasswordField("", 10); - - private JButton okButton; - private JButton cancelButton; - - private Boolean wasProvided = false; - - public PasswordDialog(String title) { - setTitle(title); - setModal(true); - setLocationRelativeTo(null); - setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); - - JPanel p1 = new JPanel(new GridLayout(1, 2, 3, 3)); - p1.add(new JLabel("Password")); - password.setActionCommand(OK); - password.addActionListener(this); - p1.add(password); - add("Center", p1); - - Panel p2 = new Panel(); - okButton = addButton(p2, "OK"); - okButton.setActionCommand(OK); - cancelButton = addButton(p2, "Cancel"); - add("South", p2); - setSize(240, 120); - - pack(); - } - - /** To be overridden */ - protected void useCredentials(char[] password) { - // does nothing - } - - private JButton addButton(Container c, String name) { - JButton button = new JButton(name); - button.addActionListener(this); - c.add(button); - return button; - } - - public final void actionPerformed(ActionEvent evt) { - Object source = evt.getSource(); - if (source == okButton || evt.getActionCommand().equals(OK)) { - char[] p = password.getPassword(); - useCredentials(p); - wasProvided = true; - Arrays.fill(p, '0'); - cleanUp(); - } else if (source == cancelButton) - cleanUp(); - } - - private void cleanUp() { - password.setText(""); - dispose(); - } - - public Boolean getWasProvided() { - return wasProvided; - } - - } - -}