Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Entry improvements for String CD fields #243

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 30 additions & 3 deletions src/org/openlcb/cdi/swing/CdiPanel.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.Robot;
import java.awt.Toolkit;
import java.awt.Window;
Expand Down Expand Up @@ -64,6 +65,7 @@
import javax.swing.BorderFactory;
import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.InputVerifier;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JComponent;
Expand All @@ -86,7 +88,11 @@
import javax.swing.WindowConstants;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;
import javax.swing.text.AttributeSet;
import javax.swing.text.BadLocationException;
import javax.swing.text.Document;
import javax.swing.text.JTextComponent;
import javax.swing.text.PlainDocument;

import util.CollapsiblePanel;

Expand Down Expand Up @@ -2290,6 +2296,13 @@ protected String getCurrentValue() {

}

Font textAreaFont;
{
Font existingFont = UIManager.getFont("TextArea.font");
int size = existingFont.getSize();
textAreaFont = new Font(Font.MONOSPACED, Font.PLAIN, size);
}

private class StringPane extends EntryPane {
JTextComponent textField;
private final ConfigRepresentation.StringEntry entry;
Expand All @@ -2298,20 +2311,34 @@ private class StringPane extends EntryPane {
super(e, "String");
this.entry = e;

if (entry.size <= 64) {
JTextField jtf = new JTextField(entry.size) {
Document doc = new PlainDocument(){
// limit the size of the text contents to the size of the data field
public void insertString(int offset, String str, AttributeSet a) throws BadLocationException {
if (getLength() + str.length() > entry.size-1) // -1 for trailing zero
java.awt.Toolkit.getDefaultToolkit().beep();
else
super.insertString(offset, str, a);
}
};

if (entry.size <= 64) { // somewhat arbitrary maximum length of single-line entry

JTextField jtf = new JTextField(doc, "", entry.size-1) { // -1 for trailing zero
public Dimension getMaximumSize() {
return getPreferredSize();
}
};
jtf.setFont(textAreaFont);
jtf = factory.handleStringValue(jtf);

textField = jtf;
} else {
// Long string. Show multi-line editor
JTextArea jta = new JTextArea(Math.min(40, (int)(entry.size / 60)), 80);
JTextArea jta = new JTextArea(doc, "", Math.min(40, (int)(entry.size / 40)), 80);// line count is heuristic
jta.setEditable(true);
jta.setLineWrap(true);
jta.setWrapStyleWord(true);
jta.setFont(textAreaFont);
jta = factory.handleEditorValue(jta);
textField = jta;
}
Expand Down
Loading