allow cursor movement in DiskLayoutImage

This commit is contained in:
Denis Molony 2016-03-01 18:38:20 +11:00
parent 691fb5c08a
commit fade0a4d7d
5 changed files with 99 additions and 21 deletions

View File

@ -97,10 +97,10 @@ public class AppleDisk implements Disk
if (debug)
System.out.printf ("Blocks : %,d%n", blocks);
int format = buffer[12] & 0xFF;
// int format = buffer[12] & 0xFF;
// if (blocks == 0 && format == 1)
{
this.blocks = diskData / 4096 * 8; // reduces blocks to a legal multiple
this.blocks = diskData / 4096 * 8; // reduces blocks to a legal multiple
if (debug)
System.out.printf ("Blocks : %,d%n", blocks);
}
@ -355,7 +355,8 @@ public class AppleDisk implements Disk
if (!isValidAddress (block))
{
System.out.println ("Invalid block : " + block);
return null;
// return null;
return new AppleDiskAddress (0, this);
}
return new AppleDiskAddress (block, this);
}

View File

@ -315,15 +315,16 @@ public class DiskFactory
}
// assumes a track is 4096 bytes
if ((file.length () % 4096) != 0)
{
if (debug)
System.out.printf ("file length not divisible by 4096 : %d%n%n", file.length ());
return null;
}
// if ((file.length () % 4096) != 0)
// {
// if (debug)
// System.out.printf ("file length not divisible by 4096 : %d%n%n", file.length ());
// return null;
// }
try
{
// truncate the file if necessary
AppleDisk disk = new AppleDisk (file, (int) file.length () / 4096, 8);
if (ProdosDisk.isCorrectFormat (disk))
{

View File

@ -2,6 +2,8 @@ package com.bytezone.diskbrowser.gui;
import java.awt.*;
import java.awt.event.InputEvent;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.List;
@ -22,16 +24,16 @@ class DiskLayoutImage extends JPanel implements Scrollable
static final Cursor crosshairCursor = new Cursor (Cursor.CROSSHAIR_CURSOR);
private FormattedDisk disk;
LayoutDetails layoutDetails;
private LayoutDetails layoutDetails;
private boolean showFreeSectors;
DiskLayoutSelection selectionHandler = new DiskLayoutSelection ();
boolean redo;
private final DiskLayoutSelection selectionHandler = new DiskLayoutSelection ();
private boolean redo;
// set defaults (used until a real disk is set)
int bw = 30;
int bh = 15;
int gw = 8;
int gh = 35;
private int bw = 30;
private int bh = 15;
private int gw = 8;
private int gh = 35;
public DiskLayoutImage ()
{
@ -39,6 +41,9 @@ class DiskLayoutImage extends JPanel implements Scrollable
addMouseListener (new MyMouseListener ());
setBackground (Color.WHITE);
setOpaque (true);
addKeyListener (new MyKeyListener ());
setFocusable (true);
}
public void setDisk (FormattedDisk disk, LayoutDetails details)
@ -235,6 +240,24 @@ class DiskLayoutImage extends JPanel implements Scrollable
listenerList.remove (SectorSelectionListener.class, listener);
}
class MyKeyListener extends KeyAdapter
{
@Override
public void keyPressed (KeyEvent e)
{
switch (e.getKeyCode ())
{
case KeyEvent.VK_LEFT:
case KeyEvent.VK_RIGHT:
case KeyEvent.VK_UP:
case KeyEvent.VK_DOWN:
selectionHandler.keyPress (e);
fireSectorSelectionEvent ();
repaint ();
}
}
}
class MyMouseListener extends MouseAdapter
{
private Cursor currentCursor;
@ -253,6 +276,7 @@ class DiskLayoutImage extends JPanel implements Scrollable
selectionHandler.doClick (disk.getDisk (), da, extend, append);
fireSectorSelectionEvent ();
repaint ();
requestFocusInWindow ();
}
@Override

View File

@ -47,8 +47,8 @@ class DiskLayoutPanel extends JPanel implements DiskSelectionListener,
horizontalRuler = new ScrollRuler (image, ScrollRuler.HORIZONTAL);
legendPanel = new DiskLegendPanel ();
this.setBackground (Color.WHITE);
this.setOpaque (true);
setBackground (Color.WHITE);
setOpaque (true);
sp = new JScrollPane (image, VERTICAL_SCROLLBAR_ALWAYS, HORIZONTAL_SCROLLBAR_ALWAYS);
sp.setBackground (Color.WHITE);

View File

@ -1,5 +1,6 @@
package com.bytezone.diskbrowser.gui;
import java.awt.event.KeyEvent;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
@ -11,6 +12,7 @@ import com.bytezone.diskbrowser.disk.DiskAddress;
class DiskLayoutSelection implements Iterable<DiskAddress>
{
private final List<DiskAddress> highlights;
private Disk currentDisk;
public DiskLayoutSelection ()
{
@ -19,6 +21,8 @@ class DiskLayoutSelection implements Iterable<DiskAddress>
public void doClick (Disk disk, DiskAddress da, boolean extend, boolean append)
{
currentDisk = disk;
/*
* Single click without modifiers - just replace previous highlights with the new
* sector. If there are no current highlights then even modifiers have the same
@ -63,6 +67,54 @@ class DiskLayoutSelection implements Iterable<DiskAddress>
Collections.sort (highlights);
}
public void keyPress (KeyEvent e)
{
if (highlights.size () == 0)
{
System.out.println ("Nothing to move");
return;
}
DiskAddress first = highlights.get (0);
DiskAddress last = highlights.get (highlights.size () - 1);
highlights.clear ();
int totalBlocks = currentDisk.getTotalBlocks ();
int rowSize = currentDisk.getTrackSize () / currentDisk.getBlockSize ();
switch (e.getKeyCode ())
{
case KeyEvent.VK_LEFT:
int block = first.getBlock () - 1;
if (block < 0)
block = totalBlocks - 1;
highlights.add (currentDisk.getDiskAddress (block));
break;
case KeyEvent.VK_RIGHT:
block = last.getBlock () + 1;
if (block >= totalBlocks)
block = 0;
highlights.add (currentDisk.getDiskAddress (block));
break;
case KeyEvent.VK_UP:
block = first.getBlock () - rowSize;
if (block < 0)
block += totalBlocks;
highlights.add (currentDisk.getDiskAddress (block));
break;
case KeyEvent.VK_DOWN:
block = last.getBlock () + rowSize;
if (block >= totalBlocks)
block -= totalBlocks;
highlights.add (currentDisk.getDiskAddress (block));
break;
}
}
@Override
public Iterator<DiskAddress> iterator ()
{
return highlights.iterator ();
@ -83,8 +135,8 @@ class DiskLayoutSelection implements Iterable<DiskAddress>
private boolean checkContiguous ()
{
int range =
highlights.get (highlights.size () - 1).getBlock () - highlights.get (0).getBlock () + 1;
int range = highlights.get (highlights.size () - 1).getBlock ()
- highlights.get (0).getBlock () + 1;
return (range == highlights.size ());
}
@ -113,7 +165,7 @@ class DiskLayoutSelection implements Iterable<DiskAddress>
{
// If we are outside the discontiguous range, just extend as usual
if (da.getBlock () < highlights.get (0).getBlock ()
|| da.getBlock () > highlights.get (highlights.size () - 1).getBlock ())
|| da.getBlock () > highlights.get (highlights.size () - 1).getBlock ())
{
extendHighlights (disk, da);
return;