settaggio font e qualche miglioreria, da sistemare spai nelle text

This commit is contained in:
2026-03-03 15:59:16 +01:00
parent 2c4b58ef5b
commit 95b71342f7
10 changed files with 2232 additions and 78 deletions

73
build.xml Normal file
View File

@@ -0,0 +1,73 @@
<?xml version="1.0" encoding="UTF-8"?>
<!-- You may freely edit this file. See commented blocks below for -->
<!-- some examples of how to customize the build. -->
<!-- (If you delete it and reopen the project it will be recreated.) -->
<!-- By default, only the Clean and Build commands use this build script. -->
<!-- Commands such as Run, Debug, and Test only use this build script if -->
<!-- the Compile on Save feature is turned off for the project. -->
<!-- You can turn off the Compile on Save (or Deploy on Save) setting -->
<!-- in the project's Project Properties dialog box.-->
<project name="hexeditorcomponent" default="default" basedir=".">
<description>Builds, tests, and runs the project hexeditorcomponent.</description>
<import file="nbproject/build-impl.xml"/>
<!--
There exist several targets which are by default empty and which can be
used for execution of your tasks. These targets are usually executed
before and after some main targets. They are:
-pre-init: called before initialization of project properties
-post-init: called after initialization of project properties
-pre-compile: called before javac compilation
-post-compile: called after javac compilation
-pre-compile-single: called before javac compilation of single file
-post-compile-single: called after javac compilation of single file
-pre-compile-test: called before javac compilation of JUnit tests
-post-compile-test: called after javac compilation of JUnit tests
-pre-compile-test-single: called before javac compilation of single JUnit test
-post-compile-test-single: called after javac compilation of single JUunit test
-pre-jar: called before JAR building
-post-jar: called after JAR building
-post-clean: called after cleaning build products
(Targets beginning with '-' are not intended to be called on their own.)
Example of inserting an obfuscator after compilation could look like this:
<target name="-post-compile">
<obfuscate>
<fileset dir="${build.classes.dir}"/>
</obfuscate>
</target>
For list of available properties check the imported
nbproject/build-impl.xml file.
Another way to customize the build is by overriding existing main targets.
The targets of interest are:
-init-macrodef-javac: defines macro for javac compilation
-init-macrodef-junit: defines macro for junit execution
-init-macrodef-debug: defines macro for class debugging
-init-macrodef-java: defines macro for class execution
-do-jar: JAR building
run: execution of project
-javadoc-build: Javadoc generation
test-report: JUnit report generation
An example of overriding the target for project execution could look like this:
<target name="run" depends="hexeditorcomponent-impl.jar">
<exec dir="bin" executable="launcher.exe">
<arg file="${dist.jar}"/>
</exec>
</target>
Notice that the overridden target depends on the jar target and not only on
the compile target as the regular run target does. Again, for a list of available
properties which you can use, check the target you are overriding in the
nbproject/build-impl.xml file.
-->
</project>

3
manifest.mf Normal file
View File

@@ -0,0 +1,3 @@
Manifest-Version: 1.0
X-COMMENT: Main-Class will be added automatically by build

1771
nbproject/build-impl.xml Normal file

File diff suppressed because it is too large Load Diff

View File

@@ -0,0 +1,8 @@
build.xml.data.CRC32=bf95bee6
build.xml.script.CRC32=b5c25125
build.xml.stylesheet.CRC32=f85dc8f2@1.117.0.48
# This file is used by a NetBeans-based IDE to track changes in generated files such as build-impl.xml.
# Do not edit this file. You may delete it but then the IDE will never regenerate such files for you.
nbproject/build-impl.xml.data.CRC32=bf95bee6
nbproject/build-impl.xml.script.CRC32=2e76ecbc
nbproject/build-impl.xml.stylesheet.CRC32=12e0a6c2@1.117.0.48

View File

@@ -0,0 +1,95 @@
annotation.processing.enabled=true
annotation.processing.enabled.in.editor=false
annotation.processing.processor.options=
annotation.processing.processors.list=
annotation.processing.run.all.processors=true
annotation.processing.source.output=${build.generated.sources.dir}/ap-source-output
build.classes.dir=${build.dir}/classes
build.classes.excludes=**/*.java,**/*.form
# This directory is removed when the project is cleaned:
build.dir=build
build.generated.dir=${build.dir}/generated
build.generated.sources.dir=${build.dir}/generated-sources
# Only compile against the classpath explicitly listed here:
build.sysclasspath=ignore
build.test.classes.dir=${build.dir}/test/classes
build.test.results.dir=${build.dir}/test/results
# Uncomment to specify the preferred debugger connection transport:
#debug.transport=dt_socket
debug.classpath=\
${run.classpath}
debug.modulepath=\
${run.modulepath}
debug.test.classpath=\
${run.test.classpath}
debug.test.modulepath=\
${run.test.modulepath}
# Files in build.classes.dir which should be excluded from distribution jar
dist.archive.excludes=
# This directory is removed when the project is cleaned:
dist.dir=dist
dist.jar=${dist.dir}/hexeditorcomponent.jar
dist.javadoc.dir=${dist.dir}/javadoc
dist.jlink.dir=${dist.dir}/jlink
dist.jlink.output=${dist.jlink.dir}/hexeditorcomponent
excludes=
includes=**
jar.compress=false
javac.classpath=
# Space-separated list of extra javac options
javac.compilerargs=
javac.deprecation=false
javac.external.vm=true
javac.modulepath=
javac.processormodulepath=
javac.processorpath=\
${javac.classpath}
javac.source=21
javac.target=21
javac.test.classpath=\
${javac.classpath}:\
${build.classes.dir}
javac.test.modulepath=\
${javac.modulepath}
javac.test.processorpath=\
${javac.test.classpath}
javadoc.additionalparam=
javadoc.author=false
javadoc.encoding=${source.encoding}
javadoc.html5=false
javadoc.noindex=false
javadoc.nonavbar=false
javadoc.notree=false
javadoc.private=false
javadoc.splitindex=true
javadoc.use=true
javadoc.version=false
javadoc.windowtitle=
# The jlink additional root modules to resolve
jlink.additionalmodules=
# The jlink additional command line parameters
jlink.additionalparam=
jlink.launcher=true
jlink.launcher.name=hexeditorcomponent
main.class=hexeditorcomponent.Hexeditorcomponent
manifest.file=manifest.mf
meta.inf.dir=${src.dir}/META-INF
mkdist.disabled=false
platform.active=default_platform
run.classpath=\
${javac.classpath}:\
${build.classes.dir}
# Space-separated list of JVM arguments used when running the project.
# You may also define separate properties like run-sys-prop.name=value instead of -Dname=value.
# To set system properties for unit tests define test-sys-prop.name=value:
run.jvmargs=
run.modulepath=\
${javac.modulepath}
run.test.classpath=\
${javac.test.classpath}:\
${build.test.classes.dir}
run.test.modulepath=\
${javac.test.modulepath}
source.encoding=UTF-8
src.dir=src
test.src.dir=test

15
nbproject/project.xml Normal file
View File

@@ -0,0 +1,15 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://www.netbeans.org/ns/project/1">
<type>org.netbeans.modules.java.j2seproject</type>
<configuration>
<data xmlns="http://www.netbeans.org/ns/j2se-project/3">
<name>hexeditorcomponent</name>
<source-roots>
<root id="src.dir"/>
</source-roots>
<test-roots>
<root id="test.src.dir"/>
</test-roots>
</data>
</configuration>
</project>

View File

@@ -22,12 +22,15 @@ public class HexTextBoxPanel extends JPanel {
private List<JTextField> hexFields = new ArrayList<>();
private JTextField asciiField;
// private double fontSize = 12f; // dimensione di default
private int numBytes = 8;
private Color hexFieldBackground = Color.WHITE;
private byte[] xByte;
private boolean updatingFromAscii = false; // evita loop
// ---------------- COSTRUTTORE ----------------
// ---------------- COSTRUTTORI ----------------
public HexTextBoxPanel() {
initComponent();
}
@@ -38,135 +41,253 @@ public class HexTextBoxPanel extends JPanel {
}
// ---------------- INIZIALIZZAZIONE COMPONENTI ----------------
private void initComponent() {
removeAll();
hexFields.clear();
private void initComponent() {
removeAll();
if (xByte == null) {
xByte = new byte[numBytes];
} else if (xByte.length != numBytes) {
byte[] tmp = new byte[numBytes];
System.arraycopy(
xByte, 0,
tmp, 0,
Math.min(xByte.length, numBytes)
);
xByte = tmp;
}
hexFields.clear();
setLayout(new FlowLayout(FlowLayout.LEFT));
// Textbox per ogni byte
for (int i = 0; i < numBytes; i++) {
JTextField tf = new JTextField(2);
tf.setDocument(new HexDocument());
tf.setHorizontalAlignment(JTextField.CENTER);
tf.setBackground(hexFieldBackground);
tf.setText(String.format("%02X", xByte[i] & 0xFF));
tf.setText("00");
tf.addFocusListener(new java.awt.event.FocusAdapter() {
@Override
public void focusLost(java.awt.event.FocusEvent e) {
String text = tf.getText();
if (text.length() == 1) {
tf.setText("0" + text.toUpperCase());
}
if (text.length() < 1) {
tf.setText("00");
}
}
@Override
public void focusGained (java.awt.event.FocusEvent e) {
public void focusGained(java.awt.event.FocusEvent e) {
tf.selectAll();
}
});
tf.getDocument().addDocumentListener(new SimpleDocumentListener() {
@Override
public void update() {
if (!updatingFromAscii) {
updateAsciiField();
tf.getDocument().addDocumentListener(
new SimpleDocumentListener() {
@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.setFont(new Font("Monospaced", Font.PLAIN, 12));
asciiField.setEditable(true);
asciiField.setDocument(new FixedLengthDocument(numBytes));
asciiField.setEditable(true);
asciiField.setHorizontalAlignment(JTextField.CENTER);
asciiField.setMargin(new Insets(0,0,0,0));
asciiField.getDocument().addDocumentListener(new SimpleDocumentListener() {
@Override
public void update() {
updateHexFieldsFromAscii();
}
});
//Font mono = new Font("Monospaced", Font.PLAIN, (int) Math.round(fontSize));
//asciiField.setFont(mono);
add(asciiField);
//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);
updateAsciiField();
revalidate();
repaint();
}
// ---------------- AGGIORNA ASCII DAI BYTE ----------------
private void updateAsciiField() {
StringBuilder sb = new StringBuilder();
// ---------------- FONT SIZE ----------------
for (JTextField tf : hexFields) {
String hex = tf.getText();
// public double getFontSize() {
// return fontSize;
// }
// ---------------- FONT ----------------
if (hex.length() == 2) {
try {
int val = Integer.parseInt(hex, 16);
@Override
public void setFont(Font font) {
super.setFont(font);
// ASCII stampabile
if (val >= 32 && val <= 126) {
sb.append((char) val);
} else {
sb.append('.');
}
} catch (NumberFormatException ignored) {
sb.append('.');
}
} else {
sb.append('.');
if (hexFields != null) {
for (JTextField tf : hexFields) {
tf.setFont(font);
}
}
updatingFromAscii = true;
asciiField.setText(sb.toString());
updatingFromAscii = false;
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() {
//
// Font newFont = new Font(
// "Monospaced",
// Font.PLAIN,
// (int) Math.round(fontSize)
// );
//
// for (JTextField tf : hexFields) {
// tf.setFont(newFont);
// tf.setPreferredSize(null); // importantissimo
// }
//
// if (asciiField != null) {
// asciiField.setFont(newFont);
// asciiField.setPreferredSize(null);
// }
//
// revalidate();
// repaint();
// }
// ---------------- AGGIORNA ASCII DAI BYTE ----------------
private void updateAsciiField() {
StringBuilder sb = new StringBuilder();
for (JTextField tf : hexFields) {
String hex = tf.getText();
if (hex.length() == 2) {
try {
int val = Integer.parseInt(hex, 16);
if (val >= 32 && val <= 126) {
sb.append((char) val);
} else {
sb.append('.');
}
} catch (NumberFormatException ignored) {
sb.append('.');
}
} else {
sb.append('.');
}
}
updatingFromAscii = true;
asciiField.setText(sb.toString());
updatingFromAscii = false;
}
// ---------------- GET / SET BYTES ----------------
public byte[] getBytes() {
byte[] data = new byte[hexFields.size()];
for (int i = 0; i < hexFields.size(); i++) {
JTextField tf = hexFields.get(i);
String hex = tf.getText();
try {
data[i] = (byte) Integer.parseInt(hex, 16);
} catch (NumberFormatException e) {
data[i] = 0; // se il campo non è valido, restituisci 0
data[i] = 0;
}
}
return data;
}
public void setBytes(byte[] data) {
int len = Math.min(data.length, hexFields.size());
for (int i = 0; i < len; i++) {
hexFields.get(i).setText(String.format("%02X", data[i]));
}
}
public void clear(){
public void setBytes(byte[] data) {
if (data == null) return;
xByte = new byte[numBytes];
System.arraycopy(
data, 0,
xByte, 0,
Math.min(data.length, numBytes)
);
for (int i = 0; i < hexFields.size(); i++) {
hexFields.get(i).setText("00");
hexFields.get(i)
.setText(String.format("%02X", xByte[i] & 0xFF));
}
updateAsciiField();
}
public void clear() {
for (int i = 0; i < hexFields.size(); i++) {
hexFields.get(i)
.setText(String.format("%02X", xByte[i]));
}
}
// ---------------- AGGIORNA BYTE DALL'ASCII ----------------
private void updateHexFieldsFromAscii() {
if (updatingFromAscii) return;
updatingFromAscii = true;
String text = asciiField.getText();
for (int i = 0; i < hexFields.size(); i++) {
JTextField tf = hexFields.get(i);
if (i < text.length()) {
int val = (int) text.charAt(i);
tf.setText(String.format("%02X", val));
@@ -174,16 +295,20 @@ public class HexTextBoxPanel extends JPanel {
tf.setText("00");
}
}
updatingFromAscii = false;
}
// ---------------- PROPRIETÀ GUI BUILDER ----------------
public int getNumBytes() {
return numBytes;
}
public void setNumBytes(int numBytes) {
if (numBytes <= 0) return;
this.numBytes = numBytes;
initComponent();
}
@@ -193,58 +318,88 @@ public class HexTextBoxPanel extends JPanel {
}
public void setHexFieldBackground(Color hexFieldBackground) {
this.hexFieldBackground = hexFieldBackground;
for (JTextField tf : hexFields) {
tf.setBackground(hexFieldBackground);
}
repaint();
}
private static class FixedLengthDocument extends PlainDocument {
private final int maxLength;
// ---------------- DOCUMENT ASCII LUNGHEZZA FISSA ----------------
public FixedLengthDocument(int maxLength) {
this.maxLength = maxLength;
}
private static class FixedLengthDocument extends PlainDocument {
@Override
public void insertString(int offs, String str, AttributeSet a)
throws BadLocationException {
private final int maxLength;
if (str == null) return;
public FixedLengthDocument(int maxLength) {
this.maxLength = maxLength;
}
if ((getLength() + str.length()) <= maxLength) {
super.insertString(offs, str, a);
@Override
public void insertString(int offs, String str, AttributeSet a)
throws BadLocationException {
if (str == null) return;
if ((getLength() + str.length()) <= maxLength) {
super.insertString(offs, str, a);
}
}
}
}
// ---------------- DOCUMENT PER VALIDAZIONE HEX ----------------
// ---------------- DOCUMENT VALIDAZIONE HEX ----------------
private static class HexDocument extends PlainDocument {
@Override
public void insertString(int offs, String str, AttributeSet a) throws BadLocationException {
public void insertString(int offs, String str, AttributeSet a)
throws BadLocationException {
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);
}
}
if ((getLength() + filtered.length()) <= 2) {
super.insertString(offs, filtered.toString(), a);
super.insertString(
offs,
filtered.toString(),
a
);
}
}
}
// ---------------- DOCUMENT LISTENER SEMPLIFICATO ----------------
private abstract static class SimpleDocumentListener implements javax.swing.event.DocumentListener {
public abstract void update();
@Override public void insertUpdate(javax.swing.event.DocumentEvent e) { update(); }
@Override public void removeUpdate(javax.swing.event.DocumentEvent e) { update(); }
@Override public void changedUpdate(javax.swing.event.DocumentEvent e) { update(); }
}
private abstract static class SimpleDocumentListener
implements javax.swing.event.DocumentListener {
public abstract void update();
@Override
public void insertUpdate(javax.swing.event.DocumentEvent e) {
update();
}
@Override
public void removeUpdate(javax.swing.event.DocumentEvent e) {
update();
}
@Override
public void changedUpdate(javax.swing.event.DocumentEvent e) {
update();
}
}
}

View File

@@ -0,0 +1,34 @@
/*
* Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
* Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java to edit this template
*/
package hexeditorcomponent;
/**
*
* @author mstc
*/
import java.awt.Image;
import static java.beans.BeanInfo.ICON_COLOR_16x16;
import static java.beans.BeanInfo.ICON_COLOR_32x32;
import java.beans.SimpleBeanInfo;
import javax.swing.ImageIcon;
public class HexTextBoxPanelBeanInfo extends SimpleBeanInfo {
@Override
public Image getIcon(int iconKind) {
switch (iconKind) {
case ICON_COLOR_16x16:
return new ImageIcon(
getClass().getResource("hex16.png")
).getImage();
case ICON_COLOR_32x32:
return new ImageIcon(
getClass().getResource("hex32.png")
).getImage();
default:
return null;
}
}
}

Binary file not shown.

After

Width:  |  Height:  |  Size: 400 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 713 B