This repository contains MadHelix itself, along with compiled libraries with its dependencies.

[[ 🗃 ^ZEkyo madhelix ]] :: [📥 Inbox] [📤 Outbox] [🐤 Followers] [🤝 Collaborators] [🛠 Commits]

Clone

HTTPS: git clone https://vervis.peers.community/repos/ZEkyo

SSH: git clone USERNAME@vervis.peers.community:ZEkyo

Branches

Tags

master :: src / org / ultrasonicmadness / madhelix / dialogs /

HistoryDialog.java

/*
 * MadHelix is a Java Swing-based GUI frontend for SoundHelix.
 * Copyright (C) 2018 UltrasonicMadness
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License version 3 only,
 * as published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

package org.ultrasonicmadness.madhelix.dialogs;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;

import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;

import java.awt.event.ActionEvent;
import java.awt.event.KeyEvent;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;

import javax.swing.AbstractAction;
import javax.swing.KeyStroke;
import javax.swing.ListSelectionModel;

import org.json.simple.JSONArray;
import org.json.simple.parser.ParseException;

import org.ultrasonicmadness.madhelix.MadHelix;
import org.ultrasonicmadness.madhelix.models.HistoryTableModel;
import org.ultrasonicmadness.madhelix.utils.PathUtils;

public class HistoryDialog extends JDialog
{
    private final String HISTORY_PATH = PathUtils.getHistoryPath();
    
    private MadHelix mainWindow = null;
    private JScrollPane historyScrollPane = null;
    private JPanel mainPanel = new JPanel();
    private JTable historyTable = null;
    private HistoryTableModel tableModel = null;
    private JPanel buttonPanel = new JPanel();
    
    public HistoryDialog(MadHelix mainWindow)
    {
        super(mainWindow, false);
        
        this.mainWindow = mainWindow;
        
        this.setTitle("MadHelix song history");
        addHistory();
        setUpButtonPanel();
    }
    
    private void addHistory()
    {
        try
        {
            tableModel = new HistoryTableModel();
            historyTable = new JTable(tableModel);
            
            historyTable.getInputMap(JTable.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT)
                    .put(KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, 0),
                    "history-play");
            
            historyTable.getActionMap().put("history-play", new AbstractAction()
            {
                @Override
                public void actionPerformed(ActionEvent ev)
                {
                    playSong();
                }
            });
            
            historyTable.addMouseListener(new MouseListener()
            {
                @Override
                public void mouseClicked(MouseEvent ev)
                {
                    if (ev.getButton() == MouseEvent.BUTTON1 &&
                            ev.getClickCount() == 2)
                    {
                        playSong();
                    }
                }
                
                @Override
                public void mouseEntered(MouseEvent ev) {}
                
                @Override
                public void mouseExited(MouseEvent ev) {}
                
                @Override
                public void mousePressed(MouseEvent ev) {}
                
                @Override
                public void mouseReleased(MouseEvent ev) {}
            });
            
            historyTable.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
            historyTable.setAutoResizeMode(JTable.AUTO_RESIZE_LAST_COLUMN);
            
            historyTable.getColumn("Song name").setPreferredWidth(96);
            historyTable.getColumn("Style").setPreferredWidth(96);
            historyTable.getColumn("Length").setPreferredWidth(8);
            historyTable.getColumn("Time played").setPreferredWidth(96);
            
            historyScrollPane = new JScrollPane(historyTable);
            
            mainPanel.setLayout(new GridBagLayout());
            
            GridBagConstraints constraints = new GridBagConstraints();
            constraints.fill = GridBagConstraints.BOTH;
            
            constraints.gridy = 0;
            constraints.weightx = 1;
            constraints.weighty = 1;
            mainPanel.add(historyScrollPane, constraints);
            
            constraints.gridy = 1;
            constraints.weightx = 0;
            constraints.weighty = 0;
            mainPanel.add(buttonPanel, constraints);
            
            this.add(mainPanel);
            this.setMinimumSize(new Dimension(240, 180));
            this.setSize(480, 240);
            this.setLocationRelativeTo(null);
        }
        catch (IOException x)
        {
            JOptionPane.showMessageDialog(this,
                x.getMessage(),
                "I/O Error opening history.json, disabling logging of songs.",
                JOptionPane.ERROR_MESSAGE
            );
            
            mainWindow.setLoggingSongs(false);
        }
        catch (NullPointerException | ParseException x)
        {
            int replaceHistoryFile = JOptionPane.showConfirmDialog(this,
                "Malformed history.json file, do you wish to overwrite it? " +
                "Choosing no will disable the logging of songs.",
                "I/O error",
                JOptionPane.YES_NO_OPTION
            );
            
            if (replaceHistoryFile == JOptionPane.YES_OPTION)
            {
                try
                {
                    FileWriter writer = new FileWriter(HISTORY_PATH);
                    JSONArray historyJson = new JSONArray();
                    
                    historyJson.writeJSONString(writer);
                    writer.close();
                    
                    addHistory();
                }
                catch (IOException ex)
                {
                    JOptionPane.showMessageDialog(this,
                        "Could not overwrite malformed history.json, " +
                        "disabling song logging.",
                        "I/O error",
                        JOptionPane.ERROR_MESSAGE
                    );
                    
                    mainWindow.setLoggingSongs(false);
                }
            }
            else
            {
                mainWindow.setLoggingSongs(false);
            }
        }
    }
    
    private void setUpButtonPanel()
    {
        // Set up the close button
        JButton closeButton = new JButton("Close");
        JButton clearButton = new JButton("Clear history");
        
        // Set up the layout
        buttonPanel.setLayout(new FlowLayout(FlowLayout.RIGHT));
        
        // Add listener to button
        closeButton.addActionListener(e ->
        {
            this.dispose();
        });
        
        clearButton.addActionListener(e ->
        {
            File historyFile = new File(HISTORY_PATH);
            
            if (!historyFile.exists())
            {
                JOptionPane.showMessageDialog(this, "The log is already " +
                        "clear.", "MadHelix", JOptionPane.INFORMATION_MESSAGE);
            }
            else
            {
                int userClearHistory = JOptionPane.showConfirmDialog(this,
                    "Are you sure you wish to clear your song history? " +
                    "This action cannot be reversed.",
                    "MadHelix",
                    JOptionPane.YES_NO_OPTION
                );
                
                if (userClearHistory == JOptionPane.YES_OPTION)
                {
                    if (historyFile.delete())
                    {
                        tableModel.clear();
                    }
                    else
                    {
                        JOptionPane.showMessageDialog(this, "MadHelix could " +
                            "not delete history.json.",
                            "I/O error",
                            JOptionPane.ERROR_MESSAGE
                        );
                    }
                }
            }
        });
        
        // Add the button to the panel
        buttonPanel.add(clearButton);
        buttonPanel.add(closeButton);
    }
    
    private void playSong()
    {
        int selectedRow = historyTable.getSelectedRow();
        
        if (selectedRow >= 0)
        {
            String songName = (String)tableModel.getValueAt(selectedRow, 0);
            String styleName = (String)tableModel.getValueAt(selectedRow, 1);
            
            mainWindow.playSong(songName, styleName, true);
        }
    }
    
    public void logSong(String songName, String style, int length) throws IOException
    {
        tableModel.logSong(songName, style, length);
    }
}

[See repo JSON]