diff options
author | Andrew Su <[email protected]> | 2010-12-21 16:48:12 -0500 |
---|---|---|
committer | Andrew Su <[email protected]> | 2010-12-21 16:48:12 -0500 |
commit | 93b8643ec02f2f7d3da57f784241da3cfec98afb (patch) | |
tree | dea2ce9eb13a5da490639291fa44846a2e9b1e7a /netx | |
parent | 4a5fd97006874988975bf3e6843335ce7c279878 (diff) |
Remove need for MiddleClickListener.java.
Diffstat (limited to 'netx')
4 files changed, 118 insertions, 150 deletions
diff --git a/netx/net/sourceforge/jnlp/controlpanel/AdvancedProxySettingsPane.java b/netx/net/sourceforge/jnlp/controlpanel/AdvancedProxySettingsPane.java index 0017dd1..f808b8c 100644 --- a/netx/net/sourceforge/jnlp/controlpanel/AdvancedProxySettingsPane.java +++ b/netx/net/sourceforge/jnlp/controlpanel/AdvancedProxySettingsPane.java @@ -100,9 +100,7 @@ public class AdvancedProxySettingsPane extends JPanel { } /** - * Add the components to the panel. FIXME: We should probably refactor the - * listeners and do them in a more generic way. Also can change this from - * using textfield to using a table. + * Add the components to the panel. */ private void addComponents() { JPanel topPanel = new JPanel(new GridBagLayout()); @@ -119,77 +117,29 @@ public class AdvancedProxySettingsPane extends JPanel { JLabel http = new JLabel(Translator.R("APSLabelHTTP") + ":"); final JTextField httpAddressField = new JTextField(fields[0]); final JTextField httpPortField = new JTextField(fields[1]); - httpAddressField.addKeyListener(new KeyAdapter() { - @Override - public void keyReleased(KeyEvent e) { - fields[0] = httpAddressField.getText(); - } - }); - httpPortField.addKeyListener(new KeyAdapter() { - @Override - public void keyReleased(KeyEvent e) { - fields[1] = httpPortField.getText(); - } - }); - httpAddressField.addMouseListener(new MiddleClickListener(this.config, properties[0])); - httpPortField.addMouseListener(new MiddleClickListener(this.config, properties[1])); + httpAddressField.getDocument().addDocumentListener(new DocumentAdapter(fields, 0)); + httpPortField.getDocument().addDocumentListener(new DocumentAdapter(fields, 1)); // This addresses the HTTPS proxy settings. JLabel secure = new JLabel(Translator.R("APSLabelSecure") + ":"); final JTextField secureAddressField = new JTextField(fields[2]); final JTextField securePortField = new JTextField(fields[3]); - secureAddressField.addKeyListener(new KeyAdapter() { - @Override - public void keyReleased(KeyEvent e) { - fields[2] = secureAddressField.getText(); - } - }); - securePortField.addKeyListener(new KeyAdapter() { - @Override - public void keyReleased(KeyEvent e) { - fields[3] = securePortField.getText(); - } - }); - secureAddressField.addMouseListener(new MiddleClickListener(this.config, properties[2])); - securePortField.addMouseListener(new MiddleClickListener(this.config, properties[3])); + secureAddressField.getDocument().addDocumentListener(new DocumentAdapter(fields, 2)); + securePortField.getDocument().addDocumentListener(new DocumentAdapter(fields, 3)); // This addresses the FTP proxy settings. JLabel ftp = new JLabel(Translator.R("APSLabelFTP") + ":"); final JTextField ftpAddressField = new JTextField(fields[4]); final JTextField ftpPortField = new JTextField(fields[5]); - ftpAddressField.addKeyListener(new KeyAdapter() { - @Override - public void keyReleased(KeyEvent e) { - fields[4] = ftpAddressField.getText(); - } - }); - ftpPortField.addKeyListener(new KeyAdapter() { - @Override - public void keyReleased(KeyEvent e) { - fields[5] = ftpPortField.getText(); - } - }); - ftpAddressField.addMouseListener(new MiddleClickListener(this.config, properties[4])); - ftpPortField.addMouseListener(new MiddleClickListener(this.config, properties[5])); + ftpAddressField.getDocument().addDocumentListener(new DocumentAdapter(fields, 4)); + ftpPortField.getDocument().addDocumentListener(new DocumentAdapter(fields, 5)); // This addresses the Socks proxy settings. JLabel socks = new JLabel(Translator.R("APSLabelSocks") + ":"); final JTextField socksAddressField = new JTextField(fields[6]); final JTextField socksPortField = new JTextField(fields[7]); - socksAddressField.addKeyListener(new KeyAdapter() { - @Override - public void keyReleased(KeyEvent e) { - fields[6] = socksAddressField.getText(); - } - }); - socksPortField.addKeyListener(new KeyAdapter() { - @Override - public void keyReleased(KeyEvent e) { - fields[7] = socksPortField.getText(); - } - }); - socksAddressField.addMouseListener(new MiddleClickListener(this.config, properties[6])); - socksPortField.addMouseListener(new MiddleClickListener(this.config, properties[7])); + socksAddressField.getDocument().addDocumentListener(new DocumentAdapter(fields, 6)); + socksPortField.getDocument().addDocumentListener(new DocumentAdapter(fields, 7)); JCheckBox sameProxyForAll = new JCheckBox(Translator.R("APSSameProxyForAllProtocols"), Boolean.parseBoolean(fields[8])); sameProxyForAll.addItemListener(new ItemListener() { @@ -235,13 +185,8 @@ public class AdvancedProxySettingsPane extends JPanel { final JTextArea exceptionListArea = new JTextArea(); exceptionListArea.setLineWrap(true); exceptionListArea.setText(fields[9]); - exceptionListArea.addKeyListener(new KeyAdapter() { - @Override - public void keyReleased(KeyEvent e) { - fields[9] = exceptionListArea.getText(); - } - }); - exceptionListArea.addMouseListener(new MiddleClickListener(this.config, fields[9])); + exceptionListArea.getDocument().addDocumentListener(new DocumentAdapter(fields, 9)); + JLabel exceptionFormat = new JLabel(Translator.R("APSExceptionInstruction")); JScrollPane exceptionScroll = new JScrollPane(exceptionListArea, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, JScrollPane.HORIZONTAL_SCROLLBAR_NEVER); exceptions.add(exceptionDescription, BorderLayout.NORTH); diff --git a/netx/net/sourceforge/jnlp/controlpanel/DocumentAdapter.java b/netx/net/sourceforge/jnlp/controlpanel/DocumentAdapter.java new file mode 100644 index 0000000..2bcb0c2 --- /dev/null +++ b/netx/net/sourceforge/jnlp/controlpanel/DocumentAdapter.java @@ -0,0 +1,102 @@ +/* DocumentAdapter.java -- Updates properties. +Copyright (C) 2010 Red Hat + +This program is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2 of the License, or +(at your option) any later version. + +This program is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with this program; if not, write to the Free Software +Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + */ +package net.sourceforge.jnlp.controlpanel; + +import javax.swing.event.DocumentEvent; +import javax.swing.event.DocumentListener; +import javax.swing.text.BadLocationException; +import javax.swing.text.Document; + +import net.sourceforge.jnlp.config.DeploymentConfiguration; + +/** + * Updates the property as it happens. + * + * @author Andrew Su ([email protected], [email protected]) + * + */ +public class DocumentAdapter implements DocumentListener { + + String[] fields; + int index; + String property; + DeploymentConfiguration config; + int mode; + + /** + * This creates a new instance of DocumentAdapter. + * + * @param fields The list of property. + * @param index Location of property to modify. + */ + public DocumentAdapter(String[] fields, int index) { + this.fields = fields; + this.index = index; + mode = 1; + } + + /** + * This creates a new instance of DocumentAdapter. This allows modifying + * the configuration directly. + * + * @param config ConfigurationFile containing the properties. + * @param property Name of property to modify. + */ + public DocumentAdapter(DeploymentConfiguration config, String property) { + this.property = property; + this.config = config; + mode = 2; + } + + @Override + public void insertUpdate(DocumentEvent e) { + update(e); + } + + @Override + public void removeUpdate(DocumentEvent e) { + update(e); + } + + @Override + public void changedUpdate(DocumentEvent e) { + + } + + /** + * Update the property as on the appropriate items. + * + * @param e The event that caused the call. + */ + private void update(DocumentEvent e) { + Document d = e.getDocument(); + try { + String value = d.getText(0, d.getLength()).trim(); + value = (value.length() == 0) ? null : value; + if (mode == 1) { + fields[index] = value; + } else if (mode == 2) { + config.setProperty(property, value); + } + } catch (BadLocationException e1) { + // TODO Auto-generated catch block + e1.printStackTrace(); + } + } + +} diff --git a/netx/net/sourceforge/jnlp/controlpanel/MiddleClickListener.java b/netx/net/sourceforge/jnlp/controlpanel/MiddleClickListener.java deleted file mode 100644 index 7a92830..0000000 --- a/netx/net/sourceforge/jnlp/controlpanel/MiddleClickListener.java +++ /dev/null @@ -1,64 +0,0 @@ -/* MiddleClickListener.java -- Update configuration when pasting with middle click. -Copyright (C) 2010 Red Hat - -This program is free software; you can redistribute it and/or modify -it under the terms of the GNU General Public License as published by -the Free Software Foundation; either version 2 of the License, or -(at your option) any later version. - -This program is distributed in the hope that it will be useful, but -WITHOUT ANY WARRANTY; without even the implied warranty of -MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU -General Public License for more details. - -You should have received a copy of the GNU General Public License -along with this program; if not, write to the Free Software -Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA - */ -package net.sourceforge.jnlp.controlpanel; - -import java.awt.event.MouseAdapter; -import java.awt.event.MouseEvent; - -import javax.swing.JTextArea; -import javax.swing.JTextField; - -import net.sourceforge.jnlp.config.DeploymentConfiguration; - -/** - * When middle click pastes to the textboxes it doesn't register it... This is - * to fix that problem. Not needed in Windows. - * - * @author Andrew Su <[email protected], [email protected]> - * - */ -class MiddleClickListener extends MouseAdapter { - - DeploymentConfiguration config; - private String property; - - /** - * Creates a new instance of middle-click listener. - * - * @param config - * Loaded DeploymentConfiguration file. - * @param property - * the property in configuration file to edit. - */ - public MiddleClickListener(DeploymentConfiguration config, String property) { - this.config = config; - this.property = property; - } - - @Override - public void mouseClicked(MouseEvent e) { - Object obj = e.getSource(); - String result = null; - if (obj instanceof JTextField) - result = ((JTextField) obj).getText(); - else if (obj instanceof JTextArea) - result = ((JTextArea) obj).getText(); - - config.setProperty(property, result); - } -} diff --git a/netx/net/sourceforge/jnlp/controlpanel/NetworkSettingsPanel.java b/netx/net/sourceforge/jnlp/controlpanel/NetworkSettingsPanel.java index f49c672..cb41425 100644 --- a/netx/net/sourceforge/jnlp/controlpanel/NetworkSettingsPanel.java +++ b/netx/net/sourceforge/jnlp/controlpanel/NetworkSettingsPanel.java @@ -114,19 +114,11 @@ public class NetworkSettingsPanel extends JPanel implements ActionListener { JLabel addressLabel = new JLabel(Translator.R("NSAddress") + ":"); JLabel portLabel = new JLabel(Translator.R("NSPort") + ":"); final JTextField addressField = new JTextField(config.getProperty(properties[1]), 10); - addressField.addKeyListener(new KeyAdapter() { - public void keyReleased(KeyEvent e) { - config.setProperty(properties[1], addressField.getText()); - } - }); - - addressField.addMouseListener(new MiddleClickListener(this.config, properties[1])); + addressField.getDocument().addDocumentListener(new DocumentAdapter(config, properties[1])); + final JTextField portField = new JTextField(config.getProperty(properties[2]), 3); - portField.addKeyListener(new KeyAdapter() { - public void keyReleased(KeyEvent e) { - config.setProperty(properties[2], portField.getText()); - } - }); + portField.getDocument().addDocumentListener(new DocumentAdapter(config, properties[1])); + // Create the button which allows setting of other types of proxy. JButton advancedProxyButton = new JButton(Translator.R("NSAdvanced") + "..."); advancedProxyButton.addActionListener(new ActionListener() { @@ -187,14 +179,7 @@ public class NetworkSettingsPanel extends JPanel implements ActionListener { JPanel proxyAutoPanel = new JPanel(new FlowLayout(FlowLayout.LEADING)); JLabel locationLabel = new JLabel(Translator.R("NSScriptLocation") + ":"); final JTextField locationField = new JTextField(config.getProperty(properties[4]), 20); - locationField.addKeyListener(new KeyAdapter() { - public void keyReleased(KeyEvent e) { - String value = locationField.getText(); - if (value.trim().equals("")) - value = null; - config.setProperty(properties[4], value); - } - }); + locationField.getDocument().addDocumentListener(new DocumentAdapter(config, properties[4])); proxyAutoPanel.add(Box.createRigidArea(new Dimension(13, 0))); proxyAutoPanel.add(locationLabel); |