3 import java.awt.event.*;
4 import javax.sound.midi.*;
9 * A generator class for the application's dialogs.
11 * @author Björn Lanneskog
14 public class MooDialog extends JDialog {
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,
30 * Constructor of the dialogs.
32 public MooDialog(int type) {
33 super(Moosique.getGUI(), false);
35 Container pane = getContentPane();
38 Track[] tracks = Moosique.getSequence().getTracks();
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;
53 * Builds the add track popupdialog.
54 * @param pane The container to put the dialog in.
55 * @param tracks A array containing miditracks.
57 private void makeAddDialog(Container pane, Track[] tracks) {
59 setTitle("Add track");
60 labelA = new JLabel("Name of track", JLabel.CENTER);
62 textFieldA = new JTextField();
64 labelB = new JLabel("Add after", JLabel.CENTER);
66 trackListA = new JComboBox();
67 for (int i = 1; i <= tracks.length; i++) trackListA.addItem("Track " + i);
69 cancelButton = new JButton("Cancel");
70 pane.add(cancelButton);
71 okButton = new JButton("OK");
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);
84 setLocationRelativeTo(Moosique.getGUI());
89 * Builds the delete track popupdialog.
90 * @param pane The container to put the dialog in.
91 * @param tracks A array containing miditracks.
93 private void makeDelDialog(Container pane, Track[] tracks) {
95 setTitle("Delete track");
96 labelB = new JLabel("Delete track", JLabel.CENTER);
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");
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);
114 setLocationRelativeTo(Moosique.getGUI());
119 * Builds the copy track popupdialog.
120 * @param pane The container to put the dialog in.
121 * @param tracks A array containing miditracks.
123 private void makeCopyDialog(Container pane, Track[] tracks) {
125 setTitle("Copy Track");
126 labelA = new JLabel("Track to copy", JLabel.CENTER);
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);
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");
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);
151 setLocationRelativeTo(Moosique.getGUI());
156 * Builds the move track popupdialog
157 * @param pane The container to put the dialog in.
158 * @param tracks A array containing miditracks.
160 private void makeMoveDialog(Container pane, Track[] tracks) {
162 setTitle("Move track");
163 labelA = new JLabel("Track to move", JLabel.CENTER);
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);
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");
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);
188 setLocationRelativeTo(Moosique.getGUI());
193 * Builds the set position dialog.
194 * @param pane The container to put the dialog in.
195 * @param tracks A array containing miditracks.
197 private void makeSetPositionDialog(Container pane) {
199 setTitle("Set edit position");
200 labelA = new JLabel("Measure", JLabel.CENTER);
202 labelB = new JLabel("Beat", JLabel.CENTER);
204 labelC = new JLabel("Tick", JLabel.CENTER);
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");
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);
229 setLocationRelativeTo(Moosique.getGUI());
234 * Builds the insert measure popupdialog.
235 * @param pane The container to put the dialog in.
237 private void makeInsertMeasureDialog(Container pane){
239 setTitle("Insert Measure");
240 labelA = new JLabel("Insert at:", JLabel.RIGHT);
242 labelB = new JLabel("Measure count:", JLabel.RIGHT);
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");
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);
263 setLocationRelativeTo(Moosique.getGUI());
268 * Builds the delete measure popupdialog.
269 * @param pane The container to put the dialog in.
271 private void makeDeleteMeasureDialog(Container pane) {
273 setTitle("Delete Measure");
274 labelA = new JLabel("Delete at:", JLabel.RIGHT);
276 labelB = new JLabel("Measure count:", JLabel.RIGHT);
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");
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);
297 setLocationRelativeTo(Moosique.getGUI());
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);
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";
314 pane.add(new JScrollPane(new JTextArea(s, 30, 95)));
315 Action close = new AbstractAction("Close") {
316 public void actionPerformed(ActionEvent ae) {
319 JButton closeButton = new JButton(close);
320 closeButton.setAlignmentX(Component.CENTER_ALIGNMENT);
321 pane.add(closeButton);
324 setLocationRelativeTo(Moosique.getGUI());
328 private MooNote note;
329 private JOptionPane optionPane;
330 private JTextField pitch;
331 private JTextField velocity;
332 private JTextField length;
335 * Creates a new note preference dialog.
336 * @param mn the note that will be graphically represented
338 public MooDialog(MooNote mn) {
339 super(Moosique.getGUI(), "Note properties", false);
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);
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);
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);
356 Object[] array = {"Set the note properties",
361 final String btnString1 = "Apply changes";
362 final String btnString2 = "Cancel";
363 Object[] options = {btnString1, btnString2};
365 optionPane = new JOptionPane(array,
366 JOptionPane.QUESTION_MESSAGE,
367 JOptionPane.YES_NO_OPTION,
371 setContentPane(optionPane);
372 setDefaultCloseOperation(DISPOSE_ON_CLOSE);
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());
381 else if (num > 127 && s != length)
383 s.setText(new Integer(num).toString());
387 pitch.addActionListener(intValidator);
388 velocity.addActionListener(intValidator);
389 length.addActionListener(intValidator);
391 optionPane.addPropertyChangeListener(new PropertyChangeListener() {
392 public void propertyChange(PropertyChangeEvent e) {
393 String prop = e.getPropertyName();
396 && (e.getSource() == optionPane)
397 && (prop.equals(JOptionPane.VALUE_PROPERTY) ||
398 prop.equals(JOptionPane.INPUT_VALUE_PROPERTY))) {
399 Object value = optionPane.getValue();
401 if (value == JOptionPane.UNINITIALIZED_VALUE) {
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.
411 JOptionPane.UNINITIALIZED_VALUE);
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();
419 } else { // user closed dialog or clicked cancel