]> ruin.nu Git - moosique.git/blob - MooNoteProp.java
mostly done
[moosique.git] / MooNoteProp.java
1 import javax.swing.*;
2 import java.awt.*;
3 import java.awt.event.*;
4 import java.beans.*;
5
6 /**
7  * Graphical representation of a MIDI note.
8  * 
9  * @author  Andersson, Andreen, Lanneskog, Pehrson
10  * @version 1
11  */
12
13 public class MooNoteProp extends JDialog{
14
15         private MooNote note;
16     private JOptionPane optionPane;
17         private JTextField pitch;
18         private JTextField velocity;
19         private JTextField length;
20
21         /** 
22          * Creates a new note preference dialog.
23          * @param mn The note that will be graphically represented
24          */
25     public MooNoteProp(MooNote mn) {
26         super((JFrame)null, true);
27                 note = mn;
28         setTitle("Note properties");
29
30                 pitch = new JTextField(new Integer(note.getPitch()).toString(),3);
31                 JPanel pitchpanel = new JPanel();
32                 pitchpanel.add(new Label("Pitch: "));
33                 pitchpanel.add(pitch);
34
35
36                 velocity = new JTextField(new Integer(note.getVelocity()).toString(),3);
37                 JPanel velocitypanel = new JPanel();
38                 velocitypanel.add(new Label("Velocity: "));
39                 velocitypanel.add(velocity);
40
41                 length = new JTextField(new Integer(note.getDuration()).toString(),5);
42                 JPanel lengthpanel = new JPanel();
43                 lengthpanel.add(new Label("Length: "));
44                 lengthpanel.add(length);
45
46         Object[] array = {"Set the note properties",
47                         pitchpanel,
48                         velocitypanel,
49                         lengthpanel};
50
51         final String btnString1 = "Enter";
52         final String btnString2 = "Cancel";
53         Object[] options = {btnString1, btnString2};
54
55         optionPane = new JOptionPane(array, 
56                                     JOptionPane.QUESTION_MESSAGE,
57                                     JOptionPane.YES_NO_OPTION,
58                                     null,
59                                     options,
60                                     options[0]);
61         setContentPane(optionPane);
62         setDefaultCloseOperation(EXIT_ON_CLOSE);
63 /*
64         textField.addActionListener(new ActionListener() {
65             public void actionPerformed(ActionEvent e) {
66                 optionPane.setValue(btnString1);
67             }
68         });
69 */
70         optionPane.addPropertyChangeListener(new PropertyChangeListener() {
71             public void propertyChange(PropertyChangeEvent e) {
72                 String prop = e.getPropertyName();
73
74                 if (isVisible() 
75                  && (e.getSource() == optionPane)
76                  && (prop.equals(JOptionPane.VALUE_PROPERTY) ||
77                      prop.equals(JOptionPane.INPUT_VALUE_PROPERTY))) {
78                     Object value = optionPane.getValue();
79
80                     if (value == JOptionPane.UNINITIALIZED_VALUE) {
81                         //ignore reset
82                         return;
83                     }
84
85                     // Reset the JOptionPane's value.
86                     // If you don't do this, then if the user
87                     // presses the same button next time, no
88                     // property change event will be fired.
89                     optionPane.setValue(
90                             JOptionPane.UNINITIALIZED_VALUE);
91
92                     if (value.equals(btnString1)) {
93                                                 note.setPitch(Integer.parseInt(pitch.getText()));
94                                                 note.setVelocity(Integer.parseInt(velocity.getText()));
95                                                 note.setDuration(Integer.parseInt(length.getText()));
96
97                         setVisible(false);
98                     } else { // user closed dialog or clicked cancel
99                         setVisible(false);
100                     }
101                 }
102             }
103         });
104                 pack();
105     }
106
107         public static void main(String[] args){
108
109                 MooNote n = new MooNote(0,0,10,20,0,30);
110                 MooNoteProp prop = new MooNoteProp(n);
111                 prop.setVisible(true);
112                 prop = new MooNoteProp(n);
113                 prop.setVisible(true);
114
115         }
116 }