PrefsDialog.java 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. /*
  2. * MadHelix is a Java Swing-based GUI frontend for SoundHelix.
  3. * Copyright (C) 2018 UltrasonicMadness
  4. *
  5. * This program is free software: you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License version 3 only,
  7. * as published by the Free Software Foundation.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  16. */
  17. package org.ultrasonicmadness.madhelix.dialogs;
  18. // Assorted imports
  19. import java.io.File;
  20. // AWT widgets
  21. import java.awt.FlowLayout;
  22. import java.awt.GridBagConstraints;
  23. import java.awt.GridBagLayout;
  24. import java.awt.Insets;
  25. // Swing widgets
  26. import javax.swing.JButton;
  27. import javax.swing.JCheckBox;
  28. import javax.swing.JDialog;
  29. import javax.swing.JFileChooser;
  30. import javax.swing.JFrame;
  31. import javax.swing.JLabel;
  32. import javax.swing.JPanel;
  33. import javax.swing.JTextField;
  34. // MadHelix imports
  35. import org.ultrasonicmadness.madhelix.MadHelix;
  36. public class PrefsDialog extends JDialog
  37. {
  38. // The settings are stored in the main window if the OK button is clicked.
  39. private MadHelix mainWindow;
  40. // Components
  41. private JPanel prefsPanel = new JPanel();
  42. private JPanel fileLocationPanel = new JPanel();
  43. private JPanel buttonPanel = new JPanel();
  44. private JFileChooser fileChooser = new JFileChooser();
  45. // Option components
  46. JCheckBox fileExportCheckBox;
  47. JTextField fileLocationField = new JTextField();
  48. // Initial options
  49. boolean initExportingMidi;
  50. // Options
  51. boolean exportingMidi;
  52. public PrefsDialog(MadHelix mainWindow)
  53. {
  54. super(mainWindow, true);
  55. this.mainWindow = mainWindow;
  56. initExportingMidi = mainWindow.isExportingMidi();
  57. setUpSettingsDialog();
  58. }
  59. private void setUpSettingsDialog()
  60. {
  61. this.setTitle("MadHelix preferences");
  62. JPanel mainPanel = new JPanel();
  63. setUpPrefsPanel();
  64. setUpButtonPanel();
  65. mainPanel.setLayout(new GridBagLayout());
  66. GridBagConstraints constraints = new GridBagConstraints();
  67. constraints.insets = new Insets(4, 4, 4, 4);
  68. constraints.fill = GridBagConstraints.BOTH;
  69. constraints.weightx = 1;
  70. constraints.gridy = 0;
  71. constraints.weighty = 1;
  72. mainPanel.add(prefsPanel, constraints);
  73. constraints.gridy = 1;
  74. constraints.weighty = 0;
  75. mainPanel.add(buttonPanel, constraints);
  76. this.add(mainPanel);
  77. this.pack();
  78. this.setResizable(false);
  79. this.setSize(320, 160);
  80. this.setLocationRelativeTo(null);
  81. }
  82. private void setUpPrefsPanel()
  83. {
  84. fileExportCheckBox = new JCheckBox("Export MIDI files when playing",
  85. mainWindow.isExportingMidi());
  86. fileExportCheckBox.addActionListener(ev ->
  87. {
  88. exportingMidi = fileExportCheckBox.isSelected();
  89. });
  90. setUpFileLocationPanel();
  91. GridBagConstraints constraints = new GridBagConstraints();
  92. constraints.insets = new Insets(4, 4, 4, 4);
  93. constraints.fill = GridBagConstraints.BOTH;
  94. constraints.weightx = 1;
  95. prefsPanel.setLayout(new GridBagLayout());
  96. constraints.gridy = 0;
  97. prefsPanel.add(fileLocationPanel, constraints);
  98. constraints.gridy = 1;
  99. prefsPanel.add(fileExportCheckBox, constraints);
  100. }
  101. private void setUpFileLocationPanel()
  102. {
  103. JLabel fileLocationLabel = new JLabel("MIDI output directory",
  104. JLabel.LEFT);
  105. JButton fileBrowseButton = new JButton("Browse...");
  106. fileChooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
  107. fileLocationField.setText(mainWindow.getMidiOutDir().toString());
  108. fileBrowseButton.addActionListener(ev ->
  109. {
  110. int fileStatus = fileChooser.showOpenDialog(this);
  111. // If 'Open' is clicked
  112. if (fileStatus == 0)
  113. {
  114. fileLocationField.setText(fileChooser.getSelectedFile().toString());
  115. }
  116. });
  117. fileLocationPanel.setLayout(new GridBagLayout());
  118. GridBagConstraints constraints = new GridBagConstraints();
  119. constraints.fill = GridBagConstraints.BOTH;
  120. constraints.gridwidth = 2;
  121. constraints.gridy = 0;
  122. fileLocationPanel.add(fileLocationLabel, constraints);
  123. constraints.gridwidth = 1;
  124. constraints.gridy = 1;
  125. constraints.gridx = 0;
  126. constraints.weightx = 1;
  127. fileLocationPanel.add(fileLocationField, constraints);
  128. constraints.gridx = 1;
  129. constraints.weightx = 0;
  130. fileLocationPanel.add(fileBrowseButton, constraints);
  131. }
  132. private void setUpButtonPanel()
  133. {
  134. JButton okButton = new JButton("OK");
  135. JButton cancelButton = new JButton("Cancel");
  136. // Set up the layout
  137. buttonPanel.setLayout(new FlowLayout(FlowLayout.RIGHT));
  138. // Add listeners to buttons
  139. okButton.addActionListener(e ->
  140. {
  141. initExportingMidi = exportingMidi;
  142. mainWindow.setExportingMidi(initExportingMidi);
  143. mainWindow.setMidiOutDir(new File(fileLocationField.getText()));
  144. this.dispose();
  145. });
  146. cancelButton.addActionListener(e ->
  147. {
  148. fileExportCheckBox.setSelected(initExportingMidi);
  149. fileLocationField.setText(mainWindow.getMidiOutDir().toString());
  150. this.dispose();
  151. });
  152. // Add the button to the panel
  153. buttonPanel.add(okButton);
  154. buttonPanel.add(cancelButton);
  155. }
  156. }