]> ruin.nu Git - moosique.git/blob - MooDialog.java
nu är den klar
[moosique.git] / MooDialog.java
1 import javax.swing.*;
2 import java.awt.*;
3 import java.awt.event.*;
4 import javax.sound.midi.*;
5 import java.beans.*;
6
7 /*
8  * The add dialog that pops up if the user clicks on the add track menuitem
9  *
10  * @author Björn Lanneskog
11  */
12  
13 public class MooDialog extends JDialog {
14
15         private JLabel name, whichtoedit;
16         private JTextField textfield;
17         private JComboBox trackList, trackLust; 
18         private JButton okbutton, cancelbutton;
19         public static final int ADD_TRACK = 1,
20                                 DELETE_TRACK = 2,
21                                 COPY_TRACK = 3,
22                                 MOVE_TRACK = 4;
23         
24         /*
25          * Creates the add dialog
26          */
27          public MooDialog(int type) {
28                 super(Moosique.getGUI(), "Add track", false);
29                         
30                 Container pane = getContentPane();
31                 pane.setLayout(null);
32
33                 Track[] tracks = Moosique.getSequence().getTracks();
34
35                 switch (type) {
36                         case ADD_TRACK:
37                                         
38                                 name = new JLabel("Name of track", JLabel.CENTER);
39                                 pane.add(name);
40                                 textfield = new JTextField();
41                                 pane.add(textfield);
42                                 whichtoedit = new JLabel("Add after", JLabel.CENTER);
43                                 pane.add(whichtoedit);
44                                 trackList = new JComboBox();
45                                 for (int i = 1; i <= tracks.length; i++) trackList.addItem("Track " + i);
46                                 pane.add(trackList);
47                                 cancelbutton = new JButton("Cancel");
48                                 pane.add(cancelbutton);
49                                 okbutton = new JButton("OK");
50                                 pane.add(okbutton);
51                                         
52                                 name.setBounds(50, 10, 100, 20);
53                                 textfield.setBounds(40, 35, 120, 20);
54                                 whichtoedit.setBounds(50, 70, 100, 20);
55                                 trackList.setBounds(40, 95, 120, 20);
56                                 cancelbutton.setBounds(10, 150, 80, 30);
57                                 okbutton.setBounds(120, 150, 60, 30);
58                         
59                                 setLocation((Toolkit.getDefaultToolkit().getScreenSize().width - this.getWidth()) / 2,
60                                 (Toolkit.getDefaultToolkit().getScreenSize().height - this.getHeight()) / 2);
61                                 setResizable(false);
62                                 pack();
63                                 setSize(200,220);
64                                 setVisible(true);
65                                 break;
66                                 
67                         case DELETE_TRACK:
68                         
69                                 whichtoedit = new JLabel("Delete track", JLabel.CENTER);
70                                 pane.add(whichtoedit);
71                                 trackList = new JComboBox();
72                                 for (int i = 1; i <= tracks.length; i++) trackList.addItem("Track " + i);
73                                 pane.add(trackList);
74                                 cancelbutton = new JButton("Cancel");
75                                 pane.add(cancelbutton);
76                                 okbutton = new JButton("OK");
77                                 pane.add(okbutton);
78                                         
79                                 whichtoedit.setBounds(50, 10, 100, 20);
80                                 trackList.setBounds(40, 35, 120, 20);
81                                 cancelbutton.setBounds(10, 90, 80, 30);
82                                 okbutton.setBounds(120, 90, 60, 30);
83                         
84                                 setLocation((Toolkit.getDefaultToolkit().getScreenSize().width - this.getWidth()) / 2,
85                                 (Toolkit.getDefaultToolkit().getScreenSize().height - this.getHeight()) / 2);
86                                 setResizable(false);
87                                 pack();
88                                 setSize(200,165);
89                                 setVisible(true);
90                                 break;
91                         
92                         case COPY_TRACK:
93                         
94                                 name = new JLabel("Track to copy", JLabel.CENTER);
95                                 pane.add(name);
96                                 trackLust = new JComboBox();
97                                 for (int i = 1; i <= tracks.length; i++) trackLust.addItem("Track " + i);
98                                 pane.add(trackLust);
99                                 whichtoedit = new JLabel("Insert after", JLabel.CENTER);
100                                 pane.add(whichtoedit);
101                                 trackList = new JComboBox();
102                                 for (int i = 1; i <= tracks.length; i++) trackList.addItem("Track " + i);
103                                 pane.add(trackList);
104                                 cancelbutton = new JButton("Cancel");
105                                 pane.add(cancelbutton);
106                                 okbutton = new JButton("OK");
107                                 pane.add(okbutton);
108                                         
109                                 name.setBounds(50, 10, 100, 20);
110                                 trackLust.setBounds(40, 35, 120, 20);
111                                 whichtoedit.setBounds(50, 70, 100, 20);
112                                 trackList.setBounds(40, 95, 120, 20);
113                                 cancelbutton.setBounds(10, 150, 80, 30);
114                                 okbutton.setBounds(120, 150, 60, 30);
115                         
116                                 setLocation((Toolkit.getDefaultToolkit().getScreenSize().width - this.getWidth()) / 2,
117                                 (Toolkit.getDefaultToolkit().getScreenSize().height - this.getHeight()) / 2);
118                                 setResizable(false);
119                                 pack();
120                                 setSize(200,220);
121                                 setVisible(true);
122                                 break;
123                         
124                         case MOVE_TRACK:
125                                 
126                                 name = new JLabel("Track to move", JLabel.CENTER);
127                                 pane.add(name);
128                                 trackLust = new JComboBox();
129                                 for (int i = 1; i <= tracks.length; i++) trackLust.addItem("Track " + i);
130                                 pane.add(trackLust);
131                                 whichtoedit = new JLabel("Insert after", JLabel.CENTER);
132                                 pane.add(whichtoedit);
133                                 trackList = new JComboBox();
134                                 for (int i = 1; i <= tracks.length; i++) trackList.addItem("Track " + i);
135                                 pane.add(trackList);
136                                 cancelbutton = new JButton("Cancel");
137                                 pane.add(cancelbutton);
138                                 okbutton = new JButton("OK");
139                                 pane.add(okbutton);
140                                         
141                                 name.setBounds(40, 10, 120, 20);
142                                 trackLust.setBounds(40, 35, 120, 20);
143                                 whichtoedit.setBounds(50, 70, 100, 20);
144                                 trackList.setBounds(40, 95, 120, 20);
145                                 cancelbutton.setBounds(10, 150, 80, 30);
146                                 okbutton.setBounds(120, 150, 60, 30);
147                         
148                                 setLocation((Toolkit.getDefaultToolkit().getScreenSize().width - this.getWidth()) / 2,
149                                 (Toolkit.getDefaultToolkit().getScreenSize().height - this.getHeight()) / 2);
150                                 setResizable(false);
151                                 pack();
152                                 setSize(200,220);
153                                 setVisible(true);
154                                 break;
155                 }
156          }
157
158         private MooNote note;
159         private JOptionPane optionPane;
160         private JTextField pitch;
161         private JTextField velocity;
162         private JTextField length;
163
164         /** 
165          * Creates a new note preference dialog.
166          * @param mn    the note that will be graphically represented
167          */
168         public MooDialog(MooNote mn) {
169                 super(Moosique.getGUI(), "Note properties", false);
170                 note = mn;
171                 pitch = new JTextField(new Integer(note.getPitch()).toString(),3);
172                 JPanel pitchpanel = new JPanel();
173                 pitchpanel.add(new Label("Pitch: "));
174                 pitchpanel.add(pitch);
175
176
177                 velocity = new JTextField(new Integer(note.getVelocity()).toString(),3);
178                 JPanel velocitypanel = new JPanel();
179                 velocitypanel.add(new Label("Velocity: "));
180                 velocitypanel.add(velocity);
181
182                 length = new JTextField(new Integer(note.getDuration()).toString(),5);
183                 JPanel lengthpanel = new JPanel();
184                 lengthpanel.add(new Label("Length: "));
185                 lengthpanel.add(length);
186
187                 Object[] array = {"Set the note properties",
188                                 pitchpanel,
189                                 velocitypanel,
190                                 lengthpanel};
191         
192                 final String btnString1 = "Enter";
193                 final String btnString2 = "Cancel";
194                 Object[] options = {btnString1, btnString2};
195         
196                 optionPane = new JOptionPane(array, 
197                                             JOptionPane.QUESTION_MESSAGE,
198                                             JOptionPane.YES_NO_OPTION,
199                                             null,
200                                             options,
201                                             options[0]);
202                 setContentPane(optionPane);
203                 setDefaultCloseOperation(DISPOSE_ON_CLOSE);
204         
205                 ActionListener intValidator = new ActionListener() {
206                     public void actionPerformed(ActionEvent e) {
207                                         if (e.getSource() instanceof JTextField){
208                                                 JTextField s = (JTextField)e.getSource();
209                                                 int num = Integer.parseInt(s.getText());
210                                                 if (num < 0)
211                                                         num = 0;
212                                                 else if (num > 127 && s != length)
213                                                         num = 127;
214                                                 s.setText(new Integer(num).toString());
215                                         }
216                     }
217                 };
218                 pitch.addActionListener(intValidator);
219                 velocity.addActionListener(intValidator);
220                 length.addActionListener(intValidator);
221
222                 optionPane.addPropertyChangeListener(new PropertyChangeListener() {
223                     public void propertyChange(PropertyChangeEvent e) {
224                         String prop = e.getPropertyName();
225         
226                         if (isVisible() 
227                          && (e.getSource() == optionPane)
228                          && (prop.equals(JOptionPane.VALUE_PROPERTY) ||
229                              prop.equals(JOptionPane.INPUT_VALUE_PROPERTY))) {
230                             Object value = optionPane.getValue();
231         
232                             if (value == JOptionPane.UNINITIALIZED_VALUE) {
233                                 //ignore reset
234                                 return;
235                             }
236         
237                             // Reset the JOptionPane's value.
238                             // If you don't do this, then if the user
239                             // presses the same button next time, no
240                             // property change event will be fired.
241                             optionPane.setValue(
242                                     JOptionPane.UNINITIALIZED_VALUE);
243         
244                             if (value.equals(btnString1)) {
245                                                         note.setPitch(Integer.parseInt(pitch.getText()));
246                                                         note.setVelocity(Integer.parseInt(velocity.getText()));
247                                                         note.setDuration(Integer.parseInt(length.getText()));
248         
249                                 setVisible(false);
250                             } else { // user closed dialog or clicked cancel
251                                 setVisible(false);
252                             }
253                         }
254                     }
255                 });
256                 pack();
257         }
258 }