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

508 lines
18 KiB
Java
Raw Normal View History

2020-02-08 10:13:51 +00:00
package com.bytezone.diskbrowser.gui;
import java.awt.Font;
import java.awt.Insets;
import java.awt.image.BufferedImage;
2021-06-02 01:48:57 +00:00
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
2020-02-08 10:13:51 +00:00
import java.util.Enumeration;
import java.util.List;
import javax.swing.AbstractButton;
import javax.swing.ButtonGroup;
import javax.swing.JCheckBoxMenuItem;
import javax.swing.JScrollPane;
import javax.swing.JTabbedPane;
import javax.swing.JTextArea;
import javax.swing.ScrollPaneConstants;
import javax.swing.SwingConstants;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
2020-12-19 08:40:59 +00:00
import com.bytezone.diskbrowser.applefile.AbstractFile;
2020-02-08 10:13:51 +00:00
import com.bytezone.diskbrowser.applefile.ApplesoftBasicProgram;
import com.bytezone.diskbrowser.applefile.AssemblerProgram;
2020-12-19 05:42:25 +00:00
import com.bytezone.diskbrowser.applefile.BasicTextFile;
import com.bytezone.diskbrowser.applefile.BootSector;
2020-02-08 10:13:51 +00:00
import com.bytezone.diskbrowser.applefile.HiResImage;
import com.bytezone.diskbrowser.applefile.Palette;
import com.bytezone.diskbrowser.applefile.PaletteFactory.CycleDirection;
import com.bytezone.diskbrowser.applefile.QuickDrawFont;
2020-05-16 07:45:58 +00:00
import com.bytezone.diskbrowser.applefile.SHRPictureFile2;
2020-02-08 10:13:51 +00:00
import com.bytezone.diskbrowser.disk.DiskAddress;
import com.bytezone.diskbrowser.disk.SectorList;
2020-06-29 02:29:26 +00:00
import com.bytezone.diskbrowser.gui.FontAction.FontChangeEvent;
import com.bytezone.diskbrowser.gui.FontAction.FontChangeListener;
import com.bytezone.diskbrowser.wizardry.MazeLevel;
2020-02-08 10:13:51 +00:00
// -----------------------------------------------------------------------------------//
2021-09-18 13:44:48 +00:00
public class OutputPanel extends JTabbedPane
2021-06-02 01:48:57 +00:00
implements DiskSelectionListener, FileSelectionListener, SectorSelectionListener,
FileNodeSelectionListener, FontChangeListener, BasicPreferencesListener,
AssemblerPreferencesListener, TextPreferencesListener, PropertyChangeListener
2020-02-08 10:13:51 +00:00
// -----------------------------------------------------------------------------------//
{
private static final int TEXT_WIDTH = 65;
2021-09-19 07:09:34 +00:00
// final MenuHandler menuHandler;
2020-02-08 10:13:51 +00:00
private final JTextArea formattedText;
private final JTextArea hexText;
private final JTextArea disassemblyText;
// these two panes are interchangeable
private final JScrollPane formattedPane;
private final JScrollPane imagePane;
2021-06-03 08:11:44 +00:00
2020-05-19 08:22:10 +00:00
private boolean imageVisible = false;
2020-02-08 10:13:51 +00:00
2021-06-03 08:11:44 +00:00
private final ImagePanel imagePanel = new ImagePanel ();
private AnimationWorker animation;
2020-02-08 10:13:51 +00:00
private boolean debugMode;
2021-06-03 08:11:44 +00:00
private DataSource currentDataSource;
2020-02-08 10:13:51 +00:00
// used to determine whether the text has been set
2021-06-03 08:11:44 +00:00
private boolean formattedTextValid;
private boolean hexTextValid;
private boolean assemblerTextValid;
2020-02-08 10:13:51 +00:00
2021-06-03 08:11:44 +00:00
private DebuggingAction debuggingAction = new DebuggingAction ();
private MonochromeAction monochromeAction = new MonochromeAction ();
private ColourQuirksAction colourQuirksAction = new ColourQuirksAction ();
private LineWrapAction lineWrapAction = new LineWrapAction ();
2021-06-02 01:48:57 +00:00
2021-06-03 08:11:44 +00:00
private enum TabType
2020-12-19 08:40:59 +00:00
{
FORMATTED, HEX, DISASSEMBLED
}
2020-02-08 10:13:51 +00:00
// ---------------------------------------------------------------------------------//
2021-09-18 13:44:48 +00:00
public OutputPanel (MenuHandler menuHandler)
2020-02-08 10:13:51 +00:00
// ---------------------------------------------------------------------------------//
{
setTabPlacement (SwingConstants.BOTTOM);
formattedText = new JTextArea (10, TEXT_WIDTH);
formattedPane = setPanel (formattedText, "Formatted");
2021-09-19 07:09:34 +00:00
// formattedText.setLineWrap (prefs.getBoolean (MenuHandler.PREFS_LINE_WRAP, true));
2020-02-08 10:13:51 +00:00
formattedText.setText ("Please use the 'File->Set HOME folder...' command to "
+ "\ntell DiskBrowser where your Apple disks are located."
+ "\n\nTo see the contents of a disk in more detail, double-click"
+ "\nthe disk. You will then be able to select individual files to " + "view them.");
2020-02-08 10:13:51 +00:00
hexText = new JTextArea (10, TEXT_WIDTH);
setPanel (hexText, "Hex dump");
disassemblyText = new JTextArea (10, TEXT_WIDTH);
setPanel (disassemblyText, "Disassembly");
imagePane = new JScrollPane (imagePanel, ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS,
ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED);
2020-02-08 10:13:51 +00:00
imagePane.setBorder (null);
imagePane.getVerticalScrollBar ().setUnitIncrement (50);
imagePane.getHorizontalScrollBar ().setUnitIncrement (25);
addChangeListener (new ChangeListener ()
{
@Override
public void stateChanged (ChangeEvent e)
{
switch (getSelectedIndex ())
{
2020-12-19 05:42:25 +00:00
case 0: // Formatted
2020-02-08 10:13:51 +00:00
if (!formattedTextValid)
{
if (currentDataSource == null)
formattedText.setText ("");
else
setText (formattedText, currentDataSource.getText ());
formattedTextValid = true;
}
break;
2021-06-03 08:11:44 +00:00
2020-12-19 05:42:25 +00:00
case 1: // Hex
2020-02-08 10:13:51 +00:00
if (!hexTextValid)
{
if (currentDataSource == null)
hexText.setText ("");
else
setText (hexText, currentDataSource.getHexDump ());
hexTextValid = true;
}
break;
2021-06-03 08:11:44 +00:00
2020-12-19 05:42:25 +00:00
case 2: // Assembler
2020-02-08 10:13:51 +00:00
if (!assemblerTextValid)
{
if (currentDataSource == null)
disassemblyText.setText ("");
else
setText (disassemblyText, currentDataSource.getAssembler ());
assemblerTextValid = true;
}
break;
2021-06-03 08:11:44 +00:00
2020-02-08 10:13:51 +00:00
default:
2021-06-03 08:11:44 +00:00
System.out.println ("Impossible - Invalid index selected in DataPanel");
2020-02-08 10:13:51 +00:00
}
}
});
menuHandler.lineWrapItem.setAction (lineWrapAction);
2021-06-03 04:52:10 +00:00
lineWrapAction.addPropertyChangeListener (this);
2020-02-08 10:13:51 +00:00
2021-06-02 01:48:57 +00:00
colourQuirksAction.addPropertyChangeListener (this);
2021-06-01 10:29:34 +00:00
menuHandler.colourQuirksItem.setAction (colourQuirksAction);
2021-06-02 01:48:57 +00:00
monochromeAction.addPropertyChangeListener (this);
2021-06-01 10:29:34 +00:00
menuHandler.monochromeItem.setAction (monochromeAction);
2021-06-01 10:21:21 +00:00
2021-06-02 01:48:57 +00:00
debuggingAction.addPropertyChangeListener (this);
2021-06-01 10:21:21 +00:00
menuHandler.debuggingItem.setAction (debuggingAction);
2020-02-08 10:13:51 +00:00
// fill in the placeholders created by the MenuHandler
List<Palette> palettes = HiResImage.getPalettes ();
ButtonGroup buttonGroup = menuHandler.paletteGroup;
Enumeration<AbstractButton> enumeration = buttonGroup.getElements ();
int ndx = 0;
while (enumeration.hasMoreElements ())
{
JCheckBoxMenuItem item = (JCheckBoxMenuItem) enumeration.nextElement ();
item.setAction (new PaletteAction (this, palettes.get (ndx++)));
}
2021-06-03 08:11:44 +00:00
2020-02-08 10:13:51 +00:00
menuHandler.nextPaletteItem.setAction (new NextPaletteAction (this, buttonGroup));
menuHandler.prevPaletteItem.setAction (new PreviousPaletteAction (this, buttonGroup));
}
// ---------------------------------------------------------------------------------//
public void selectPalette (Palette palette)
// ---------------------------------------------------------------------------------//
{
HiResImage.getPaletteFactory ().setCurrentPalette (palette);
2021-09-19 07:09:34 +00:00
if (currentDataSource instanceof HiResImage image)
2020-02-08 10:13:51 +00:00
{
image.setPalette ();
imagePanel.setImage (image.getImage ());
}
}
// ---------------------------------------------------------------------------------//
public Palette cyclePalette (CycleDirection direction)
// ---------------------------------------------------------------------------------//
{
Palette palette = HiResImage.getPaletteFactory ().cyclePalette (direction);
2021-09-19 07:09:34 +00:00
if (currentDataSource instanceof HiResImage image)
2020-02-08 10:13:51 +00:00
{
image.setPalette ();
imagePanel.setImage (image.getImage ());
}
return palette;
}
2021-06-02 01:48:57 +00:00
// ---------------------------------------------------------------------------------//
@Override
public void propertyChange (PropertyChangeEvent evt)
// ---------------------------------------------------------------------------------//
{
if (evt.getSource () == debuggingAction)
setDebug ((Boolean) evt.getNewValue ());
else if (evt.getSource () == monochromeAction)
setMonochrome ((Boolean) evt.getNewValue ());
else if (evt.getSource () == colourQuirksAction)
setColourQuirks ((Boolean) evt.getNewValue ());
2021-06-03 04:52:10 +00:00
else if (evt.getSource () == lineWrapAction)
setLineWrap ((Boolean) evt.getNewValue ());
2021-06-02 01:48:57 +00:00
}
2020-02-08 10:13:51 +00:00
// ---------------------------------------------------------------------------------//
void setLineWrap (boolean lineWrap)
// ---------------------------------------------------------------------------------//
{
formattedText.setLineWrap (lineWrap);
}
// ---------------------------------------------------------------------------------//
public void setColourQuirks (boolean value)
// ---------------------------------------------------------------------------------//
{
2021-09-19 07:09:34 +00:00
if (currentDataSource instanceof HiResImage image)
2020-02-08 10:13:51 +00:00
{
image.setColourQuirks (value);
imagePanel.setImage (image.getImage ());
}
}
// ---------------------------------------------------------------------------------//
public void setMonochrome (boolean value)
// ---------------------------------------------------------------------------------//
{
2021-09-19 07:09:34 +00:00
if (currentDataSource instanceof HiResImage image)
2020-02-08 10:13:51 +00:00
{
image.setMonochrome (value);
imagePanel.setImage (image.getImage ());
}
}
2020-04-09 09:47:30 +00:00
// ---------------------------------------------------------------------------------//
public void setScale (double scale)
// ---------------------------------------------------------------------------------//
{
imagePanel.setScale (scale);
2021-09-19 07:09:34 +00:00
if (currentDataSource instanceof HiResImage image)
2020-04-09 09:47:30 +00:00
imagePanel.setImage (image.getImage ());
}
2020-05-16 07:45:58 +00:00
// ---------------------------------------------------------------------------------//
public void update ()
// ---------------------------------------------------------------------------------//
{
2021-09-19 07:09:34 +00:00
if (currentDataSource instanceof HiResImage image)
2020-05-16 07:45:58 +00:00
imagePanel.setImage (image.getImage ());
}
2020-02-08 10:13:51 +00:00
// ---------------------------------------------------------------------------------//
public void setDebug (boolean value)
// ---------------------------------------------------------------------------------//
{
debugMode = value;
2020-12-19 08:40:59 +00:00
AbstractFile.setDebug (value);
setText (formattedText, currentDataSource.getText ());
2020-12-20 01:34:42 +00:00
if (currentDataSource instanceof HiResImage //
|| currentDataSource instanceof MazeLevel // Wizardry
2020-02-08 10:13:51 +00:00
|| currentDataSource instanceof QuickDrawFont)
setDataSource (currentDataSource); // toggles text/image
2020-02-08 10:13:51 +00:00
}
// ---------------------------------------------------------------------------------//
private void setTabsFont (Font font)
// ---------------------------------------------------------------------------------//
{
formattedText.setFont (font);
hexText.setFont (font);
disassemblyText.setFont (font);
imagePane.getVerticalScrollBar ().setUnitIncrement (font.getSize ());
}
// ---------------------------------------------------------------------------------//
public String getCurrentText ()
// ---------------------------------------------------------------------------------//
{
int index = getSelectedIndex ();
2020-12-19 05:42:25 +00:00
return index == 0 ? formattedText.getText ()
: index == 1 ? hexText.getText () : disassemblyText.getText ();
2020-02-08 10:13:51 +00:00
}
// ---------------------------------------------------------------------------------//
private JScrollPane setPanel (JTextArea outputPanel, String tabName)
// ---------------------------------------------------------------------------------//
{
outputPanel.setEditable (false);
outputPanel.setMargin (new Insets (5, 5, 5, 5));
JScrollPane outputScrollPane =
new JScrollPane (outputPanel, ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS,
ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED);
2021-06-03 08:11:44 +00:00
2020-02-08 10:13:51 +00:00
outputScrollPane.setBorder (null); // remove the ugly default border
add (outputScrollPane, tabName);
return outputScrollPane;
}
// ---------------------------------------------------------------------------------//
private void setDataSource (DataSource dataSource)
// ---------------------------------------------------------------------------------//
{
currentDataSource = dataSource;
if (dataSource == null)
{
formattedText.setText ("");
hexText.setText ("");
disassemblyText.setText ("");
removeImage ();
return;
}
2020-12-19 08:40:59 +00:00
switch (TabType.values ()[getSelectedIndex ()])
2020-02-08 10:13:51 +00:00
{
2020-12-19 08:40:59 +00:00
case FORMATTED:
2020-02-08 10:13:51 +00:00
try
{
setText (formattedText, dataSource.getText ());
}
catch (Exception e)
{
setText (formattedText, e.toString ());
e.printStackTrace ();
}
hexTextValid = false;
assemblerTextValid = false;
break;
2020-12-19 08:40:59 +00:00
case HEX:
2020-02-08 10:13:51 +00:00
setText (hexText, dataSource.getHexDump ());
formattedTextValid = false;
assemblerTextValid = false;
break;
2020-12-19 08:40:59 +00:00
case DISASSEMBLED:
2020-02-08 10:13:51 +00:00
setText (disassemblyText, dataSource.getAssembler ());
hexTextValid = false;
formattedTextValid = false;
break;
2020-12-20 01:34:42 +00:00
default:
System.out.println ("Unexpected Tab #" + getSelectedIndex ());
2020-02-08 10:13:51 +00:00
}
BufferedImage image = dataSource.getImage ();
if (image == null || debugMode)
removeImage ();
else
{
2021-09-28 09:17:10 +00:00
if (dataSource instanceof HiResImage hri)
2020-02-08 10:13:51 +00:00
{
2021-09-28 09:17:10 +00:00
hri.checkPalette ();
2020-02-08 10:13:51 +00:00
image = dataSource.getImage ();
2020-05-16 07:45:58 +00:00
if (((HiResImage) dataSource).isAnimation ())
{
if (animation != null)
animation.cancel ();
animation = new AnimationWorker (this, (SHRPictureFile2) dataSource);
2020-05-17 07:30:31 +00:00
animation.execute ();
2020-05-16 07:45:58 +00:00
}
2020-02-08 10:13:51 +00:00
}
2020-05-16 07:45:58 +00:00
2020-02-08 10:13:51 +00:00
imagePanel.setImage (image);
imagePane.setViewportView (imagePanel);
if (!imageVisible)
{
int selected = getSelectedIndex ();
remove (formattedPane);
add (imagePane, "Formatted", 0);
setSelectedIndex (selected);
imageVisible = true;
2020-05-16 07:45:58 +00:00
2020-02-08 10:13:51 +00:00
}
}
}
// ---------------------------------------------------------------------------------//
private void removeImage ()
// ---------------------------------------------------------------------------------//
{
if (imageVisible)
{
int selected = getSelectedIndex ();
remove (imagePane);
add (formattedPane, "Formatted", 0);
setSelectedIndex (selected);
imageVisible = false;
}
2020-05-16 07:45:58 +00:00
if (animation != null)
{
animation.cancel ();
animation = null;
}
2020-02-08 10:13:51 +00:00
}
// ---------------------------------------------------------------------------------//
private void setText (JTextArea textArea, String text)
// ---------------------------------------------------------------------------------//
{
textArea.setText (text);
textArea.setCaretPosition (0);
}
// ---------------------------------------------------------------------------------//
@Override
public void diskSelected (DiskSelectedEvent event)
// ---------------------------------------------------------------------------------//
{
setSelectedIndex (0);
setDataSource (null);
2021-06-04 06:53:04 +00:00
2020-02-08 10:13:51 +00:00
if (event.getFormattedDisk () != null)
setDataSource (event.getFormattedDisk ().getCatalog ().getDataSource ());
else
System.out.println ("bollocks in diskSelected()");
}
// ---------------------------------------------------------------------------------//
@Override
public void fileSelected (FileSelectedEvent event)
// ---------------------------------------------------------------------------------//
{
DataSource dataSource = event.appleFileSource.getDataSource ();
setDataSource (dataSource);
}
// ---------------------------------------------------------------------------------//
@Override
public void sectorSelected (SectorSelectedEvent event)
// ---------------------------------------------------------------------------------//
{
List<DiskAddress> sectors = event.getSectors ();
if (sectors == null || sectors.size () == 0)
return;
if (sectors.size () == 1)
{
DiskAddress da = sectors.get (0);
if (da != null)
setDataSource (event.getFormattedDisk ().getFormattedSector (da));
}
else
setDataSource (new SectorList (event.getFormattedDisk (), sectors));
}
// ---------------------------------------------------------------------------------//
@Override
public void fileNodeSelected (FileNodeSelectedEvent event)
// ---------------------------------------------------------------------------------//
{
setSelectedIndex (0);
setDataSource (event.getFileNode ());
}
// ---------------------------------------------------------------------------------//
@Override
public void changeFont (FontChangeEvent fontChangeEvent)
// ---------------------------------------------------------------------------------//
{
setTabsFont (fontChangeEvent.font);
}
// ---------------------------------------------------------------------------------//
@Override
public void setBasicPreferences (BasicPreferences basicPreferences)
// ---------------------------------------------------------------------------------//
{
if (currentDataSource instanceof ApplesoftBasicProgram)
setDataSource (currentDataSource);
}
// ---------------------------------------------------------------------------------//
@Override
public void setAssemblerPreferences (AssemblerPreferences assemblerPreferences)
// ---------------------------------------------------------------------------------//
{
if (currentDataSource instanceof AssemblerProgram || currentDataSource instanceof BootSector)
2020-02-08 10:13:51 +00:00
setDataSource (currentDataSource);
}
2020-05-16 07:45:58 +00:00
2020-09-13 00:22:49 +00:00
// ---------------------------------------------------------------------------------//
@Override
public void setTextPreferences (TextPreferences textPreferences)
// ---------------------------------------------------------------------------------//
{
2020-09-14 09:51:14 +00:00
if (currentDataSource instanceof BasicTextFile)
2020-09-13 00:22:49 +00:00
setDataSource (currentDataSource);
}
2015-06-01 09:35:51 +00:00
}