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

144 lines
4.9 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.diskbrowser.gui.DiskLayoutPanel.LayoutDetails;
2020-06-29 02:29:26 +00:00
import com.bytezone.diskbrowser.utilities.FontUtility;
import com.bytezone.diskbrowser.utilities.FontUtility.FontSize;
import com.bytezone.diskbrowser.utilities.FontUtility.FontType;
2015-06-01 09:35:51 +00:00
2020-02-08 22:20:08 +00:00
// -----------------------------------------------------------------------------------//
2015-06-01 09:35:51 +00:00
class ScrollRuler extends JComponent
2020-02-08 22:20:08 +00:00
// -----------------------------------------------------------------------------------//
2015-06-01 09:35:51 +00:00
{
// 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;
2020-06-29 02:29:26 +00:00
private final Font font = FontUtility.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;
2021-07-25 08:30:22 +00:00
private final DiskLayoutImage diskLayoutImage;
2015-06-01 09:35:51 +00:00
2020-02-08 22:20:08 +00:00
// ---------------------------------------------------------------------------------//
2021-07-25 08:30:22 +00:00
ScrollRuler (DiskLayoutImage diskLayoutImage, int orientation)
2020-02-08 22:20:08 +00:00
// ---------------------------------------------------------------------------------//
2015-06-01 09:35:51 +00:00
{
this.orientation = orientation;
2021-07-25 08:30:22 +00:00
this.diskLayoutImage = diskLayoutImage;
2015-06-01 09:35:51 +00:00
// 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));
}
2020-02-08 22:20:08 +00:00
// ---------------------------------------------------------------------------------//
2021-07-25 08:30:22 +00:00
public void setLayout (LayoutDetails layoutDetails)
2020-02-08 22:20:08 +00:00
// ---------------------------------------------------------------------------------//
2015-06-01 09:35:51 +00:00
{
2021-07-25 08:30:22 +00:00
this.layoutDetails = layoutDetails;
2015-06-01 09:35:51 +00:00
// Must match the preferred size of DiskLayoutImage
if (orientation == HORIZONTAL)
2021-07-25 08:30:22 +00:00
setPreferredSize (new Dimension (
layoutDetails.block.width * layoutDetails.grid.width + 1, HEIGHT));
2015-06-01 09:35:51 +00:00
else
2021-07-25 08:30:22 +00:00
setPreferredSize (new Dimension (WIDTH,
layoutDetails.block.height * layoutDetails.grid.height + 1));
2015-06-01 09:35:51 +00:00
2021-07-25 08:30:22 +00:00
setTrackMode (layoutDetails.grid.width == 16 || layoutDetails.grid.width == 13);
2015-06-01 09:35:51 +00:00
}
2020-02-08 22:20:08 +00:00
// ---------------------------------------------------------------------------------//
2016-07-18 05:02:04 +00:00
public void setTrackMode (boolean trackMode)
2020-02-08 22:20:08 +00:00
// ---------------------------------------------------------------------------------//
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 ();
}
2020-02-08 22:20:08 +00:00
// ---------------------------------------------------------------------------------//
2016-07-18 05:02:04 +00:00
public void setHex (boolean hex)
2020-02-08 22:20:08 +00:00
// ---------------------------------------------------------------------------------//
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 ();
}
2020-02-08 22:20:08 +00:00
// ---------------------------------------------------------------------------------//
2015-06-01 09:35:51 +00:00
@Override
protected void paintComponent (Graphics g)
2020-02-08 22:20:08 +00:00
// ---------------------------------------------------------------------------------//
2015-06-01 09:35:51 +00:00
{
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);
}
2020-02-08 22:20:08 +00:00
// ---------------------------------------------------------------------------------//
2015-06-01 09:35:51 +00:00
private void drawHorizontal (Graphics g, Rectangle clipRect, int width)
2020-02-08 22:20:08 +00:00
// ---------------------------------------------------------------------------------//
2015-06-01 09:35:51 +00:00
{
int start = (clipRect.x / width);
int end = start + clipRect.width / width;
2021-07-25 08:30:22 +00:00
end = Math.min (end, diskLayoutImage.getWidth () / width - 1);
2015-06-01 09:35:51 +00:00
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);
}
2020-02-08 22:20:08 +00:00
// ---------------------------------------------------------------------------------//
2015-06-01 09:35:51 +00:00
private void drawVertical (Graphics g, Rectangle clipRect, int height)
2020-02-08 22:20:08 +00:00
// ---------------------------------------------------------------------------------//
2015-06-01 09:35:51 +00:00
{
int start = (clipRect.y / height);
int end = start + clipRect.height / height;
2021-07-25 08:30:22 +00:00
end = Math.min (end, diskLayoutImage.getHeight () / height - 1);
2015-06-01 09:35:51 +00:00
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);
}
}
}