From fade0a4d7d7f6aa18560d3b61a952851fc0b1f62 Mon Sep 17 00:00:00 2001 From: Denis Molony Date: Tue, 1 Mar 2016 18:38:20 +1100 Subject: [PATCH] allow cursor movement in DiskLayoutImage --- .../bytezone/diskbrowser/disk/AppleDisk.java | 7 ++- .../diskbrowser/disk/DiskFactory.java | 13 +++-- .../diskbrowser/gui/DiskLayoutImage.java | 38 +++++++++--- .../diskbrowser/gui/DiskLayoutPanel.java | 4 +- .../diskbrowser/gui/DiskLayoutSelection.java | 58 ++++++++++++++++++- 5 files changed, 99 insertions(+), 21 deletions(-) diff --git a/src/com/bytezone/diskbrowser/disk/AppleDisk.java b/src/com/bytezone/diskbrowser/disk/AppleDisk.java index 97df5d1..b4bcbb3 100755 --- a/src/com/bytezone/diskbrowser/disk/AppleDisk.java +++ b/src/com/bytezone/diskbrowser/disk/AppleDisk.java @@ -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); } diff --git a/src/com/bytezone/diskbrowser/disk/DiskFactory.java b/src/com/bytezone/diskbrowser/disk/DiskFactory.java index 37ab700..161e16b 100755 --- a/src/com/bytezone/diskbrowser/disk/DiskFactory.java +++ b/src/com/bytezone/diskbrowser/disk/DiskFactory.java @@ -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)) { diff --git a/src/com/bytezone/diskbrowser/gui/DiskLayoutImage.java b/src/com/bytezone/diskbrowser/gui/DiskLayoutImage.java index a04b318..9c159eb 100644 --- a/src/com/bytezone/diskbrowser/gui/DiskLayoutImage.java +++ b/src/com/bytezone/diskbrowser/gui/DiskLayoutImage.java @@ -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 diff --git a/src/com/bytezone/diskbrowser/gui/DiskLayoutPanel.java b/src/com/bytezone/diskbrowser/gui/DiskLayoutPanel.java index 0205089..5c0a848 100644 --- a/src/com/bytezone/diskbrowser/gui/DiskLayoutPanel.java +++ b/src/com/bytezone/diskbrowser/gui/DiskLayoutPanel.java @@ -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); diff --git a/src/com/bytezone/diskbrowser/gui/DiskLayoutSelection.java b/src/com/bytezone/diskbrowser/gui/DiskLayoutSelection.java index 5513ea6..5cadb7c 100755 --- a/src/com/bytezone/diskbrowser/gui/DiskLayoutSelection.java +++ b/src/com/bytezone/diskbrowser/gui/DiskLayoutSelection.java @@ -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 { private final List highlights; + private Disk currentDisk; public DiskLayoutSelection () { @@ -19,6 +21,8 @@ class DiskLayoutSelection implements Iterable 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 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 iterator () { return highlights.iterator (); @@ -83,8 +135,8 @@ class DiskLayoutSelection implements Iterable 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 { // 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;