]> ruin.nu Git - moosique.git/blob - MooNoteProp.java
*** empty log message ***
[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(DISPOSE_ON_CLOSE);
63
64         ActionListener intValidator = new ActionListener() {
65             public void actionPerformed(ActionEvent e) {
66                                 if (e.getSource() instanceof JTextField){
67                                         JTextField s = (JTextField)e.getSource();
68                                         int num = Integer.parseInt(s.getText());
69                                         if (num < 0)
70                                                 num = 0;
71                                         else if (num > 127 && s != length)
72                                                 num = 127;
73                                         s.setText(new Integer(num).toString());
74                                 }
75             }
76         };
77                 pitch.addActionListener(intValidator);
78                 velocity.addActionListener(intValidator);
79                 length.addActionListener(intValidator);
80
81         optionPane.addPropertyChangeListener(new PropertyChangeListener() {
82             public void propertyChange(PropertyChangeEvent e) {
83                 String prop = e.getPropertyName();
84
85                 if (isVisible() 
86                  && (e.getSource() == optionPane)
87                  && (prop.equals(JOptionPane.VALUE_PROPERTY) ||
88                      prop.equals(JOptionPane.INPUT_VALUE_PROPERTY))) {
89                     Object value = optionPane.getValue();
90
91                     if (value == JOptionPane.UNINITIALIZED_VALUE) {
92                         //ignore reset
93                         return;
94                     }
95
96                     // Reset the JOptionPane's value.
97                     // If you don't do this, then if the user
98                     // presses the same button next time, no
99                     // property change event will be fired.
100                     optionPane.setValue(
101                             JOptionPane.UNINITIALIZED_VALUE);
102
103                     if (value.equals(btnString1)) {
104                                                 note.setPitch(Integer.parseInt(pitch.getText()));
105                                                 note.setVelocity(Integer.parseInt(velocity.getText()));
106                                                 note.setDuration(Integer.parseInt(length.getText()));
107
108                         setVisible(false);
109                     } else { // user closed dialog or clicked cancel
110                         setVisible(false);
111                     }
112                 }
113             }
114         });
115                 pack();
116     }
117
118         public static void main(String[] args){
119
120                 MooNote n = new MooNote(0,0,10,20,0,30);
121                 MooNoteProp prop = new MooNoteProp(n);
122                 prop.setVisible(true);
123                 prop = new MooNoteProp(n);
124                 prop.setVisible(true);
125
126         }
127 }