X-Git-Url: https://ruin.nu/git/?p=moosique.git;a=blobdiff_plain;f=MooDialog.java;h=860b91edc83ac8951e043b3fc5a6aa14c2618875;hp=ee95f09eebb8a2c7fa6d95ece10275368f3ffb23;hb=a8dda23889d7c48a8b3313e2a141ed378bf8ff1c;hpb=c62f88aadcb432edd30b81305821a0d56b5e34eb diff --git a/MooDialog.java b/MooDialog.java index ee95f09..860b91e 100644 --- a/MooDialog.java +++ b/MooDialog.java @@ -1,158 +1,558 @@ import javax.swing.*; import java.awt.*; +import java.awt.event.*; import javax.sound.midi.*; +import java.io.*; +import java.beans.*; -/* - * The add dialog that pops up if the user clicks on the add track menuitem +/** + * A generator class for the application's dialogs. * * @author Björn Lanneskog */ public class MooDialog extends JDialog { - private JLabel name, whichtoedit; - private JTextField textfield; - private JComboBox trackList, trackLust; - private JButton okbutton, cancelbutton; - public static final int ADD_TRACK = 1, DELETE_TRACK = 2, COPY_TRACK = 3, MOVE_TRACK = 4; - - /* - * Creates the add dialog + private JLabel labelA, labelB, labelC, labelD, labelE, labelF; + private JTextField textFieldA, textFieldB, textFieldC, textFieldD, textFieldE, textFieldF; + private JTextField textFieldG, textFieldH, textFieldI, textFieldJ, textFieldK; + private JComboBox trackListA, trackListB; + private JButton okButton, cancelButton; + public static final int ADD_TRACK = 1, + DELETE_TRACK = 2, + COPY_TRACK = 3, + MOVE_TRACK = 4, + SET_POSITION = 5, + MANUAL = 6, + INSERT_MEASURE = 7, + DELETE_MEASURE = 8, + SET_TEMPO = 9, + PREFERENCES = 10; + + /** + * Constructor of the dialogs. */ public MooDialog(int type) { - super(Moosique.getGUI(), "Add track", false); + super(Moosique.getGUI(), false); Container pane = getContentPane(); pane.setLayout(null); - + Track[] tracks = Moosique.getSequence().getTracks(); switch (type) { - case ADD_TRACK: - - name = new JLabel("Name of track", JLabel.CENTER); - pane.add(name); - textfield = new JTextField(); - pane.add(textfield); - whichtoedit = new JLabel("Add after", JLabel.CENTER); - pane.add(whichtoedit); - trackList = new JComboBox(); - for (int i = 1; i <= tracks.length; i++) trackList.addItem("Track " + i); - pane.add(trackList); - cancelbutton = new JButton("Cancel"); - pane.add(cancelbutton); - okbutton = new JButton("OK"); - pane.add(okbutton); - - name.setBounds(50, 10, 100, 20); - textfield.setBounds(40, 35, 120, 20); - whichtoedit.setBounds(50, 70, 100, 20); - trackList.setBounds(40, 95, 120, 20); - cancelbutton.setBounds(10, 150, 80, 30); - okbutton.setBounds(120, 150, 60, 30); - - setLocation((Toolkit.getDefaultToolkit().getScreenSize().width - this.getWidth()) / 2, - (Toolkit.getDefaultToolkit().getScreenSize().height - this.getHeight()) / 2); - setResizable(false); - pack(); - setSize(200,220); - setVisible(true); - break; - - case DELETE_TRACK: - - whichtoedit = new JLabel("Delete track", JLabel.CENTER); - pane.add(whichtoedit); - trackList = new JComboBox(); - for (int i = 1; i <= tracks.length; i++) trackList.addItem("Track " + i); - pane.add(trackList); - cancelbutton = new JButton("Cancel"); - pane.add(cancelbutton); - okbutton = new JButton("OK"); - pane.add(okbutton); - - whichtoedit.setBounds(50, 10, 100, 20); - trackList.setBounds(40, 35, 120, 20); - cancelbutton.setBounds(10, 90, 80, 30); - okbutton.setBounds(120, 90, 60, 30); - - setLocation((Toolkit.getDefaultToolkit().getScreenSize().width - this.getWidth()) / 2, - (Toolkit.getDefaultToolkit().getScreenSize().height - this.getHeight()) / 2); - setResizable(false); - pack(); - setSize(200,165); - setVisible(true); - break; - - case COPY_TRACK: - - name = new JLabel("Track to copy", JLabel.CENTER); - pane.add(name); - trackLust = new JComboBox(); - for (int i = 1; i <= tracks.length; i++) trackLust.addItem("Track " + i); - pane.add(trackLust); - whichtoedit = new JLabel("Insert after", JLabel.CENTER); - pane.add(whichtoedit); - trackList = new JComboBox(); - for (int i = 1; i <= tracks.length; i++) trackList.addItem("Track " + i); - pane.add(trackList); - cancelbutton = new JButton("Cancel"); - pane.add(cancelbutton); - okbutton = new JButton("OK"); - pane.add(okbutton); - - name.setBounds(50, 10, 100, 20); - trackLust.setBounds(40, 35, 120, 20); - whichtoedit.setBounds(50, 70, 100, 20); - trackList.setBounds(40, 95, 120, 20); - cancelbutton.setBounds(10, 150, 80, 30); - okbutton.setBounds(120, 150, 60, 30); - - setLocation((Toolkit.getDefaultToolkit().getScreenSize().width - this.getWidth()) / 2, - (Toolkit.getDefaultToolkit().getScreenSize().height - this.getHeight()) / 2); - setResizable(false); - pack(); - setSize(200,220); - setVisible(true); - break; - - case MOVE_TRACK: - - name = new JLabel("Track to move", JLabel.CENTER); - pane.add(name); - trackLust = new JComboBox(); - for (int i = 1; i <= tracks.length; i++) trackLust.addItem("Track " + i); - pane.add(trackLust); - whichtoedit = new JLabel("Insert after", JLabel.CENTER); - pane.add(whichtoedit); - trackList = new JComboBox(); - for (int i = 1; i <= tracks.length; i++) trackList.addItem("Track " + i); - pane.add(trackList); - cancelbutton = new JButton("Cancel"); - pane.add(cancelbutton); - okbutton = new JButton("OK"); - pane.add(okbutton); - - name.setBounds(40, 10, 120, 20); - trackLust.setBounds(40, 35, 120, 20); - whichtoedit.setBounds(50, 70, 100, 20); - trackList.setBounds(40, 95, 120, 20); - cancelbutton.setBounds(10, 150, 80, 30); - okbutton.setBounds(120, 150, 60, 30); - - setLocation((Toolkit.getDefaultToolkit().getScreenSize().width - this.getWidth()) / 2, - (Toolkit.getDefaultToolkit().getScreenSize().height - this.getHeight()) / 2); - setResizable(false); - pack(); - setSize(200,220); - setVisible(true); - break; + case ADD_TRACK: makeAddDialog(pane, tracks); break; + case DELETE_TRACK: makeDelDialog(pane, tracks); break; + case COPY_TRACK: makeCopyDialog(pane, tracks); break; + case MOVE_TRACK: makeMoveDialog(pane, tracks); break; + case SET_POSITION: makeSetPositionDialog(pane); break; + case MANUAL: makeTextDialog(pane, "Manual.txt"); break; + case INSERT_MEASURE: makeInsertMeasureDialog(pane); break; + case DELETE_MEASURE: makeDeleteMeasureDialog(pane); break; + case SET_TEMPO: makeSetTempoDialog(pane); break; + case PREFERENCES: makePrefsDialog(pane); break; } - + } + + /** + * Builds the add track popupdialog. + * @param pane The container to put the dialog in. + * @param tracks A array containing miditracks. + */ + private void makeAddDialog(Container pane, Track[] tracks) { + + setTitle("Add track"); + labelA = new JLabel("Name of track", JLabel.CENTER); + pane.add(labelA); + textFieldA = new JTextField(); + pane.add(textFieldA); + labelB = new JLabel("Add after", JLabel.CENTER); + pane.add(labelB); + trackListA = new JComboBox(); + for (int i = 1; i <= tracks.length; i++) trackListA.addItem("Track " + i); + pane.add(trackListA); + cancelButton = new JButton("Cancel"); + pane.add(cancelButton); + okButton = new JButton("OK"); + pane.add(okButton); + + labelA.setBounds(50, 10, 100, 20); + textFieldA.setBounds(40, 35, 120, 20); + labelB.setBounds(50, 70, 100, 20); + trackListA.setBounds(40, 95, 120, 20); + cancelButton.setBounds(10, 150, 80, 30); + okButton.setBounds(120, 150, 60, 30); + + setResizable(false); + pack(); + setSize(200,220); + setLocationRelativeTo(Moosique.getGUI()); + setVisible(true); + } + + /** + * Builds the delete track popupdialog. + * @param pane The container to put the dialog in. + * @param tracks A array containing miditracks. + */ + private void makeDelDialog(Container pane, Track[] tracks) { + + setTitle("Delete track"); + labelB = new JLabel("Delete track", JLabel.CENTER); + pane.add(labelB); + trackListA = new JComboBox(); + for (int i = 1; i <= tracks.length; i++) trackListA.addItem("Track " + i); + pane.add(trackListA); + cancelButton = new JButton("Cancel"); + pane.add(cancelButton); + okButton = new JButton("OK"); + pane.add(okButton); + + labelB.setBounds(50, 10, 100, 20); + trackListA.setBounds(40, 35, 120, 20); + cancelButton.setBounds(10, 90, 80, 30); + okButton.setBounds(120, 90, 60, 30); + + setResizable(false); + pack(); + setSize(200,165); + setLocationRelativeTo(Moosique.getGUI()); + setVisible(true); + } + + /** + * Builds the copy track popupdialog. + * @param pane The container to put the dialog in. + * @param tracks A array containing miditracks. + */ + private void makeCopyDialog(Container pane, Track[] tracks) { + + setTitle("Copy Track"); + labelA = new JLabel("Track to copy", JLabel.CENTER); + pane.add(labelA); + trackListA = new JComboBox(); + for (int i = 1; i <= tracks.length; i++) trackListA.addItem("Track " + i); + pane.add(trackListA); + labelB = new JLabel("Insert after", JLabel.CENTER); + pane.add(labelB); + trackListB = new JComboBox(); + for (int i = 1; i <= tracks.length; i++) trackListB.addItem("Track " + i); + pane.add(trackListB); + cancelButton = new JButton("Cancel"); + pane.add(cancelButton); + okButton = new JButton("OK"); + pane.add(okButton); + labelA.setBounds(50, 10, 100, 20); + trackListA.setBounds(40, 35, 120, 20); + labelB.setBounds(50, 70, 100, 20); + trackListB.setBounds(40, 95, 120, 20); + cancelButton.setBounds(10, 150, 80, 30); + okButton.setBounds(120, 150, 60, 30); + + setResizable(false); + pack(); + setSize(200,220); + setLocationRelativeTo(Moosique.getGUI()); + setVisible(true); + } + + /** + * Builds the move track popupdialog + * @param pane The container to put the dialog in. + * @param tracks A array containing miditracks. + */ + private void makeMoveDialog(Container pane, Track[] tracks) { + + setTitle("Move track"); + labelA = new JLabel("Track to move", JLabel.CENTER); + pane.add(labelA); + trackListA = new JComboBox(); + for (int i = 1; i <= tracks.length; i++) trackListA.addItem("Track " + i); + pane.add(trackListA); + labelB = new JLabel("Insert after", JLabel.CENTER); + pane.add(labelB); + trackListB = new JComboBox(); + for (int i = 1; i <= tracks.length; i++) trackListB.addItem("Track " + i); + pane.add(trackListB); + cancelButton = new JButton("Cancel"); + pane.add(cancelButton); + okButton = new JButton("OK"); + pane.add(okButton); + + labelA.setBounds(40, 10, 120, 20); + trackListA.setBounds(40, 35, 120, 20); + labelB.setBounds(50, 70, 100, 20); + trackListB.setBounds(40, 95, 120, 20); + cancelButton.setBounds(10, 150, 80, 30); + okButton.setBounds(120, 150, 60, 30); + + setResizable(false); + pack(); + setSize(200,220); + setLocationRelativeTo(Moosique.getGUI()); + setVisible(true); + } + + /** + * Builds the set position dialog. + * @param pane The container to put the dialog in. + * @param tracks A array containing miditracks. + */ + private void makeSetPositionDialog(Container pane) { + setTitle("Set edit position"); + labelA = new JLabel("Measure", JLabel.CENTER); + pane.add(labelA); + labelB = new JLabel("Beat", JLabel.CENTER); + pane.add(labelB); + labelC = new JLabel("Tick", JLabel.CENTER); + pane.add(labelC); + textFieldA = new JTextField(); + pane.add(textFieldA); + textFieldB = new JTextField(); + pane.add(textFieldB); + textFieldC = new JTextField(); + pane.add(textFieldC); + cancelButton = new JButton("Cancel"); + pane.add(cancelButton); + okButton = new JButton("OK"); + pane.add(okButton); + + labelA.setBounds(40, 25, 50, 20); + labelB.setBounds(105, 25, 50, 20); + labelC.setBounds(170, 25, 50, 20); + textFieldA.setBounds(40, 45, 50, 20); + textFieldB.setBounds(105, 45, 50, 20); + textFieldC.setBounds(170, 45, 50, 20); + cancelButton.setBounds(35, 90, 80, 30); + okButton.setBounds(155, 90, 60, 30); - - } + setResizable(false); + pack(); + setSize(260,165); + setLocationRelativeTo(Moosique.getGUI()); + setVisible(true); + } + + /** + * Builds the insert measure popupdialog. + * @param pane The container to put the dialog in. + */ + private void makeInsertMeasureDialog(Container pane){ + + setTitle("Insert Measure"); + labelA = new JLabel("Insert at:", JLabel.RIGHT); + pane.add(labelA); + labelB = new JLabel("Measure count:", JLabel.RIGHT); + pane.add(labelB); + textFieldA = new JTextField(); + pane.add(textFieldA); + textFieldB = new JTextField(); + pane.add(textFieldB); + cancelButton = new JButton("Cancel"); + pane.add(cancelButton); + okButton = new JButton("OK"); + pane.add(okButton); + + labelA.setBounds(20, 20, 110 ,20); + labelB.setBounds(20, 50, 110, 20); + textFieldA.setBounds(140 ,20 , 40, 20); + textFieldB.setBounds(140,50, 40, 20); + cancelButton.setBounds(20 ,95 , 80, 30); + okButton.setBounds(120, 95, 60, 30); + + setResizable(false); + pack(); + setSize(210,175); + setLocationRelativeTo(Moosique.getGUI()); + setVisible(true); + } + + /** + * Builds the delete measure popupdialog. + * @param pane The container to put the dialog in. + */ + private void makeDeleteMeasureDialog(Container pane) { + + setTitle("Delete Measure"); + labelA = new JLabel("Delete at:", JLabel.RIGHT); + pane.add(labelA); + labelB = new JLabel("Measure count:", JLabel.RIGHT); + pane.add(labelB); + textFieldA = new JTextField(); + pane.add(textFieldA); + textFieldB = new JTextField(); + pane.add(textFieldB); + cancelButton = new JButton("Cancel"); + pane.add(cancelButton); + okButton = new JButton("OK"); + pane.add(okButton); + + labelA.setBounds(20, 20, 110 ,20); + labelB.setBounds(20, 50, 110, 20); + textFieldA.setBounds(140 ,20 , 40, 20); + textFieldB.setBounds(140,50, 40, 20); + cancelButton.setBounds(20 ,95 , 80, 30); + okButton.setBounds(120, 95, 60, 30); + + setResizable(false); + pack(); + setSize(210,175); + setLocationRelativeTo(Moosique.getGUI()); + setVisible(true); + } + + /** + * Builds the set tempo dialog. + * @param pane The container to put the dialog in. + * @param tracks A array containing miditracks. + */ + private void makeSetTempoDialog(Container pane) { + + setTitle("Set tempo"); + // creating labels and adding them to a container + labelA = new JLabel("Measure", JLabel.CENTER); + pane.add(labelA); + labelB = new JLabel("Beat", JLabel.CENTER); + pane.add(labelB); + labelC = new JLabel("Tick", JLabel.CENTER); + pane.add(labelC); + labelD = new JLabel("Start at:", JLabel.RIGHT); + pane.add(labelD); + labelE = new JLabel("End at:", JLabel.RIGHT); + pane.add(labelE); + labelF = new JLabel("to", JLabel.CENTER); + pane.add(labelF); + + // the starting textfields and adding them to the container + textFieldA = new JTextField(); + pane.add(textFieldA); + textFieldB = new JTextField(); + pane.add(textFieldB); + textFieldC = new JTextField(); + pane.add(textFieldC); + // ending textfields and adding them to the container + textFieldD = new JTextField(); + pane.add(textFieldD); + textFieldE = new JTextField(); + pane.add(textFieldE); + textFieldF = new JTextField(); + pane.add(textFieldF); + // the amount to edit textfields and adding them to a container + textFieldG = new JTextField(); + pane.add(textFieldG); + textFieldH = new JTextField(); + pane.add(textFieldH); + textFieldI = new JTextField(); + pane.add(textFieldI); + textFieldJ = new JTextField(); + pane.add(textFieldJ); + textFieldK = new JTextField(); + pane.add(textFieldK); + + // creating the buttons and adding them to the container + JRadioButton constant = new JRadioButton("Set constant tempo of:"); + constant.setSelected(true); + pane.add(constant); + JRadioButton change = new JRadioButton("Gradually change tempo:"); + pane.add(change); + JRadioButton scale = new JRadioButton("Scale tempo in %:"); + pane.add(scale); + JRadioButton add = new JRadioButton("Add to current tempo:"); + pane.add(add); + + // creating a buttongroup and adding the buttons above + ButtonGroup group = new ButtonGroup(); + group.add(constant); + group.add(change); + group.add(scale); + group.add(add); + + // creating the cancel and ok button + cancelButton = new JButton("Cancel"); + pane.add(cancelButton); + okButton = new JButton("OK"); + pane.add(okButton); + + + labelA.setBounds(80, 25, 50, 20); + labelB.setBounds(145, 25, 50, 20); + labelC.setBounds(210, 25, 50, 20); + labelD.setBounds(20, 45, 60, 20); + labelE.setBounds(20, 65, 60, 20); + labelF.setBounds(255, 125, 20 ,20); + + textFieldA.setBounds(80, 45, 50, 20); + textFieldB.setBounds(145, 45, 50, 20); + textFieldC.setBounds(210, 45, 50, 20); + textFieldD.setBounds(80, 65, 50, 20); + textFieldE.setBounds(145, 65, 50, 20); + textFieldF.setBounds(210, 65, 50, 20); + + textFieldG.setBounds(205, 100, 35, 20); + textFieldH.setBounds(220, 125, 35, 20); + textFieldI.setBounds(280, 125, 35, 20); + textFieldJ.setBounds(170, 150, 35, 20); + textFieldK.setBounds(200, 175, 35, 20); + + constant.setBounds(20, 100, 180, 20); + change.setBounds(20, 125, 200, 20); + scale.setBounds(20, 150, 150, 20); + add.setBounds(20, 175, 175, 20); + + cancelButton.setBounds(75, 215, 80, 30); + okButton.setBounds(195, 215, 60, 30); + + setResizable(false); + pack(); + setSize(340,300); + setLocationRelativeTo(Moosique.getGUI()); + setVisible(true); + } + + + private void makeTextDialog(Container pane, String filename) { + setTitle("User Manual"); + pane.setLayout(new BoxLayout(pane, BoxLayout.Y_AXIS)); + File manual = new File(filename); + String s; + try { + BufferedReader br = new BufferedReader(new FileReader(manual)); + char[] chars = new char[(int)manual.length()]; + br.read(chars, 0, (int)manual.length()); + s = new String(chars); + } catch (Exception ex) { + s = "Manual not found"; + } + pane.add(new JScrollPane(new JTextArea(s, 30, 95))); + Action close = new AbstractAction("Close") { + public void actionPerformed(ActionEvent ae) { + setVisible(false); + }}; + JButton closeButton = new JButton(close); + closeButton.setAlignmentX(Component.CENTER_ALIGNMENT); + pane.add(closeButton); + setResizable(false); + pack(); + setLocationRelativeTo(Moosique.getGUI()); + setVisible(true); + } + private void makePrefsDialog(Container pane) { + /* + MidiDevice.Info[] devInfo = MidiSystem.getMidiDeviceInfo(); + for (int i = 0; i < devInfo.length; i++) { + if (MidiSystem.getMidiDevice(devInfo[i]) instanceof Sequencer) { + + } else if (MidiSystem.getMidiDevice(devInfo[i]) instanceof Synthesizer) { + + } + } + String[] seqNames, synthNames; + JPanel pane = (JPanel) this.getContentPane(); + pane.add(new JLabel("Sequencer")); + JComboBox seqBox = new JComboBox(seqNames); + pane.add(new JLabel("Synthesizer")); + JComboBox synthBox = new JComboBox(synthNames); + */ + } + + private MooNote note; + private JOptionPane optionPane; + private JTextField pitch; + private JTextField velocity; + private JTextField length; + + /** + * Creates a new note preference dialog. + * @param mn the note that will be graphically represented + */ + public MooDialog(MooNote mn) { + super(Moosique.getGUI(), "Note properties", true); + JPanel panel = new JPanel(); + panel.setLayout(new GridLayout(3,2)); + note = mn; + pitch = new JTextField(new Integer(note.getPitch()).toString(),3); + panel.add(new Label("Pitch: ")); + panel.add(pitch); + + velocity = new JTextField(new Integer(note.getVelocity()).toString(),3); + panel.add(new Label("Velocity: ")); + panel.add(velocity); + + length = new JTextField(new Integer(note.getDuration()).toString(),5); + panel.add(new Label("Length: ")); + panel.add(length); + + Object[] array = {"Set the note properties", + panel}; + + final String btnString1 = "Apply changes"; + final String btnString2 = "Cancel"; + Object[] options = {btnString1, btnString2}; + + optionPane = new JOptionPane(array, + JOptionPane.QUESTION_MESSAGE, + JOptionPane.YES_NO_OPTION, + null, + options, + options[0]); + setContentPane(optionPane); + setDefaultCloseOperation(DISPOSE_ON_CLOSE); + + ActionListener intValidator = new ActionListener() { + public void actionPerformed(ActionEvent e) { + if (e.getSource() instanceof JTextField){ + JTextField s = (JTextField)e.getSource(); + int num = Integer.parseInt(s.getText()); + if (num < 0) + num = 0; + else if (num > 127 && s != length) + num = 127; + s.setText(new Integer(num).toString()); + } + } + }; + pitch.addActionListener(intValidator); + velocity.addActionListener(intValidator); + length.addActionListener(intValidator); + + optionPane.addPropertyChangeListener(new PropertyChangeListener() { + public void propertyChange(PropertyChangeEvent e) { + String prop = e.getPropertyName(); + + if (isVisible() + && (e.getSource() == optionPane) + && (prop.equals(JOptionPane.VALUE_PROPERTY) || + prop.equals(JOptionPane.INPUT_VALUE_PROPERTY))) { + Object value = optionPane.getValue(); + + if (value == JOptionPane.UNINITIALIZED_VALUE) { + //ignore reset + return; + } + + // Reset the JOptionPane's value. + // If you don't do this, then if the user + // presses the same button next time, no + // property change event will be fired. + optionPane.setValue( + JOptionPane.UNINITIALIZED_VALUE); + + if (value.equals(btnString1)) { + note.setPitch(Integer.parseInt(pitch.getText())); + note.setVelocity(Integer.parseInt(velocity.getText())); + note.setDuration(Integer.parseInt(length.getText())); + Moosique.setEdited(); + setVisible(false); + } else { // user closed dialog or clicked cancel + setVisible(false); + } + } + } + }); + pack(); + setVisible(true); + } }