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 /

PrefsDialog.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;

// Assorted imports
import java.io.File;

// AWT widgets
import java.awt.FlowLayout;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;

// Swing widgets
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JDialog;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JSpinner;
import javax.swing.JTextField;

// Other swing modules
import javax.swing.SpinnerNumberModel;

// MadHelix imports
import org.ultrasonicmadness.madhelix.MadHelix;

public class PrefsDialog extends JDialog
{
    // Constants
    private final int MAX_TIMER_INTERVAL = 5000;
    
    // The settings are stored in the main window if the OK button is clicked.
    private MadHelix mainWindow;
    
    // Components
    private JPanel prefsPanel = new JPanel();
    private JPanel fileLocationPanel = new JPanel();
    private JPanel timerIntervalPanel = new JPanel();
    private JPanel buttonPanel = new JPanel();
    private JFileChooser fileChooser = new JFileChooser();
    
    // Option components
    JCheckBox fileExportCheckBox;
    JCheckBox loggingSongsCheckBox;
    JTextField fileLocationField = new JTextField();
    JSpinner timerIntervalSpinner;
    
    // Initial options
    boolean initExportingMidi;
    boolean initLoggingSongs;
    
    // Options
    boolean exportingMidi;
    boolean loggingSongs;
    
    public PrefsDialog(MadHelix mainWindow)
    {
        super(mainWindow, true);
        this.mainWindow = mainWindow;
        
        initExportingMidi = mainWindow.isExportingMidi();
        initLoggingSongs = mainWindow.isLoggingSongs();
        
        exportingMidi = initExportingMidi;
        loggingSongs = initLoggingSongs;
        
        setUpSettingsDialog();
    }
    
    private void setUpSettingsDialog()
    {
        this.setTitle("MadHelix preferences");
        JPanel mainPanel = new JPanel();
        
        setUpPrefsPanel();
        setUpButtonPanel();
        
        mainPanel.setLayout(new GridBagLayout());
        
        GridBagConstraints constraints = new GridBagConstraints();
        constraints.fill = GridBagConstraints.BOTH;
        constraints.weightx = 1;
        
        constraints.gridy = 0;
        constraints.weighty = 1;
        mainPanel.add(prefsPanel, constraints);
        
        constraints.gridy = 1;
        constraints.weighty = 0;
        mainPanel.add(buttonPanel, constraints);
        
        this.add(mainPanel);
        this.pack();
        this.setResizable(false);
        this.setLocationRelativeTo(null);
    }
    
    private void setUpPrefsPanel()
    {
        fileExportCheckBox = new JCheckBox("Export MIDI files when playing",
                mainWindow.isExportingMidi());
        
        loggingSongsCheckBox = new JCheckBox("Log played songs to history",
                mainWindow.isLoggingSongs());
        
        fileExportCheckBox.addActionListener(ev ->
        {
            exportingMidi = fileExportCheckBox.isSelected();
        });
        
        loggingSongsCheckBox.addActionListener(ev ->
        {
            loggingSongs = loggingSongsCheckBox.isSelected();
        });
        
        setUpFileLocationPanel();
        setUpTimerIntervalPanel();
        
        GridBagConstraints constraints = new GridBagConstraints();
        constraints.insets = new Insets(4, 4, 4, 4);
        constraints.fill = GridBagConstraints.BOTH;
        constraints.weightx = 1;
        
        prefsPanel.setLayout(new GridBagLayout());
        
        constraints.gridy = 0;
        prefsPanel.add(fileLocationPanel, constraints);
        
        constraints.gridy = 1;
        prefsPanel.add(fileExportCheckBox, constraints);
        
        constraints.gridy = 2;
        prefsPanel.add(loggingSongsCheckBox, constraints);
        
        constraints.gridy = 3;
        prefsPanel.add(timerIntervalPanel, constraints);
    }
    
    private void setUpFileLocationPanel()
    {
        JLabel fileLocationLabel = new JLabel("MIDI output directory",
                JLabel.LEFT);
        JButton fileBrowseButton = new JButton("Browse...");
        
        fileChooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
        fileLocationField.setText(mainWindow.getMidiOutDir().toString());
        
        fileBrowseButton.addActionListener(ev ->
        {
            int fileStatus = fileChooser.showOpenDialog(this);
            
            // If 'Open' is clicked
            if (fileStatus == 0)
            {
                fileLocationField.setText(fileChooser.getSelectedFile().toString());
            }
        });
        
        fileLocationPanel.setLayout(new GridBagLayout());
        GridBagConstraints constraints = new GridBagConstraints();
        constraints.fill = GridBagConstraints.BOTH;
        
        constraints.gridwidth = 2;
        constraints.gridy = 0;
        fileLocationPanel.add(fileLocationLabel, constraints);
        
        constraints.gridwidth = 1;
        constraints.gridy = 1;
        
        constraints.gridx = 0;
        constraints.weightx = 1;
        fileLocationPanel.add(fileLocationField, constraints);
        
        constraints.gridx = 1;
        constraints.weightx = 0;
        fileLocationPanel.add(fileBrowseButton, constraints);
    }
    
    private void setUpTimerIntervalPanel()
    {
        JLabel timerIntervalLabel = new JLabel("Timer update interval");
        JLabel timerUnitLabel = new JLabel("ms");
        
        timerIntervalSpinner = new JSpinner(new SpinnerNumberModel(
                mainWindow.getTimerInterval(), 0, MAX_TIMER_INTERVAL, 1));
        
        timerIntervalSpinner.setToolTipText("If MadHelix lags when playing " +
                "songs, increase this value.");
        
        timerIntervalPanel.setLayout(new GridBagLayout());
        GridBagConstraints constraints = new GridBagConstraints();
        constraints.anchor = GridBagConstraints.WEST;
        constraints.insets = new Insets(0, 4, 0, 0);
        
        constraints.gridx = 0;
        constraints.weightx = 0;
        timerIntervalPanel.add(timerIntervalLabel, constraints);
        
        constraints.gridx = 1;
        constraints.weightx = 0;
        timerIntervalPanel.add(timerIntervalSpinner, constraints);
        
        constraints.gridx = 2;
        constraints.weightx = 1;
        timerIntervalPanel.add(timerUnitLabel, constraints);
    }
    
    private void setUpButtonPanel()
    {
        JButton okButton = new JButton("OK");
        JButton cancelButton = new JButton("Cancel");
        
        // Set up the layout
        buttonPanel.setLayout(new FlowLayout(FlowLayout.RIGHT));
        
        // Add listeners to buttons
        okButton.addActionListener(e ->
        {
            initExportingMidi = exportingMidi;
            initLoggingSongs = loggingSongs;
            
            mainWindow.setExportingMidi(initExportingMidi);
            mainWindow.setLoggingSongs(initLoggingSongs);
            mainWindow.setMidiOutDir(new File(fileLocationField.getText()));
            
            mainWindow.setTimerInterval(
                    ((Integer)timerIntervalSpinner.getValue()).intValue());
            
            this.dispose();
        });
        
        cancelButton.addActionListener(e ->
        {
            fileExportCheckBox.setSelected(initExportingMidi);
            loggingSongsCheckBox.setSelected(initLoggingSongs);
            fileLocationField.setText(mainWindow.getMidiOutDir().toString());
            timerIntervalSpinner.setValue(mainWindow.getTimerInterval());
            
            this.dispose();
        });
        
        // Add the button to the panel
        buttonPanel.add(okButton);
        buttonPanel.add(cancelButton);
    }
}

[See repo JSON]