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

339 lines
12 KiB
Java
Raw Permalink Normal View History

2020-02-12 13:30:08 +00:00
package com.bytezone.diskbrowser.gui;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Desktop;
import java.awt.EventQueue;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
2021-06-04 06:53:04 +00:00
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
2020-02-12 13:30:08 +00:00
import java.util.ArrayList;
import java.util.List;
import java.util.prefs.Preferences;
import javax.swing.AbstractAction;
import javax.swing.BorderFactory;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JToolBar;
import javax.swing.UIManager;
import com.bytezone.diskbrowser.duplicates.RootFolderData;
// -----------------------------------------------------------------------------------//
2021-06-04 06:53:04 +00:00
public class DiskBrowser extends JFrame
implements DiskSelectionListener, QuitListener, PropertyChangeListener
2020-02-12 13:30:08 +00:00
// -----------------------------------------------------------------------------------//
{
private static String[] args;
private static final String windowTitle = "Apple ][ Disk Browser";
private final Preferences prefs = Preferences.userNodeForPackage (this.getClass ());
private WindowSaver windowSaver;
private static final String OS = System.getProperty ("os.name").toLowerCase ();
private static final boolean MAC = OS.startsWith ("mac os");
private final RootFolderData rootFolderData = new RootFolderData ();
2021-05-22 10:15:20 +00:00
private final List<QuitListener> quitListeners = new ArrayList<> ();
2021-06-04 06:53:04 +00:00
private final JPanel catalogBorderPanel;
private final JPanel layoutBorderPanel;
2021-06-06 06:27:30 +00:00
private final HideCatalogAction hideCatalogAction = new HideCatalogAction ();
private final HideLayoutAction hideLayoutAction = new HideLayoutAction ();
2021-06-04 06:53:04 +00:00
2020-02-12 13:30:08 +00:00
// ---------------------------------------------------------------------------------//
public DiskBrowser ()
// ---------------------------------------------------------------------------------//
{
super (windowTitle);
// UIManager.put ("TabbedPane.foreground", Color.BLACK); // java bug fix
2021-03-03 13:19:13 +00:00
2020-02-12 13:30:08 +00:00
if (args.length > 0 && "-reset".equals (args[0]))
new WindowState (prefs).clear ();
JToolBar toolBar = new JToolBar ("Toolbar", JToolBar.HORIZONTAL);
MenuHandler menuHandler = new MenuHandler ();
setJMenuBar (menuHandler.menuBar);
setLayout (new BorderLayout ());
add (toolBar, BorderLayout.NORTH);
// add navigation buttons
RedoHandler redoHandler = new RedoHandler (getRootPane (), toolBar);
toolBar.addSeparator ();
// create and add the left-hand catalog panel
CatalogPanel catalogPanel = new CatalogPanel (redoHandler);
2021-06-04 06:53:04 +00:00
catalogBorderPanel = addPanel (catalogPanel, "Catalog", BorderLayout.WEST);
2020-02-12 13:30:08 +00:00
// create and add the centre output panel
2021-09-18 13:44:48 +00:00
OutputPanel dataPanel = new OutputPanel (menuHandler);
2020-02-12 13:30:08 +00:00
addPanel (dataPanel, "Output", BorderLayout.CENTER);
// create and add the right-hand disk layout panel
DiskLayoutPanel diskLayoutPanel = new DiskLayoutPanel ();
2021-06-04 06:53:04 +00:00
layoutBorderPanel = addPanel (diskLayoutPanel, "Disk layout", BorderLayout.EAST);
2020-02-12 13:30:08 +00:00
// create actions
DuplicateAction duplicateAction = new DuplicateAction (rootFolderData);
RootDirectoryAction rootDirectoryAction = new RootDirectoryAction ();
RefreshTreeAction refreshTreeAction = new RefreshTreeAction (catalogPanel);
2021-09-16 08:24:13 +00:00
// PreferencesAction preferencesAction = new PreferencesAction (this, prefs);
2020-02-12 13:30:08 +00:00
AbstractAction print = new PrintAction (dataPanel);
2021-09-16 08:24:13 +00:00
// AboutAction aboutAction = new AboutAction ();
// HideLayoutAction hideLayoutAction = new HideLayoutAction (this, layoutBorderPanel);
2021-06-04 06:53:04 +00:00
ShowFreeSectorsAction showFreeAction = new ShowFreeSectorsAction ();
2020-02-12 13:30:08 +00:00
CloseTabAction closeTabAction = new CloseTabAction (catalogPanel);
2021-09-16 08:24:13 +00:00
// closeTabAction.addPropertyChangeListener (catalogPanel);
2020-02-12 13:30:08 +00:00
2021-06-04 06:53:04 +00:00
hideCatalogAction.addPropertyChangeListener (this);
hideLayoutAction.addPropertyChangeListener (this);
2020-02-12 13:30:08 +00:00
// add action buttons to toolbar
toolBar.add (rootDirectoryAction);
toolBar.add (refreshTreeAction);
2021-09-16 08:24:13 +00:00
// toolBar.add (preferencesAction);
2020-02-12 13:30:08 +00:00
toolBar.add (duplicateAction);
toolBar.add (print);
2021-09-16 08:24:13 +00:00
// toolBar.add (aboutAction);
2020-02-12 13:30:08 +00:00
// set the listeners
2021-06-06 06:27:30 +00:00
rootDirectoryAction.addPropertyChangeListener (rootFolderData);
rootDirectoryAction.addPropertyChangeListener (catalogPanel);
rootDirectoryAction.addPropertyChangeListener (duplicateAction);
2021-05-22 10:15:20 +00:00
catalogPanel.addDiskSelectionListener (this, dataPanel, diskLayoutPanel, redoHandler,
menuHandler, menuHandler.saveDiskAction);
catalogPanel.addFileSelectionListener (dataPanel, diskLayoutPanel, redoHandler, menuHandler,
menuHandler.saveFileAction);
2021-05-22 10:15:20 +00:00
catalogPanel.addFileNodeSelectionListener (dataPanel, redoHandler);
diskLayoutPanel.addSectorSelectionListener (dataPanel, redoHandler, catalogPanel,
menuHandler.saveSectorsAction);
2020-02-12 13:30:08 +00:00
duplicateAction.addTableSelectionListener (catalogPanel);
2020-04-09 09:47:30 +00:00
menuHandler.scale1Item.setAction (new ScaleAction (dataPanel, 1.0, 1));
menuHandler.scale2Item.setAction (new ScaleAction (dataPanel, 1.5, 2));
menuHandler.scale3Item.setAction (new ScaleAction (dataPanel, 2.0, 3));
2020-02-12 13:30:08 +00:00
redoHandler.addRedoListener (catalogPanel);
redoHandler.addRedoListener (diskLayoutPanel);
menuHandler.fontAction.addFontChangeListener (dataPanel);
menuHandler.fontAction.addFontChangeListener (catalogPanel);
2021-09-16 08:24:13 +00:00
// menuHandler.fontAction.addFontChangeListener (diskLayoutPanel);
2020-02-12 13:30:08 +00:00
// set the MenuItem Actions
menuHandler.printItem.setAction (print);
menuHandler.refreshTreeItem.setAction (refreshTreeAction);
menuHandler.rootItem.setAction (rootDirectoryAction);
menuHandler.showCatalogItem.setAction (hideCatalogAction);
menuHandler.showLayoutItem.setAction (hideLayoutAction);
menuHandler.showFreeSectorsItem.setAction (showFreeAction);
menuHandler.duplicateItem.setAction (duplicateAction);
menuHandler.closeTabItem.setAction (closeTabAction);
2021-06-04 06:53:04 +00:00
showFreeAction.addPropertyChangeListener (diskLayoutPanel);
2021-05-22 10:15:20 +00:00
quitListeners.add (rootDirectoryAction);
quitListeners.add (menuHandler);
quitListeners.add (catalogPanel);
quitListeners.add (this);
2020-02-12 13:30:08 +00:00
if (Desktop.isDesktopSupported ())
{
Desktop desktop = Desktop.getDesktop ();
if (desktop.isSupported (Desktop.Action.APP_ABOUT))
desktop.setAboutHandler (e -> JOptionPane.showMessageDialog (null,
"Author - Denis Molony\nGitHub - https://github.com/dmolony/DiskBrowser",
"About DiskBrowser", JOptionPane.INFORMATION_MESSAGE));
// if (desktop.isSupported (Desktop.Action.APP_PREFERENCES) && false)
// desktop.setPreferencesHandler (
// e -> JOptionPane.showMessageDialog (null, "Preferences dialog"));
2020-02-12 13:30:08 +00:00
if (desktop.isSupported (Desktop.Action.APP_QUIT_HANDLER))
2021-06-01 10:21:21 +00:00
desktop.setQuitHandler ( (e, r) -> fireQuitEvent ()); // needed for cmd-Q
2021-09-16 08:24:13 +00:00
// else
2021-06-01 10:21:21 +00:00
setQuitHandler (); // needed for the close button
2020-02-12 13:30:08 +00:00
}
else
{
System.out.println ("Desktop not supported");
setQuitHandler ();
}
catalogPanel.setCloseTabAction (closeTabAction);
pack ();
// restore the menuHandler items before they are referenced
fireRestoreEvent ();
diskLayoutPanel.setFree (menuHandler.showFreeSectorsItem.isSelected ());
dataPanel.setLineWrap (menuHandler.lineWrapItem.isSelected ());
// Remove the two optional panels if they were previously hidden
if (!menuHandler.showLayoutItem.isSelected ())
2021-06-04 06:53:04 +00:00
setLayoutPanel (false);
2020-02-12 13:30:08 +00:00
if (!menuHandler.showCatalogItem.isSelected ())
2021-06-04 06:53:04 +00:00
setCatalogPanel (false);
2020-02-12 13:30:08 +00:00
menuHandler.addBasicPreferencesListener (dataPanel);
menuHandler.addAssemblerPreferencesListener (dataPanel);
2020-09-13 00:22:49 +00:00
menuHandler.addTextPreferencesListener (dataPanel);
2020-02-12 13:30:08 +00:00
// activate the highest panel now that the listeners are ready
catalogPanel.activate ();
}
// ---------------------------------------------------------------------------------//
private void setQuitHandler ()
// ---------------------------------------------------------------------------------//
{
addWindowListener (new WindowAdapter ()
{
@Override
public void windowClosing (WindowEvent e)
{
fireQuitEvent ();
}
});
}
// ---------------------------------------------------------------------------------//
private JPanel addPanel (JComponent pane, String title, String location)
// ---------------------------------------------------------------------------------//
{
JPanel panel = new JPanel (new BorderLayout ());
panel.setBackground (Color.WHITE);
panel.setBorder (BorderFactory.createTitledBorder (title));
panel.add (pane);
add (panel, location);
return panel;
}
2021-06-04 06:53:04 +00:00
// ---------------------------------------------------------------------------------//
@Override
public void propertyChange (PropertyChangeEvent evt)
// ---------------------------------------------------------------------------------//
{
if (evt.getSource () == hideCatalogAction)
setCatalogPanel ((boolean) evt.getNewValue ());
else if (evt.getSource () == hideLayoutAction)
setLayoutPanel ((boolean) evt.getNewValue ());
}
// ---------------------------------------------------------------------------------//
private void setCatalogPanel (boolean show)
// ---------------------------------------------------------------------------------//
{
if (show)
add (catalogBorderPanel, BorderLayout.WEST);
else
remove (catalogBorderPanel);
validate ();
}
// ---------------------------------------------------------------------------------//
private void setLayoutPanel (boolean show)
// ---------------------------------------------------------------------------------//
{
if (show)
add (layoutBorderPanel, BorderLayout.EAST);
else
remove (layoutBorderPanel);
validate ();
}
2020-02-12 13:30:08 +00:00
// ---------------------------------------------------------------------------------//
@Override
public void diskSelected (DiskSelectedEvent e)
// ---------------------------------------------------------------------------------//
{
setTitle (windowTitle + e.getFormattedDisk () == null ? "" : e.getFormattedDisk ().getName ());
2020-02-12 13:30:08 +00:00
}
// ---------------------------------------------------------------------------------//
@Override
public void quit (Preferences preferences)
// ---------------------------------------------------------------------------------//
{
windowSaver.saveWindow ();
}
// ---------------------------------------------------------------------------------//
@Override
public void restore (Preferences preferences)
// ---------------------------------------------------------------------------------//
{
windowSaver = new WindowSaver (prefs, this, "DiskBrowser");
windowSaver.restoreWindow ();
}
// ---------------------------------------------------------------------------------//
private static void setLookAndFeel ()
// ---------------------------------------------------------------------------------//
{
2021-09-16 08:24:13 +00:00
// FlatLightLaf.install ();
2020-02-12 13:30:08 +00:00
try
{
UIManager.setLookAndFeel (UIManager.getSystemLookAndFeelClassName ());
2021-09-16 08:24:13 +00:00
// UIManager.setLookAndFeel (new FlatLightLaf ());
2020-02-12 13:30:08 +00:00
if (MAC)
System.setProperty ("apple.laf.useScreenMenuBar", "true");
}
catch (Exception e)
{
e.printStackTrace ();
}
}
// ---------------------------------------------------------------------------------//
private void fireQuitEvent ()
// ---------------------------------------------------------------------------------//
{
for (QuitListener listener : quitListeners)
listener.quit (prefs);
System.exit (0);
}
// ---------------------------------------------------------------------------------//
private void fireRestoreEvent ()
// ---------------------------------------------------------------------------------//
{
for (QuitListener listener : quitListeners)
listener.restore (prefs);
}
2021-05-22 10:15:20 +00:00
// ---------------------------------------------------------------------------------//
public static void main (String[] args)
// ---------------------------------------------------------------------------------//
{
DiskBrowser.args = args;
EventQueue.invokeLater (new Runnable ()
{
@Override
public void run ()
{
setLookAndFeel ();
new DiskBrowser ().setVisible (true);
}
});
}
2015-06-01 09:35:51 +00:00
}