dmolony-DiskBrowser/src/com/bytezone/diskbrowser/gui/ScrollRuler.java

128 lines
3.4 KiB
Java
Raw Normal View History

2015-06-01 09:35:51 +00:00
package com.bytezone.diskbrowser.gui;
2018-07-20 09:31:44 +00:00
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Rectangle;
2015-06-01 09:35:51 +00:00
import javax.swing.JComponent;
import com.bytezone.common.Platform;
import com.bytezone.common.Platform.FontSize;
import com.bytezone.common.Platform.FontType;
import com.bytezone.diskbrowser.gui.DiskLayoutPanel.LayoutDetails;
class ScrollRuler extends JComponent
{
// dimensions of the ruler
public static final int HEIGHT = 20;
public static final int WIDTH = 40;
public static final int HORIZONTAL = 0;
public static final int VERTICAL = 1;
2016-07-29 12:28:11 +00:00
private final Font font = Platform.getFont (FontType.SANS_SERIF, FontSize.BASE);
2016-07-18 05:02:04 +00:00
private final int orientation;
private boolean isHex = true;
private boolean isTrackMode = true;
private LayoutDetails layoutDetails;
private final JComponent image;
2015-06-01 09:35:51 +00:00
public ScrollRuler (JComponent image, int orientation)
{
this.orientation = orientation;
this.image = image;
// set defaults until setLayout is called
if (orientation == HORIZONTAL)
2018-08-16 00:09:26 +00:00
setPreferredSize (new Dimension (0, HEIGHT)); // width/height
2015-06-01 09:35:51 +00:00
else
setPreferredSize (new Dimension (WIDTH, 0));
}
public void setLayout (LayoutDetails layout)
{
this.layoutDetails = layout;
// Must match the preferred size of DiskLayoutImage
if (orientation == HORIZONTAL)
2018-07-20 09:31:44 +00:00
setPreferredSize (
new Dimension (layout.block.width * layout.grid.width + 1, HEIGHT));
2015-06-01 09:35:51 +00:00
else
2018-07-20 09:31:44 +00:00
setPreferredSize (
new Dimension (WIDTH, layout.block.height * layout.grid.height + 1));
2015-06-01 09:35:51 +00:00
2016-07-18 05:02:04 +00:00
setTrackMode (layout.grid.width == 16 || layout.grid.width == 13);
2015-06-01 09:35:51 +00:00
}
2016-07-18 05:02:04 +00:00
public void setTrackMode (boolean trackMode)
2015-06-01 09:35:51 +00:00
{
2016-07-18 05:02:04 +00:00
isTrackMode = trackMode;
2015-06-01 09:35:51 +00:00
repaint ();
}
2016-07-18 05:02:04 +00:00
public void setHex (boolean hex)
2015-06-01 09:35:51 +00:00
{
2016-07-18 05:02:04 +00:00
isHex = hex;
2015-06-01 09:35:51 +00:00
repaint ();
}
@Override
protected void paintComponent (Graphics g)
{
Rectangle clipRect = g.getClipBounds ();
g.setColor (Color.WHITE);
g.fillRect (clipRect.x, clipRect.y, clipRect.width, clipRect.height);
if (layoutDetails == null)
return;
2016-07-18 05:02:04 +00:00
g.setFont (font); // how do I do this in the constructor?
2015-06-01 09:35:51 +00:00
g.setColor (Color.black);
if (orientation == HORIZONTAL)
drawHorizontal (g, clipRect, layoutDetails.block.width);
else
drawVertical (g, clipRect, layoutDetails.block.height);
}
private void drawHorizontal (Graphics g, Rectangle clipRect, int width)
{
int start = (clipRect.x / width);
int end = start + clipRect.width / width;
end = Math.min (end, image.getWidth () / width - 1);
String format;
int offset;
2018-08-16 00:09:26 +00:00
if (layoutDetails.block.width <= 16)
2015-06-01 09:35:51 +00:00
{
format = isHex ? "%1X" : "%1d";
offset = isHex ? 4 : 0;
}
else
{
format = isHex ? "%02X" : "%02d";
offset = 7;
}
for (int i = start; i <= end; i++)
g.drawString (String.format (format, i), i * width + offset, 15);
}
private void drawVertical (Graphics g, Rectangle clipRect, int height)
{
int start = (clipRect.y / height);
int end = start + clipRect.height / height;
end = Math.min (end, image.getHeight () / height - 1);
String format = isHex ? "%04X" : "%04d";
for (int i = start; i <= end; i++)
{
int value = isTrackMode ? i : i * layoutDetails.grid.width;
g.drawString (String.format (format, value), 4, i * height + 13);
}
}
}