]> ruin.nu Git - moosique.git/blob - MooDialog.java
setting mute and solo on channel instead..
[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 GUI-class representing the add-, delete-, copy- or movetrack popup dialogs.
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 trackListA, trackListB; 
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          * Create the parts that the dialogs have in common.
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                                 // Create the content of the add dialog and put them in a container.
39                                 name = new JLabel("Name of track", JLabel.CENTER);
40                                 pane.add(name);
41                                 textField = new JTextField();
42                                 pane.add(textField);
43                                 whichToEdit = new JLabel("Add after", JLabel.CENTER);
44                                 pane.add(whichToEdit);
45                                 trackListA = new JComboBox();
46                                 for (int i = 1; i <= tracks.length; i++) trackListA.addItem("Track " + i);
47                                 pane.add(trackListA);
48                                 cancelButton = new JButton("Cancel");
49                                 pane.add(cancelButton);
50                                 okButton = new JButton("OK");
51                                 pane.add(okButton);
52                                 
53                                 // Set the layout properties of the dialog.
54                                 name.setBounds(50, 10, 100, 20);
55                                 textField.setBounds(40, 35, 120, 20);
56                                 whichToEdit.setBounds(50, 70, 100, 20);
57                                 trackListA.setBounds(40, 95, 120, 20);
58                                 cancelButton.setBounds(10, 150, 80, 30);
59                                 okButton.setBounds(120, 150, 60, 30);
60                         
61                                 setLocation((Toolkit.getDefaultToolkit().getScreenSize().width - this.getWidth()) / 2,
62                                 (Toolkit.getDefaultToolkit().getScreenSize().height - this.getHeight()) / 2);
63                                 setResizable(false);
64                                 pack();
65                                 setSize(200,220);
66                                 setVisible(true);
67                                 break;
68                                 
69                         case DELETE_TRACK:
70                                 
71                                 // Create the contents of the delete dialog and put them in a container.
72                                 whichToEdit = new JLabel("Delete track", JLabel.CENTER);
73                                 pane.add(whichToEdit);
74                                 trackListA = new JComboBox();
75                                 for (int i = 1; i <= tracks.length; i++) trackListA.addItem("Track " + i);
76                                 pane.add(trackListA);
77                                 cancelButton = new JButton("Cancel");
78                                 pane.add(cancelButton);
79                                 okButton = new JButton("OK");
80                                 pane.add(okButton);
81                                 
82                                 // Set the layout properties of the dialog.
83                                 whichToEdit.setBounds(50, 10, 100, 20);
84                                 trackListA.setBounds(40, 35, 120, 20);
85                                 cancelButton.setBounds(10, 90, 80, 30);
86                                 okButton.setBounds(120, 90, 60, 30);
87                         
88                                 setLocation((Toolkit.getDefaultToolkit().getScreenSize().width - this.getWidth()) / 2,
89                                 (Toolkit.getDefaultToolkit().getScreenSize().height - this.getHeight()) / 2);
90                                 setResizable(false);
91                                 pack();
92                                 setSize(200,165);
93                                 setVisible(true);
94                                 break;
95                         
96                         case COPY_TRACK:
97                         
98                                 // Create the content of the copy dialog and put them in a container.
99                                 name = new JLabel("Track to copy", JLabel.CENTER);
100                                 pane.add(name);
101                                 trackListA = new JComboBox();
102                                 for (int i = 1; i <= tracks.length; i++) trackListA.addItem("Track " + i);
103                                 pane.add(trackListA);
104                                 whichToEdit = new JLabel("Insert after", JLabel.CENTER);
105                                 pane.add(whichToEdit);
106                                 trackListB = new JComboBox();
107                                 for (int i = 1; i <= tracks.length; i++) trackListB.addItem("Track " + i);
108                                 pane.add(trackListB);
109                                 cancelButton = new JButton("Cancel");
110                                 pane.add(cancelButton);
111                                 okButton = new JButton("OK");
112                                 pane.add(okButton);
113                                 
114                                 // Set the layout properties of the dialog.
115                                 name.setBounds(50, 10, 100, 20);
116                                 trackListA.setBounds(40, 35, 120, 20);
117                                 whichToEdit.setBounds(50, 70, 100, 20);
118                                 trackListB.setBounds(40, 95, 120, 20);
119                                 cancelButton.setBounds(10, 150, 80, 30);
120                                 okButton.setBounds(120, 150, 60, 30);
121                         
122                                 setLocation((Toolkit.getDefaultToolkit().getScreenSize().width - this.getWidth()) / 2,
123                                 (Toolkit.getDefaultToolkit().getScreenSize().height - this.getHeight()) / 2);
124                                 setResizable(false);
125                                 pack();
126                                 setSize(200,220);
127                                 setVisible(true);
128                                 break;
129                         
130                         case MOVE_TRACK:
131                                 
132                                 // Create the content of the move dialog and put them in a container.
133                                 name = new JLabel("Track to move", JLabel.CENTER);
134                                 pane.add(name);
135                                 trackListA = new JComboBox();
136                                 for (int i = 1; i <= tracks.length; i++) trackListA.addItem("Track " + i);
137                                 pane.add(trackListA);
138                                 whichToEdit = new JLabel("Insert after", JLabel.CENTER);
139                                 pane.add(whichToEdit);
140                                 trackListB = new JComboBox();
141                                 for (int i = 1; i <= tracks.length; i++) trackListB.addItem("Track " + i);
142                                 pane.add(trackListB);
143                                 cancelButton = new JButton("Cancel");
144                                 pane.add(cancelButton);
145                                 okButton = new JButton("OK");
146                                 pane.add(okButton);
147                                 
148                                 // Set the layout properties of the dialog.
149                                 name.setBounds(40, 10, 120, 20);
150                                 trackListA.setBounds(40, 35, 120, 20);
151                                 whichToEdit.setBounds(50, 70, 100, 20);
152                                 trackListB.setBounds(40, 95, 120, 20);
153                                 cancelButton.setBounds(10, 150, 80, 30);
154                                 okButton.setBounds(120, 150, 60, 30);
155                         
156                                 setLocation((Toolkit.getDefaultToolkit().getScreenSize().width - this.getWidth()) / 2,
157                                 (Toolkit.getDefaultToolkit().getScreenSize().height - this.getHeight()) / 2);
158                                 setResizable(false);
159                                 pack();
160                                 setSize(200,220);
161                                 setVisible(true);
162                                 break;
163                 }
164          }
165         
166         
167         private MooNote note;
168         private JOptionPane optionPane;
169         private JTextField pitch;
170         private JTextField velocity;
171         private JTextField length;
172         
173         /** 
174          * Creates a new note preference dialog.
175          * @param mn    the note that will be graphically represented
176          */
177         public MooDialog(MooNote mn) {
178                 super(Moosique.getGUI(), "Note properties", false);
179                 note = mn;
180                 pitch = new JTextField(new Integer(note.getPitch()).toString(),3);
181                 JPanel pitchpanel = new JPanel();
182                 pitchpanel.add(new Label("Pitch: "));
183                 pitchpanel.add(pitch);
184
185
186                 velocity = new JTextField(new Integer(note.getVelocity()).toString(),3);
187                 JPanel velocitypanel = new JPanel();
188                 velocitypanel.add(new Label("Velocity: "));
189                 velocitypanel.add(velocity);
190
191                 length = new JTextField(new Integer(note.getDuration()).toString(),5);
192                 JPanel lengthpanel = new JPanel();
193                 lengthpanel.add(new Label("Length: "));
194                 lengthpanel.add(length);
195
196                 Object[] array = {"Set the note properties",
197                                 pitchpanel,
198                                 velocitypanel,
199                                 lengthpanel};
200         
201                 final String btnString1 = "Enter";
202                 final String btnString2 = "Cancel";
203                 Object[] options = {btnString1, btnString2};
204         
205                 optionPane = new JOptionPane(array, 
206                                             JOptionPane.QUESTION_MESSAGE,
207                                             JOptionPane.YES_NO_OPTION,
208                                             null,
209                                             options,
210                                             options[0]);
211                 setContentPane(optionPane);
212                 setDefaultCloseOperation(DISPOSE_ON_CLOSE);
213         
214                 ActionListener intValidator = new ActionListener() {
215                     public void actionPerformed(ActionEvent e) {
216                                         if (e.getSource() instanceof JTextField){
217                                                 JTextField s = (JTextField)e.getSource();
218                                                 int num = Integer.parseInt(s.getText());
219                                                 if (num < 0)
220                                                         num = 0;
221                                                 else if (num > 127 && s != length)
222                                                         num = 127;
223                                                 s.setText(new Integer(num).toString());
224                                         }
225                     }
226                 };
227                 pitch.addActionListener(intValidator);
228                 velocity.addActionListener(intValidator);
229                 length.addActionListener(intValidator);
230
231                 optionPane.addPropertyChangeListener(new PropertyChangeListener() {
232                     public void propertyChange(PropertyChangeEvent e) {
233                         String prop = e.getPropertyName();
234         
235                         if (isVisible() 
236                          && (e.getSource() == optionPane)
237                          && (prop.equals(JOptionPane.VALUE_PROPERTY) ||
238                              prop.equals(JOptionPane.INPUT_VALUE_PROPERTY))) {
239                             Object value = optionPane.getValue();
240         
241                             if (value == JOptionPane.UNINITIALIZED_VALUE) {
242                                 //ignore reset
243                                 return;
244                             }
245         
246                             // Reset the JOptionPane's value.
247                             // If you don't do this, then if the user
248                             // presses the same button next time, no
249                             // property change event will be fired.
250                             optionPane.setValue(
251                                     JOptionPane.UNINITIALIZED_VALUE);
252         
253                             if (value.equals(btnString1)) {
254                                                         note.setPitch(Integer.parseInt(pitch.getText()));
255                                                         note.setVelocity(Integer.parseInt(velocity.getText()));
256                                                         note.setDuration(Integer.parseInt(length.getText()));
257         
258                                 setVisible(false);
259                             } else { // user closed dialog or clicked cancel
260                                 setVisible(false);
261                             }
262                         }
263                     }
264                 });
265                 pack();
266         }
267 }