]> ruin.nu Git - moosique.git/blobdiff - MooDialog.java
*** empty log message ***
[moosique.git] / MooDialog.java
index ee95f09eebb8a2c7fa6d95ece10275368f3ffb23..f8fae518aadc7c6e85821c235034774a1afcee24 100644 (file)
@@ -1,55 +1,62 @@
 import javax.swing.*;
 import java.awt.*;
+import java.awt.event.*;
 import javax.sound.midi.*;
+import java.beans.*;
 
 /*
- * The add dialog that pops up if the user clicks on the add track menuitem
+ * The GUI-class representing the add-, delete-, copy- or movetrack popup 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;
+       private JLabel name, whichToEdit;
+       private JTextField textField;
+       private JComboBox trackListA, trackListB; 
+       private JButton okButton, cancelButton;
+       public static final int ADD_TRACK = 1,
+                               DELETE_TRACK = 2,
+                               COPY_TRACK = 3,
+                               MOVE_TRACK = 4;
        
        /*
-        * Creates the add dialog
+        * Create the parts that the dialogs have in common.
         */
         public MooDialog(int type) {
                super(Moosique.getGUI(), "Add track", false);
                        
                Container pane = getContentPane();
                pane.setLayout(null);
-
+               
                Track[] tracks = Moosique.getSequence().getTracks();
 
                switch (type) {
                        case ADD_TRACK:
-                                       
+                               
+                               // Create the content of the add dialog and put them in a container.
                                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);
-                                       
+                               textField = new JTextField();
+                               pane.add(textField);
+                               whichToEdit = new JLabel("Add after", JLabel.CENTER);
+                               pane.add(whichToEdit);
+                               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);
+                               
+                               // Set the layout properties of the dialog.
                                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);
+                               textField.setBounds(40, 35, 120, 20);
+                               whichToEdit.setBounds(50, 70, 100, 20);
+                               trackListA.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);
@@ -60,21 +67,23 @@ public class MooDialog extends JDialog {
                                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);
+                               
+                               // Create the contents of the delete dialog and put them in a container.
+                               whichToEdit = new JLabel("Delete track", JLabel.CENTER);
+                               pane.add(whichToEdit);
+                               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);
+                               
+                               // Set the layout properties of the dialog.
+                               whichToEdit.setBounds(50, 10, 100, 20);
+                               trackListA.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);
@@ -86,27 +95,29 @@ public class MooDialog extends JDialog {
                        
                        case COPY_TRACK:
                        
+                               // Create the content of the copy dialog and put them in a container.
                                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);
-                                       
+                               trackListA = new JComboBox();
+                               for (int i = 1; i <= tracks.length; i++) trackListA.addItem("Track " + i);
+                               pane.add(trackListA);
+                               whichToEdit = new JLabel("Insert after", JLabel.CENTER);
+                               pane.add(whichToEdit);
+                               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);
+                               
+                               // Set the layout properties of the dialog.
                                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);
+                               trackListA.setBounds(40, 35, 120, 20);
+                               whichToEdit.setBounds(50, 70, 100, 20);
+                               trackListB.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);
@@ -118,27 +129,29 @@ public class MooDialog extends JDialog {
                        
                        case MOVE_TRACK:
                                
+                               // Create the content of the move dialog and put them in a container.
                                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);
-                                       
+                               trackListA = new JComboBox();
+                               for (int i = 1; i <= tracks.length; i++) trackListA.addItem("Track " + i);
+                               pane.add(trackListA);
+                               whichToEdit = new JLabel("Insert after", JLabel.CENTER);
+                               pane.add(whichToEdit);
+                               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);
+                               
+                               // Set the layout properties of the dialog.
                                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);
+                               trackListA.setBounds(40, 35, 120, 20);
+                               whichToEdit.setBounds(50, 70, 100, 20);
+                               trackListB.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);
@@ -148,11 +161,107 @@ public class MooDialog extends JDialog {
                                setVisible(true);
                                break;
                }
-                               
-               
-               
-               
-                        
         }
+       
+       
+       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", false);
+               note = mn;
+               pitch = new JTextField(new Integer(note.getPitch()).toString(),3);
+               JPanel pitchpanel = new JPanel();
+               pitchpanel.add(new Label("Pitch: "));
+               pitchpanel.add(pitch);
+
+
+               velocity = new JTextField(new Integer(note.getVelocity()).toString(),3);
+               JPanel velocitypanel = new JPanel();
+               velocitypanel.add(new Label("Velocity: "));
+               velocitypanel.add(velocity);
+
+               length = new JTextField(new Integer(note.getDuration()).toString(),5);
+               JPanel lengthpanel = new JPanel();
+               lengthpanel.add(new Label("Length: "));
+               lengthpanel.add(length);
+
+               Object[] array = {"Set the note properties",
+                               pitchpanel,
+                               velocitypanel,
+                               lengthpanel};
+       
+               final String btnString1 = "Enter";
+               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()));
+       
+                               setVisible(false);
+                           } else { // user closed dialog or clicked cancel
+                               setVisible(false);
+                           }
+                       }
+                   }
+               });
+               pack();
+       }
 }