]> ruin.nu Git - moosique.git/blob - MooDialog.java
no message
[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.io.*;
6 import java.beans.*;
7
8 /**
9  * The GUI-class representing the popupdialogs in the Track menu.
10  *
11  * @author Björn Lanneskog
12  */
13  
14 public class MooDialog extends JDialog {
15
16         private JLabel labelA, labelB, labelC;
17         private JTextField textFieldA, textFieldB, textFieldC;
18         private JComboBox trackListA, trackListB; 
19         private JButton okButton, cancelButton;
20         public static final int ADD_TRACK = 1,
21                                 DELETE_TRACK = 2,
22                                 COPY_TRACK = 3,
23                                 MOVE_TRACK = 4,
24                                 JUMP = 5,
25                                 CONTENTS = 6;
26         
27         /**
28          * Constructor of the dialogs.
29          */
30          public MooDialog(int type) {
31                 super(Moosique.getGUI(), false);
32                         
33                 Container pane = getContentPane();
34                 pane.setLayout(null);
35                 
36                 Track[] tracks = Moosique.getSequence().getTracks();
37
38                 switch (type) {
39                         case ADD_TRACK:         makeAddDialog(pane, tracks); break;
40                         case DELETE_TRACK:      makeDelDialog(pane, tracks); break;
41                         case COPY_TRACK:        makeCopyDialog(pane, tracks); break;
42                         case MOVE_TRACK:        makeMoveDialog(pane, tracks); break;
43                         case JUMP:              makeJumpDialog(pane); break;
44                         case CONTENTS:          makeTextDialog(pane, "Manual.txt"); break;
45                 }
46          }
47         
48         /**
49          * Builds the add track popupdialog.
50          * @param pane          The container to put the dialog in.
51          * @param tracks        A array containing miditracks.
52          */
53         private void makeAddDialog(Container pane, Track[] tracks) {
54         
55                 setTitle("Add track");
56                 labelA = new JLabel("Name of track", JLabel.CENTER);
57                 pane.add(labelA);
58                 textFieldA = new JTextField();  
59                 pane.add(textFieldA);
60                 labelB = new JLabel("Add after", JLabel.CENTER);
61                 pane.add(labelB);
62                 trackListA = new JComboBox();
63                 for (int i = 1; i <= tracks.length; i++) trackListA.addItem("Track " + i);
64                 pane.add(trackListA);
65                 cancelButton = new JButton("Cancel");
66                 pane.add(cancelButton);
67                 okButton = new JButton("OK");
68                 pane.add(okButton);
69         
70                 labelA.setBounds(50, 10, 100, 20);
71                 textFieldA.setBounds(40, 35, 120, 20);
72                 labelB.setBounds(50, 70, 100, 20);
73                 trackListA.setBounds(40, 95, 120, 20);
74                 cancelButton.setBounds(10, 150, 80, 30);
75                 okButton.setBounds(120, 150, 60, 30);
76         
77                 setLocation((Toolkit.getDefaultToolkit().getScreenSize().width - this.getWidth()) / 2,
78                 (Toolkit.getDefaultToolkit().getScreenSize().height - this.getHeight()) / 2);
79                 setResizable(false);
80                 pack();
81                 setSize(200,220);
82                 setVisible(true);
83         }
84         
85         /**
86          * Builds the delete track popupdialog.
87          * @param pane          The container to put the dialog in.
88          * @param tracks        A array containing miditracks.
89          */
90         private void makeDelDialog(Container pane, Track[] tracks) {
91         
92                 setTitle("Delete track");
93                 labelB = new JLabel("Delete track", JLabel.CENTER);
94                 pane.add(labelB);
95                 trackListA = new JComboBox();
96                 for (int i = 1; i <= tracks.length; i++) trackListA.addItem("Track " + i);
97                 pane.add(trackListA);
98                 cancelButton = new JButton("Cancel");
99                 pane.add(cancelButton);
100                 okButton = new JButton("OK");
101                 pane.add(okButton);
102                         
103                 labelB.setBounds(50, 10, 100, 20);
104                 trackListA.setBounds(40, 35, 120, 20);
105                 cancelButton.setBounds(10, 90, 80, 30);
106                 okButton.setBounds(120, 90, 60, 30);
107         
108                 setLocation((Toolkit.getDefaultToolkit().getScreenSize().width - this.getWidth()) / 2,
109                 (Toolkit.getDefaultToolkit().getScreenSize().height - this.getHeight()) / 2);
110                 setResizable(false);
111                 pack();
112                 setSize(200,165);
113                 setVisible(true);
114         }
115         
116         /**
117          * Builds the copy track popupdialog.
118          * @param pane          The container to put the dialog in.
119          * @param tracks        A array containing miditracks.
120          */
121         private void makeCopyDialog(Container pane, Track[] tracks) {
122         
123                 setTitle("Copy Track");
124                 labelA = new JLabel("Track to copy", JLabel.CENTER);
125                 pane.add(labelA);
126                 trackListA = new JComboBox();
127                 for (int i = 1; i <= tracks.length; i++) trackListA.addItem("Track " + i);
128                 pane.add(trackListA);
129                 labelB = new JLabel("Insert after", JLabel.CENTER);
130                 pane.add(labelB);
131                 trackListB = new JComboBox();
132                 for (int i = 1; i <= tracks.length; i++) trackListB.addItem("Track " + i);
133                 pane.add(trackListB);
134                 cancelButton = new JButton("Cancel");
135                 pane.add(cancelButton);
136                 okButton = new JButton("OK");
137                 pane.add(okButton);
138                 
139                 labelA.setBounds(50, 10, 100, 20);
140                 trackListA.setBounds(40, 35, 120, 20);
141                 labelB.setBounds(50, 70, 100, 20);
142                 trackListB.setBounds(40, 95, 120, 20);
143                 cancelButton.setBounds(10, 150, 80, 30);
144                 okButton.setBounds(120, 150, 60, 30);
145         
146                 setLocation((Toolkit.getDefaultToolkit().getScreenSize().width - this.getWidth()) / 2,
147                 (Toolkit.getDefaultToolkit().getScreenSize().height - this.getHeight()) / 2);
148                 setResizable(false);
149                 pack();
150                 setSize(200,220);
151                 setVisible(true);
152         }
153         
154         /**
155          * Builds the move track popupdialog
156          * @param pane          The container to put the dialog in.
157          * @param tracks        A array containing miditracks.
158          */
159         private void makeMoveDialog(Container pane, Track[] tracks) {
160         
161                 setTitle("Move track");
162                 labelA = new JLabel("Track to move", JLabel.CENTER);
163                 pane.add(labelA);
164                 trackListA = new JComboBox();
165                 for (int i = 1; i <= tracks.length; i++) trackListA.addItem("Track " + i);
166                 pane.add(trackListA);
167                 labelB = new JLabel("Insert after", JLabel.CENTER);
168                 pane.add(labelB);
169                 trackListB = new JComboBox();
170                 for (int i = 1; i <= tracks.length; i++) trackListB.addItem("Track " + i);
171                 pane.add(trackListB);
172                 cancelButton = new JButton("Cancel");
173                 pane.add(cancelButton);
174                 okButton = new JButton("OK");
175                 pane.add(okButton);
176                 
177                 labelA.setBounds(40, 10, 120, 20);
178                 trackListA.setBounds(40, 35, 120, 20);
179                 labelB.setBounds(50, 70, 100, 20);
180                 trackListB.setBounds(40, 95, 120, 20);
181                 cancelButton.setBounds(10, 150, 80, 30);
182                 okButton.setBounds(120, 150, 60, 30);
183         
184                 setLocation((Toolkit.getDefaultToolkit().getScreenSize().width - this.getWidth()) / 2,
185                 (Toolkit.getDefaultToolkit().getScreenSize().height - this.getHeight()) / 2);
186                 setResizable(false);
187                 pack();
188                 setSize(200,220);
189                 setVisible(true);
190         }
191         
192         /**
193          * Builds the jump popupdialog.
194          * @param pane          The container to put the dialog in.
195          * @param tracks        A array containing miditracks.
196          */
197         private void makeJumpDialog(Container pane) {
198                 
199                 setTitle("Jump");
200                 labelA = new JLabel("Msr", JLabel.CENTER);
201                 pane.add(labelA);
202                 labelB = new JLabel("Beat", JLabel.CENTER);
203                 pane.add(labelB);
204                 labelC = new JLabel("Tick", JLabel.CENTER);
205                 pane.add(labelC);
206                 textFieldA = new JTextField();
207                 pane.add(textFieldA);
208                 textFieldB = new JTextField();
209                 pane.add(textFieldB);
210                 textFieldC = new JTextField();
211                 pane.add(textFieldC);
212                 cancelButton = new JButton("Cancel");
213                 pane.add(cancelButton);
214                 okButton = new JButton("OK");
215                 pane.add(okButton);
216                 
217                 labelA.setBounds(30, 25, 50, 20);
218                 labelB.setBounds(105, 25, 50, 20);
219                 labelC.setBounds(180, 25, 50, 20);
220                 textFieldA.setBounds(30, 45, 50, 20);
221                 textFieldB.setBounds(105, 45, 50, 20);
222                 textFieldC.setBounds(180, 45, 50, 20);
223                 cancelButton.setBounds(35, 90, 80, 30);
224                 okButton.setBounds(155, 90, 60, 30);
225                 
226                 setLocation((Toolkit.getDefaultToolkit().getScreenSize().width - this.getWidth()) / 2,
227                 (Toolkit.getDefaultToolkit().getScreenSize().height - this.getHeight()) / 2);
228                 setResizable(false);
229                 pack();
230                 setSize(260,175);
231                 setVisible(true);
232         }
233
234         private void makeTextDialog(Container pane, String filename) {
235                 setTitle("Contents");
236                 File manual = new File(filename);
237                 String s;
238                 try {
239                         BufferedReader br = new BufferedReader(new FileReader(manual));
240                         char[] chars = new char[(int)manual.length()];
241                         br.read(chars, 0, (int)manual.length());
242                         s = new String(chars);
243                 } catch (Exception ex) {
244                         s = "Manual not found";
245                 }
246                 JTextArea contents = new JTextArea(s, 30, 40);
247                 contents.setAutoscrolls(true);
248                 pane.add(contents);
249                 contents.setBounds(10, 10, 500, 350);
250                 setResizable(false);
251                 pack();
252                 setSize(600,400);
253                 setLocation((Toolkit.getDefaultToolkit().getScreenSize().width - this.getWidth()) / 2,
254                 (Toolkit.getDefaultToolkit().getScreenSize().height - this.getHeight()) / 2);
255                 setVisible(true);
256         }
257
258         private MooNote note;
259         private JOptionPane optionPane;
260         private JTextField pitch;
261         private JTextField velocity;
262         private JTextField length;
263         
264         /** 
265          * Creates a new note preference dialog.
266          * @param mn    the note that will be graphically represented
267          */
268         public MooDialog(MooNote mn) {
269                 super(Moosique.getGUI(), "Note properties", false);
270                 note = mn;
271                 pitch = new JTextField(new Integer(note.getPitch()).toString(),3);
272                 JPanel pitchpanel = new JPanel();
273                 pitchpanel.add(new Label("Pitch: "));
274                 pitchpanel.add(pitch);
275
276
277                 velocity = new JTextField(new Integer(note.getVelocity()).toString(),3);
278                 JPanel velocitypanel = new JPanel();
279                 velocitypanel.add(new Label("Velocity: "));
280                 velocitypanel.add(velocity);
281
282                 length = new JTextField(new Integer(note.getDuration()).toString(),5);
283                 JPanel lengthpanel = new JPanel();
284                 lengthpanel.add(new Label("Length: "));
285                 lengthpanel.add(length);
286
287                 Object[] array = {"Set the note properties",
288                                 pitchpanel,
289                                 velocitypanel,
290                                 lengthpanel};
291         
292                 final String btnString1 = "Enter";
293                 final String btnString2 = "Cancel";
294                 Object[] options = {btnString1, btnString2};
295         
296                 optionPane = new JOptionPane(array, 
297                                             JOptionPane.QUESTION_MESSAGE,
298                                             JOptionPane.YES_NO_OPTION,
299                                             null,
300                                             options,
301                                             options[0]);
302                 setContentPane(optionPane);
303                 setDefaultCloseOperation(DISPOSE_ON_CLOSE);
304         
305                 ActionListener intValidator = new ActionListener() {
306                     public void actionPerformed(ActionEvent e) {
307                                         if (e.getSource() instanceof JTextField){
308                                                 JTextField s = (JTextField)e.getSource();
309                                                 int num = Integer.parseInt(s.getText());
310                                                 if (num < 0)
311                                                         num = 0;
312                                                 else if (num > 127 && s != length)
313                                                         num = 127;
314                                                 s.setText(new Integer(num).toString());
315                                         }
316                     }
317                 };
318                 pitch.addActionListener(intValidator);
319                 velocity.addActionListener(intValidator);
320                 length.addActionListener(intValidator);
321
322                 optionPane.addPropertyChangeListener(new PropertyChangeListener() {
323                     public void propertyChange(PropertyChangeEvent e) {
324                         String prop = e.getPropertyName();
325         
326                         if (isVisible() 
327                          && (e.getSource() == optionPane)
328                          && (prop.equals(JOptionPane.VALUE_PROPERTY) ||
329                              prop.equals(JOptionPane.INPUT_VALUE_PROPERTY))) {
330                             Object value = optionPane.getValue();
331         
332                             if (value == JOptionPane.UNINITIALIZED_VALUE) {
333                                 //ignore reset
334                                 return;
335                             }
336         
337                             // Reset the JOptionPane's value.
338                             // If you don't do this, then if the user
339                             // presses the same button next time, no
340                             // property change event will be fired.
341                             optionPane.setValue(
342                                     JOptionPane.UNINITIALIZED_VALUE);
343         
344                             if (value.equals(btnString1)) {
345                                                         note.setPitch(Integer.parseInt(pitch.getText()));
346                                                         note.setVelocity(Integer.parseInt(velocity.getText()));
347                                                         note.setDuration(Integer.parseInt(length.getText()));
348         
349                                 setVisible(false);
350                             } else { // user closed dialog or clicked cancel
351                                 setVisible(false);
352                             }
353                         }
354                     }
355                 });
356                 pack();
357         }
358 }