]> 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  * A generator class for the application's dialogs.
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                                 SET_POSITION = 5,
25                                 MANUAL = 6,
26                                 INSERT_MEASURE = 7,
27                                 DELETE_MEASURE = 8;
28         
29         /**
30          * Constructor of the dialogs.
31          */
32          public MooDialog(int type) {
33                 super(Moosique.getGUI(), false);
34                         
35                 Container pane = getContentPane();
36                 pane.setLayout(null);
37                 
38                 Track[] tracks = Moosique.getSequence().getTracks();
39
40                 switch (type) {
41                         case ADD_TRACK:         makeAddDialog(pane, tracks); break;
42                         case DELETE_TRACK:      makeDelDialog(pane, tracks); break;
43                         case COPY_TRACK:        makeCopyDialog(pane, tracks); break;
44                         case MOVE_TRACK:        makeMoveDialog(pane, tracks); break;
45                         case SET_POSITION:      makeSetPositionDialog(pane); break;
46                         case MANUAL:            makeTextDialog(pane, "Manual.txt"); break;
47                         case INSERT_MEASURE:    makeInsertMeasureDialog(pane); break;
48                         case DELETE_MEASURE:    makeDeleteMeasureDialog(pane); break;
49                 }
50          }
51         
52         /**
53          * Builds the add track popupdialog.
54          * @param pane          The container to put the dialog in.
55          * @param tracks        A array containing miditracks.
56          */
57         private void makeAddDialog(Container pane, Track[] tracks) {
58         
59                 setTitle("Add track");
60                 labelA = new JLabel("Name of track", JLabel.CENTER);
61                 pane.add(labelA);
62                 textFieldA = new JTextField();  
63                 pane.add(textFieldA);
64                 labelB = new JLabel("Add after", JLabel.CENTER);
65                 pane.add(labelB);
66                 trackListA = new JComboBox();
67                 for (int i = 1; i <= tracks.length; i++) trackListA.addItem("Track " + i);
68                 pane.add(trackListA);
69                 cancelButton = new JButton("Cancel");
70                 pane.add(cancelButton);
71                 okButton = new JButton("OK");
72                 pane.add(okButton);
73         
74                 labelA.setBounds(50, 10, 100, 20);
75                 textFieldA.setBounds(40, 35, 120, 20);
76                 labelB.setBounds(50, 70, 100, 20);
77                 trackListA.setBounds(40, 95, 120, 20);
78                 cancelButton.setBounds(10, 150, 80, 30);
79                 okButton.setBounds(120, 150, 60, 30);
80         
81                 setResizable(false);
82                 pack();
83                 setSize(200,220);
84                 setLocationRelativeTo(Moosique.getGUI());
85                 setVisible(true);
86         }
87         
88         /**
89          * Builds the delete track popupdialog.
90          * @param pane          The container to put the dialog in.
91          * @param tracks        A array containing miditracks.
92          */
93         private void makeDelDialog(Container pane, Track[] tracks) {
94         
95                 setTitle("Delete track");
96                 labelB = new JLabel("Delete track", JLabel.CENTER);
97                 pane.add(labelB);
98                 trackListA = new JComboBox();
99                 for (int i = 1; i <= tracks.length; i++) trackListA.addItem("Track " + i);
100                 pane.add(trackListA);
101                 cancelButton = new JButton("Cancel");
102                 pane.add(cancelButton);
103                 okButton = new JButton("OK");
104                 pane.add(okButton);
105                         
106                 labelB.setBounds(50, 10, 100, 20);
107                 trackListA.setBounds(40, 35, 120, 20);
108                 cancelButton.setBounds(10, 90, 80, 30);
109                 okButton.setBounds(120, 90, 60, 30);
110         
111                 setResizable(false);
112                 pack();
113                 setSize(200,165);
114                 setLocationRelativeTo(Moosique.getGUI());
115                 setVisible(true);
116         }
117         
118         /**
119          * Builds the copy track popupdialog.
120          * @param pane          The container to put the dialog in.
121          * @param tracks        A array containing miditracks.
122          */
123         private void makeCopyDialog(Container pane, Track[] tracks) {
124         
125                 setTitle("Copy Track");
126                 labelA = new JLabel("Track to copy", JLabel.CENTER);
127                 pane.add(labelA);
128                 trackListA = new JComboBox();
129                 for (int i = 1; i <= tracks.length; i++) trackListA.addItem("Track " + i);
130                 pane.add(trackListA);
131                 labelB = new JLabel("Insert after", JLabel.CENTER);
132                 pane.add(labelB);
133                 trackListB = new JComboBox();
134                 for (int i = 1; i <= tracks.length; i++) trackListB.addItem("Track " + i);
135                 pane.add(trackListB);
136                 cancelButton = new JButton("Cancel");
137                 pane.add(cancelButton);
138                 okButton = new JButton("OK");
139                 pane.add(okButton);
140                 
141                 labelA.setBounds(50, 10, 100, 20);
142                 trackListA.setBounds(40, 35, 120, 20);
143                 labelB.setBounds(50, 70, 100, 20);
144                 trackListB.setBounds(40, 95, 120, 20);
145                 cancelButton.setBounds(10, 150, 80, 30);
146                 okButton.setBounds(120, 150, 60, 30);
147         
148                 setResizable(false);
149                 pack();
150                 setSize(200,220);
151                 setLocationRelativeTo(Moosique.getGUI());
152                 setVisible(true);
153         }
154         
155         /**
156          * Builds the move track popupdialog
157          * @param pane          The container to put the dialog in.
158          * @param tracks        A array containing miditracks.
159          */
160         private void makeMoveDialog(Container pane, Track[] tracks) {
161         
162                 setTitle("Move track");
163                 labelA = new JLabel("Track to move", JLabel.CENTER);
164                 pane.add(labelA);
165                 trackListA = new JComboBox();
166                 for (int i = 1; i <= tracks.length; i++) trackListA.addItem("Track " + i);
167                 pane.add(trackListA);
168                 labelB = new JLabel("Insert after", JLabel.CENTER);
169                 pane.add(labelB);
170                 trackListB = new JComboBox();
171                 for (int i = 1; i <= tracks.length; i++) trackListB.addItem("Track " + i);
172                 pane.add(trackListB);
173                 cancelButton = new JButton("Cancel");
174                 pane.add(cancelButton);
175                 okButton = new JButton("OK");
176                 pane.add(okButton);
177                 
178                 labelA.setBounds(40, 10, 120, 20);
179                 trackListA.setBounds(40, 35, 120, 20);
180                 labelB.setBounds(50, 70, 100, 20);
181                 trackListB.setBounds(40, 95, 120, 20);
182                 cancelButton.setBounds(10, 150, 80, 30);
183                 okButton.setBounds(120, 150, 60, 30);
184         
185                 setResizable(false);
186                 pack();
187                 setSize(200,220);
188                 setLocationRelativeTo(Moosique.getGUI());
189                 setVisible(true);
190         }
191         
192         /**
193          * Builds the set position dialog.
194          * @param pane          The container to put the dialog in.
195          * @param tracks        A array containing miditracks.
196          */
197         private void makeSetPositionDialog(Container pane) {
198                 
199                 setTitle("Set edit position");
200                 labelA = new JLabel("Measure", 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                 setResizable(false);
227                 pack();
228                 setSize(260,165);
229                 setLocationRelativeTo(Moosique.getGUI());
230                 setVisible(true);
231         }
232         
233         /**
234          * Builds the insert measure popupdialog.
235          * @param pane          The container to put the dialog in.
236          */
237         private void makeInsertMeasureDialog(Container pane){
238         
239                 setTitle("Insert Measure");
240                 labelA = new JLabel("Insert at:", JLabel.RIGHT);
241                 pane.add(labelA);
242                 labelB = new JLabel("Measure count:", JLabel.RIGHT);
243                 pane.add(labelB);
244                 textFieldA = new JTextField();
245                 pane.add(textFieldA);
246                 textFieldB = new JTextField();
247                 pane.add(textFieldB);
248                 cancelButton = new JButton("Cancel");
249                 pane.add(cancelButton);
250                 okButton = new JButton("OK");
251                 pane.add(okButton);
252                 
253                 labelA.setBounds(20, 20, 110 ,20);
254                 labelB.setBounds(20, 50, 110, 20);
255                 textFieldA.setBounds(140 ,20 , 40, 20);
256                 textFieldB.setBounds(140,50, 40, 20);
257                 cancelButton.setBounds(20 ,95 , 80, 30);
258                 okButton.setBounds(120, 95, 60, 30);
259                 
260                 setResizable(false);
261                 pack();
262                 setSize(210,175);
263                 setLocationRelativeTo(Moosique.getGUI());
264                 setVisible(true);
265         }
266         
267         /**
268          * Builds the delete measure popupdialog.
269          * @param pane          The container to put the dialog in.
270          */
271         private void makeDeleteMeasureDialog(Container pane) {
272         
273                 setTitle("Delete Measure");
274                 labelA = new JLabel("Delete at:", JLabel.RIGHT);
275                 pane.add(labelA);
276                 labelB = new JLabel("Measure count:", JLabel.RIGHT);
277                 pane.add(labelB);
278                 textFieldA = new JTextField();
279                 pane.add(textFieldA);
280                 textFieldB = new JTextField();
281                 pane.add(textFieldB);
282                 cancelButton = new JButton("Cancel");
283                 pane.add(cancelButton);
284                 okButton = new JButton("OK");
285                 pane.add(okButton);
286                 
287                 labelA.setBounds(20, 20, 110 ,20);
288                 labelB.setBounds(20, 50, 110, 20);
289                 textFieldA.setBounds(140 ,20 , 40, 20);
290                 textFieldB.setBounds(140,50, 40, 20);
291                 cancelButton.setBounds(20 ,95 , 80, 30);
292                 okButton.setBounds(120, 95, 60, 30);
293                 
294                 setResizable(false);
295                 pack();
296                 setSize(210,175);
297                 setLocationRelativeTo(Moosique.getGUI());
298                 setVisible(true);
299         }
300         
301         private void makeTextDialog(Container pane, String filename) {
302                 setTitle("User Manual");
303                 pane.setLayout(new BoxLayout(pane, BoxLayout.Y_AXIS));
304                 File manual = new File(filename);
305                 String s;
306                 try {
307                         BufferedReader br = new BufferedReader(new FileReader(manual));
308                         char[] chars = new char[(int)manual.length()];
309                         br.read(chars, 0, (int)manual.length());
310                         s = new String(chars);
311                 } catch (Exception ex) {
312                         s = "Manual not found";
313                 }
314                 pane.add(new JScrollPane(new JTextArea(s, 30, 95)));
315                 Action close = new AbstractAction("Close") {
316                         public void actionPerformed(ActionEvent ae) {
317                                 setVisible(false);
318                         }};
319                 JButton closeButton = new JButton(close);
320                 closeButton.setAlignmentX(Component.CENTER_ALIGNMENT);
321                 pane.add(closeButton);
322                 setResizable(false);
323                 pack();
324                 setLocationRelativeTo(Moosique.getGUI());
325                 setVisible(true);
326         }
327
328         private MooNote note;
329         private JOptionPane optionPane;
330         private JTextField pitch;
331         private JTextField velocity;
332         private JTextField length;
333         
334         /** 
335          * Creates a new note preference dialog.
336          * @param mn    the note that will be graphically represented
337          */
338         public MooDialog(MooNote mn) {
339                 super(Moosique.getGUI(), "Note properties", false);
340                 note = mn;
341                 pitch = new JTextField(new Integer(note.getPitch()).toString(),3);
342                 JPanel pitchpanel = new JPanel();
343                 pitchpanel.add(new Label("Pitch: "));
344                 pitchpanel.add(pitch);
345
346                 velocity = new JTextField(new Integer(note.getVelocity()).toString(),3);
347                 JPanel velocitypanel = new JPanel();
348                 velocitypanel.add(new Label("Velocity: "));
349                 velocitypanel.add(velocity);
350
351                 length = new JTextField(new Integer(note.getDuration()).toString(),5);
352                 JPanel lengthpanel = new JPanel();
353                 lengthpanel.add(new Label("Length: "));
354                 lengthpanel.add(length);
355
356                 Object[] array = {"Set the note properties",
357                                 pitchpanel,
358                                 velocitypanel,
359                                 lengthpanel};
360         
361                 final String btnString1 = "Apply changes";
362                 final String btnString2 = "Cancel";
363                 Object[] options = {btnString1, btnString2};
364         
365                 optionPane = new JOptionPane(array, 
366                                             JOptionPane.QUESTION_MESSAGE,
367                                             JOptionPane.YES_NO_OPTION,
368                                             null,
369                                             options,
370                                             options[0]);
371                 setContentPane(optionPane);
372                 setDefaultCloseOperation(DISPOSE_ON_CLOSE);
373         
374                 ActionListener intValidator = new ActionListener() {
375                     public void actionPerformed(ActionEvent e) {
376                                         if (e.getSource() instanceof JTextField){
377                                                 JTextField s = (JTextField)e.getSource();
378                                                 int num = Integer.parseInt(s.getText());
379                                                 if (num < 0)
380                                                         num = 0;
381                                                 else if (num > 127 && s != length)
382                                                         num = 127;
383                                                 s.setText(new Integer(num).toString());
384                                         }
385                     }
386                 };
387                 pitch.addActionListener(intValidator);
388                 velocity.addActionListener(intValidator);
389                 length.addActionListener(intValidator);
390
391                 optionPane.addPropertyChangeListener(new PropertyChangeListener() {
392                     public void propertyChange(PropertyChangeEvent e) {
393                         String prop = e.getPropertyName();
394         
395                         if (isVisible() 
396                          && (e.getSource() == optionPane)
397                          && (prop.equals(JOptionPane.VALUE_PROPERTY) ||
398                              prop.equals(JOptionPane.INPUT_VALUE_PROPERTY))) {
399                             Object value = optionPane.getValue();
400         
401                             if (value == JOptionPane.UNINITIALIZED_VALUE) {
402                                 //ignore reset
403                                 return;
404                             }
405         
406                             // Reset the JOptionPane's value.
407                             // If you don't do this, then if the user
408                             // presses the same button next time, no
409                             // property change event will be fired.
410                             optionPane.setValue(
411                                     JOptionPane.UNINITIALIZED_VALUE);
412         
413                             if (value.equals(btnString1)) {
414                                                         note.setPitch(Integer.parseInt(pitch.getText()));
415                                                         note.setVelocity(Integer.parseInt(velocity.getText()));
416                                                         note.setDuration(Integer.parseInt(length.getText()));
417                                                         Moosique.setEdited();
418                                                         setVisible(false);
419                             } else { // user closed dialog or clicked cancel
420                                 setVisible(false);
421                             }
422                         }
423                     }
424                 });
425                 pack();
426                 setVisible(true);
427         }
428 }