migliorato fontsize, da sistemare spai nelle text
This commit is contained in:
@@ -10,12 +10,10 @@ import java.util.List;
|
||||
/**
|
||||
* HexTextBoxPanel 2.0 - Bidirezionale
|
||||
*
|
||||
* Ogni textbox HEX aggiorna la casella ASCII
|
||||
* Scrivendo nella casella ASCII si aggiornano automaticamente i campi HEX
|
||||
* Ogni textbox HEX aggiorna la casella ASCII Scrivendo nella casella ASCII si
|
||||
* aggiornano automaticamente i campi HEX
|
||||
*
|
||||
* Proprietà per GUI Builder:
|
||||
* - numBytes
|
||||
* - hexFieldBackground
|
||||
* Proprietà per GUI Builder: - numBytes - hexFieldBackground
|
||||
*/
|
||||
public class HexTextBoxPanel extends JPanel {
|
||||
|
||||
@@ -28,9 +26,9 @@ public class HexTextBoxPanel extends JPanel {
|
||||
private byte[] xByte;
|
||||
|
||||
private boolean updatingFromAscii = false; // evita loop
|
||||
private int fontSize = 12;
|
||||
|
||||
// ---------------- COSTRUTTORI ----------------
|
||||
|
||||
public HexTextBoxPanel() {
|
||||
initComponent();
|
||||
}
|
||||
@@ -41,7 +39,6 @@ public class HexTextBoxPanel extends JPanel {
|
||||
}
|
||||
|
||||
// ---------------- INIZIALIZZAZIONE COMPONENTI ----------------
|
||||
|
||||
private void initComponent() {
|
||||
|
||||
removeAll();
|
||||
@@ -59,15 +56,16 @@ public class HexTextBoxPanel extends JPanel {
|
||||
}
|
||||
|
||||
hexFields.clear();
|
||||
setLayout(new FlowLayout(FlowLayout.LEFT));
|
||||
|
||||
// setLayout(new FlowLayout(FlowLayout.LEFT));
|
||||
setLayout(new BoxLayout(this, BoxLayout.X_AXIS));
|
||||
// Textbox per ogni byte
|
||||
for (int i = 0; i < numBytes; i++) {
|
||||
|
||||
JTextField tf = new JTextField(2);
|
||||
tf.setDocument(new HexDocument());
|
||||
JTextField tf = new JTextField();
|
||||
tf.setColumns(2);
|
||||
tf.setHorizontalAlignment(JTextField.CENTER);
|
||||
tf.setBackground(hexFieldBackground);
|
||||
tf.setMargin(new Insets(0, 0, 0, 0));
|
||||
tf.setText(String.format("%02X", xByte[i] & 0xFF));
|
||||
|
||||
tf.addFocusListener(new java.awt.event.FocusAdapter() {
|
||||
@@ -93,82 +91,130 @@ public class HexTextBoxPanel extends JPanel {
|
||||
|
||||
tf.getDocument().addDocumentListener(
|
||||
new SimpleDocumentListener() {
|
||||
@Override
|
||||
public void update() {
|
||||
if (!updatingFromAscii) {
|
||||
updateAsciiField();
|
||||
}
|
||||
}
|
||||
@Override
|
||||
public void update() {
|
||||
if (!updatingFromAscii) {
|
||||
updateAsciiField();
|
||||
}
|
||||
}
|
||||
}
|
||||
);
|
||||
|
||||
hexFields.add(tf);
|
||||
add(tf);
|
||||
}
|
||||
|
||||
// Textbox ASCII bidirezionale
|
||||
asciiField = new JTextField(numBytes);
|
||||
asciiField.setDocument(new FixedLengthDocument(numBytes));
|
||||
asciiField.setEditable(true);
|
||||
asciiField.setHorizontalAlignment(JTextField.CENTER);
|
||||
asciiField.setMargin(new Insets(0,0,0,0));
|
||||
|
||||
//Font mono = new Font("Monospaced", Font.PLAIN, (int) Math.round(fontSize));
|
||||
//asciiField.setFont(mono);
|
||||
|
||||
//FontMetrics fm = asciiField.getFontMetrics(mono);
|
||||
//asciiField.setPreferredSize(new Dimension(fm.charWidth('M') * numBytes, fm.getHeight()));
|
||||
|
||||
asciiField.getDocument().addDocumentListener(new SimpleDocumentListener() {
|
||||
@Override
|
||||
public void update() {
|
||||
updateHexFieldsFromAscii();
|
||||
}
|
||||
});
|
||||
|
||||
add(asciiField);
|
||||
asciiField = new JTextField();
|
||||
asciiField.setDocument(new FixedLengthDocument(numBytes));
|
||||
asciiField.setEditable(true);
|
||||
// asciiField.setHorizontalAlignment(JTextField.CENTER);
|
||||
// asciiField.setMargin(new Insets(0, 0, 0, 0));
|
||||
//
|
||||
// Font currentFont = getFont();
|
||||
// if (currentFont != null) {
|
||||
// asciiField.setFont(currentFont);
|
||||
//
|
||||
// FontMetrics fm = asciiField.getFontMetrics(currentFont);
|
||||
// asciiField.setPreferredSize(
|
||||
// new Dimension(fm.charWidth('M') * numBytes, fm.getHeight())
|
||||
// );
|
||||
// }
|
||||
//
|
||||
asciiField.getDocument().addDocumentListener(new SimpleDocumentListener() {
|
||||
@Override
|
||||
public void update() {
|
||||
updateHexFieldsFromAscii();
|
||||
}
|
||||
});
|
||||
applyCurrentFont();
|
||||
add(asciiField);
|
||||
|
||||
updateAsciiField();
|
||||
revalidate();
|
||||
repaint();
|
||||
}
|
||||
|
||||
// ---------------- FONT SIZE ----------------
|
||||
private void applyCurrentFont() {
|
||||
|
||||
if (hexFields == null) {
|
||||
return;
|
||||
}
|
||||
|
||||
Font f = getFont();
|
||||
if (f == null) {
|
||||
return;
|
||||
}
|
||||
|
||||
FontMetrics fm = getFontMetrics(f);
|
||||
|
||||
int charWidth = fm.charWidth('M');
|
||||
int height = fm.getHeight();
|
||||
|
||||
for (JTextField tf : hexFields) {
|
||||
|
||||
tf.setFont(f);
|
||||
tf.setMargin(new Insets(0, 0, 0, 0));
|
||||
|
||||
int width = charWidth * 2;
|
||||
|
||||
Dimension d = new Dimension(width, height);
|
||||
|
||||
tf.setPreferredSize(d);
|
||||
tf.setMinimumSize(d);
|
||||
tf.setMaximumSize(d);
|
||||
}
|
||||
|
||||
if (asciiField != null) {
|
||||
|
||||
asciiField.setFont(f);
|
||||
asciiField.setMargin(new Insets(0, 0, 0, 0));
|
||||
|
||||
int width = charWidth * numBytes;
|
||||
|
||||
Dimension d = new Dimension(width, height);
|
||||
|
||||
asciiField.setPreferredSize(d);
|
||||
asciiField.setMinimumSize(d);
|
||||
asciiField.setMaximumSize(d);
|
||||
}
|
||||
|
||||
revalidate();
|
||||
repaint();
|
||||
}
|
||||
|
||||
// ---------------- FONT SIZE ----------------
|
||||
// public double getFontSize() {
|
||||
// return fontSize;
|
||||
// }
|
||||
// ---------------- FONT ----------------
|
||||
@Override
|
||||
public void setFont(Font font) {
|
||||
super.setFont(font);
|
||||
|
||||
@Override
|
||||
public void setFont(Font font) {
|
||||
super.setFont(font);
|
||||
if (hexFields == null) {
|
||||
return;
|
||||
}
|
||||
|
||||
applyCurrentFont();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void updateUI() {
|
||||
super.updateUI();
|
||||
|
||||
Font font = getFont();
|
||||
if (font == null || hexFields == null) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (hexFields != null) {
|
||||
for (JTextField tf : hexFields) {
|
||||
tf.setFont(font);
|
||||
}
|
||||
}
|
||||
|
||||
if (asciiField != null) {
|
||||
asciiField.setFont(font);
|
||||
if (asciiField != null) {
|
||||
asciiField.setFont(font);
|
||||
}
|
||||
}
|
||||
}
|
||||
@Override
|
||||
public void updateUI() {
|
||||
super.updateUI();
|
||||
|
||||
Font font = getFont();
|
||||
if (font == null || hexFields == null) return;
|
||||
|
||||
for (JTextField tf : hexFields) {
|
||||
tf.setFont(font);
|
||||
}
|
||||
|
||||
if (asciiField != null) {
|
||||
asciiField.setFont(font);
|
||||
}
|
||||
}
|
||||
|
||||
// private void updateFontSize() {
|
||||
//
|
||||
@@ -191,9 +237,7 @@ public void updateUI() {
|
||||
// revalidate();
|
||||
// repaint();
|
||||
// }
|
||||
|
||||
// ---------------- AGGIORNA ASCII DAI BYTE ----------------
|
||||
|
||||
private void updateAsciiField() {
|
||||
|
||||
StringBuilder sb = new StringBuilder();
|
||||
@@ -226,7 +270,6 @@ public void updateUI() {
|
||||
}
|
||||
|
||||
// ---------------- GET / SET BYTES ----------------
|
||||
|
||||
public byte[] getBytes() {
|
||||
|
||||
byte[] data = new byte[hexFields.size()];
|
||||
@@ -248,7 +291,9 @@ public void updateUI() {
|
||||
|
||||
public void setBytes(byte[] data) {
|
||||
|
||||
if (data == null) return;
|
||||
if (data == null) {
|
||||
return;
|
||||
}
|
||||
|
||||
xByte = new byte[numBytes];
|
||||
|
||||
@@ -275,10 +320,11 @@ public void updateUI() {
|
||||
}
|
||||
|
||||
// ---------------- AGGIORNA BYTE DALL'ASCII ----------------
|
||||
|
||||
private void updateHexFieldsFromAscii() {
|
||||
|
||||
if (updatingFromAscii) return;
|
||||
if (updatingFromAscii) {
|
||||
return;
|
||||
}
|
||||
|
||||
updatingFromAscii = true;
|
||||
|
||||
@@ -300,14 +346,15 @@ public void updateUI() {
|
||||
}
|
||||
|
||||
// ---------------- PROPRIETÀ GUI BUILDER ----------------
|
||||
|
||||
public int getNumBytes() {
|
||||
return numBytes;
|
||||
}
|
||||
|
||||
public void setNumBytes(int numBytes) {
|
||||
|
||||
if (numBytes <= 0) return;
|
||||
if (numBytes <= 0) {
|
||||
return;
|
||||
}
|
||||
|
||||
this.numBytes = numBytes;
|
||||
initComponent();
|
||||
@@ -329,7 +376,6 @@ public void updateUI() {
|
||||
}
|
||||
|
||||
// ---------------- DOCUMENT ASCII LUNGHEZZA FISSA ----------------
|
||||
|
||||
private static class FixedLengthDocument extends PlainDocument {
|
||||
|
||||
private final int maxLength;
|
||||
@@ -342,7 +388,9 @@ public void updateUI() {
|
||||
public void insertString(int offs, String str, AttributeSet a)
|
||||
throws BadLocationException {
|
||||
|
||||
if (str == null) return;
|
||||
if (str == null) {
|
||||
return;
|
||||
}
|
||||
|
||||
if ((getLength() + str.length()) <= maxLength) {
|
||||
super.insertString(offs, str, a);
|
||||
@@ -351,21 +399,22 @@ public void updateUI() {
|
||||
}
|
||||
|
||||
// ---------------- DOCUMENT VALIDAZIONE HEX ----------------
|
||||
|
||||
private static class HexDocument extends PlainDocument {
|
||||
|
||||
@Override
|
||||
public void insertString(int offs, String str, AttributeSet a)
|
||||
throws BadLocationException {
|
||||
|
||||
if (str == null) return;
|
||||
if (str == null) {
|
||||
return;
|
||||
}
|
||||
|
||||
str = str.toUpperCase();
|
||||
StringBuilder filtered = new StringBuilder();
|
||||
|
||||
for (char c : str.toCharArray()) {
|
||||
if ((c >= '0' && c <= '9') ||
|
||||
(c >= 'A' && c <= 'F')) {
|
||||
if ((c >= '0' && c <= '9')
|
||||
|| (c >= 'A' && c <= 'F')) {
|
||||
filtered.append(c);
|
||||
}
|
||||
}
|
||||
@@ -381,7 +430,6 @@ public void updateUI() {
|
||||
}
|
||||
|
||||
// ---------------- DOCUMENT LISTENER SEMPLIFICATO ----------------
|
||||
|
||||
private abstract static class SimpleDocumentListener
|
||||
implements javax.swing.event.DocumentListener {
|
||||
|
||||
@@ -402,4 +450,4 @@ public void updateUI() {
|
||||
update();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user