Skip to content

Commit

Permalink
Core Editor: Dynamic line width in the hex viewer
Browse files Browse the repository at this point in the history
  • Loading branch information
ShadelessFox committed Mar 3, 2024
1 parent 0bc36a4 commit 82e75d8
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ public class HexEditor extends JComponent implements Scrollable {
public static final Color COLOR_DIVIDER_FOREGROUND = UIColor.named("HexEditor.dividerForeground");
public static final Color COLOR_DIVIDER_SELECTION_FOREGROUND = UIColor.named("HexEditor.dividerSelectionForeground");


private HexModel model;
private HexCaret caret;
private Font boldFont;
Expand Down Expand Up @@ -220,6 +219,11 @@ public int getRowCount() {
}
}

public int getPreferredRowLength(int width) {
final BorderLayout layout = (BorderLayout) getLayout();
return Math.max(1, (width - rowsPanel.getWidth() - layout.getHgap() * 2) / (mainPanel.getColumnWidth() + textPanel.getColumnWidth()));
}

@Override
public Dimension getPreferredScrollableViewportSize() {
return getPreferredSize();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,11 +42,6 @@ protected void paintComponent(Graphics g) {
final Rectangle bounds = g.getClipBounds();
final int startIndex = getClosestIndexAt(bounds.x, bounds.y);
final int endIndex = getClosestIndexAt(bounds.x + bounds.width, bounds.y + bounds.height);

if (startIndex == endIndex) {
return;
}

final Graphics2D g2 = (Graphics2D) g.create();

try {
Expand Down Expand Up @@ -153,7 +148,7 @@ public void stateChanged(ChangeEvent e) {
repaint(rect.x, rect.y, rect.width, rect.height);
}

protected int getColumnWidth() {
public int getColumnWidth() {
return editor.getColumnWidth();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ protected void doPaint(@NotNull Graphics2D g, int startIndex, int endIndex) {
}

@Override
protected int getColumnWidth() {
public int getColumnWidth() {
return editor.getMainPanel().getColumnWidth() + editor.getTextPanel().getColumnWidth();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import com.shade.platform.model.Disposable;
import com.shade.platform.model.util.BufferUtils;
import com.shade.platform.model.util.IOUtils;
import com.shade.platform.ui.UIColor;
import com.shade.platform.ui.util.UIUtils;
import com.shade.util.NotNull;
import com.shade.util.Nullable;
Expand All @@ -15,6 +16,8 @@
import javax.swing.table.AbstractTableModel;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ComponentAdapter;
import java.awt.event.ComponentEvent;
import java.io.File;
import java.io.IOException;
import java.nio.ByteBuffer;
Expand All @@ -25,7 +28,7 @@

public class BinaryViewerPanel extends JPanel implements Disposable {
private static final Inspector[] INSPECTORS = {
new NumberInspector<>("Binary", ByteBuffer::get, x -> "0b%8s".formatted(Integer.toBinaryString(x & 0xff)).replace(' ', '0'), Byte.BYTES),
new NumberInspector<>("Binary", ByteBuffer::get, x -> "%8s".formatted(Integer.toBinaryString(x & 0xff)).replace(' ', '0'), Byte.BYTES),
new NumberInspector<>("UInt8", ByteBuffer::get, x -> String.valueOf(x & 0xff), Byte.BYTES),
new NumberInspector<>("Int8", ByteBuffer::get, String::valueOf, Byte.BYTES),
new NumberInspector<>("UInt16", ByteBuffer::getShort, x -> String.valueOf(x & 0xffff), Short.BYTES),
Expand All @@ -48,12 +51,26 @@ public BinaryViewerPanel() {

final JScrollPane editorPane = UIUtils.createBorderlessScrollPane(editor);
editorPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
editorPane.addComponentListener(new ComponentAdapter() {
@Override
public void componentResized(ComponentEvent e) {
editor.setRowLength(editor.getPreferredRowLength(editorPane.getViewport().getWidth()));
}
});

final InspectorTableModel inspectorTableModel = new InspectorTableModel();
final JTable inspectorTable = new JTable(inspectorTableModel);
inspectorTable.getSelectionModel().setSelectionMode(ListSelectionModel.SINGLE_SELECTION);

final JScrollPane inspectorPane = UIUtils.createBorderlessScrollPane(inspectorTable);
inspectorTable.getColumnModel().getColumn(0).setPreferredWidth(70);
inspectorTable.getColumnModel().getColumn(0).setMaxWidth(70);

final JScrollPane inspectorPane = new JScrollPane(inspectorTable) {
@Override
public void updateUI() {
super.updateUI();
setBorder(BorderFactory.createMatteBorder(0, 0, 1, 0, UIColor.SHADOW));
}
};
inspectorPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);

final JSplitPane pane = new JSplitPane(JSplitPane.VERTICAL_SPLIT);
Expand Down

0 comments on commit 82e75d8

Please sign in to comment.