Skip to content
This repository has been archived by the owner on Apr 16, 2022. It is now read-only.

Commit

Permalink
Fix #128. (#132)
Browse files Browse the repository at this point in the history
o  New flavor of stackoverflow approach for number-only JSpinners
o  JSpinners don't fire focus events, so use ChangeListener instead
o  Actually ran ./gradlew test check this time (sorry)
  • Loading branch information
thedavidmccann authored and yanokwa committed Jun 15, 2017
1 parent ec92ba6 commit 7c782ba
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 41 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ public static void setBriefcaseProxyProperty(HttpHost value) {
Preference.APPLICATION_SCOPED.remove(BRIEFCASE_PROXY_PORT_PROPERTY);
} else {
Preference.APPLICATION_SCOPED.put(BriefcasePreferences.BRIEFCASE_PROXY_HOST_PROPERTY, value.getHostName());
Preference.APPLICATION_SCOPED.put(BriefcasePreferences.BRIEFCASE_PROXY_PORT_PROPERTY, "" + value.getPort());
Preference.APPLICATION_SCOPED.put(BriefcasePreferences.BRIEFCASE_PROXY_PORT_PROPERTY, new Integer(value.getPort()).toString());
}
}

Expand Down
45 changes: 10 additions & 35 deletions src/org/opendatakit/briefcase/ui/JIntegerSpinner.java
Original file line number Diff line number Diff line change
@@ -1,49 +1,24 @@
package org.opendatakit.briefcase.ui;

import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.text.DecimalFormat;

import javax.swing.JFormattedTextField;
import javax.swing.JSpinner;
import javax.swing.SpinnerNumberModel;
import javax.swing.text.AttributeSet;
import javax.swing.text.BadLocationException;
import javax.swing.text.DocumentFilter;
import javax.swing.text.JTextComponent;
import javax.swing.text.PlainDocument;
import javax.swing.text.NumberFormatter;

//Sources:
//https://stackoverflow.com/questions/16632104/jspinner-with-display-format-numbers-only-and-manual-edit
//http://stackoverflow.com/questions/20541230/allow-only-numbers-in-jtextfield
//https://stackoverflow.com/questions/6449350/make-jspinner-completely-numeric
public class JIntegerSpinner extends JSpinner {

public JIntegerSpinner(int value, int min, int max, int step) {
super (new SpinnerNumberModel(8080, 0, 65535, 1));
JTextComponent txt = ((JSpinner.DefaultEditor) this.getEditor()).getTextField();
txt.setDocument(new IntegerDocument());
}

private class IntegerDocument extends PlainDocument {
private IntegerDocumentFilter filter;

protected IntegerDocument() {
filter = new IntegerDocumentFilter();
}
@Override
public DocumentFilter getDocumentFilter() {
return filter;
}
}

private class IntegerDocumentFilter extends DocumentFilter {
@Override
public void replace(FilterBypass fb, int offset, int length, String text, AttributeSet attrs) throws BadLocationException {
Pattern regEx = Pattern.compile("\\d*");
Matcher matcher = regEx.matcher(text);
if(!matcher.matches()){
return;
}
super.replace(fb, offset, length, text, attrs);
}
JFormattedTextField txt = ((JSpinner.NumberEditor) this.getEditor()).getTextField();
NumberFormatter formatter = (NumberFormatter)txt.getFormatter();
formatter.setFormat(new DecimalFormat("#####"));
formatter.setAllowsInvalid(false);
((NumberFormatter) txt.getFormatter()).setAllowsInvalid(false);
txt.setValue(value);
}
}

17 changes: 12 additions & 5 deletions src/org/opendatakit/briefcase/ui/SettingsPanel.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
import javax.swing.JSpinner;
import javax.swing.JTextField;
import javax.swing.LayoutStyle.ComponentPlacement;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;

import org.apache.http.HttpHost;
import org.opendatakit.briefcase.model.BriefcasePreferences;
Expand Down Expand Up @@ -56,18 +58,18 @@ public SettingsPanel(MainBriefcaseWindow parentWindow) {
btnChoose = new JButton("Change...");
btnChoose.addActionListener(new FolderActionListener());

FocusListener proxyFocusListener = new ProxyFocusListener();
ProxyChangeListener proxyChangeListener = new ProxyChangeListener();

lblHost = new JLabel(MessageStrings.PROXY_HOST);
txtHost = new JTextField();
txtHost.setEnabled(false);
txtHost.setColumns(20);
txtHost.addFocusListener(proxyFocusListener);
txtHost.addFocusListener(proxyChangeListener);

lblPort = new JLabel(MessageStrings.PROXY_PORT);
spinPort = new JIntegerSpinner(8080, 0, 65535, 1);
spinPort.setEnabled(false);
spinPort.addFocusListener(proxyFocusListener);
spinPort.addChangeListener(proxyChangeListener);

lblProxy = new JLabel(MessageStrings.PROXY_TOGGLE);
chkProxy = new JCheckBox();
Expand Down Expand Up @@ -185,17 +187,22 @@ public void actionPerformed(ActionEvent e) {

}

class ProxyFocusListener implements FocusListener {
class ProxyChangeListener implements FocusListener, ChangeListener {

@Override
public void focusGained(FocusEvent e) {
public void focusGained(FocusEvent e) {
}

@Override
public void focusLost(FocusEvent e) {
updateProxySettings();
}

@Override
public void stateChanged(ChangeEvent e) {
updateProxySettings();
}

}

}
Expand Down

0 comments on commit 7c782ba

Please sign in to comment.