Skip to content

Commit

Permalink
Merge pull request #11 from Mingun/use-jhexview
Browse files Browse the repository at this point in the history
Replace HEX viewer library by JHexView
  • Loading branch information
GreyCat authored Feb 23, 2020
2 parents 59d4ae4 + d09b578 commit 42f5c97
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 47 deletions.
6 changes: 2 additions & 4 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,12 @@ jobs:
runs-on: ubuntu-latest
strategy:
matrix:
java: [ '7.x', '8.x', '9.x', '10.x', '11.x', '12.x', '13.x' ]
java: [ '8.x', '9.x', '10.x', '11.x', '12.x', '13.x' ]
steps:
- uses: actions/checkout@v1
- name: Set up JDK ${{ matrix.java }}
uses: actions/setup-java@v1
with:
java-version: ${{ matrix.java }}
- name: Build with Maven
run: |
mvn install:install-file -Dfile=lib/HexLib.jar -DgroupId=at.HexLib -DartifactId=HexLib -Dversion=0.0.0 -Dpackaging=jar -DlocalRepositoryPath=./lib
mvn -B package --file pom.xml
run: mvn -B package --file pom.xml
22 changes: 2 additions & 20 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,30 +28,12 @@ access to it in a nice, easy-to-comprehend API.
## Build
Install java, maven and, if on windows, git-bash

First, install dependencies into local maven repository:

```bash
mvn install:install-file -Dfile=lib/HexLib.jar -DgroupId=at.HexLib -DartifactId=HexLib -Dversion=0.0.0 -Dpackaging=jar -DlocalRepositoryPath=./lib
```

Then build project as usual:
Run in console:

```bash
mvn install
```

If you got error:
```bash
[WARNING] The POM for kaitai_struct_visualizer_java:HexLib:jar:0.0.0 is missing, no dependency information available
```
it means, that you already tried to build project without success and maven cache
unsuccessful state of dependency resolution. Just add switch `-U` to maven invocation
to force maven re-check dependencies:

```bash
$ mvn -U install
```

## Licensing

This GUI vis tool project itself is copyright (C) 2016-2019 Kaitai
Expand All @@ -77,4 +59,4 @@ Vis tool depends on the following libraries:
* [kaitai_struct_compiler](https://github.com/kaitai_struct_compiler) — GPLv3+ license
* [fastparse](http://www.lihaoyi.com/fastparse/) — MIT license
* [snakeyaml](https://bitbucket.org/asomov/snakeyaml) — Apache 2.0 license
* [HexLib](http://hexedit-lib.sourceforge.net/)FreeBSD license
* [JHexView](https://github.com/Mingun/JHexView)LGPL-2.1 license
16 changes: 9 additions & 7 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,9 @@
<version>0.8</version>
</dependency>
<dependency>
<groupId>at.HexLib</groupId>
<artifactId>HexLib</artifactId>
<version>0.0.0</version>
<groupId>ru.mingun</groupId>
<artifactId>JHexView</artifactId>
<version>2.1</version>
</dependency>
<dependency>
<groupId>com.github.olivergondza</groupId>
Expand All @@ -63,8 +63,8 @@
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.1</version>
<configuration>
<source>1.7</source>
<target>1.7</target>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
<plugin>
Expand Down Expand Up @@ -112,8 +112,10 @@
</snapshots>
</repository>
<repository>
<id>local-jars</id>
<url>file://${project.basedir}/lib</url>
<!--JHexView-->
<id>bintray-mingun-maven</id>
<name>bintray</name>
<url>https://dl.bintray.com/mingun/maven</url>
</repository>
</repositories>

Expand Down
60 changes: 44 additions & 16 deletions src/main/java/io/kaitai/struct/visualizer/VisualizerPanel.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package io.kaitai.struct.visualizer;

import java.awt.Point;
import java.awt.Color;
import java.awt.Font;
import java.io.IOException;
import java.lang.reflect.Constructor;
import java.lang.reflect.Method;
Expand All @@ -9,9 +10,6 @@
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import at.HexLib.library.HexLib;
import at.HexLib.library.HexLibSelectionModel;

import io.kaitai.struct.ByteBufferKaitaiStream;
import io.kaitai.struct.CompileLog;
import io.kaitai.struct.KaitaiStream;
Expand All @@ -37,6 +35,9 @@
import javax.swing.JSplitPane;
import javax.swing.JTree;

import tv.porst.jhexview.JHexView;
import tv.porst.jhexview.SimpleDataProvider;

public class VisualizerPanel extends JPanel {
private static final String DEST_PACKAGE = "io.kaitai.struct.visualized";
/**
Expand All @@ -51,9 +52,16 @@ public class VisualizerPanel extends JPanel {
/** Regexp, used to get parameter names from the generated source. */
private static final Pattern PARAMETER_NAME = Pattern.compile(", \\S+ ([^,\\s]+)");

/** Color of hex editor section headers. */
private static final Color HEADER = new Color(0x0000c0);
/** Color of hex data in HEX and ASCII sections. */
private static final Color UNMODIFIED = Color.BLACK;
/** Background color selected hex data in HEX and ASCII sections. */
private static final Color SELECTION = new Color(0xc0c0c0);

private final JTree tree = new JTree();
private final DefaultTreeModel model = new DefaultTreeModel(null);
private final HexLib hexEditor = new HexLib(new byte[0]);
private final JHexView hexEditor = new JHexView();
private final JSplitPane splitPane;

private KaitaiStruct struct;
Expand All @@ -62,6 +70,24 @@ public VisualizerPanel() throws IOException {
super();
JScrollPane treeScroll = new JScrollPane(tree);

hexEditor.setSeparatorsVisible(false);
hexEditor.setBytesPerColumn(1);
hexEditor.setColumnSpacing(8);
hexEditor.setHeaderFontStyle(Font.BOLD);

hexEditor.setFontColorHeader(HEADER);
hexEditor.setFontColorOffsetView(HEADER);

hexEditor.setFontColorHexView1(UNMODIFIED);
hexEditor.setFontColorHexView2(UNMODIFIED);
hexEditor.setFontColorAsciiView(UNMODIFIED);

hexEditor.setSelectionColor(SELECTION);

hexEditor.setBackgroundColorOffsetView(hexEditor.getBackground());
hexEditor.setBackgroundColorHexView(hexEditor.getBackground());
hexEditor.setBackgroundColorAsciiView(hexEditor.getBackground());

splitPane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, treeScroll, hexEditor);

tree.setShowsRootHandles(true);
Expand All @@ -79,7 +105,8 @@ public void loadAll(String dataFileName, String ksyFileName) throws Exception {
private void loadStruct() throws IOException {
struct._io().seek(0);
byte[] buf = struct._io().readBytesFull();
hexEditor.setByteContent(buf);
hexEditor.setData(new SimpleDataProvider(buf));
hexEditor.setDefinitionStatus(JHexView.DefinitionStatus.DEFINED);

final DataNode root = new DataNode(0, struct, "[root]");
model.setRoot(root);
Expand Down Expand Up @@ -195,16 +222,17 @@ public void treeWillCollapse(TreeExpansionEvent event) throws ExpandVetoExceptio

@Override
public void valueChanged(TreeSelectionEvent event) {
TreePath path = event.getPath();
if (path.getLastPathComponent() instanceof DataNode) {
DataNode node = (DataNode) path.getLastPathComponent();
if (node.posStart() == null || node.posEnd() == null)
return;
HexLibSelectionModel select = hexEditor.getSelectionModel();
ArrayList<Point> intervals = new ArrayList<>();
intervals.add(new Point(node.posStart(), node.posEnd()));
select.setSelectionIntervals(intervals);
System.out.println(node.posStart() + " - " + node.posEnd());
hexEditor.getSelectionModel().clearSelection();
for (final TreePath path : tree.getSelectionPaths()) {
final Object selected = path.getLastPathComponent();
if (!(selected instanceof DataNode)) continue;

final DataNode node = (DataNode)selected;
final Integer start = node.posStart();
final Integer end = node.posEnd();
if (start == null || end == null) continue;
// Selection in nibbles, so multiply by 2
hexEditor.getSelectionModel().addSelectionInterval(2*start, 2*end-1);
}
}
}
Expand Down

0 comments on commit 42f5c97

Please sign in to comment.